blob: f2400d8ca3c62aaafd334159ba215b91afb8f73f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Chet Haaseb39f0512011-05-24 14:36:40 -070019import android.util.FloatProperty;
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -070020import android.util.LocaleUtil;
Chet Haaseb39f0512011-05-24 14:36:40 -070021import android.util.Property;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080022import com.android.internal.R;
23import com.android.internal.util.Predicate;
24import com.android.internal.view.menu.MenuBuilder;
25
Christopher Tatea53146c2010-09-07 11:57:52 -070026import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080028import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.res.Resources;
30import android.content.res.TypedArray;
31import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070032import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.graphics.Canvas;
Mike Cleronf116bf82009-09-27 19:14:12 -070034import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.graphics.LinearGradient;
36import android.graphics.Matrix;
37import android.graphics.Paint;
38import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070039import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.PorterDuff;
41import android.graphics.PorterDuffXfermode;
42import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070043import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.graphics.Region;
45import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.graphics.drawable.ColorDrawable;
47import android.graphics.drawable.Drawable;
48import android.os.Handler;
49import android.os.IBinder;
50import android.os.Message;
51import android.os.Parcel;
52import android.os.Parcelable;
53import android.os.RemoteException;
54import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070057import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070059import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.util.Pools;
61import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080062import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070064import android.view.accessibility.AccessibilityEvent;
65import android.view.accessibility.AccessibilityEventSource;
66import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070067import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070069import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070070import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.inputmethod.InputConnection;
72import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.widget.ScrollBarDrawable;
74
Christopher Tatea0374192010-10-05 13:06:41 -070075import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070076import java.lang.reflect.InvocationTargetException;
77import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import java.util.ArrayList;
79import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070080import java.util.Locale;
Romain Guyd90a3312009-05-06 14:54:28 -070081import java.util.WeakHashMap;
Adam Powell4afd62b2011-02-18 15:02:18 -080082import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
84/**
85 * <p>
86 * This class represents the basic building block for user interface components. A View
87 * occupies a rectangular area on the screen and is responsible for drawing and
88 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070089 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
91 * are invisible containers that hold other Views (or other ViewGroups) and define
92 * their layout properties.
93 * </p>
94 *
95 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070096 * <p>For an introduction to using this class to develop your
97 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -070099 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
101 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
102 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
103 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
104 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
105 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
106 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
107 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
108 * </p>
109 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700110 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 * <a name="Using"></a>
112 * <h3>Using Views</h3>
113 * <p>
114 * All of the views in a window are arranged in a single tree. You can add views
115 * either from code or by specifying a tree of views in one or more XML layout
116 * files. There are many specialized subclasses of views that act as controls or
117 * are capable of displaying text, images, or other content.
118 * </p>
119 * <p>
120 * Once you have created a tree of views, there are typically a few types of
121 * common operations you may wish to perform:
122 * <ul>
123 * <li><strong>Set properties:</strong> for example setting the text of a
124 * {@link android.widget.TextView}. The available properties and the methods
125 * that set them will vary among the different subclasses of views. Note that
126 * properties that are known at build time can be set in the XML layout
127 * files.</li>
128 * <li><strong>Set focus:</strong> The framework will handled moving focus in
129 * response to user input. To force focus to a specific view, call
130 * {@link #requestFocus}.</li>
131 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
132 * that will be notified when something interesting happens to the view. For
133 * example, all views will let you set a listener to be notified when the view
134 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700135 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
136 * Other view subclasses offer more specialized listeners. For example, a Button
137 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700139 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * </ul>
141 * </p>
142 * <p><em>
143 * Note: The Android framework is responsible for measuring, laying out and
144 * drawing views. You should not call methods that perform these actions on
145 * views yourself unless you are actually implementing a
146 * {@link android.view.ViewGroup}.
147 * </em></p>
148 *
149 * <a name="Lifecycle"></a>
150 * <h3>Implementing a Custom View</h3>
151 *
152 * <p>
153 * To implement a custom view, you will usually begin by providing overrides for
154 * some of the standard methods that the framework calls on all views. You do
155 * not need to override all of these methods. In fact, you can start by just
156 * overriding {@link #onDraw(android.graphics.Canvas)}.
157 * <table border="2" width="85%" align="center" cellpadding="5">
158 * <thead>
159 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
160 * </thead>
161 *
162 * <tbody>
163 * <tr>
164 * <td rowspan="2">Creation</td>
165 * <td>Constructors</td>
166 * <td>There is a form of the constructor that are called when the view
167 * is created from code and a form that is called when the view is
168 * inflated from a layout file. The second form should parse and apply
169 * any attributes defined in the layout file.
170 * </td>
171 * </tr>
172 * <tr>
173 * <td><code>{@link #onFinishInflate()}</code></td>
174 * <td>Called after a view and all of its children has been inflated
175 * from XML.</td>
176 * </tr>
177 *
178 * <tr>
179 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700180 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <td>Called to determine the size requirements for this view and all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700186 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * <td>Called when this view should assign a size and position to all
188 * of its children.
189 * </td>
190 * </tr>
191 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700192 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * <td>Called when the size of this view has changed.
194 * </td>
195 * </tr>
196 *
197 * <tr>
198 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when the view should render its content.
201 * </td>
202 * </tr>
203 *
204 * <tr>
205 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700206 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * <td>Called when a new key event occurs.
208 * </td>
209 * </tr>
210 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700211 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 * <td>Called when a key up event occurs.
213 * </td>
214 * </tr>
215 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700216 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 * <td>Called when a trackball motion event occurs.
218 * </td>
219 * </tr>
220 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700221 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <td>Called when a touch screen motion event occurs.
223 * </td>
224 * </tr>
225 *
226 * <tr>
227 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700234 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * <td>Called when the window containing the view gains or loses focus.
236 * </td>
237 * </tr>
238 *
239 * <tr>
240 * <td rowspan="3">Attaching</td>
241 * <td><code>{@link #onAttachedToWindow()}</code></td>
242 * <td>Called when the view is attached to a window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
247 * <td><code>{@link #onDetachedFromWindow}</code></td>
248 * <td>Called when the view is detached from its window.
249 * </td>
250 * </tr>
251 *
252 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700253 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * <td>Called when the visibility of the window containing the view
255 * has changed.
256 * </td>
257 * </tr>
258 * </tbody>
259 *
260 * </table>
261 * </p>
262 *
263 * <a name="IDs"></a>
264 * <h3>IDs</h3>
265 * Views may have an integer id associated with them. These ids are typically
266 * assigned in the layout XML files, and are used to find specific views within
267 * the view tree. A common pattern is to:
268 * <ul>
269 * <li>Define a Button in the layout file and assign it a unique ID.
270 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700271 * &lt;Button
272 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 * android:layout_width="wrap_content"
274 * android:layout_height="wrap_content"
275 * android:text="@string/my_button_text"/&gt;
276 * </pre></li>
277 * <li>From the onCreate method of an Activity, find the Button
278 * <pre class="prettyprint">
279 * Button myButton = (Button) findViewById(R.id.my_button);
280 * </pre></li>
281 * </ul>
282 * <p>
283 * View IDs need not be unique throughout the tree, but it is good practice to
284 * ensure that they are at least unique within the part of the tree you are
285 * searching.
286 * </p>
287 *
288 * <a name="Position"></a>
289 * <h3>Position</h3>
290 * <p>
291 * The geometry of a view is that of a rectangle. A view has a location,
292 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
293 * two dimensions, expressed as a width and a height. The unit for location
294 * and dimensions is the pixel.
295 * </p>
296 *
297 * <p>
298 * It is possible to retrieve the location of a view by invoking the methods
299 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
300 * coordinate of the rectangle representing the view. The latter returns the
301 * top, or Y, coordinate of the rectangle representing the view. These methods
302 * both return the location of the view relative to its parent. For instance,
303 * when getLeft() returns 20, that means the view is located 20 pixels to the
304 * right of the left edge of its direct parent.
305 * </p>
306 *
307 * <p>
308 * In addition, several convenience methods are offered to avoid unnecessary
309 * computations, namely {@link #getRight()} and {@link #getBottom()}.
310 * These methods return the coordinates of the right and bottom edges of the
311 * rectangle representing the view. For instance, calling {@link #getRight()}
312 * is similar to the following computation: <code>getLeft() + getWidth()</code>
313 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
314 * </p>
315 *
316 * <a name="SizePaddingMargins"></a>
317 * <h3>Size, padding and margins</h3>
318 * <p>
319 * The size of a view is expressed with a width and a height. A view actually
320 * possess two pairs of width and height values.
321 * </p>
322 *
323 * <p>
324 * The first pair is known as <em>measured width</em> and
325 * <em>measured height</em>. These dimensions define how big a view wants to be
326 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
327 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
328 * and {@link #getMeasuredHeight()}.
329 * </p>
330 *
331 * <p>
332 * The second pair is simply known as <em>width</em> and <em>height</em>, or
333 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
334 * dimensions define the actual size of the view on screen, at drawing time and
335 * after layout. These values may, but do not have to, be different from the
336 * measured width and height. The width and height can be obtained by calling
337 * {@link #getWidth()} and {@link #getHeight()}.
338 * </p>
339 *
340 * <p>
341 * To measure its dimensions, a view takes into account its padding. The padding
342 * is expressed in pixels for the left, top, right and bottom parts of the view.
343 * Padding can be used to offset the content of the view by a specific amount of
344 * pixels. For instance, a left padding of 2 will push the view's content by
345 * 2 pixels to the right of the left edge. Padding can be set using the
346 * {@link #setPadding(int, int, int, int)} method and queried by calling
347 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
348 * {@link #getPaddingRight()} and {@link #getPaddingBottom()}.
349 * </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 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700675 * This view is visible. Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 */
677 public static final int VISIBLE = 0x00000000;
678
679 /**
680 * This view is invisible, but it still takes up space for layout purposes.
Romain Guy5c22a8c2011-05-13 11:48:45 -0700681 * Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 */
683 public static final int INVISIBLE = 0x00000004;
684
685 /**
686 * This view is invisible, and it doesn't take any space for layout
Romain Guy5c22a8c2011-05-13 11:48:45 -0700687 * purposes. Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 */
689 public static final int GONE = 0x00000008;
690
691 /**
692 * Mask for use with setFlags indicating bits used for visibility.
693 * {@hide}
694 */
695 static final int VISIBILITY_MASK = 0x0000000C;
696
697 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
698
699 /**
700 * This view is enabled. Intrepretation varies by subclass.
701 * Use with ENABLED_MASK when calling setFlags.
702 * {@hide}
703 */
704 static final int ENABLED = 0x00000000;
705
706 /**
707 * This view is disabled. Intrepretation varies by subclass.
708 * Use with ENABLED_MASK when calling setFlags.
709 * {@hide}
710 */
711 static final int DISABLED = 0x00000020;
712
713 /**
714 * Mask for use with setFlags indicating bits used for indicating whether
715 * this view is enabled
716 * {@hide}
717 */
718 static final int ENABLED_MASK = 0x00000020;
719
720 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700721 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
722 * called and further optimizations will be performed. It is okay to have
723 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 * {@hide}
725 */
726 static final int WILL_NOT_DRAW = 0x00000080;
727
728 /**
729 * Mask for use with setFlags indicating bits used for indicating whether
730 * this view is will draw
731 * {@hide}
732 */
733 static final int DRAW_MASK = 0x00000080;
734
735 /**
736 * <p>This view doesn't show scrollbars.</p>
737 * {@hide}
738 */
739 static final int SCROLLBARS_NONE = 0x00000000;
740
741 /**
742 * <p>This view shows horizontal scrollbars.</p>
743 * {@hide}
744 */
745 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
746
747 /**
748 * <p>This view shows vertical scrollbars.</p>
749 * {@hide}
750 */
751 static final int SCROLLBARS_VERTICAL = 0x00000200;
752
753 /**
754 * <p>Mask for use with setFlags indicating bits used for indicating which
755 * scrollbars are enabled.</p>
756 * {@hide}
757 */
758 static final int SCROLLBARS_MASK = 0x00000300;
759
Jeff Brown85a31762010-09-01 17:01:00 -0700760 /**
761 * Indicates that the view should filter touches when its window is obscured.
762 * Refer to the class comments for more information about this security feature.
763 * {@hide}
764 */
765 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
766
767 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768
769 /**
770 * <p>This view doesn't show fading edges.</p>
771 * {@hide}
772 */
773 static final int FADING_EDGE_NONE = 0x00000000;
774
775 /**
776 * <p>This view shows horizontal fading edges.</p>
777 * {@hide}
778 */
779 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
780
781 /**
782 * <p>This view shows vertical fading edges.</p>
783 * {@hide}
784 */
785 static final int FADING_EDGE_VERTICAL = 0x00002000;
786
787 /**
788 * <p>Mask for use with setFlags indicating bits used for indicating which
789 * fading edges are enabled.</p>
790 * {@hide}
791 */
792 static final int FADING_EDGE_MASK = 0x00003000;
793
794 /**
795 * <p>Indicates this view can be clicked. When clickable, a View reacts
796 * to clicks by notifying the OnClickListener.<p>
797 * {@hide}
798 */
799 static final int CLICKABLE = 0x00004000;
800
801 /**
802 * <p>Indicates this view is caching its drawing into a bitmap.</p>
803 * {@hide}
804 */
805 static final int DRAWING_CACHE_ENABLED = 0x00008000;
806
807 /**
808 * <p>Indicates that no icicle should be saved for this view.<p>
809 * {@hide}
810 */
811 static final int SAVE_DISABLED = 0x000010000;
812
813 /**
814 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
815 * property.</p>
816 * {@hide}
817 */
818 static final int SAVE_DISABLED_MASK = 0x000010000;
819
820 /**
821 * <p>Indicates that no drawing cache should ever be created for this view.<p>
822 * {@hide}
823 */
824 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
825
826 /**
827 * <p>Indicates this view can take / keep focus when int touch mode.</p>
828 * {@hide}
829 */
830 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
831
832 /**
833 * <p>Enables low quality mode for the drawing cache.</p>
834 */
835 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
836
837 /**
838 * <p>Enables high quality mode for the drawing cache.</p>
839 */
840 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
841
842 /**
843 * <p>Enables automatic quality mode for the drawing cache.</p>
844 */
845 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
846
847 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
848 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
849 };
850
851 /**
852 * <p>Mask for use with setFlags indicating bits used for the cache
853 * quality property.</p>
854 * {@hide}
855 */
856 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
857
858 /**
859 * <p>
860 * Indicates this view can be long clicked. When long clickable, a View
861 * reacts to long clicks by notifying the OnLongClickListener or showing a
862 * context menu.
863 * </p>
864 * {@hide}
865 */
866 static final int LONG_CLICKABLE = 0x00200000;
867
868 /**
869 * <p>Indicates that this view gets its drawable states from its direct parent
870 * and ignores its original internal states.</p>
871 *
872 * @hide
873 */
874 static final int DUPLICATE_PARENT_STATE = 0x00400000;
875
876 /**
877 * The scrollbar style to display the scrollbars inside the content area,
878 * without increasing the padding. The scrollbars will be overlaid with
879 * translucency on the view's content.
880 */
881 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
882
883 /**
884 * The scrollbar style to display the scrollbars inside the padded area,
885 * increasing the padding of the view. The scrollbars will not overlap the
886 * content area of the view.
887 */
888 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
889
890 /**
891 * The scrollbar style to display the scrollbars at the edge of the view,
892 * without increasing the padding. The scrollbars will be overlaid with
893 * translucency.
894 */
895 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
896
897 /**
898 * The scrollbar style to display the scrollbars at the edge of the view,
899 * increasing the padding of the view. The scrollbars will only overlap the
900 * background, if any.
901 */
902 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
903
904 /**
905 * Mask to check if the scrollbar style is overlay or inset.
906 * {@hide}
907 */
908 static final int SCROLLBARS_INSET_MASK = 0x01000000;
909
910 /**
911 * Mask to check if the scrollbar style is inside or outside.
912 * {@hide}
913 */
914 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
915
916 /**
917 * Mask for scrollbar style.
918 * {@hide}
919 */
920 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
921
922 /**
923 * View flag indicating that the screen should remain on while the
924 * window containing this view is visible to the user. This effectively
925 * takes care of automatically setting the WindowManager's
926 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
927 */
928 public static final int KEEP_SCREEN_ON = 0x04000000;
929
930 /**
931 * View flag indicating whether this view should have sound effects enabled
932 * for events such as clicking and touching.
933 */
934 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
935
936 /**
937 * View flag indicating whether this view should have haptic feedback
938 * enabled for events such as long presses.
939 */
940 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
941
942 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700943 * <p>Indicates that the view hierarchy should stop saving state when
944 * it reaches this view. If state saving is initiated immediately at
945 * the view, it will be allowed.
946 * {@hide}
947 */
948 static final int PARENT_SAVE_DISABLED = 0x20000000;
949
950 /**
951 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
952 * {@hide}
953 */
954 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
955
956 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800957 * Horizontal direction of this view is from Left to Right.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700958 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800959 * {@hide}
960 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700961 public static final int LAYOUT_DIRECTION_LTR = 0x00000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800962
963 /**
964 * Horizontal direction of this view is from Right to Left.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700965 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800966 * {@hide}
967 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700968 public static final int LAYOUT_DIRECTION_RTL = 0x40000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800969
970 /**
971 * Horizontal direction of this view is inherited from its parent.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700972 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800973 * {@hide}
974 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700975 public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800976
977 /**
978 * Horizontal direction of this view is from deduced from the default language
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700979 * script for the locale. Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800980 * {@hide}
981 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700982 public static final int LAYOUT_DIRECTION_LOCALE = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800983
984 /**
985 * Mask for use with setFlags indicating bits used for horizontalDirection.
986 * {@hide}
987 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700988 static final int LAYOUT_DIRECTION_MASK = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800989
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700990 /*
991 * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct
992 * flag value.
993 * {@hide}
994 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700995 private static final int[] LAYOUT_DIRECTION_FLAGS = {LAYOUT_DIRECTION_LTR,
996 LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE};
Cibu Johny7632cb92010-02-22 13:01:02 -0800997
998 /**
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700999 * Default horizontalDirection.
1000 * {@hide}
1001 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07001002 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001003
1004 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001005 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1006 * should add all focusable Views regardless if they are focusable in touch mode.
1007 */
1008 public static final int FOCUSABLES_ALL = 0x00000000;
1009
1010 /**
1011 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1012 * should add only Views focusable in touch mode.
1013 */
1014 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1015
1016 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001017 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 * item.
1019 */
1020 public static final int FOCUS_BACKWARD = 0x00000001;
1021
1022 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001023 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 * item.
1025 */
1026 public static final int FOCUS_FORWARD = 0x00000002;
1027
1028 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001029 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 */
1031 public static final int FOCUS_LEFT = 0x00000011;
1032
1033 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001034 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 */
1036 public static final int FOCUS_UP = 0x00000021;
1037
1038 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001039 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 */
1041 public static final int FOCUS_RIGHT = 0x00000042;
1042
1043 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001044 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 */
1046 public static final int FOCUS_DOWN = 0x00000082;
1047
1048 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001049 * Bits of {@link #getMeasuredWidthAndState()} and
1050 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1051 */
1052 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1053
1054 /**
1055 * Bits of {@link #getMeasuredWidthAndState()} and
1056 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1057 */
1058 public static final int MEASURED_STATE_MASK = 0xff000000;
1059
1060 /**
1061 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1062 * for functions that combine both width and height into a single int,
1063 * such as {@link #getMeasuredState()} and the childState argument of
1064 * {@link #resolveSizeAndState(int, int, int)}.
1065 */
1066 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1067
1068 /**
1069 * Bit of {@link #getMeasuredWidthAndState()} and
1070 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1071 * is smaller that the space the view would like to have.
1072 */
1073 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1074
1075 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 * Base View state sets
1077 */
1078 // Singles
1079 /**
1080 * Indicates the view has no states set. States are used with
1081 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1082 * view depending on its state.
1083 *
1084 * @see android.graphics.drawable.Drawable
1085 * @see #getDrawableState()
1086 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001087 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 /**
1089 * Indicates the view is enabled. States are used with
1090 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1091 * view depending on its state.
1092 *
1093 * @see android.graphics.drawable.Drawable
1094 * @see #getDrawableState()
1095 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001096 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 /**
1098 * Indicates the view is focused. States are used with
1099 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1100 * view depending on its state.
1101 *
1102 * @see android.graphics.drawable.Drawable
1103 * @see #getDrawableState()
1104 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001105 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 /**
1107 * Indicates the view is selected. States are used with
1108 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1109 * view depending on its state.
1110 *
1111 * @see android.graphics.drawable.Drawable
1112 * @see #getDrawableState()
1113 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001114 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 /**
1116 * Indicates the view is pressed. States are used with
1117 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1118 * view depending on its state.
1119 *
1120 * @see android.graphics.drawable.Drawable
1121 * @see #getDrawableState()
1122 * @hide
1123 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001124 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 /**
1126 * Indicates the view's window has focus. States are used with
1127 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1128 * view depending on its state.
1129 *
1130 * @see android.graphics.drawable.Drawable
1131 * @see #getDrawableState()
1132 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001133 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 // Doubles
1135 /**
1136 * Indicates the view is enabled and has the focus.
1137 *
1138 * @see #ENABLED_STATE_SET
1139 * @see #FOCUSED_STATE_SET
1140 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001141 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 /**
1143 * Indicates the view is enabled and selected.
1144 *
1145 * @see #ENABLED_STATE_SET
1146 * @see #SELECTED_STATE_SET
1147 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001148 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 /**
1150 * Indicates the view is enabled and that its window has focus.
1151 *
1152 * @see #ENABLED_STATE_SET
1153 * @see #WINDOW_FOCUSED_STATE_SET
1154 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001155 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 /**
1157 * Indicates the view is focused and selected.
1158 *
1159 * @see #FOCUSED_STATE_SET
1160 * @see #SELECTED_STATE_SET
1161 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001162 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 /**
1164 * Indicates the view has the focus and that its window has the focus.
1165 *
1166 * @see #FOCUSED_STATE_SET
1167 * @see #WINDOW_FOCUSED_STATE_SET
1168 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001169 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 /**
1171 * Indicates the view is selected and that its window has the focus.
1172 *
1173 * @see #SELECTED_STATE_SET
1174 * @see #WINDOW_FOCUSED_STATE_SET
1175 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001176 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 // Triples
1178 /**
1179 * Indicates the view is enabled, focused and selected.
1180 *
1181 * @see #ENABLED_STATE_SET
1182 * @see #FOCUSED_STATE_SET
1183 * @see #SELECTED_STATE_SET
1184 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001185 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 /**
1187 * Indicates the view is enabled, focused and its window has the focus.
1188 *
1189 * @see #ENABLED_STATE_SET
1190 * @see #FOCUSED_STATE_SET
1191 * @see #WINDOW_FOCUSED_STATE_SET
1192 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001193 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 /**
1195 * Indicates the view is enabled, selected and its window has the focus.
1196 *
1197 * @see #ENABLED_STATE_SET
1198 * @see #SELECTED_STATE_SET
1199 * @see #WINDOW_FOCUSED_STATE_SET
1200 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001201 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 /**
1203 * Indicates the view is focused, selected and its window has the focus.
1204 *
1205 * @see #FOCUSED_STATE_SET
1206 * @see #SELECTED_STATE_SET
1207 * @see #WINDOW_FOCUSED_STATE_SET
1208 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001209 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 /**
1211 * Indicates the view is enabled, focused, selected and its window
1212 * has the focus.
1213 *
1214 * @see #ENABLED_STATE_SET
1215 * @see #FOCUSED_STATE_SET
1216 * @see #SELECTED_STATE_SET
1217 * @see #WINDOW_FOCUSED_STATE_SET
1218 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001219 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 /**
1221 * Indicates the view is pressed and its window has the focus.
1222 *
1223 * @see #PRESSED_STATE_SET
1224 * @see #WINDOW_FOCUSED_STATE_SET
1225 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001226 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 /**
1228 * Indicates the view is pressed and selected.
1229 *
1230 * @see #PRESSED_STATE_SET
1231 * @see #SELECTED_STATE_SET
1232 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001233 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 /**
1235 * Indicates the view is pressed, selected and its window has the focus.
1236 *
1237 * @see #PRESSED_STATE_SET
1238 * @see #SELECTED_STATE_SET
1239 * @see #WINDOW_FOCUSED_STATE_SET
1240 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001241 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 /**
1243 * Indicates the view is pressed and focused.
1244 *
1245 * @see #PRESSED_STATE_SET
1246 * @see #FOCUSED_STATE_SET
1247 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001248 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 /**
1250 * Indicates the view is pressed, focused and its window has the focus.
1251 *
1252 * @see #PRESSED_STATE_SET
1253 * @see #FOCUSED_STATE_SET
1254 * @see #WINDOW_FOCUSED_STATE_SET
1255 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001256 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 /**
1258 * Indicates the view is pressed, focused and selected.
1259 *
1260 * @see #PRESSED_STATE_SET
1261 * @see #SELECTED_STATE_SET
1262 * @see #FOCUSED_STATE_SET
1263 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001264 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 /**
1266 * Indicates the view is pressed, focused, selected and its window has the focus.
1267 *
1268 * @see #PRESSED_STATE_SET
1269 * @see #FOCUSED_STATE_SET
1270 * @see #SELECTED_STATE_SET
1271 * @see #WINDOW_FOCUSED_STATE_SET
1272 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001273 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 /**
1275 * Indicates the view is pressed and enabled.
1276 *
1277 * @see #PRESSED_STATE_SET
1278 * @see #ENABLED_STATE_SET
1279 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001280 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 /**
1282 * Indicates the view is pressed, enabled and its window has the focus.
1283 *
1284 * @see #PRESSED_STATE_SET
1285 * @see #ENABLED_STATE_SET
1286 * @see #WINDOW_FOCUSED_STATE_SET
1287 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001288 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 /**
1290 * Indicates the view is pressed, enabled and selected.
1291 *
1292 * @see #PRESSED_STATE_SET
1293 * @see #ENABLED_STATE_SET
1294 * @see #SELECTED_STATE_SET
1295 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001296 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 /**
1298 * Indicates the view is pressed, enabled, selected and its window has the
1299 * focus.
1300 *
1301 * @see #PRESSED_STATE_SET
1302 * @see #ENABLED_STATE_SET
1303 * @see #SELECTED_STATE_SET
1304 * @see #WINDOW_FOCUSED_STATE_SET
1305 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001306 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 /**
1308 * Indicates the view is pressed, enabled and focused.
1309 *
1310 * @see #PRESSED_STATE_SET
1311 * @see #ENABLED_STATE_SET
1312 * @see #FOCUSED_STATE_SET
1313 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001314 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 /**
1316 * Indicates the view is pressed, enabled, focused and its window has the
1317 * focus.
1318 *
1319 * @see #PRESSED_STATE_SET
1320 * @see #ENABLED_STATE_SET
1321 * @see #FOCUSED_STATE_SET
1322 * @see #WINDOW_FOCUSED_STATE_SET
1323 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001324 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 /**
1326 * Indicates the view is pressed, enabled, focused and selected.
1327 *
1328 * @see #PRESSED_STATE_SET
1329 * @see #ENABLED_STATE_SET
1330 * @see #SELECTED_STATE_SET
1331 * @see #FOCUSED_STATE_SET
1332 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001333 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 /**
1335 * Indicates the view is pressed, enabled, focused, selected and its window
1336 * has the focus.
1337 *
1338 * @see #PRESSED_STATE_SET
1339 * @see #ENABLED_STATE_SET
1340 * @see #SELECTED_STATE_SET
1341 * @see #FOCUSED_STATE_SET
1342 * @see #WINDOW_FOCUSED_STATE_SET
1343 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001344 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345
1346 /**
1347 * The order here is very important to {@link #getDrawableState()}
1348 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001349 private static final int[][] VIEW_STATE_SETS;
1350
Romain Guyb051e892010-09-28 19:09:36 -07001351 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1352 static final int VIEW_STATE_SELECTED = 1 << 1;
1353 static final int VIEW_STATE_FOCUSED = 1 << 2;
1354 static final int VIEW_STATE_ENABLED = 1 << 3;
1355 static final int VIEW_STATE_PRESSED = 1 << 4;
1356 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001357 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001358 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001359 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1360 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001361
1362 static final int[] VIEW_STATE_IDS = new int[] {
1363 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1364 R.attr.state_selected, VIEW_STATE_SELECTED,
1365 R.attr.state_focused, VIEW_STATE_FOCUSED,
1366 R.attr.state_enabled, VIEW_STATE_ENABLED,
1367 R.attr.state_pressed, VIEW_STATE_PRESSED,
1368 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001369 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001370 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001371 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1372 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 };
1374
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001375 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001376 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1377 throw new IllegalStateException(
1378 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1379 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001380 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001381 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001382 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001383 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001385 orderedIds[i * 2] = viewState;
1386 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001387 }
1388 }
1389 }
Romain Guyb051e892010-09-28 19:09:36 -07001390 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1391 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1392 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001393 int numBits = Integer.bitCount(i);
1394 int[] set = new int[numBits];
1395 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001396 for (int j = 0; j < orderedIds.length; j += 2) {
1397 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001398 set[pos++] = orderedIds[j];
1399 }
1400 }
1401 VIEW_STATE_SETS[i] = set;
1402 }
1403
1404 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1405 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1406 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1407 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1408 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1409 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1410 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1411 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1412 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1413 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1414 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1416 | VIEW_STATE_FOCUSED];
1417 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1418 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1419 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1420 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1421 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1422 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1424 | VIEW_STATE_ENABLED];
1425 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1426 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1427 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1428 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1429 | VIEW_STATE_ENABLED];
1430 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1431 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1432 | VIEW_STATE_ENABLED];
1433 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1434 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1435 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1436
1437 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1438 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1439 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1440 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1441 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1442 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1444 | VIEW_STATE_PRESSED];
1445 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1447 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1449 | VIEW_STATE_PRESSED];
1450 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1451 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1452 | VIEW_STATE_PRESSED];
1453 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1454 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1455 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1456 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1457 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1458 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1459 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1460 | VIEW_STATE_PRESSED];
1461 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1462 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1463 | VIEW_STATE_PRESSED];
1464 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1465 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1466 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1467 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1468 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1469 | VIEW_STATE_PRESSED];
1470 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1471 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1472 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1473 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1474 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1475 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1476 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1477 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1478 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1479 | VIEW_STATE_PRESSED];
1480 }
1481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 * Temporary Rect currently for use in setBackground(). This will probably
1484 * be extended in the future to hold our own class with more than just
1485 * a Rect. :)
1486 */
1487 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001488
1489 /**
1490 * Map used to store views' tags.
1491 */
1492 private static WeakHashMap<View, SparseArray<Object>> sTags;
1493
1494 /**
1495 * Lock used to access sTags.
1496 */
1497 private static final Object sTagsLock = new Object();
1498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001500 * The next available accessiiblity id.
1501 */
1502 private static int sNextAccessibilityViewId;
1503
1504 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 * The animation currently associated with this view.
1506 * @hide
1507 */
1508 protected Animation mCurrentAnimation = null;
1509
1510 /**
1511 * Width as measured during measure pass.
1512 * {@hide}
1513 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001514 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001515 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516
1517 /**
1518 * Height as measured during measure pass.
1519 * {@hide}
1520 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001521 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001522 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523
1524 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001525 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1526 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1527 * its display list. This flag, used only when hw accelerated, allows us to clear the
1528 * flag while retaining this information until it's needed (at getDisplayList() time and
1529 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1530 *
1531 * {@hide}
1532 */
1533 boolean mRecreateDisplayList = false;
1534
1535 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 * The view's identifier.
1537 * {@hide}
1538 *
1539 * @see #setId(int)
1540 * @see #getId()
1541 */
1542 @ViewDebug.ExportedProperty(resolveId = true)
1543 int mID = NO_ID;
1544
1545 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001546 * The stable ID of this view for accessibility porposes.
1547 */
1548 int mAccessibilityViewId = NO_ID;
1549
1550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 * The view's tag.
1552 * {@hide}
1553 *
1554 * @see #setTag(Object)
1555 * @see #getTag()
1556 */
1557 protected Object mTag;
1558
1559 // for mPrivateFlags:
1560 /** {@hide} */
1561 static final int WANTS_FOCUS = 0x00000001;
1562 /** {@hide} */
1563 static final int FOCUSED = 0x00000002;
1564 /** {@hide} */
1565 static final int SELECTED = 0x00000004;
1566 /** {@hide} */
1567 static final int IS_ROOT_NAMESPACE = 0x00000008;
1568 /** {@hide} */
1569 static final int HAS_BOUNDS = 0x00000010;
1570 /** {@hide} */
1571 static final int DRAWN = 0x00000020;
1572 /**
1573 * When this flag is set, this view is running an animation on behalf of its
1574 * children and should therefore not cancel invalidate requests, even if they
1575 * lie outside of this view's bounds.
1576 *
1577 * {@hide}
1578 */
1579 static final int DRAW_ANIMATION = 0x00000040;
1580 /** {@hide} */
1581 static final int SKIP_DRAW = 0x00000080;
1582 /** {@hide} */
1583 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1584 /** {@hide} */
1585 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1586 /** {@hide} */
1587 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1588 /** {@hide} */
1589 static final int MEASURED_DIMENSION_SET = 0x00000800;
1590 /** {@hide} */
1591 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001592 /** {@hide} */
1593 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594
1595 private static final int PRESSED = 0x00004000;
1596
1597 /** {@hide} */
1598 static final int DRAWING_CACHE_VALID = 0x00008000;
1599 /**
1600 * Flag used to indicate that this view should be drawn once more (and only once
1601 * more) after its animation has completed.
1602 * {@hide}
1603 */
1604 static final int ANIMATION_STARTED = 0x00010000;
1605
1606 private static final int SAVE_STATE_CALLED = 0x00020000;
1607
1608 /**
1609 * Indicates that the View returned true when onSetAlpha() was called and that
1610 * the alpha must be restored.
1611 * {@hide}
1612 */
1613 static final int ALPHA_SET = 0x00040000;
1614
1615 /**
1616 * Set by {@link #setScrollContainer(boolean)}.
1617 */
1618 static final int SCROLL_CONTAINER = 0x00080000;
1619
1620 /**
1621 * Set by {@link #setScrollContainer(boolean)}.
1622 */
1623 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1624
1625 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001626 * View flag indicating whether this view was invalidated (fully or partially.)
1627 *
1628 * @hide
1629 */
1630 static final int DIRTY = 0x00200000;
1631
1632 /**
1633 * View flag indicating whether this view was invalidated by an opaque
1634 * invalidate request.
1635 *
1636 * @hide
1637 */
1638 static final int DIRTY_OPAQUE = 0x00400000;
1639
1640 /**
1641 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1642 *
1643 * @hide
1644 */
1645 static final int DIRTY_MASK = 0x00600000;
1646
1647 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001648 * Indicates whether the background is opaque.
1649 *
1650 * @hide
1651 */
1652 static final int OPAQUE_BACKGROUND = 0x00800000;
1653
1654 /**
1655 * Indicates whether the scrollbars are opaque.
1656 *
1657 * @hide
1658 */
1659 static final int OPAQUE_SCROLLBARS = 0x01000000;
1660
1661 /**
1662 * Indicates whether the view is opaque.
1663 *
1664 * @hide
1665 */
1666 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001667
Adam Powelle14579b2009-12-16 18:39:52 -08001668 /**
1669 * Indicates a prepressed state;
1670 * the short time between ACTION_DOWN and recognizing
1671 * a 'real' press. Prepressed is used to recognize quick taps
1672 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001673 *
Adam Powelle14579b2009-12-16 18:39:52 -08001674 * @hide
1675 */
1676 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001677
Adam Powellc9fbaab2010-02-16 17:16:19 -08001678 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001679 * Indicates whether the view is temporarily detached.
1680 *
1681 * @hide
1682 */
1683 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001684
Adam Powell8568c3a2010-04-19 14:26:11 -07001685 /**
1686 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001687 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001688 * @hide
1689 */
1690 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001691
1692 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001693 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1694 * @hide
1695 */
1696 private static final int HOVERED = 0x10000000;
1697
1698 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001699 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1700 * for transform operations
1701 *
1702 * @hide
1703 */
Adam Powellf37df072010-09-17 16:22:49 -07001704 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001705
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001706 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001707 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001708
Chet Haasefd2b0022010-08-06 13:08:56 -07001709 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001710 * Indicates that this view was specifically invalidated, not just dirtied because some
1711 * child view was invalidated. The flag is used to determine when we need to recreate
1712 * a view's display list (as opposed to just returning a reference to its existing
1713 * display list).
1714 *
1715 * @hide
1716 */
1717 static final int INVALIDATED = 0x80000000;
1718
Christopher Tate3d4bf172011-03-28 16:16:46 -07001719 /* Masks for mPrivateFlags2 */
1720
1721 /**
1722 * Indicates that this view has reported that it can accept the current drag's content.
1723 * Cleared when the drag operation concludes.
1724 * @hide
1725 */
1726 static final int DRAG_CAN_ACCEPT = 0x00000001;
1727
1728 /**
1729 * Indicates that this view is currently directly under the drag location in a
1730 * drag-and-drop operation involving content that it can accept. Cleared when
1731 * the drag exits the view, or when the drag operation concludes.
1732 * @hide
1733 */
1734 static final int DRAG_HOVERED = 0x00000002;
1735
Cibu Johny86666632010-02-22 13:01:02 -08001736 /**
1737 * Indicates whether the view is drawn in right-to-left direction.
1738 *
1739 * @hide
1740 */
1741 static final int RESOLVED_LAYOUT_RTL = 0x00000004;
1742
Christopher Tate3d4bf172011-03-28 16:16:46 -07001743 /* End of masks for mPrivateFlags2 */
1744
1745 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1746
Chet Haasedaf98e92011-01-10 14:10:36 -08001747 /**
Adam Powell637d3372010-08-25 14:37:03 -07001748 * Always allow a user to over-scroll this view, provided it is a
1749 * view that can scroll.
1750 *
1751 * @see #getOverScrollMode()
1752 * @see #setOverScrollMode(int)
1753 */
1754 public static final int OVER_SCROLL_ALWAYS = 0;
1755
1756 /**
1757 * Allow a user to over-scroll this view only if the content is large
1758 * enough to meaningfully scroll, provided it is a view that can scroll.
1759 *
1760 * @see #getOverScrollMode()
1761 * @see #setOverScrollMode(int)
1762 */
1763 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1764
1765 /**
1766 * Never allow a user to over-scroll this view.
1767 *
1768 * @see #getOverScrollMode()
1769 * @see #setOverScrollMode(int)
1770 */
1771 public static final int OVER_SCROLL_NEVER = 2;
1772
1773 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001774 * View has requested the status bar to be visible (the default).
1775 *
Joe Malin32736f02011-01-19 16:14:20 -08001776 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001777 */
1778 public static final int STATUS_BAR_VISIBLE = 0;
1779
1780 /**
Jeff Brown05dc66a2011-03-02 14:41:58 -08001781 * View has requested the status bar to be hidden.
Joe Onorato664644d2011-01-23 17:53:23 -08001782 *
Joe Malin32736f02011-01-19 16:14:20 -08001783 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001784 */
1785 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1786
1787 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001788 * @hide
1789 *
1790 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1791 * out of the public fields to keep the undefined bits out of the developer's way.
1792 *
1793 * Flag to make the status bar not expandable. Unless you also
1794 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1795 */
1796 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1797
1798 /**
1799 * @hide
1800 *
1801 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1802 * out of the public fields to keep the undefined bits out of the developer's way.
1803 *
1804 * Flag to hide notification icons and scrolling ticker text.
1805 */
1806 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1807
1808 /**
1809 * @hide
1810 *
1811 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1812 * out of the public fields to keep the undefined bits out of the developer's way.
1813 *
1814 * Flag to disable incoming notification alerts. This will not block
1815 * icons, but it will block sound, vibrating and other visual or aural notifications.
1816 */
1817 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1818
1819 /**
1820 * @hide
1821 *
1822 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1823 * out of the public fields to keep the undefined bits out of the developer's way.
1824 *
1825 * Flag to hide only the scrolling ticker. Note that
1826 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1827 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1828 */
1829 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1830
1831 /**
1832 * @hide
1833 *
1834 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1835 * out of the public fields to keep the undefined bits out of the developer's way.
1836 *
1837 * Flag to hide the center system info area.
1838 */
1839 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1840
1841 /**
1842 * @hide
1843 *
1844 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1845 * out of the public fields to keep the undefined bits out of the developer's way.
1846 *
1847 * Flag to hide only the navigation buttons. Don't use this
1848 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001849 *
1850 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001851 */
1852 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
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 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001860 * Flag to hide only the back button. Don't use this
1861 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1862 */
1863 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1864
1865 /**
1866 * @hide
1867 *
1868 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1869 * out of the public fields to keep the undefined bits out of the developer's way.
1870 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001871 * Flag to hide only the clock. You might use this if your activity has
1872 * its own clock making the status bar's clock redundant.
1873 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001874 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1875
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001876 /**
1877 * @hide
1878 */
1879 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001880
1881 /**
Adam Powell637d3372010-08-25 14:37:03 -07001882 * Controls the over-scroll mode for this view.
1883 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1884 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1885 * and {@link #OVER_SCROLL_NEVER}.
1886 */
1887 private int mOverScrollMode;
1888
1889 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 * The parent this view is attached to.
1891 * {@hide}
1892 *
1893 * @see #getParent()
1894 */
1895 protected ViewParent mParent;
1896
1897 /**
1898 * {@hide}
1899 */
1900 AttachInfo mAttachInfo;
1901
1902 /**
1903 * {@hide}
1904 */
Romain Guy809a7f62009-05-14 15:44:42 -07001905 @ViewDebug.ExportedProperty(flagMapping = {
1906 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1907 name = "FORCE_LAYOUT"),
1908 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1909 name = "LAYOUT_REQUIRED"),
1910 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001911 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001912 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1913 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1914 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1915 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1916 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001918 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919
1920 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001921 * This view's request for the visibility of the status bar.
1922 * @hide
1923 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001924 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001925 int mSystemUiVisibility;
1926
1927 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 * Count of how many windows this view has been attached to.
1929 */
1930 int mWindowAttachCount;
1931
1932 /**
1933 * The layout parameters associated with this view and used by the parent
1934 * {@link android.view.ViewGroup} to determine how this view should be
1935 * laid out.
1936 * {@hide}
1937 */
1938 protected ViewGroup.LayoutParams mLayoutParams;
1939
1940 /**
1941 * The view flags hold various views states.
1942 * {@hide}
1943 */
1944 @ViewDebug.ExportedProperty
1945 int mViewFlags;
1946
1947 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001948 * The transform matrix for the View. This transform is calculated internally
1949 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1950 * is used by default. Do *not* use this variable directly; instead call
1951 * getMatrix(), which will automatically recalculate the matrix if necessary
1952 * to get the correct matrix based on the latest rotation and scale properties.
1953 */
1954 private final Matrix mMatrix = new Matrix();
1955
1956 /**
1957 * The transform matrix for the View. This transform is calculated internally
1958 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1959 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001960 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001961 * to get the correct matrix based on the latest rotation and scale properties.
1962 */
1963 private Matrix mInverseMatrix;
1964
1965 /**
1966 * An internal variable that tracks whether we need to recalculate the
1967 * transform matrix, based on whether the rotation or scaleX/Y properties
1968 * have changed since the matrix was last calculated.
1969 */
Chet Haasea00f3862011-02-22 06:34:40 -08001970 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07001971
1972 /**
1973 * An internal variable that tracks whether we need to recalculate the
1974 * transform matrix, based on whether the rotation or scaleX/Y properties
1975 * have changed since the matrix was last calculated.
1976 */
1977 private boolean mInverseMatrixDirty = true;
1978
1979 /**
1980 * A variable that tracks whether we need to recalculate the
1981 * transform matrix, based on whether the rotation or scaleX/Y properties
1982 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001983 * is only valid after a call to updateMatrix() or to a function that
1984 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001985 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001986 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001987
1988 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001989 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
1990 */
1991 private Camera mCamera = null;
1992
1993 /**
1994 * This matrix is used when computing the matrix for 3D rotations.
1995 */
1996 private Matrix matrix3D = null;
1997
1998 /**
1999 * These prev values are used to recalculate a centered pivot point when necessary. The
2000 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2001 * set), so thes values are only used then as well.
2002 */
2003 private int mPrevWidth = -1;
2004 private int mPrevHeight = -1;
2005
Joe Malin32736f02011-01-19 16:14:20 -08002006 private boolean mLastIsOpaque;
2007
Chet Haasefd2b0022010-08-06 13:08:56 -07002008 /**
2009 * Convenience value to check for float values that are close enough to zero to be considered
2010 * zero.
2011 */
Romain Guy2542d192010-08-18 11:47:12 -07002012 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002013
2014 /**
2015 * The degrees rotation around the vertical axis through the pivot point.
2016 */
2017 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002018 float mRotationY = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002019
2020 /**
2021 * The degrees rotation around the horizontal axis through the pivot point.
2022 */
2023 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002024 float mRotationX = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002025
2026 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002027 * The degrees rotation around the pivot point.
2028 */
2029 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002030 float mRotation = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002031
2032 /**
Chet Haasedf030d22010-07-30 17:22:38 -07002033 * The amount of translation of the object away from its left property (post-layout).
2034 */
2035 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002036 float mTranslationX = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002037
2038 /**
2039 * The amount of translation of the object away from its top property (post-layout).
2040 */
2041 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002042 float mTranslationY = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002043
2044 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002045 * The amount of scale in the x direction around the pivot point. A
2046 * value of 1 means no scaling is applied.
2047 */
2048 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002049 float mScaleX = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002050
2051 /**
2052 * The amount of scale in the y direction around the pivot point. A
2053 * value of 1 means no scaling is applied.
2054 */
2055 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002056 float mScaleY = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002057
2058 /**
2059 * The amount of scale in the x direction around the pivot point. A
2060 * value of 1 means no scaling is applied.
2061 */
2062 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002063 float mPivotX = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002064
2065 /**
2066 * The amount of scale in the y direction around the pivot point. A
2067 * value of 1 means no scaling is applied.
2068 */
2069 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002070 float mPivotY = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002071
2072 /**
2073 * The opacity of the View. This is a value from 0 to 1, where 0 means
2074 * completely transparent and 1 means completely opaque.
2075 */
2076 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002077 float mAlpha = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002078
2079 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 * The distance in pixels from the left edge of this view's parent
2081 * to the left edge of this view.
2082 * {@hide}
2083 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002084 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 protected int mLeft;
2086 /**
2087 * The distance in pixels from the left edge of this view's parent
2088 * to the right edge of this view.
2089 * {@hide}
2090 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002091 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 protected int mRight;
2093 /**
2094 * The distance in pixels from the top edge of this view's parent
2095 * to the top edge of this view.
2096 * {@hide}
2097 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002098 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 protected int mTop;
2100 /**
2101 * The distance in pixels from the top edge of this view's parent
2102 * to the bottom edge of this view.
2103 * {@hide}
2104 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002105 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 protected int mBottom;
2107
2108 /**
2109 * The offset, in pixels, by which the content of this view is scrolled
2110 * horizontally.
2111 * {@hide}
2112 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002113 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 protected int mScrollX;
2115 /**
2116 * The offset, in pixels, by which the content of this view is scrolled
2117 * vertically.
2118 * {@hide}
2119 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002120 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 protected int mScrollY;
2122
2123 /**
2124 * The left padding in pixels, that is the distance in pixels between the
2125 * left edge of this view and the left edge of its content.
2126 * {@hide}
2127 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002128 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 protected int mPaddingLeft;
2130 /**
2131 * The right padding in pixels, that is the distance in pixels between the
2132 * right edge of this view and the right edge of its content.
2133 * {@hide}
2134 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002135 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 protected int mPaddingRight;
2137 /**
2138 * The top padding in pixels, that is the distance in pixels between the
2139 * top edge of this view and the top edge of its content.
2140 * {@hide}
2141 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002142 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 protected int mPaddingTop;
2144 /**
2145 * The bottom padding in pixels, that is the distance in pixels between the
2146 * bottom edge of this view and the bottom edge of its content.
2147 * {@hide}
2148 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002149 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 protected int mPaddingBottom;
2151
2152 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002153 * Briefly describes the view and is primarily used for accessibility support.
2154 */
2155 private CharSequence mContentDescription;
2156
2157 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 * Cache the paddingRight set by the user to append to the scrollbar's size.
2159 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002160 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 int mUserPaddingRight;
2162
2163 /**
2164 * Cache the paddingBottom set by the user to append to the scrollbar's size.
2165 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002166 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 int mUserPaddingBottom;
2168
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002169 /**
Adam Powell20232d02010-12-08 21:08:53 -08002170 * Cache the paddingLeft set by the user to append to the scrollbar's size.
2171 */
2172 @ViewDebug.ExportedProperty(category = "padding")
2173 int mUserPaddingLeft;
2174
2175 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002176 * @hide
2177 */
2178 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2179 /**
2180 * @hide
2181 */
2182 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183
2184 private Resources mResources = null;
2185
2186 private Drawable mBGDrawable;
2187
2188 private int mBackgroundResource;
2189 private boolean mBackgroundSizeChanged;
2190
2191 /**
2192 * Listener used to dispatch focus change events.
2193 * This field should be made private, so it is hidden from the SDK.
2194 * {@hide}
2195 */
2196 protected OnFocusChangeListener mOnFocusChangeListener;
2197
2198 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002199 * Listeners for layout change events.
2200 */
2201 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2202
2203 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002204 * Listeners for attach events.
2205 */
2206 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2207
2208 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 * Listener used to dispatch click events.
2210 * This field should be made private, so it is hidden from the SDK.
2211 * {@hide}
2212 */
2213 protected OnClickListener mOnClickListener;
2214
2215 /**
2216 * Listener used to dispatch long click events.
2217 * This field should be made private, so it is hidden from the SDK.
2218 * {@hide}
2219 */
2220 protected OnLongClickListener mOnLongClickListener;
2221
2222 /**
2223 * Listener used to build the context menu.
2224 * This field should be made private, so it is hidden from the SDK.
2225 * {@hide}
2226 */
2227 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2228
2229 private OnKeyListener mOnKeyListener;
2230
2231 private OnTouchListener mOnTouchListener;
2232
Jeff Brown33bbfd22011-02-24 20:55:35 -08002233 private OnGenericMotionListener mOnGenericMotionListener;
2234
Chris Tate32affef2010-10-18 15:29:21 -07002235 private OnDragListener mOnDragListener;
2236
Joe Onorato664644d2011-01-23 17:53:23 -08002237 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 /**
2240 * The application environment this view lives in.
2241 * This field should be made private, so it is hidden from the SDK.
2242 * {@hide}
2243 */
2244 protected Context mContext;
2245
2246 private ScrollabilityCache mScrollCache;
2247
2248 private int[] mDrawableState = null;
2249
Romain Guy0211a0a2011-02-14 16:34:59 -08002250 /**
2251 * Set to true when drawing cache is enabled and cannot be created.
2252 *
2253 * @hide
2254 */
2255 public boolean mCachingFailed;
2256
Romain Guy02890fd2010-08-06 17:58:44 -07002257 private Bitmap mDrawingCache;
2258 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002259 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002260 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261
2262 /**
2263 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2264 * the user may specify which view to go to next.
2265 */
2266 private int mNextFocusLeftId = View.NO_ID;
2267
2268 /**
2269 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2270 * the user may specify which view to go to next.
2271 */
2272 private int mNextFocusRightId = View.NO_ID;
2273
2274 /**
2275 * When this view has focus and the next focus is {@link #FOCUS_UP},
2276 * the user may specify which view to go to next.
2277 */
2278 private int mNextFocusUpId = View.NO_ID;
2279
2280 /**
2281 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2282 * the user may specify which view to go to next.
2283 */
2284 private int mNextFocusDownId = View.NO_ID;
2285
Jeff Brown4e6319b2010-12-13 10:36:51 -08002286 /**
2287 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2288 * the user may specify which view to go to next.
2289 */
2290 int mNextFocusForwardId = View.NO_ID;
2291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002293 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002294 private PerformClick mPerformClick;
Joe Malin32736f02011-01-19 16:14:20 -08002295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 private UnsetPressedState mUnsetPressedState;
2297
2298 /**
2299 * Whether the long press's action has been invoked. The tap's action is invoked on the
2300 * up event while a long press is invoked as soon as the long press duration is reached, so
2301 * a long press could be performed before the tap is checked, in which case the tap's action
2302 * should not be invoked.
2303 */
2304 private boolean mHasPerformedLongPress;
2305
2306 /**
2307 * The minimum height of the view. We'll try our best to have the height
2308 * of this view to at least this amount.
2309 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002310 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 private int mMinHeight;
2312
2313 /**
2314 * The minimum width of the view. We'll try our best to have the width
2315 * of this view to at least this amount.
2316 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002317 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 private int mMinWidth;
2319
2320 /**
2321 * The delegate to handle touch events that are physically in this view
2322 * but should be handled by another view.
2323 */
2324 private TouchDelegate mTouchDelegate = null;
2325
2326 /**
2327 * Solid color to use as a background when creating the drawing cache. Enables
2328 * the cache to use 16 bit bitmaps instead of 32 bit.
2329 */
2330 private int mDrawingCacheBackgroundColor = 0;
2331
2332 /**
2333 * Special tree observer used when mAttachInfo is null.
2334 */
2335 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002336
Adam Powelle14579b2009-12-16 18:39:52 -08002337 /**
2338 * Cache the touch slop from the context that created the view.
2339 */
2340 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002343 * Object that handles automatic animation of view properties.
2344 */
2345 private ViewPropertyAnimator mAnimator = null;
2346
2347 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002348 * Flag indicating that a drag can cross window boundaries. When
2349 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2350 * with this flag set, all visible applications will be able to participate
2351 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002352 *
2353 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002354 */
2355 public static final int DRAG_FLAG_GLOBAL = 1;
2356
2357 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002358 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2359 */
2360 private float mVerticalScrollFactor;
2361
2362 /**
Adam Powell20232d02010-12-08 21:08:53 -08002363 * Position of the vertical scroll bar.
2364 */
2365 private int mVerticalScrollbarPosition;
2366
2367 /**
2368 * Position the scroll bar at the default position as determined by the system.
2369 */
2370 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2371
2372 /**
2373 * Position the scroll bar along the left edge.
2374 */
2375 public static final int SCROLLBAR_POSITION_LEFT = 1;
2376
2377 /**
2378 * Position the scroll bar along the right edge.
2379 */
2380 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2381
2382 /**
Romain Guy171c5922011-01-06 10:04:23 -08002383 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002384 *
2385 * @see #getLayerType()
2386 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002387 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002388 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002389 */
2390 public static final int LAYER_TYPE_NONE = 0;
2391
2392 /**
2393 * <p>Indicates that the view has a software layer. A software layer is backed
2394 * by a bitmap and causes the view to be rendered using Android's software
2395 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002396 *
Romain Guy171c5922011-01-06 10:04:23 -08002397 * <p>Software layers have various usages:</p>
2398 * <p>When the application is not using hardware acceleration, a software layer
2399 * is useful to apply a specific color filter and/or blending mode and/or
2400 * translucency to a view and all its children.</p>
2401 * <p>When the application is using hardware acceleration, a software layer
2402 * is useful to render drawing primitives not supported by the hardware
2403 * accelerated pipeline. It can also be used to cache a complex view tree
2404 * into a texture and reduce the complexity of drawing operations. For instance,
2405 * when animating a complex view tree with a translation, a software layer can
2406 * be used to render the view tree only once.</p>
2407 * <p>Software layers should be avoided when the affected view tree updates
2408 * often. Every update will require to re-render the software layer, which can
2409 * potentially be slow (particularly when hardware acceleration is turned on
2410 * since the layer will have to be uploaded into a hardware texture after every
2411 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002412 *
2413 * @see #getLayerType()
2414 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002415 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002416 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002417 */
2418 public static final int LAYER_TYPE_SOFTWARE = 1;
2419
2420 /**
2421 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2422 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2423 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2424 * rendering pipeline, but only if hardware acceleration is turned on for the
2425 * view hierarchy. When hardware acceleration is turned off, hardware layers
2426 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002427 *
Romain Guy171c5922011-01-06 10:04:23 -08002428 * <p>A hardware layer is useful to apply a specific color filter and/or
2429 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002430 * <p>A hardware layer can be used to cache a complex view tree into a
2431 * texture and reduce the complexity of drawing operations. For instance,
2432 * when animating a complex view tree with a translation, a hardware layer can
2433 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002434 * <p>A hardware layer can also be used to increase the rendering quality when
2435 * rotation transformations are applied on a view. It can also be used to
2436 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002437 *
2438 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002439 * @see #setLayerType(int, android.graphics.Paint)
2440 * @see #LAYER_TYPE_NONE
2441 * @see #LAYER_TYPE_SOFTWARE
2442 */
2443 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002444
Romain Guy3aaff3a2011-01-12 14:18:47 -08002445 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2446 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2447 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2448 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2449 })
Romain Guy171c5922011-01-06 10:04:23 -08002450 int mLayerType = LAYER_TYPE_NONE;
2451 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002452 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002453
2454 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002455 * Consistency verifier for debugging purposes.
2456 * @hide
2457 */
2458 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2459 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2460 new InputEventConsistencyVerifier(this, 0) : null;
2461
2462 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 * Simple constructor to use when creating a view from code.
2464 *
2465 * @param context The Context the view is running in, through which it can
2466 * access the current theme, resources, etc.
2467 */
2468 public View(Context context) {
2469 mContext = context;
2470 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002471 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002472 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002473 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 }
2475
2476 /**
2477 * Constructor that is called when inflating a view from XML. This is called
2478 * when a view is being constructed from an XML file, supplying attributes
2479 * that were specified in the XML file. This version uses a default style of
2480 * 0, so the only attribute values applied are those in the Context's Theme
2481 * and the given AttributeSet.
2482 *
2483 * <p>
2484 * The method onFinishInflate() will be called after all children have been
2485 * added.
2486 *
2487 * @param context The Context the view is running in, through which it can
2488 * access the current theme, resources, etc.
2489 * @param attrs The attributes of the XML tag that is inflating the view.
2490 * @see #View(Context, AttributeSet, int)
2491 */
2492 public View(Context context, AttributeSet attrs) {
2493 this(context, attrs, 0);
2494 }
2495
2496 /**
2497 * Perform inflation from XML and apply a class-specific base style. This
2498 * constructor of View allows subclasses to use their own base style when
2499 * they are inflating. For example, a Button class's constructor would call
2500 * this version of the super class constructor and supply
2501 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2502 * the theme's button style to modify all of the base view attributes (in
2503 * particular its background) as well as the Button class's attributes.
2504 *
2505 * @param context The Context the view is running in, through which it can
2506 * access the current theme, resources, etc.
2507 * @param attrs The attributes of the XML tag that is inflating the view.
2508 * @param defStyle The default style to apply to this view. If 0, no style
2509 * will be applied (beyond what is included in the theme). This may
2510 * either be an attribute resource, whose value will be retrieved
2511 * from the current theme, or an explicit style resource.
2512 * @see #View(Context, AttributeSet)
2513 */
2514 public View(Context context, AttributeSet attrs, int defStyle) {
2515 this(context);
2516
2517 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2518 defStyle, 0);
2519
2520 Drawable background = null;
2521
2522 int leftPadding = -1;
2523 int topPadding = -1;
2524 int rightPadding = -1;
2525 int bottomPadding = -1;
2526
2527 int padding = -1;
2528
2529 int viewFlagValues = 0;
2530 int viewFlagMasks = 0;
2531
2532 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 int x = 0;
2535 int y = 0;
2536
Chet Haase73066682010-11-29 15:55:32 -08002537 float tx = 0;
2538 float ty = 0;
2539 float rotation = 0;
2540 float rotationX = 0;
2541 float rotationY = 0;
2542 float sx = 1f;
2543 float sy = 1f;
2544 boolean transformSet = false;
2545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2547
Adam Powell637d3372010-08-25 14:37:03 -07002548 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 final int N = a.getIndexCount();
2550 for (int i = 0; i < N; i++) {
2551 int attr = a.getIndex(i);
2552 switch (attr) {
2553 case com.android.internal.R.styleable.View_background:
2554 background = a.getDrawable(attr);
2555 break;
2556 case com.android.internal.R.styleable.View_padding:
2557 padding = a.getDimensionPixelSize(attr, -1);
2558 break;
2559 case com.android.internal.R.styleable.View_paddingLeft:
2560 leftPadding = a.getDimensionPixelSize(attr, -1);
2561 break;
2562 case com.android.internal.R.styleable.View_paddingTop:
2563 topPadding = a.getDimensionPixelSize(attr, -1);
2564 break;
2565 case com.android.internal.R.styleable.View_paddingRight:
2566 rightPadding = a.getDimensionPixelSize(attr, -1);
2567 break;
2568 case com.android.internal.R.styleable.View_paddingBottom:
2569 bottomPadding = a.getDimensionPixelSize(attr, -1);
2570 break;
2571 case com.android.internal.R.styleable.View_scrollX:
2572 x = a.getDimensionPixelOffset(attr, 0);
2573 break;
2574 case com.android.internal.R.styleable.View_scrollY:
2575 y = a.getDimensionPixelOffset(attr, 0);
2576 break;
Chet Haase73066682010-11-29 15:55:32 -08002577 case com.android.internal.R.styleable.View_alpha:
2578 setAlpha(a.getFloat(attr, 1f));
2579 break;
2580 case com.android.internal.R.styleable.View_transformPivotX:
2581 setPivotX(a.getDimensionPixelOffset(attr, 0));
2582 break;
2583 case com.android.internal.R.styleable.View_transformPivotY:
2584 setPivotY(a.getDimensionPixelOffset(attr, 0));
2585 break;
2586 case com.android.internal.R.styleable.View_translationX:
2587 tx = a.getDimensionPixelOffset(attr, 0);
2588 transformSet = true;
2589 break;
2590 case com.android.internal.R.styleable.View_translationY:
2591 ty = a.getDimensionPixelOffset(attr, 0);
2592 transformSet = true;
2593 break;
2594 case com.android.internal.R.styleable.View_rotation:
2595 rotation = a.getFloat(attr, 0);
2596 transformSet = true;
2597 break;
2598 case com.android.internal.R.styleable.View_rotationX:
2599 rotationX = a.getFloat(attr, 0);
2600 transformSet = true;
2601 break;
2602 case com.android.internal.R.styleable.View_rotationY:
2603 rotationY = a.getFloat(attr, 0);
2604 transformSet = true;
2605 break;
2606 case com.android.internal.R.styleable.View_scaleX:
2607 sx = a.getFloat(attr, 1f);
2608 transformSet = true;
2609 break;
2610 case com.android.internal.R.styleable.View_scaleY:
2611 sy = a.getFloat(attr, 1f);
2612 transformSet = true;
2613 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 case com.android.internal.R.styleable.View_id:
2615 mID = a.getResourceId(attr, NO_ID);
2616 break;
2617 case com.android.internal.R.styleable.View_tag:
2618 mTag = a.getText(attr);
2619 break;
2620 case com.android.internal.R.styleable.View_fitsSystemWindows:
2621 if (a.getBoolean(attr, false)) {
2622 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2623 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2624 }
2625 break;
2626 case com.android.internal.R.styleable.View_focusable:
2627 if (a.getBoolean(attr, false)) {
2628 viewFlagValues |= FOCUSABLE;
2629 viewFlagMasks |= FOCUSABLE_MASK;
2630 }
2631 break;
2632 case com.android.internal.R.styleable.View_focusableInTouchMode:
2633 if (a.getBoolean(attr, false)) {
2634 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2635 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2636 }
2637 break;
2638 case com.android.internal.R.styleable.View_clickable:
2639 if (a.getBoolean(attr, false)) {
2640 viewFlagValues |= CLICKABLE;
2641 viewFlagMasks |= CLICKABLE;
2642 }
2643 break;
2644 case com.android.internal.R.styleable.View_longClickable:
2645 if (a.getBoolean(attr, false)) {
2646 viewFlagValues |= LONG_CLICKABLE;
2647 viewFlagMasks |= LONG_CLICKABLE;
2648 }
2649 break;
2650 case com.android.internal.R.styleable.View_saveEnabled:
2651 if (!a.getBoolean(attr, true)) {
2652 viewFlagValues |= SAVE_DISABLED;
2653 viewFlagMasks |= SAVE_DISABLED_MASK;
2654 }
2655 break;
2656 case com.android.internal.R.styleable.View_duplicateParentState:
2657 if (a.getBoolean(attr, false)) {
2658 viewFlagValues |= DUPLICATE_PARENT_STATE;
2659 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2660 }
2661 break;
2662 case com.android.internal.R.styleable.View_visibility:
2663 final int visibility = a.getInt(attr, 0);
2664 if (visibility != 0) {
2665 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2666 viewFlagMasks |= VISIBILITY_MASK;
2667 }
2668 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002669 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002670 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002671 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002672 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002673 final int layoutDirection = a.getInt(attr, -1);
2674 if (layoutDirection != -1) {
2675 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002676 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002677 // Set to default (LAYOUT_DIRECTION_INHERIT)
2678 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002679 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002680 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002681 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 case com.android.internal.R.styleable.View_drawingCacheQuality:
2683 final int cacheQuality = a.getInt(attr, 0);
2684 if (cacheQuality != 0) {
2685 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2686 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2687 }
2688 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002689 case com.android.internal.R.styleable.View_contentDescription:
2690 mContentDescription = a.getString(attr);
2691 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2693 if (!a.getBoolean(attr, true)) {
2694 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2695 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2696 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002697 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2699 if (!a.getBoolean(attr, true)) {
2700 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2701 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2702 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002703 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 case R.styleable.View_scrollbars:
2705 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2706 if (scrollbars != SCROLLBARS_NONE) {
2707 viewFlagValues |= scrollbars;
2708 viewFlagMasks |= SCROLLBARS_MASK;
2709 initializeScrollbars(a);
2710 }
2711 break;
2712 case R.styleable.View_fadingEdge:
2713 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2714 if (fadingEdge != FADING_EDGE_NONE) {
2715 viewFlagValues |= fadingEdge;
2716 viewFlagMasks |= FADING_EDGE_MASK;
2717 initializeFadingEdge(a);
2718 }
2719 break;
2720 case R.styleable.View_scrollbarStyle:
2721 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2722 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2723 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2724 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2725 }
2726 break;
2727 case R.styleable.View_isScrollContainer:
2728 setScrollContainer = true;
2729 if (a.getBoolean(attr, false)) {
2730 setScrollContainer(true);
2731 }
2732 break;
2733 case com.android.internal.R.styleable.View_keepScreenOn:
2734 if (a.getBoolean(attr, false)) {
2735 viewFlagValues |= KEEP_SCREEN_ON;
2736 viewFlagMasks |= KEEP_SCREEN_ON;
2737 }
2738 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002739 case R.styleable.View_filterTouchesWhenObscured:
2740 if (a.getBoolean(attr, false)) {
2741 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2742 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2743 }
2744 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 case R.styleable.View_nextFocusLeft:
2746 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2747 break;
2748 case R.styleable.View_nextFocusRight:
2749 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2750 break;
2751 case R.styleable.View_nextFocusUp:
2752 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2753 break;
2754 case R.styleable.View_nextFocusDown:
2755 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2756 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002757 case R.styleable.View_nextFocusForward:
2758 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2759 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 case R.styleable.View_minWidth:
2761 mMinWidth = a.getDimensionPixelSize(attr, 0);
2762 break;
2763 case R.styleable.View_minHeight:
2764 mMinHeight = a.getDimensionPixelSize(attr, 0);
2765 break;
Romain Guy9a817362009-05-01 10:57:14 -07002766 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002767 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002768 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002769 + "be used within a restricted context");
2770 }
2771
Romain Guy9a817362009-05-01 10:57:14 -07002772 final String handlerName = a.getString(attr);
2773 if (handlerName != null) {
2774 setOnClickListener(new OnClickListener() {
2775 private Method mHandler;
2776
2777 public void onClick(View v) {
2778 if (mHandler == null) {
2779 try {
2780 mHandler = getContext().getClass().getMethod(handlerName,
2781 View.class);
2782 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002783 int id = getId();
2784 String idText = id == NO_ID ? "" : " with id '"
2785 + getContext().getResources().getResourceEntryName(
2786 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002787 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002788 handlerName + "(View) in the activity "
2789 + getContext().getClass() + " for onClick handler"
2790 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002791 }
2792 }
2793
2794 try {
2795 mHandler.invoke(getContext(), View.this);
2796 } catch (IllegalAccessException e) {
2797 throw new IllegalStateException("Could not execute non "
2798 + "public method of the activity", e);
2799 } catch (InvocationTargetException e) {
2800 throw new IllegalStateException("Could not execute "
2801 + "method of the activity", e);
2802 }
2803 }
2804 });
2805 }
2806 break;
Adam Powell637d3372010-08-25 14:37:03 -07002807 case R.styleable.View_overScrollMode:
2808 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2809 break;
Adam Powell20232d02010-12-08 21:08:53 -08002810 case R.styleable.View_verticalScrollbarPosition:
2811 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2812 break;
Romain Guy171c5922011-01-06 10:04:23 -08002813 case R.styleable.View_layerType:
2814 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2815 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 }
2817 }
2818
Adam Powell637d3372010-08-25 14:37:03 -07002819 setOverScrollMode(overScrollMode);
2820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 if (background != null) {
2822 setBackgroundDrawable(background);
2823 }
2824
2825 if (padding >= 0) {
2826 leftPadding = padding;
2827 topPadding = padding;
2828 rightPadding = padding;
2829 bottomPadding = padding;
2830 }
2831
2832 // If the user specified the padding (either with android:padding or
2833 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2834 // use the default padding or the padding from the background drawable
2835 // (stored at this point in mPadding*)
2836 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2837 topPadding >= 0 ? topPadding : mPaddingTop,
2838 rightPadding >= 0 ? rightPadding : mPaddingRight,
2839 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2840
2841 if (viewFlagMasks != 0) {
2842 setFlags(viewFlagValues, viewFlagMasks);
2843 }
2844
2845 // Needs to be called after mViewFlags is set
2846 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2847 recomputePadding();
2848 }
2849
2850 if (x != 0 || y != 0) {
2851 scrollTo(x, y);
2852 }
2853
Chet Haase73066682010-11-29 15:55:32 -08002854 if (transformSet) {
2855 setTranslationX(tx);
2856 setTranslationY(ty);
2857 setRotation(rotation);
2858 setRotationX(rotationX);
2859 setRotationY(rotationY);
2860 setScaleX(sx);
2861 setScaleY(sy);
2862 }
2863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2865 setScrollContainer(true);
2866 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002867
2868 computeOpaqueFlags();
2869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 a.recycle();
2871 }
2872
2873 /**
2874 * Non-public constructor for use in testing
2875 */
2876 View() {
2877 }
2878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 /**
2880 * <p>
2881 * Initializes the fading edges from a given set of styled attributes. This
2882 * method should be called by subclasses that need fading edges and when an
2883 * instance of these subclasses is created programmatically rather than
2884 * being inflated from XML. This method is automatically called when the XML
2885 * is inflated.
2886 * </p>
2887 *
2888 * @param a the styled attributes set to initialize the fading edges from
2889 */
2890 protected void initializeFadingEdge(TypedArray a) {
2891 initScrollCache();
2892
2893 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2894 R.styleable.View_fadingEdgeLength,
2895 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2896 }
2897
2898 /**
2899 * Returns the size of the vertical faded edges used to indicate that more
2900 * content in this view is visible.
2901 *
2902 * @return The size in pixels of the vertical faded edge or 0 if vertical
2903 * faded edges are not enabled for this view.
2904 * @attr ref android.R.styleable#View_fadingEdgeLength
2905 */
2906 public int getVerticalFadingEdgeLength() {
2907 if (isVerticalFadingEdgeEnabled()) {
2908 ScrollabilityCache cache = mScrollCache;
2909 if (cache != null) {
2910 return cache.fadingEdgeLength;
2911 }
2912 }
2913 return 0;
2914 }
2915
2916 /**
2917 * Set the size of the faded edge used to indicate that more content in this
2918 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07002919 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
2920 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
2921 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 *
2923 * @param length The size in pixels of the faded edge used to indicate that more
2924 * content in this view is visible.
2925 */
2926 public void setFadingEdgeLength(int length) {
2927 initScrollCache();
2928 mScrollCache.fadingEdgeLength = length;
2929 }
2930
2931 /**
2932 * Returns the size of the horizontal faded edges used to indicate that more
2933 * content in this view is visible.
2934 *
2935 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2936 * faded edges are not enabled for this view.
2937 * @attr ref android.R.styleable#View_fadingEdgeLength
2938 */
2939 public int getHorizontalFadingEdgeLength() {
2940 if (isHorizontalFadingEdgeEnabled()) {
2941 ScrollabilityCache cache = mScrollCache;
2942 if (cache != null) {
2943 return cache.fadingEdgeLength;
2944 }
2945 }
2946 return 0;
2947 }
2948
2949 /**
2950 * Returns the width of the vertical scrollbar.
2951 *
2952 * @return The width in pixels of the vertical scrollbar or 0 if there
2953 * is no vertical scrollbar.
2954 */
2955 public int getVerticalScrollbarWidth() {
2956 ScrollabilityCache cache = mScrollCache;
2957 if (cache != null) {
2958 ScrollBarDrawable scrollBar = cache.scrollBar;
2959 if (scrollBar != null) {
2960 int size = scrollBar.getSize(true);
2961 if (size <= 0) {
2962 size = cache.scrollBarSize;
2963 }
2964 return size;
2965 }
2966 return 0;
2967 }
2968 return 0;
2969 }
2970
2971 /**
2972 * Returns the height of the horizontal scrollbar.
2973 *
2974 * @return The height in pixels of the horizontal scrollbar or 0 if
2975 * there is no horizontal scrollbar.
2976 */
2977 protected int getHorizontalScrollbarHeight() {
2978 ScrollabilityCache cache = mScrollCache;
2979 if (cache != null) {
2980 ScrollBarDrawable scrollBar = cache.scrollBar;
2981 if (scrollBar != null) {
2982 int size = scrollBar.getSize(false);
2983 if (size <= 0) {
2984 size = cache.scrollBarSize;
2985 }
2986 return size;
2987 }
2988 return 0;
2989 }
2990 return 0;
2991 }
2992
2993 /**
2994 * <p>
2995 * Initializes the scrollbars from a given set of styled attributes. This
2996 * method should be called by subclasses that need scrollbars and when an
2997 * instance of these subclasses is created programmatically rather than
2998 * being inflated from XML. This method is automatically called when the XML
2999 * is inflated.
3000 * </p>
3001 *
3002 * @param a the styled attributes set to initialize the scrollbars from
3003 */
3004 protected void initializeScrollbars(TypedArray a) {
3005 initScrollCache();
3006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003008
Mike Cleronf116bf82009-09-27 19:14:12 -07003009 if (scrollabilityCache.scrollBar == null) {
3010 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3011 }
Joe Malin32736f02011-01-19 16:14:20 -08003012
Romain Guy8bda2482010-03-02 11:42:11 -08003013 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014
Mike Cleronf116bf82009-09-27 19:14:12 -07003015 if (!fadeScrollbars) {
3016 scrollabilityCache.state = ScrollabilityCache.ON;
3017 }
3018 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003019
3020
Mike Cleronf116bf82009-09-27 19:14:12 -07003021 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3022 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3023 .getScrollBarFadeDuration());
3024 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3025 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3026 ViewConfiguration.getScrollDefaultDelay());
3027
Joe Malin32736f02011-01-19 16:14:20 -08003028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3030 com.android.internal.R.styleable.View_scrollbarSize,
3031 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3032
3033 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3034 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3035
3036 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3037 if (thumb != null) {
3038 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3039 }
3040
3041 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3042 false);
3043 if (alwaysDraw) {
3044 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3045 }
3046
3047 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3048 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3049
3050 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3051 if (thumb != null) {
3052 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3053 }
3054
3055 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3056 false);
3057 if (alwaysDraw) {
3058 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3059 }
3060
3061 // Re-apply user/background padding so that scrollbar(s) get added
3062 recomputePadding();
3063 }
3064
3065 /**
3066 * <p>
3067 * Initalizes the scrollability cache if necessary.
3068 * </p>
3069 */
3070 private void initScrollCache() {
3071 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003072 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073 }
3074 }
3075
3076 /**
Adam Powell20232d02010-12-08 21:08:53 -08003077 * Set the position of the vertical scroll bar. Should be one of
3078 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3079 * {@link #SCROLLBAR_POSITION_RIGHT}.
3080 *
3081 * @param position Where the vertical scroll bar should be positioned.
3082 */
3083 public void setVerticalScrollbarPosition(int position) {
3084 if (mVerticalScrollbarPosition != position) {
3085 mVerticalScrollbarPosition = position;
3086 computeOpaqueFlags();
3087 recomputePadding();
3088 }
3089 }
3090
3091 /**
3092 * @return The position where the vertical scroll bar will show, if applicable.
3093 * @see #setVerticalScrollbarPosition(int)
3094 */
3095 public int getVerticalScrollbarPosition() {
3096 return mVerticalScrollbarPosition;
3097 }
3098
3099 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 * Register a callback to be invoked when focus of this view changed.
3101 *
3102 * @param l The callback that will run.
3103 */
3104 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3105 mOnFocusChangeListener = l;
3106 }
3107
3108 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003109 * Add a listener that will be called when the bounds of the view change due to
3110 * layout processing.
3111 *
3112 * @param listener The listener that will be called when layout bounds change.
3113 */
3114 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3115 if (mOnLayoutChangeListeners == null) {
3116 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3117 }
3118 mOnLayoutChangeListeners.add(listener);
3119 }
3120
3121 /**
3122 * Remove a listener for layout changes.
3123 *
3124 * @param listener The listener for layout bounds change.
3125 */
3126 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3127 if (mOnLayoutChangeListeners == null) {
3128 return;
3129 }
3130 mOnLayoutChangeListeners.remove(listener);
3131 }
3132
3133 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003134 * Add a listener for attach state changes.
3135 *
3136 * This listener will be called whenever this view is attached or detached
3137 * from a window. Remove the listener using
3138 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3139 *
3140 * @param listener Listener to attach
3141 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3142 */
3143 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3144 if (mOnAttachStateChangeListeners == null) {
3145 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3146 }
3147 mOnAttachStateChangeListeners.add(listener);
3148 }
3149
3150 /**
3151 * Remove a listener for attach state changes. The listener will receive no further
3152 * notification of window attach/detach events.
3153 *
3154 * @param listener Listener to remove
3155 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3156 */
3157 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3158 if (mOnAttachStateChangeListeners == null) {
3159 return;
3160 }
3161 mOnAttachStateChangeListeners.remove(listener);
3162 }
3163
3164 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 * Returns the focus-change callback registered for this view.
3166 *
3167 * @return The callback, or null if one is not registered.
3168 */
3169 public OnFocusChangeListener getOnFocusChangeListener() {
3170 return mOnFocusChangeListener;
3171 }
3172
3173 /**
3174 * Register a callback to be invoked when this view is clicked. If this view is not
3175 * clickable, it becomes clickable.
3176 *
3177 * @param l The callback that will run
3178 *
3179 * @see #setClickable(boolean)
3180 */
3181 public void setOnClickListener(OnClickListener l) {
3182 if (!isClickable()) {
3183 setClickable(true);
3184 }
3185 mOnClickListener = l;
3186 }
3187
3188 /**
3189 * Register a callback to be invoked when this view is clicked and held. If this view is not
3190 * long clickable, it becomes long clickable.
3191 *
3192 * @param l The callback that will run
3193 *
3194 * @see #setLongClickable(boolean)
3195 */
3196 public void setOnLongClickListener(OnLongClickListener l) {
3197 if (!isLongClickable()) {
3198 setLongClickable(true);
3199 }
3200 mOnLongClickListener = l;
3201 }
3202
3203 /**
3204 * Register a callback to be invoked when the context menu for this view is
3205 * being built. If this view is not long clickable, it becomes long clickable.
3206 *
3207 * @param l The callback that will run
3208 *
3209 */
3210 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3211 if (!isLongClickable()) {
3212 setLongClickable(true);
3213 }
3214 mOnCreateContextMenuListener = l;
3215 }
3216
3217 /**
3218 * Call this view's OnClickListener, if it is defined.
3219 *
3220 * @return True there was an assigned OnClickListener that was called, false
3221 * otherwise is returned.
3222 */
3223 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003224 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003226 if (mOnClickListener != null) {
3227 playSoundEffect(SoundEffectConstants.CLICK);
3228 mOnClickListener.onClick(this);
3229 return true;
3230 }
3231
3232 return false;
3233 }
3234
3235 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003236 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3237 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003239 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 */
3241 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003242 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244 boolean handled = false;
3245 if (mOnLongClickListener != null) {
3246 handled = mOnLongClickListener.onLongClick(View.this);
3247 }
3248 if (!handled) {
3249 handled = showContextMenu();
3250 }
3251 if (handled) {
3252 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3253 }
3254 return handled;
3255 }
3256
3257 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003258 * Performs button-related actions during a touch down event.
3259 *
3260 * @param event The event.
3261 * @return True if the down was consumed.
3262 *
3263 * @hide
3264 */
3265 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3266 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3267 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3268 return true;
3269 }
3270 }
3271 return false;
3272 }
3273
3274 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003275 * Bring up the context menu for this view.
3276 *
3277 * @return Whether a context menu was displayed.
3278 */
3279 public boolean showContextMenu() {
3280 return getParent().showContextMenuForChild(this);
3281 }
3282
3283 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003284 * Bring up the context menu for this view, referring to the item under the specified point.
3285 *
3286 * @param x The referenced x coordinate.
3287 * @param y The referenced y coordinate.
3288 * @param metaState The keyboard modifiers that were pressed.
3289 * @return Whether a context menu was displayed.
3290 *
3291 * @hide
3292 */
3293 public boolean showContextMenu(float x, float y, int metaState) {
3294 return showContextMenu();
3295 }
3296
3297 /**
Adam Powell6e346362010-07-23 10:18:23 -07003298 * Start an action mode.
3299 *
3300 * @param callback Callback that will control the lifecycle of the action mode
3301 * @return The new action mode if it is started, null otherwise
3302 *
3303 * @see ActionMode
3304 */
3305 public ActionMode startActionMode(ActionMode.Callback callback) {
3306 return getParent().startActionModeForChild(this, callback);
3307 }
3308
3309 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 * Register a callback to be invoked when a key is pressed in this view.
3311 * @param l the key listener to attach to this view
3312 */
3313 public void setOnKeyListener(OnKeyListener l) {
3314 mOnKeyListener = l;
3315 }
3316
3317 /**
3318 * Register a callback to be invoked when a touch event is sent to this view.
3319 * @param l the touch listener to attach to this view
3320 */
3321 public void setOnTouchListener(OnTouchListener l) {
3322 mOnTouchListener = l;
3323 }
3324
3325 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003326 * Register a callback to be invoked when a generic motion event is sent to this view.
3327 * @param l the generic motion listener to attach to this view
3328 */
3329 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3330 mOnGenericMotionListener = l;
3331 }
3332
3333 /**
Joe Malin32736f02011-01-19 16:14:20 -08003334 * Register a drag event listener callback object for this View. The parameter is
3335 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3336 * View, the system calls the
3337 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3338 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003339 */
3340 public void setOnDragListener(OnDragListener l) {
3341 mOnDragListener = l;
3342 }
3343
3344 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003345 * Give this view focus. This will cause
3346 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003347 *
3348 * Note: this does not check whether this {@link View} should get focus, it just
3349 * gives it focus no matter what. It should only be called internally by framework
3350 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3351 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003352 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3353 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003354 * focus moved when requestFocus() is called. It may not always
3355 * apply, in which case use the default View.FOCUS_DOWN.
3356 * @param previouslyFocusedRect The rectangle of the view that had focus
3357 * prior in this View's coordinate system.
3358 */
3359 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3360 if (DBG) {
3361 System.out.println(this + " requestFocus()");
3362 }
3363
3364 if ((mPrivateFlags & FOCUSED) == 0) {
3365 mPrivateFlags |= FOCUSED;
3366
3367 if (mParent != null) {
3368 mParent.requestChildFocus(this, this);
3369 }
3370
3371 onFocusChanged(true, direction, previouslyFocusedRect);
3372 refreshDrawableState();
3373 }
3374 }
3375
3376 /**
3377 * Request that a rectangle of this view be visible on the screen,
3378 * scrolling if necessary just enough.
3379 *
3380 * <p>A View should call this if it maintains some notion of which part
3381 * of its content is interesting. For example, a text editing view
3382 * should call this when its cursor moves.
3383 *
3384 * @param rectangle The rectangle.
3385 * @return Whether any parent scrolled.
3386 */
3387 public boolean requestRectangleOnScreen(Rect rectangle) {
3388 return requestRectangleOnScreen(rectangle, false);
3389 }
3390
3391 /**
3392 * Request that a rectangle of this view be visible on the screen,
3393 * scrolling if necessary just enough.
3394 *
3395 * <p>A View should call this if it maintains some notion of which part
3396 * of its content is interesting. For example, a text editing view
3397 * should call this when its cursor moves.
3398 *
3399 * <p>When <code>immediate</code> is set to true, scrolling will not be
3400 * animated.
3401 *
3402 * @param rectangle The rectangle.
3403 * @param immediate True to forbid animated scrolling, false otherwise
3404 * @return Whether any parent scrolled.
3405 */
3406 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3407 View child = this;
3408 ViewParent parent = mParent;
3409 boolean scrolled = false;
3410 while (parent != null) {
3411 scrolled |= parent.requestChildRectangleOnScreen(child,
3412 rectangle, immediate);
3413
3414 // offset rect so next call has the rectangle in the
3415 // coordinate system of its direct child.
3416 rectangle.offset(child.getLeft(), child.getTop());
3417 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3418
3419 if (!(parent instanceof View)) {
3420 break;
3421 }
Romain Guy8506ab42009-06-11 17:35:47 -07003422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003423 child = (View) parent;
3424 parent = child.getParent();
3425 }
3426 return scrolled;
3427 }
3428
3429 /**
3430 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003431 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 */
3433 public void clearFocus() {
3434 if (DBG) {
3435 System.out.println(this + " clearFocus()");
3436 }
3437
3438 if ((mPrivateFlags & FOCUSED) != 0) {
3439 mPrivateFlags &= ~FOCUSED;
3440
3441 if (mParent != null) {
3442 mParent.clearChildFocus(this);
3443 }
3444
3445 onFocusChanged(false, 0, null);
3446 refreshDrawableState();
3447 }
3448 }
3449
3450 /**
3451 * Called to clear the focus of a view that is about to be removed.
3452 * Doesn't call clearChildFocus, which prevents this view from taking
3453 * focus again before it has been removed from the parent
3454 */
3455 void clearFocusForRemoval() {
3456 if ((mPrivateFlags & FOCUSED) != 0) {
3457 mPrivateFlags &= ~FOCUSED;
3458
3459 onFocusChanged(false, 0, null);
3460 refreshDrawableState();
3461 }
3462 }
3463
3464 /**
3465 * Called internally by the view system when a new view is getting focus.
3466 * This is what clears the old focus.
3467 */
3468 void unFocus() {
3469 if (DBG) {
3470 System.out.println(this + " unFocus()");
3471 }
3472
3473 if ((mPrivateFlags & FOCUSED) != 0) {
3474 mPrivateFlags &= ~FOCUSED;
3475
3476 onFocusChanged(false, 0, null);
3477 refreshDrawableState();
3478 }
3479 }
3480
3481 /**
3482 * Returns true if this view has focus iteself, or is the ancestor of the
3483 * view that has focus.
3484 *
3485 * @return True if this view has or contains focus, false otherwise.
3486 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003487 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 public boolean hasFocus() {
3489 return (mPrivateFlags & FOCUSED) != 0;
3490 }
3491
3492 /**
3493 * Returns true if this view is focusable or if it contains a reachable View
3494 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3495 * is a View whose parents do not block descendants focus.
3496 *
3497 * Only {@link #VISIBLE} views are considered focusable.
3498 *
3499 * @return True if the view is focusable or if the view contains a focusable
3500 * View, false otherwise.
3501 *
3502 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3503 */
3504 public boolean hasFocusable() {
3505 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3506 }
3507
3508 /**
3509 * Called by the view system when the focus state of this view changes.
3510 * When the focus change event is caused by directional navigation, direction
3511 * and previouslyFocusedRect provide insight into where the focus is coming from.
3512 * When overriding, be sure to call up through to the super class so that
3513 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003514 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 * @param gainFocus True if the View has focus; false otherwise.
3516 * @param direction The direction focus has moved when requestFocus()
3517 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003518 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3519 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3520 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003521 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3522 * system, of the previously focused view. If applicable, this will be
3523 * passed in as finer grained information about where the focus is coming
3524 * from (in addition to direction). Will be <code>null</code> otherwise.
3525 */
3526 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003527 if (gainFocus) {
3528 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3529 }
3530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 InputMethodManager imm = InputMethodManager.peekInstance();
3532 if (!gainFocus) {
3533 if (isPressed()) {
3534 setPressed(false);
3535 }
3536 if (imm != null && mAttachInfo != null
3537 && mAttachInfo.mHasWindowFocus) {
3538 imm.focusOut(this);
3539 }
Romain Guya2431d02009-04-30 16:30:00 -07003540 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003541 } else if (imm != null && mAttachInfo != null
3542 && mAttachInfo.mHasWindowFocus) {
3543 imm.focusIn(this);
3544 }
Romain Guy8506ab42009-06-11 17:35:47 -07003545
Romain Guy0fd89bf2011-01-26 15:41:30 -08003546 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547 if (mOnFocusChangeListener != null) {
3548 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3549 }
Joe Malin32736f02011-01-19 16:14:20 -08003550
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003551 if (mAttachInfo != null) {
3552 mAttachInfo.mKeyDispatchState.reset(this);
3553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 }
3555
3556 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003557 * Sends an accessibility event of the given type. If accessiiblity is
3558 * not enabled this method has no effect. The default implementation calls
3559 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3560 * to populate information about the event source (this View), then calls
3561 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3562 * populate the text content of the event source including its descendants,
3563 * and last calls
3564 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3565 * on its parent to resuest sending of the event to interested parties.
3566 *
3567 * @param eventType The type of the event to send.
3568 *
3569 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3570 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3571 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
svetoslavganov75986cf2009-05-14 22:28:01 -07003572 */
3573 public void sendAccessibilityEvent(int eventType) {
3574 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3575 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3576 }
3577 }
3578
3579 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003580 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3581 * takes as an argument an empty {@link AccessibilityEvent} and does not
3582 * perfrom a check whether accessibility is enabled.
3583 *
3584 * @param event The event to send.
3585 *
3586 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003587 */
3588 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003589 if (!isShown()) {
3590 return;
3591 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003592 onInitializeAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003593 dispatchPopulateAccessibilityEvent(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003594 // In the beginning we called #isShown(), so we know that getParent() is not null.
3595 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003596 }
3597
3598 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003599 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3600 * to its children for adding their text content to the event. Note that the
3601 * event text is populated in a separate dispatch path since we add to the
3602 * event not only the text of the source but also the text of all its descendants.
3603 * </p>
3604 * A typical implementation will call
3605 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3606 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3607 * on each child. Override this method if custom population of the event text
3608 * content is required.
svetoslavganov75986cf2009-05-14 22:28:01 -07003609 *
3610 * @param event The event.
3611 *
3612 * @return True if the event population was completed.
3613 */
3614 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003615 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003616 return false;
3617 }
3618
3619 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003620 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003621 * giving a chance to this View to populate the accessibility event with its
3622 * text content. While the implementation is free to modify other event
3623 * attributes this should be performed in
3624 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
3625 * <p>
3626 * Example: Adding formatted date string to an accessibility event in addition
3627 * to the text added by the super implementation.
3628 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003629 * public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3630 * super.onPopulateAccessibilityEvent(event);
3631 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
3632 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
3633 * mCurrentDate.getTimeInMillis(), flags);
3634 * event.getText().add(selectedDateUtterance);
3635 * }
3636 * </code></pre></p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003637 *
3638 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003639 *
3640 * @see #sendAccessibilityEvent(int)
3641 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003642 */
3643 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3644
3645 }
3646
3647 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003648 * Initializes an {@link AccessibilityEvent} with information about the
3649 * the type of the event and this View which is the event source. In other
3650 * words, the source of an accessibility event is the view whose state
3651 * change triggered firing the event.
3652 * <p>
3653 * Example: Setting the password property of an event in addition
3654 * to properties set by the super implementation.
3655 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003656 * public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3657 * super.onInitializeAccessibilityEvent(event);
3658 * event.setPassword(true);
3659 * }
3660 * </code></pre></p>
3661 * @param event The event to initialeze.
3662 *
3663 * @see #sendAccessibilityEvent(int)
3664 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3665 */
3666 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003667 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07003668 event.setClassName(getClass().getName());
3669 event.setPackageName(getContext().getPackageName());
3670 event.setEnabled(isEnabled());
3671 event.setContentDescription(mContentDescription);
3672
3673 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
3674 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3675 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
3676 event.setItemCount(focusablesTempList.size());
3677 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3678 focusablesTempList.clear();
3679 }
3680 }
3681
3682 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003683 * Returns an {@link AccessibilityNodeInfo} representing this view from the
3684 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
3685 * This method is responsible for obtaining an accessibility node info from a
3686 * pool of reusable instances and calling
3687 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
3688 * initialize the former.
3689 * <p>
3690 * Note: The client is responsible for recycling the obtained instance by calling
3691 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
3692 * </p>
3693 * @return A populated {@link AccessibilityNodeInfo}.
3694 *
3695 * @see AccessibilityNodeInfo
3696 */
3697 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
3698 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
3699 onInitializeAccessibilityNodeInfo(info);
3700 return info;
3701 }
3702
3703 /**
3704 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
3705 * The base implementation sets:
3706 * <ul>
3707 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003708 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
3709 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003710 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
3711 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
3712 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
3713 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
3714 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
3715 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
3716 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
3717 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
3718 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
3719 * </ul>
3720 * <p>
3721 * Subclasses should override this method, call the super implementation,
3722 * and set additional attributes.
3723 * </p>
3724 * @param info The instance to initialize.
3725 */
3726 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
3727 Rect bounds = mAttachInfo.mTmpInvalRect;
3728 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003729 info.setBoundsInParent(bounds);
3730
3731 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
3732 getLocationOnScreen(locationOnScreen);
3733 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
3734 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003735
3736 ViewParent parent = getParent();
3737 if (parent instanceof View) {
3738 View parentView = (View) parent;
3739 info.setParent(parentView);
3740 }
3741
3742 info.setPackageName(mContext.getPackageName());
3743 info.setClassName(getClass().getName());
3744 info.setContentDescription(getContentDescription());
3745
3746 info.setEnabled(isEnabled());
3747 info.setClickable(isClickable());
3748 info.setFocusable(isFocusable());
3749 info.setFocused(isFocused());
3750 info.setSelected(isSelected());
3751 info.setLongClickable(isLongClickable());
3752
3753 // TODO: These make sense only if we are in an AdapterView but all
3754 // views can be selected. Maybe from accessiiblity perspective
3755 // we should report as selectable view in an AdapterView.
3756 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
3757 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
3758
3759 if (isFocusable()) {
3760 if (isFocused()) {
3761 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
3762 } else {
3763 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
3764 }
3765 }
3766 }
3767
3768 /**
3769 * Gets the unique identifier of this view on the screen for accessibility purposes.
3770 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
3771 *
3772 * @return The view accessibility id.
3773 *
3774 * @hide
3775 */
3776 public int getAccessibilityViewId() {
3777 if (mAccessibilityViewId == NO_ID) {
3778 mAccessibilityViewId = sNextAccessibilityViewId++;
3779 }
3780 return mAccessibilityViewId;
3781 }
3782
3783 /**
3784 * Gets the unique identifier of the window in which this View reseides.
3785 *
3786 * @return The window accessibility id.
3787 *
3788 * @hide
3789 */
3790 public int getAccessibilityWindowId() {
3791 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
3792 }
3793
3794 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003795 * Gets the {@link View} description. It briefly describes the view and is
3796 * primarily used for accessibility support. Set this property to enable
3797 * better accessibility support for your application. This is especially
3798 * true for views that do not have textual representation (For example,
3799 * ImageButton).
3800 *
3801 * @return The content descriptiopn.
3802 *
3803 * @attr ref android.R.styleable#View_contentDescription
3804 */
3805 public CharSequence getContentDescription() {
3806 return mContentDescription;
3807 }
3808
3809 /**
3810 * Sets the {@link View} description. It briefly describes the view and is
3811 * primarily used for accessibility support. Set this property to enable
3812 * better accessibility support for your application. This is especially
3813 * true for views that do not have textual representation (For example,
3814 * ImageButton).
3815 *
3816 * @param contentDescription The content description.
3817 *
3818 * @attr ref android.R.styleable#View_contentDescription
3819 */
3820 public void setContentDescription(CharSequence contentDescription) {
3821 mContentDescription = contentDescription;
3822 }
3823
3824 /**
Romain Guya2431d02009-04-30 16:30:00 -07003825 * Invoked whenever this view loses focus, either by losing window focus or by losing
3826 * focus within its window. This method can be used to clear any state tied to the
3827 * focus. For instance, if a button is held pressed with the trackball and the window
3828 * loses focus, this method can be used to cancel the press.
3829 *
3830 * Subclasses of View overriding this method should always call super.onFocusLost().
3831 *
3832 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003833 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003834 *
3835 * @hide pending API council approval
3836 */
3837 protected void onFocusLost() {
3838 resetPressedState();
3839 }
3840
3841 private void resetPressedState() {
3842 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3843 return;
3844 }
3845
3846 if (isPressed()) {
3847 setPressed(false);
3848
3849 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05003850 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003851 }
3852 }
3853 }
3854
3855 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003856 * Returns true if this view has focus
3857 *
3858 * @return True if this view has focus, false otherwise.
3859 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003860 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003861 public boolean isFocused() {
3862 return (mPrivateFlags & FOCUSED) != 0;
3863 }
3864
3865 /**
3866 * Find the view in the hierarchy rooted at this view that currently has
3867 * focus.
3868 *
3869 * @return The view that currently has focus, or null if no focused view can
3870 * be found.
3871 */
3872 public View findFocus() {
3873 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
3874 }
3875
3876 /**
3877 * Change whether this view is one of the set of scrollable containers in
3878 * its window. This will be used to determine whether the window can
3879 * resize or must pan when a soft input area is open -- scrollable
3880 * containers allow the window to use resize mode since the container
3881 * will appropriately shrink.
3882 */
3883 public void setScrollContainer(boolean isScrollContainer) {
3884 if (isScrollContainer) {
3885 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
3886 mAttachInfo.mScrollContainers.add(this);
3887 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
3888 }
3889 mPrivateFlags |= SCROLL_CONTAINER;
3890 } else {
3891 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
3892 mAttachInfo.mScrollContainers.remove(this);
3893 }
3894 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
3895 }
3896 }
3897
3898 /**
3899 * Returns the quality of the drawing cache.
3900 *
3901 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3902 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3903 *
3904 * @see #setDrawingCacheQuality(int)
3905 * @see #setDrawingCacheEnabled(boolean)
3906 * @see #isDrawingCacheEnabled()
3907 *
3908 * @attr ref android.R.styleable#View_drawingCacheQuality
3909 */
3910 public int getDrawingCacheQuality() {
3911 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
3912 }
3913
3914 /**
3915 * Set the drawing cache quality of this view. This value is used only when the
3916 * drawing cache is enabled
3917 *
3918 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3919 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3920 *
3921 * @see #getDrawingCacheQuality()
3922 * @see #setDrawingCacheEnabled(boolean)
3923 * @see #isDrawingCacheEnabled()
3924 *
3925 * @attr ref android.R.styleable#View_drawingCacheQuality
3926 */
3927 public void setDrawingCacheQuality(int quality) {
3928 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
3929 }
3930
3931 /**
3932 * Returns whether the screen should remain on, corresponding to the current
3933 * value of {@link #KEEP_SCREEN_ON}.
3934 *
3935 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
3936 *
3937 * @see #setKeepScreenOn(boolean)
3938 *
3939 * @attr ref android.R.styleable#View_keepScreenOn
3940 */
3941 public boolean getKeepScreenOn() {
3942 return (mViewFlags & KEEP_SCREEN_ON) != 0;
3943 }
3944
3945 /**
3946 * Controls whether the screen should remain on, modifying the
3947 * value of {@link #KEEP_SCREEN_ON}.
3948 *
3949 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
3950 *
3951 * @see #getKeepScreenOn()
3952 *
3953 * @attr ref android.R.styleable#View_keepScreenOn
3954 */
3955 public void setKeepScreenOn(boolean keepScreenOn) {
3956 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
3957 }
3958
3959 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003960 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3961 * @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 -08003962 *
3963 * @attr ref android.R.styleable#View_nextFocusLeft
3964 */
3965 public int getNextFocusLeftId() {
3966 return mNextFocusLeftId;
3967 }
3968
3969 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003970 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3971 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
3972 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003973 *
3974 * @attr ref android.R.styleable#View_nextFocusLeft
3975 */
3976 public void setNextFocusLeftId(int nextFocusLeftId) {
3977 mNextFocusLeftId = nextFocusLeftId;
3978 }
3979
3980 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003981 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3982 * @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 -08003983 *
3984 * @attr ref android.R.styleable#View_nextFocusRight
3985 */
3986 public int getNextFocusRightId() {
3987 return mNextFocusRightId;
3988 }
3989
3990 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003991 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3992 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
3993 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003994 *
3995 * @attr ref android.R.styleable#View_nextFocusRight
3996 */
3997 public void setNextFocusRightId(int nextFocusRightId) {
3998 mNextFocusRightId = nextFocusRightId;
3999 }
4000
4001 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004002 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4003 * @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 -08004004 *
4005 * @attr ref android.R.styleable#View_nextFocusUp
4006 */
4007 public int getNextFocusUpId() {
4008 return mNextFocusUpId;
4009 }
4010
4011 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004012 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4013 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4014 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004015 *
4016 * @attr ref android.R.styleable#View_nextFocusUp
4017 */
4018 public void setNextFocusUpId(int nextFocusUpId) {
4019 mNextFocusUpId = nextFocusUpId;
4020 }
4021
4022 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004023 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4024 * @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 -08004025 *
4026 * @attr ref android.R.styleable#View_nextFocusDown
4027 */
4028 public int getNextFocusDownId() {
4029 return mNextFocusDownId;
4030 }
4031
4032 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004033 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4034 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4035 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004036 *
4037 * @attr ref android.R.styleable#View_nextFocusDown
4038 */
4039 public void setNextFocusDownId(int nextFocusDownId) {
4040 mNextFocusDownId = nextFocusDownId;
4041 }
4042
4043 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004044 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4045 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4046 *
4047 * @attr ref android.R.styleable#View_nextFocusForward
4048 */
4049 public int getNextFocusForwardId() {
4050 return mNextFocusForwardId;
4051 }
4052
4053 /**
4054 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4055 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4056 * decide automatically.
4057 *
4058 * @attr ref android.R.styleable#View_nextFocusForward
4059 */
4060 public void setNextFocusForwardId(int nextFocusForwardId) {
4061 mNextFocusForwardId = nextFocusForwardId;
4062 }
4063
4064 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004065 * Returns the visibility of this view and all of its ancestors
4066 *
4067 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4068 */
4069 public boolean isShown() {
4070 View current = this;
4071 //noinspection ConstantConditions
4072 do {
4073 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4074 return false;
4075 }
4076 ViewParent parent = current.mParent;
4077 if (parent == null) {
4078 return false; // We are not attached to the view root
4079 }
4080 if (!(parent instanceof View)) {
4081 return true;
4082 }
4083 current = (View) parent;
4084 } while (current != null);
4085
4086 return false;
4087 }
4088
4089 /**
4090 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4091 * is set
4092 *
4093 * @param insets Insets for system windows
4094 *
4095 * @return True if this view applied the insets, false otherwise
4096 */
4097 protected boolean fitSystemWindows(Rect insets) {
4098 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4099 mPaddingLeft = insets.left;
4100 mPaddingTop = insets.top;
4101 mPaddingRight = insets.right;
4102 mPaddingBottom = insets.bottom;
4103 requestLayout();
4104 return true;
4105 }
4106 return false;
4107 }
4108
4109 /**
4110 * Returns the visibility status for this view.
4111 *
4112 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4113 * @attr ref android.R.styleable#View_visibility
4114 */
4115 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004116 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4117 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4118 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004119 })
4120 public int getVisibility() {
4121 return mViewFlags & VISIBILITY_MASK;
4122 }
4123
4124 /**
4125 * Set the enabled state of this view.
4126 *
4127 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4128 * @attr ref android.R.styleable#View_visibility
4129 */
4130 @RemotableViewMethod
4131 public void setVisibility(int visibility) {
4132 setFlags(visibility, VISIBILITY_MASK);
4133 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4134 }
4135
4136 /**
4137 * Returns the enabled status for this view. The interpretation of the
4138 * enabled state varies by subclass.
4139 *
4140 * @return True if this view is enabled, false otherwise.
4141 */
4142 @ViewDebug.ExportedProperty
4143 public boolean isEnabled() {
4144 return (mViewFlags & ENABLED_MASK) == ENABLED;
4145 }
4146
4147 /**
4148 * Set the enabled state of this view. The interpretation of the enabled
4149 * state varies by subclass.
4150 *
4151 * @param enabled True if this view is enabled, false otherwise.
4152 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004153 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004154 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004155 if (enabled == isEnabled()) return;
4156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004157 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4158
4159 /*
4160 * The View most likely has to change its appearance, so refresh
4161 * the drawable state.
4162 */
4163 refreshDrawableState();
4164
4165 // Invalidate too, since the default behavior for views is to be
4166 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004167 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 }
4169
4170 /**
4171 * Set whether this view can receive the focus.
4172 *
4173 * Setting this to false will also ensure that this view is not focusable
4174 * in touch mode.
4175 *
4176 * @param focusable If true, this view can receive the focus.
4177 *
4178 * @see #setFocusableInTouchMode(boolean)
4179 * @attr ref android.R.styleable#View_focusable
4180 */
4181 public void setFocusable(boolean focusable) {
4182 if (!focusable) {
4183 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4184 }
4185 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4186 }
4187
4188 /**
4189 * Set whether this view can receive focus while in touch mode.
4190 *
4191 * Setting this to true will also ensure that this view is focusable.
4192 *
4193 * @param focusableInTouchMode If true, this view can receive the focus while
4194 * in touch mode.
4195 *
4196 * @see #setFocusable(boolean)
4197 * @attr ref android.R.styleable#View_focusableInTouchMode
4198 */
4199 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4200 // Focusable in touch mode should always be set before the focusable flag
4201 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4202 // which, in touch mode, will not successfully request focus on this view
4203 // because the focusable in touch mode flag is not set
4204 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4205 if (focusableInTouchMode) {
4206 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4207 }
4208 }
4209
4210 /**
4211 * Set whether this view should have sound effects enabled for events such as
4212 * clicking and touching.
4213 *
4214 * <p>You may wish to disable sound effects for a view if you already play sounds,
4215 * for instance, a dial key that plays dtmf tones.
4216 *
4217 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4218 * @see #isSoundEffectsEnabled()
4219 * @see #playSoundEffect(int)
4220 * @attr ref android.R.styleable#View_soundEffectsEnabled
4221 */
4222 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4223 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4224 }
4225
4226 /**
4227 * @return whether this view should have sound effects enabled for events such as
4228 * clicking and touching.
4229 *
4230 * @see #setSoundEffectsEnabled(boolean)
4231 * @see #playSoundEffect(int)
4232 * @attr ref android.R.styleable#View_soundEffectsEnabled
4233 */
4234 @ViewDebug.ExportedProperty
4235 public boolean isSoundEffectsEnabled() {
4236 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4237 }
4238
4239 /**
4240 * Set whether this view should have haptic feedback for events such as
4241 * long presses.
4242 *
4243 * <p>You may wish to disable haptic feedback if your view already controls
4244 * its own haptic feedback.
4245 *
4246 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4247 * @see #isHapticFeedbackEnabled()
4248 * @see #performHapticFeedback(int)
4249 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4250 */
4251 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4252 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4253 }
4254
4255 /**
4256 * @return whether this view should have haptic feedback enabled for events
4257 * long presses.
4258 *
4259 * @see #setHapticFeedbackEnabled(boolean)
4260 * @see #performHapticFeedback(int)
4261 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4262 */
4263 @ViewDebug.ExportedProperty
4264 public boolean isHapticFeedbackEnabled() {
4265 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4266 }
4267
4268 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004269 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004270 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004271 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4272 * {@link #LAYOUT_DIRECTION_RTL},
4273 * {@link #LAYOUT_DIRECTION_INHERIT} or
4274 * {@link #LAYOUT_DIRECTION_LOCALE}.
4275 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004276 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004277 * @hide
4278 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004279 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004280 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4281 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4282 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4283 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004284 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004285 public int getLayoutDirection() {
4286 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004287 }
4288
4289 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004290 * Set the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004291 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004292 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4293 * {@link #LAYOUT_DIRECTION_RTL},
4294 * {@link #LAYOUT_DIRECTION_INHERIT} or
4295 * {@link #LAYOUT_DIRECTION_LOCALE}.
4296 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004297 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004298 * @hide
4299 */
4300 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004301 public void setLayoutDirection(int layoutDirection) {
4302 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
Cibu Johny7632cb92010-02-22 13:01:02 -08004303 }
4304
4305 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004306 * Returns the resolved layout direction for this view.
4307 *
4308 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4309 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4310 *
4311 * @hide
4312 */
4313 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4314 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4315 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4316 })
4317 public int getResolvedLayoutDirection() {
4318 resolveLayoutDirection();
4319 return ((mPrivateFlags2 & RESOLVED_LAYOUT_RTL) == RESOLVED_LAYOUT_RTL) ?
4320 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4321 }
4322
4323 /**
4324 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4325 * layout attribute and/or the inherited value from the parent.</p>
4326 *
4327 * @return true if the layout is right-to-left.
4328 *
4329 * @hide
4330 */
4331 @ViewDebug.ExportedProperty(category = "layout")
4332 public boolean isLayoutRtl() {
4333 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4334 }
4335
4336 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004337 * If this view doesn't do any drawing on its own, set this flag to
4338 * allow further optimizations. By default, this flag is not set on
4339 * View, but could be set on some View subclasses such as ViewGroup.
4340 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004341 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4342 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004343 *
4344 * @param willNotDraw whether or not this View draw on its own
4345 */
4346 public void setWillNotDraw(boolean willNotDraw) {
4347 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4348 }
4349
4350 /**
4351 * Returns whether or not this View draws on its own.
4352 *
4353 * @return true if this view has nothing to draw, false otherwise
4354 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004355 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004356 public boolean willNotDraw() {
4357 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4358 }
4359
4360 /**
4361 * When a View's drawing cache is enabled, drawing is redirected to an
4362 * offscreen bitmap. Some views, like an ImageView, must be able to
4363 * bypass this mechanism if they already draw a single bitmap, to avoid
4364 * unnecessary usage of the memory.
4365 *
4366 * @param willNotCacheDrawing true if this view does not cache its
4367 * drawing, false otherwise
4368 */
4369 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4370 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4371 }
4372
4373 /**
4374 * Returns whether or not this View can cache its drawing or not.
4375 *
4376 * @return true if this view does not cache its drawing, false otherwise
4377 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004378 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379 public boolean willNotCacheDrawing() {
4380 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4381 }
4382
4383 /**
4384 * Indicates whether this view reacts to click events or not.
4385 *
4386 * @return true if the view is clickable, false otherwise
4387 *
4388 * @see #setClickable(boolean)
4389 * @attr ref android.R.styleable#View_clickable
4390 */
4391 @ViewDebug.ExportedProperty
4392 public boolean isClickable() {
4393 return (mViewFlags & CLICKABLE) == CLICKABLE;
4394 }
4395
4396 /**
4397 * Enables or disables click events for this view. When a view
4398 * is clickable it will change its state to "pressed" on every click.
4399 * Subclasses should set the view clickable to visually react to
4400 * user's clicks.
4401 *
4402 * @param clickable true to make the view clickable, false otherwise
4403 *
4404 * @see #isClickable()
4405 * @attr ref android.R.styleable#View_clickable
4406 */
4407 public void setClickable(boolean clickable) {
4408 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4409 }
4410
4411 /**
4412 * Indicates whether this view reacts to long click events or not.
4413 *
4414 * @return true if the view is long clickable, false otherwise
4415 *
4416 * @see #setLongClickable(boolean)
4417 * @attr ref android.R.styleable#View_longClickable
4418 */
4419 public boolean isLongClickable() {
4420 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4421 }
4422
4423 /**
4424 * Enables or disables long click events for this view. When a view is long
4425 * clickable it reacts to the user holding down the button for a longer
4426 * duration than a tap. This event can either launch the listener or a
4427 * context menu.
4428 *
4429 * @param longClickable true to make the view long clickable, false otherwise
4430 * @see #isLongClickable()
4431 * @attr ref android.R.styleable#View_longClickable
4432 */
4433 public void setLongClickable(boolean longClickable) {
4434 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4435 }
4436
4437 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004438 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004439 *
4440 * @see #isClickable()
4441 * @see #setClickable(boolean)
4442 *
4443 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4444 * the View's internal state from a previously set "pressed" state.
4445 */
4446 public void setPressed(boolean pressed) {
4447 if (pressed) {
4448 mPrivateFlags |= PRESSED;
4449 } else {
4450 mPrivateFlags &= ~PRESSED;
4451 }
4452 refreshDrawableState();
4453 dispatchSetPressed(pressed);
4454 }
4455
4456 /**
4457 * Dispatch setPressed to all of this View's children.
4458 *
4459 * @see #setPressed(boolean)
4460 *
4461 * @param pressed The new pressed state
4462 */
4463 protected void dispatchSetPressed(boolean pressed) {
4464 }
4465
4466 /**
4467 * Indicates whether the view is currently in pressed state. Unless
4468 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4469 * the pressed state.
4470 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004471 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004472 * @see #isClickable()
4473 * @see #setClickable(boolean)
4474 *
4475 * @return true if the view is currently pressed, false otherwise
4476 */
4477 public boolean isPressed() {
4478 return (mPrivateFlags & PRESSED) == PRESSED;
4479 }
4480
4481 /**
4482 * Indicates whether this view will save its state (that is,
4483 * whether its {@link #onSaveInstanceState} method will be called).
4484 *
4485 * @return Returns true if the view state saving is enabled, else false.
4486 *
4487 * @see #setSaveEnabled(boolean)
4488 * @attr ref android.R.styleable#View_saveEnabled
4489 */
4490 public boolean isSaveEnabled() {
4491 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4492 }
4493
4494 /**
4495 * Controls whether the saving of this view's state is
4496 * enabled (that is, whether its {@link #onSaveInstanceState} method
4497 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004498 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004499 * for its state to be saved. This flag can only disable the
4500 * saving of this view; any child views may still have their state saved.
4501 *
4502 * @param enabled Set to false to <em>disable</em> state saving, or true
4503 * (the default) to allow it.
4504 *
4505 * @see #isSaveEnabled()
4506 * @see #setId(int)
4507 * @see #onSaveInstanceState()
4508 * @attr ref android.R.styleable#View_saveEnabled
4509 */
4510 public void setSaveEnabled(boolean enabled) {
4511 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4512 }
4513
Jeff Brown85a31762010-09-01 17:01:00 -07004514 /**
4515 * Gets whether the framework should discard touches when the view's
4516 * window is obscured by another visible window.
4517 * Refer to the {@link View} security documentation for more details.
4518 *
4519 * @return True if touch filtering is enabled.
4520 *
4521 * @see #setFilterTouchesWhenObscured(boolean)
4522 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4523 */
4524 @ViewDebug.ExportedProperty
4525 public boolean getFilterTouchesWhenObscured() {
4526 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4527 }
4528
4529 /**
4530 * Sets whether the framework should discard touches when the view's
4531 * window is obscured by another visible window.
4532 * Refer to the {@link View} security documentation for more details.
4533 *
4534 * @param enabled True if touch filtering should be enabled.
4535 *
4536 * @see #getFilterTouchesWhenObscured
4537 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4538 */
4539 public void setFilterTouchesWhenObscured(boolean enabled) {
4540 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4541 FILTER_TOUCHES_WHEN_OBSCURED);
4542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004543
4544 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004545 * Indicates whether the entire hierarchy under this view will save its
4546 * state when a state saving traversal occurs from its parent. The default
4547 * is true; if false, these views will not be saved unless
4548 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4549 *
4550 * @return Returns true if the view state saving from parent is enabled, else false.
4551 *
4552 * @see #setSaveFromParentEnabled(boolean)
4553 */
4554 public boolean isSaveFromParentEnabled() {
4555 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4556 }
4557
4558 /**
4559 * Controls whether the entire hierarchy under this view will save its
4560 * state when a state saving traversal occurs from its parent. The default
4561 * is true; if false, these views will not be saved unless
4562 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4563 *
4564 * @param enabled Set to false to <em>disable</em> state saving, or true
4565 * (the default) to allow it.
4566 *
4567 * @see #isSaveFromParentEnabled()
4568 * @see #setId(int)
4569 * @see #onSaveInstanceState()
4570 */
4571 public void setSaveFromParentEnabled(boolean enabled) {
4572 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4573 }
4574
4575
4576 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004577 * Returns whether this View is able to take focus.
4578 *
4579 * @return True if this view can take focus, or false otherwise.
4580 * @attr ref android.R.styleable#View_focusable
4581 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004582 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004583 public final boolean isFocusable() {
4584 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4585 }
4586
4587 /**
4588 * When a view is focusable, it may not want to take focus when in touch mode.
4589 * For example, a button would like focus when the user is navigating via a D-pad
4590 * so that the user can click on it, but once the user starts touching the screen,
4591 * the button shouldn't take focus
4592 * @return Whether the view is focusable in touch mode.
4593 * @attr ref android.R.styleable#View_focusableInTouchMode
4594 */
4595 @ViewDebug.ExportedProperty
4596 public final boolean isFocusableInTouchMode() {
4597 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4598 }
4599
4600 /**
4601 * Find the nearest view in the specified direction that can take focus.
4602 * This does not actually give focus to that view.
4603 *
4604 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4605 *
4606 * @return The nearest focusable in the specified direction, or null if none
4607 * can be found.
4608 */
4609 public View focusSearch(int direction) {
4610 if (mParent != null) {
4611 return mParent.focusSearch(this, direction);
4612 } else {
4613 return null;
4614 }
4615 }
4616
4617 /**
4618 * This method is the last chance for the focused view and its ancestors to
4619 * respond to an arrow key. This is called when the focused view did not
4620 * consume the key internally, nor could the view system find a new view in
4621 * the requested direction to give focus to.
4622 *
4623 * @param focused The currently focused view.
4624 * @param direction The direction focus wants to move. One of FOCUS_UP,
4625 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4626 * @return True if the this view consumed this unhandled move.
4627 */
4628 public boolean dispatchUnhandledMove(View focused, int direction) {
4629 return false;
4630 }
4631
4632 /**
4633 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004634 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004635 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004636 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4637 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004638 * @return The user specified next view, or null if there is none.
4639 */
4640 View findUserSetNextFocus(View root, int direction) {
4641 switch (direction) {
4642 case FOCUS_LEFT:
4643 if (mNextFocusLeftId == View.NO_ID) return null;
4644 return findViewShouldExist(root, mNextFocusLeftId);
4645 case FOCUS_RIGHT:
4646 if (mNextFocusRightId == View.NO_ID) return null;
4647 return findViewShouldExist(root, mNextFocusRightId);
4648 case FOCUS_UP:
4649 if (mNextFocusUpId == View.NO_ID) return null;
4650 return findViewShouldExist(root, mNextFocusUpId);
4651 case FOCUS_DOWN:
4652 if (mNextFocusDownId == View.NO_ID) return null;
4653 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004654 case FOCUS_FORWARD:
4655 if (mNextFocusForwardId == View.NO_ID) return null;
4656 return findViewShouldExist(root, mNextFocusForwardId);
4657 case FOCUS_BACKWARD: {
4658 final int id = mID;
4659 return root.findViewByPredicate(new Predicate<View>() {
4660 @Override
4661 public boolean apply(View t) {
4662 return t.mNextFocusForwardId == id;
4663 }
4664 });
4665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004666 }
4667 return null;
4668 }
4669
4670 private static View findViewShouldExist(View root, int childViewId) {
4671 View result = root.findViewById(childViewId);
4672 if (result == null) {
4673 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4674 + "by user for id " + childViewId);
4675 }
4676 return result;
4677 }
4678
4679 /**
4680 * Find and return all focusable views that are descendants of this view,
4681 * possibly including this view if it is focusable itself.
4682 *
4683 * @param direction The direction of the focus
4684 * @return A list of focusable views
4685 */
4686 public ArrayList<View> getFocusables(int direction) {
4687 ArrayList<View> result = new ArrayList<View>(24);
4688 addFocusables(result, direction);
4689 return result;
4690 }
4691
4692 /**
4693 * Add any focusable views that are descendants of this view (possibly
4694 * including this view if it is focusable itself) to views. If we are in touch mode,
4695 * only add views that are also focusable in touch mode.
4696 *
4697 * @param views Focusable views found so far
4698 * @param direction The direction of the focus
4699 */
4700 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004701 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004703
svetoslavganov75986cf2009-05-14 22:28:01 -07004704 /**
4705 * Adds any focusable views that are descendants of this view (possibly
4706 * including this view if it is focusable itself) to views. This method
4707 * adds all focusable views regardless if we are in touch mode or
4708 * only views focusable in touch mode if we are in touch mode depending on
4709 * the focusable mode paramater.
4710 *
4711 * @param views Focusable views found so far or null if all we are interested is
4712 * the number of focusables.
4713 * @param direction The direction of the focus.
4714 * @param focusableMode The type of focusables to be added.
4715 *
4716 * @see #FOCUSABLES_ALL
4717 * @see #FOCUSABLES_TOUCH_MODE
4718 */
4719 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4720 if (!isFocusable()) {
4721 return;
4722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004723
svetoslavganov75986cf2009-05-14 22:28:01 -07004724 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4725 isInTouchMode() && !isFocusableInTouchMode()) {
4726 return;
4727 }
4728
4729 if (views != null) {
4730 views.add(this);
4731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004732 }
4733
4734 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004735 * Finds the Views that contain given text. The containment is case insensitive.
4736 * As View's text is considered any text content that View renders.
4737 *
4738 * @param outViews The output list of matching Views.
4739 * @param text The text to match against.
4740 */
4741 public void findViewsWithText(ArrayList<View> outViews, CharSequence text) {
4742 }
4743
4744 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004745 * Find and return all touchable views that are descendants of this view,
4746 * possibly including this view if it is touchable itself.
4747 *
4748 * @return A list of touchable views
4749 */
4750 public ArrayList<View> getTouchables() {
4751 ArrayList<View> result = new ArrayList<View>();
4752 addTouchables(result);
4753 return result;
4754 }
4755
4756 /**
4757 * Add any touchable views that are descendants of this view (possibly
4758 * including this view if it is touchable itself) to views.
4759 *
4760 * @param views Touchable views found so far
4761 */
4762 public void addTouchables(ArrayList<View> views) {
4763 final int viewFlags = mViewFlags;
4764
4765 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4766 && (viewFlags & ENABLED_MASK) == ENABLED) {
4767 views.add(this);
4768 }
4769 }
4770
4771 /**
4772 * Call this to try to give focus to a specific view or to one of its
4773 * descendants.
4774 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004775 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4776 * false), or if it is focusable and it is not focusable in touch mode
4777 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004778 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004779 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004780 * have focus, and you want your parent to look for the next one.
4781 *
4782 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4783 * {@link #FOCUS_DOWN} and <code>null</code>.
4784 *
4785 * @return Whether this view or one of its descendants actually took focus.
4786 */
4787 public final boolean requestFocus() {
4788 return requestFocus(View.FOCUS_DOWN);
4789 }
4790
4791
4792 /**
4793 * Call this to try to give focus to a specific view or to one of its
4794 * descendants and give it a hint about what direction focus is heading.
4795 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004796 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4797 * false), or if it is focusable and it is not focusable in touch mode
4798 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004799 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004800 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004801 * have focus, and you want your parent to look for the next one.
4802 *
4803 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4804 * <code>null</code> set for the previously focused rectangle.
4805 *
4806 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4807 * @return Whether this view or one of its descendants actually took focus.
4808 */
4809 public final boolean requestFocus(int direction) {
4810 return requestFocus(direction, null);
4811 }
4812
4813 /**
4814 * Call this to try to give focus to a specific view or to one of its descendants
4815 * and give it hints about the direction and a specific rectangle that the focus
4816 * is coming from. The rectangle can help give larger views a finer grained hint
4817 * about where focus is coming from, and therefore, where to show selection, or
4818 * forward focus change internally.
4819 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004820 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4821 * false), or if it is focusable and it is not focusable in touch mode
4822 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004823 *
4824 * A View will not take focus if it is not visible.
4825 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004826 * A View will not take focus if one of its parents has
4827 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
4828 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004829 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004830 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004831 * have focus, and you want your parent to look for the next one.
4832 *
4833 * You may wish to override this method if your custom {@link View} has an internal
4834 * {@link View} that it wishes to forward the request to.
4835 *
4836 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4837 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
4838 * to give a finer grained hint about where focus is coming from. May be null
4839 * if there is no hint.
4840 * @return Whether this view or one of its descendants actually took focus.
4841 */
4842 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
4843 // need to be focusable
4844 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
4845 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4846 return false;
4847 }
4848
4849 // need to be focusable in touch mode if in touch mode
4850 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004851 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
4852 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004853 }
4854
4855 // need to not have any parents blocking us
4856 if (hasAncestorThatBlocksDescendantFocus()) {
4857 return false;
4858 }
4859
4860 handleFocusGainInternal(direction, previouslyFocusedRect);
4861 return true;
4862 }
4863
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004864 /** Gets the ViewAncestor, or null if not attached. */
4865 /*package*/ ViewAncestor getViewAncestor() {
Christopher Tate2c095f32010-10-04 14:13:40 -07004866 View root = getRootView();
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004867 return root != null ? (ViewAncestor)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07004868 }
4869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004870 /**
4871 * Call this to try to give focus to a specific view or to one of its descendants. This is a
4872 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
4873 * touch mode to request focus when they are touched.
4874 *
4875 * @return Whether this view or one of its descendants actually took focus.
4876 *
4877 * @see #isInTouchMode()
4878 *
4879 */
4880 public final boolean requestFocusFromTouch() {
4881 // Leave touch mode if we need to
4882 if (isInTouchMode()) {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004883 ViewAncestor viewRoot = getViewAncestor();
Christopher Tate2c095f32010-10-04 14:13:40 -07004884 if (viewRoot != null) {
4885 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004886 }
4887 }
4888 return requestFocus(View.FOCUS_DOWN);
4889 }
4890
4891 /**
4892 * @return Whether any ancestor of this view blocks descendant focus.
4893 */
4894 private boolean hasAncestorThatBlocksDescendantFocus() {
4895 ViewParent ancestor = mParent;
4896 while (ancestor instanceof ViewGroup) {
4897 final ViewGroup vgAncestor = (ViewGroup) ancestor;
4898 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
4899 return true;
4900 } else {
4901 ancestor = vgAncestor.getParent();
4902 }
4903 }
4904 return false;
4905 }
4906
4907 /**
Romain Guya440b002010-02-24 15:57:54 -08004908 * @hide
4909 */
4910 public void dispatchStartTemporaryDetach() {
4911 onStartTemporaryDetach();
4912 }
4913
4914 /**
4915 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004916 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
4917 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08004918 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004919 */
4920 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08004921 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08004922 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08004923 }
4924
4925 /**
4926 * @hide
4927 */
4928 public void dispatchFinishTemporaryDetach() {
4929 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004930 }
Romain Guy8506ab42009-06-11 17:35:47 -07004931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004932 /**
4933 * Called after {@link #onStartTemporaryDetach} when the container is done
4934 * changing the view.
4935 */
4936 public void onFinishTemporaryDetach() {
4937 }
Romain Guy8506ab42009-06-11 17:35:47 -07004938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004939 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004940 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
4941 * for this view's window. Returns null if the view is not currently attached
4942 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07004943 * just use the standard high-level event callbacks like
4944 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004945 */
4946 public KeyEvent.DispatcherState getKeyDispatcherState() {
4947 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
4948 }
Joe Malin32736f02011-01-19 16:14:20 -08004949
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004950 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004951 * Dispatch a key event before it is processed by any input method
4952 * associated with the view hierarchy. This can be used to intercept
4953 * key events in special situations before the IME consumes them; a
4954 * typical example would be handling the BACK key to update the application's
4955 * UI instead of allowing the IME to see it and close itself.
4956 *
4957 * @param event The key event to be dispatched.
4958 * @return True if the event was handled, false otherwise.
4959 */
4960 public boolean dispatchKeyEventPreIme(KeyEvent event) {
4961 return onKeyPreIme(event.getKeyCode(), event);
4962 }
4963
4964 /**
4965 * Dispatch a key event to the next view on the focus path. This path runs
4966 * from the top of the view tree down to the currently focused view. If this
4967 * view has focus, it will dispatch to itself. Otherwise it will dispatch
4968 * the next node down the focus path. This method also fires any key
4969 * listeners.
4970 *
4971 * @param event The key event to be dispatched.
4972 * @return True if the event was handled, false otherwise.
4973 */
4974 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004975 if (mInputEventConsistencyVerifier != null) {
4976 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
4977 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004978
Jeff Brown21bc5c92011-02-28 18:27:14 -08004979 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07004980 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004981 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4982 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
4983 return true;
4984 }
4985
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004986 if (event.dispatch(this, mAttachInfo != null
4987 ? mAttachInfo.mKeyDispatchState : null, this)) {
4988 return true;
4989 }
4990
4991 if (mInputEventConsistencyVerifier != null) {
4992 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4993 }
4994 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004995 }
4996
4997 /**
4998 * Dispatches a key shortcut event.
4999 *
5000 * @param event The key event to be dispatched.
5001 * @return True if the event was handled by the view, false otherwise.
5002 */
5003 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5004 return onKeyShortcut(event.getKeyCode(), event);
5005 }
5006
5007 /**
5008 * Pass the touch screen motion event down to the target view, or this
5009 * view if it is the target.
5010 *
5011 * @param event The motion event to be dispatched.
5012 * @return True if the event was handled by the view, false otherwise.
5013 */
5014 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005015 if (mInputEventConsistencyVerifier != null) {
5016 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5017 }
5018
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005019 if (onFilterTouchEventForSecurity(event)) {
5020 //noinspection SimplifiableIfStatement
5021 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
5022 mOnTouchListener.onTouch(this, event)) {
5023 return true;
5024 }
5025
5026 if (onTouchEvent(event)) {
5027 return true;
5028 }
Jeff Brown85a31762010-09-01 17:01:00 -07005029 }
5030
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005031 if (mInputEventConsistencyVerifier != null) {
5032 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005033 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005034 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005035 }
5036
5037 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005038 * Filter the touch event to apply security policies.
5039 *
5040 * @param event The motion event to be filtered.
5041 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005042 *
Jeff Brown85a31762010-09-01 17:01:00 -07005043 * @see #getFilterTouchesWhenObscured
5044 */
5045 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005046 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005047 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5048 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5049 // Window is obscured, drop this touch.
5050 return false;
5051 }
5052 return true;
5053 }
5054
5055 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005056 * Pass a trackball motion event down to the focused view.
5057 *
5058 * @param event The motion event to be dispatched.
5059 * @return True if the event was handled by the view, false otherwise.
5060 */
5061 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005062 if (mInputEventConsistencyVerifier != null) {
5063 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5064 }
5065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005066 //Log.i("view", "view=" + this + ", " + event.toString());
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005067 if (onTrackballEvent(event)) {
5068 return true;
5069 }
5070
5071 if (mInputEventConsistencyVerifier != null) {
5072 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5073 }
5074 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005075 }
5076
5077 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005078 * Dispatch a generic motion event.
5079 * <p>
5080 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5081 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005082 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005083 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005084 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005085 *
5086 * @param event The motion event to be dispatched.
5087 * @return True if the event was handled by the view, false otherwise.
5088 */
5089 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005090 if (mInputEventConsistencyVerifier != null) {
5091 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5092 }
5093
Jeff Browna032cc02011-03-07 16:56:21 -08005094 final int source = event.getSource();
5095 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5096 final int action = event.getAction();
5097 if (action == MotionEvent.ACTION_HOVER_ENTER
5098 || action == MotionEvent.ACTION_HOVER_MOVE
5099 || action == MotionEvent.ACTION_HOVER_EXIT) {
5100 if (dispatchHoverEvent(event)) {
5101 return true;
5102 }
5103 } else if (dispatchGenericPointerEvent(event)) {
5104 return true;
5105 }
5106 } else if (dispatchGenericFocusedEvent(event)) {
5107 return true;
5108 }
5109
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005110 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08005111 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5112 && mOnGenericMotionListener.onGenericMotion(this, event)) {
5113 return true;
5114 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005115
5116 if (onGenericMotionEvent(event)) {
5117 return true;
5118 }
5119
5120 if (mInputEventConsistencyVerifier != null) {
5121 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5122 }
5123 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005124 }
5125
5126 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005127 * Dispatch a hover event.
5128 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005129 * Do not call this method directly.
5130 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005131 * </p>
5132 *
5133 * @param event The motion event to be dispatched.
5134 * @return True if the event was handled by the view, false otherwise.
5135 * @hide
5136 */
5137 protected boolean dispatchHoverEvent(MotionEvent event) {
5138 return onHoverEvent(event);
5139 }
5140
5141 /**
5142 * Dispatch a generic motion event to the view under the first pointer.
5143 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005144 * Do not call this method directly.
5145 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005146 * </p>
5147 *
5148 * @param event The motion event to be dispatched.
5149 * @return True if the event was handled by the view, false otherwise.
5150 * @hide
5151 */
5152 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5153 return false;
5154 }
5155
5156 /**
5157 * Dispatch a generic motion event to the currently focused view.
5158 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005159 * Do not call this method directly.
5160 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005161 * </p>
5162 *
5163 * @param event The motion event to be dispatched.
5164 * @return True if the event was handled by the view, false otherwise.
5165 * @hide
5166 */
5167 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5168 return false;
5169 }
5170
5171 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005172 * Dispatch a pointer event.
5173 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005174 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5175 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5176 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005177 * and should not be expected to handle other pointing device features.
5178 * </p>
5179 *
5180 * @param event The motion event to be dispatched.
5181 * @return True if the event was handled by the view, false otherwise.
5182 * @hide
5183 */
5184 public final boolean dispatchPointerEvent(MotionEvent event) {
5185 if (event.isTouchEvent()) {
5186 return dispatchTouchEvent(event);
5187 } else {
5188 return dispatchGenericMotionEvent(event);
5189 }
5190 }
5191
5192 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005193 * Called when the window containing this view gains or loses window focus.
5194 * ViewGroups should override to route to their children.
5195 *
5196 * @param hasFocus True if the window containing this view now has focus,
5197 * false otherwise.
5198 */
5199 public void dispatchWindowFocusChanged(boolean hasFocus) {
5200 onWindowFocusChanged(hasFocus);
5201 }
5202
5203 /**
5204 * Called when the window containing this view gains or loses focus. Note
5205 * that this is separate from view focus: to receive key events, both
5206 * your view and its window must have focus. If a window is displayed
5207 * on top of yours that takes input focus, then your own window will lose
5208 * focus but the view focus will remain unchanged.
5209 *
5210 * @param hasWindowFocus True if the window containing this view now has
5211 * focus, false otherwise.
5212 */
5213 public void onWindowFocusChanged(boolean hasWindowFocus) {
5214 InputMethodManager imm = InputMethodManager.peekInstance();
5215 if (!hasWindowFocus) {
5216 if (isPressed()) {
5217 setPressed(false);
5218 }
5219 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5220 imm.focusOut(this);
5221 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005222 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005223 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005224 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5226 imm.focusIn(this);
5227 }
5228 refreshDrawableState();
5229 }
5230
5231 /**
5232 * Returns true if this view is in a window that currently has window focus.
5233 * Note that this is not the same as the view itself having focus.
5234 *
5235 * @return True if this view is in a window that currently has window focus.
5236 */
5237 public boolean hasWindowFocus() {
5238 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5239 }
5240
5241 /**
Adam Powell326d8082009-12-09 15:10:07 -08005242 * Dispatch a view visibility change down the view hierarchy.
5243 * ViewGroups should override to route to their children.
5244 * @param changedView The view whose visibility changed. Could be 'this' or
5245 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005246 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5247 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005248 */
5249 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5250 onVisibilityChanged(changedView, visibility);
5251 }
5252
5253 /**
5254 * Called when the visibility of the view or an ancestor of the view is changed.
5255 * @param changedView The view whose visibility changed. Could be 'this' or
5256 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005257 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5258 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005259 */
5260 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005261 if (visibility == VISIBLE) {
5262 if (mAttachInfo != null) {
5263 initialAwakenScrollBars();
5264 } else {
5265 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5266 }
5267 }
Adam Powell326d8082009-12-09 15:10:07 -08005268 }
5269
5270 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005271 * Dispatch a hint about whether this view is displayed. For instance, when
5272 * a View moves out of the screen, it might receives a display hint indicating
5273 * the view is not displayed. Applications should not <em>rely</em> on this hint
5274 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005275 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005276 * @param hint A hint about whether or not this view is displayed:
5277 * {@link #VISIBLE} or {@link #INVISIBLE}.
5278 */
5279 public void dispatchDisplayHint(int hint) {
5280 onDisplayHint(hint);
5281 }
5282
5283 /**
5284 * Gives this view a hint about whether is displayed or not. For instance, when
5285 * a View moves out of the screen, it might receives a display hint indicating
5286 * the view is not displayed. Applications should not <em>rely</em> on this hint
5287 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005288 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005289 * @param hint A hint about whether or not this view is displayed:
5290 * {@link #VISIBLE} or {@link #INVISIBLE}.
5291 */
5292 protected void onDisplayHint(int hint) {
5293 }
5294
5295 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005296 * Dispatch a window visibility change down the view hierarchy.
5297 * ViewGroups should override to route to their children.
5298 *
5299 * @param visibility The new visibility of the window.
5300 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005301 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005302 */
5303 public void dispatchWindowVisibilityChanged(int visibility) {
5304 onWindowVisibilityChanged(visibility);
5305 }
5306
5307 /**
5308 * Called when the window containing has change its visibility
5309 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5310 * that this tells you whether or not your window is being made visible
5311 * to the window manager; this does <em>not</em> tell you whether or not
5312 * your window is obscured by other windows on the screen, even if it
5313 * is itself visible.
5314 *
5315 * @param visibility The new visibility of the window.
5316 */
5317 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005318 if (visibility == VISIBLE) {
5319 initialAwakenScrollBars();
5320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005321 }
5322
5323 /**
5324 * Returns the current visibility of the window this view is attached to
5325 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5326 *
5327 * @return Returns the current visibility of the view's window.
5328 */
5329 public int getWindowVisibility() {
5330 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5331 }
5332
5333 /**
5334 * Retrieve the overall visible display size in which the window this view is
5335 * attached to has been positioned in. This takes into account screen
5336 * decorations above the window, for both cases where the window itself
5337 * is being position inside of them or the window is being placed under
5338 * then and covered insets are used for the window to position its content
5339 * inside. In effect, this tells you the available area where content can
5340 * be placed and remain visible to users.
5341 *
5342 * <p>This function requires an IPC back to the window manager to retrieve
5343 * the requested information, so should not be used in performance critical
5344 * code like drawing.
5345 *
5346 * @param outRect Filled in with the visible display frame. If the view
5347 * is not attached to a window, this is simply the raw display size.
5348 */
5349 public void getWindowVisibleDisplayFrame(Rect outRect) {
5350 if (mAttachInfo != null) {
5351 try {
5352 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5353 } catch (RemoteException e) {
5354 return;
5355 }
5356 // XXX This is really broken, and probably all needs to be done
5357 // in the window manager, and we need to know more about whether
5358 // we want the area behind or in front of the IME.
5359 final Rect insets = mAttachInfo.mVisibleInsets;
5360 outRect.left += insets.left;
5361 outRect.top += insets.top;
5362 outRect.right -= insets.right;
5363 outRect.bottom -= insets.bottom;
5364 return;
5365 }
5366 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005367 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005368 }
5369
5370 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005371 * Dispatch a notification about a resource configuration change down
5372 * the view hierarchy.
5373 * ViewGroups should override to route to their children.
5374 *
5375 * @param newConfig The new resource configuration.
5376 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005377 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005378 */
5379 public void dispatchConfigurationChanged(Configuration newConfig) {
5380 onConfigurationChanged(newConfig);
5381 }
5382
5383 /**
5384 * Called when the current configuration of the resources being used
5385 * by the application have changed. You can use this to decide when
5386 * to reload resources that can changed based on orientation and other
5387 * configuration characterstics. You only need to use this if you are
5388 * not relying on the normal {@link android.app.Activity} mechanism of
5389 * recreating the activity instance upon a configuration change.
5390 *
5391 * @param newConfig The new resource configuration.
5392 */
5393 protected void onConfigurationChanged(Configuration newConfig) {
5394 }
5395
5396 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005397 * Private function to aggregate all per-view attributes in to the view
5398 * root.
5399 */
5400 void dispatchCollectViewAttributes(int visibility) {
5401 performCollectViewAttributes(visibility);
5402 }
5403
5404 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005405 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005406 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5407 mAttachInfo.mKeepScreenOn = true;
5408 }
5409 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5410 if (mOnSystemUiVisibilityChangeListener != null) {
5411 mAttachInfo.mHasSystemUiListeners = true;
5412 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005413 }
5414 }
5415
5416 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005417 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005418 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005419 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5420 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005421 ai.mRecomputeGlobalAttributes = true;
5422 }
5423 }
5424 }
5425
5426 /**
5427 * Returns whether the device is currently in touch mode. Touch mode is entered
5428 * once the user begins interacting with the device by touch, and affects various
5429 * things like whether focus is always visible to the user.
5430 *
5431 * @return Whether the device is in touch mode.
5432 */
5433 @ViewDebug.ExportedProperty
5434 public boolean isInTouchMode() {
5435 if (mAttachInfo != null) {
5436 return mAttachInfo.mInTouchMode;
5437 } else {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005438 return ViewAncestor.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005439 }
5440 }
5441
5442 /**
5443 * Returns the context the view is running in, through which it can
5444 * access the current theme, resources, etc.
5445 *
5446 * @return The view's Context.
5447 */
5448 @ViewDebug.CapturedViewProperty
5449 public final Context getContext() {
5450 return mContext;
5451 }
5452
5453 /**
5454 * Handle a key event before it is processed by any input method
5455 * associated with the view hierarchy. This can be used to intercept
5456 * key events in special situations before the IME consumes them; a
5457 * typical example would be handling the BACK key to update the application's
5458 * UI instead of allowing the IME to see it and close itself.
5459 *
5460 * @param keyCode The value in event.getKeyCode().
5461 * @param event Description of the key event.
5462 * @return If you handled the event, return true. If you want to allow the
5463 * event to be handled by the next receiver, return false.
5464 */
5465 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5466 return false;
5467 }
5468
5469 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005470 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5471 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005472 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5473 * is released, if the view is enabled and clickable.
5474 *
5475 * @param keyCode A key code that represents the button pressed, from
5476 * {@link android.view.KeyEvent}.
5477 * @param event The KeyEvent object that defines the button action.
5478 */
5479 public boolean onKeyDown(int keyCode, KeyEvent event) {
5480 boolean result = false;
5481
5482 switch (keyCode) {
5483 case KeyEvent.KEYCODE_DPAD_CENTER:
5484 case KeyEvent.KEYCODE_ENTER: {
5485 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5486 return true;
5487 }
5488 // Long clickable items don't necessarily have to be clickable
5489 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5490 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5491 (event.getRepeatCount() == 0)) {
5492 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005493 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005494 return true;
5495 }
5496 break;
5497 }
5498 }
5499 return result;
5500 }
5501
5502 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005503 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5504 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5505 * the event).
5506 */
5507 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5508 return false;
5509 }
5510
5511 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005512 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5513 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005514 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5515 * {@link KeyEvent#KEYCODE_ENTER} is released.
5516 *
5517 * @param keyCode A key code that represents the button pressed, from
5518 * {@link android.view.KeyEvent}.
5519 * @param event The KeyEvent object that defines the button action.
5520 */
5521 public boolean onKeyUp(int keyCode, KeyEvent event) {
5522 boolean result = false;
5523
5524 switch (keyCode) {
5525 case KeyEvent.KEYCODE_DPAD_CENTER:
5526 case KeyEvent.KEYCODE_ENTER: {
5527 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5528 return true;
5529 }
5530 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5531 setPressed(false);
5532
5533 if (!mHasPerformedLongPress) {
5534 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005535 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005536
5537 result = performClick();
5538 }
5539 }
5540 break;
5541 }
5542 }
5543 return result;
5544 }
5545
5546 /**
5547 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5548 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5549 * the event).
5550 *
5551 * @param keyCode A key code that represents the button pressed, from
5552 * {@link android.view.KeyEvent}.
5553 * @param repeatCount The number of times the action was made.
5554 * @param event The KeyEvent object that defines the button action.
5555 */
5556 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5557 return false;
5558 }
5559
5560 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005561 * Called on the focused view when a key shortcut event is not handled.
5562 * Override this method to implement local key shortcuts for the View.
5563 * Key shortcuts can also be implemented by setting the
5564 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005565 *
5566 * @param keyCode The value in event.getKeyCode().
5567 * @param event Description of the key event.
5568 * @return If you handled the event, return true. If you want to allow the
5569 * event to be handled by the next receiver, return false.
5570 */
5571 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5572 return false;
5573 }
5574
5575 /**
5576 * Check whether the called view is a text editor, in which case it
5577 * would make sense to automatically display a soft input window for
5578 * it. Subclasses should override this if they implement
5579 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005580 * a call on that method would return a non-null InputConnection, and
5581 * they are really a first-class editor that the user would normally
5582 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005583 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005584 * <p>The default implementation always returns false. This does
5585 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5586 * will not be called or the user can not otherwise perform edits on your
5587 * view; it is just a hint to the system that this is not the primary
5588 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005589 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005590 * @return Returns true if this view is a text editor, else false.
5591 */
5592 public boolean onCheckIsTextEditor() {
5593 return false;
5594 }
Romain Guy8506ab42009-06-11 17:35:47 -07005595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005596 /**
5597 * Create a new InputConnection for an InputMethod to interact
5598 * with the view. The default implementation returns null, since it doesn't
5599 * support input methods. You can override this to implement such support.
5600 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005601 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005602 * <p>When implementing this, you probably also want to implement
5603 * {@link #onCheckIsTextEditor()} to indicate you will return a
5604 * non-null InputConnection.
5605 *
5606 * @param outAttrs Fill in with attribute information about the connection.
5607 */
5608 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5609 return null;
5610 }
5611
5612 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005613 * Called by the {@link android.view.inputmethod.InputMethodManager}
5614 * when a view who is not the current
5615 * input connection target is trying to make a call on the manager. The
5616 * default implementation returns false; you can override this to return
5617 * true for certain views if you are performing InputConnection proxying
5618 * to them.
5619 * @param view The View that is making the InputMethodManager call.
5620 * @return Return true to allow the call, false to reject.
5621 */
5622 public boolean checkInputConnectionProxy(View view) {
5623 return false;
5624 }
Romain Guy8506ab42009-06-11 17:35:47 -07005625
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005626 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005627 * Show the context menu for this view. It is not safe to hold on to the
5628 * menu after returning from this method.
5629 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005630 * You should normally not overload this method. Overload
5631 * {@link #onCreateContextMenu(ContextMenu)} or define an
5632 * {@link OnCreateContextMenuListener} to add items to the context menu.
5633 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005634 * @param menu The context menu to populate
5635 */
5636 public void createContextMenu(ContextMenu menu) {
5637 ContextMenuInfo menuInfo = getContextMenuInfo();
5638
5639 // Sets the current menu info so all items added to menu will have
5640 // my extra info set.
5641 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5642
5643 onCreateContextMenu(menu);
5644 if (mOnCreateContextMenuListener != null) {
5645 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5646 }
5647
5648 // Clear the extra information so subsequent items that aren't mine don't
5649 // have my extra info.
5650 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5651
5652 if (mParent != null) {
5653 mParent.createContextMenu(menu);
5654 }
5655 }
5656
5657 /**
5658 * Views should implement this if they have extra information to associate
5659 * with the context menu. The return result is supplied as a parameter to
5660 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5661 * callback.
5662 *
5663 * @return Extra information about the item for which the context menu
5664 * should be shown. This information will vary across different
5665 * subclasses of View.
5666 */
5667 protected ContextMenuInfo getContextMenuInfo() {
5668 return null;
5669 }
5670
5671 /**
5672 * Views should implement this if the view itself is going to add items to
5673 * the context menu.
5674 *
5675 * @param menu the context menu to populate
5676 */
5677 protected void onCreateContextMenu(ContextMenu menu) {
5678 }
5679
5680 /**
5681 * Implement this method to handle trackball motion events. The
5682 * <em>relative</em> movement of the trackball since the last event
5683 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5684 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5685 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5686 * they will often be fractional values, representing the more fine-grained
5687 * movement information available from a trackball).
5688 *
5689 * @param event The motion event.
5690 * @return True if the event was handled, false otherwise.
5691 */
5692 public boolean onTrackballEvent(MotionEvent event) {
5693 return false;
5694 }
5695
5696 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005697 * Implement this method to handle generic motion events.
5698 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005699 * Generic motion events describe joystick movements, mouse hovers, track pad
5700 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005701 * {@link MotionEvent#getSource() source} of the motion event specifies
5702 * the class of input that was received. Implementations of this method
5703 * must examine the bits in the source before processing the event.
5704 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005705 * </p><p>
5706 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5707 * are delivered to the view under the pointer. All other generic motion events are
5708 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005709 * </p>
5710 * <code>
5711 * public boolean onGenericMotionEvent(MotionEvent event) {
5712 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005713 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5714 * // process the joystick movement...
5715 * return true;
5716 * }
5717 * }
5718 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5719 * switch (event.getAction()) {
5720 * case MotionEvent.ACTION_HOVER_MOVE:
5721 * // process the mouse hover movement...
5722 * return true;
5723 * case MotionEvent.ACTION_SCROLL:
5724 * // process the scroll wheel movement...
5725 * return true;
5726 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005727 * }
5728 * return super.onGenericMotionEvent(event);
5729 * }
5730 * </code>
5731 *
5732 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08005733 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08005734 */
5735 public boolean onGenericMotionEvent(MotionEvent event) {
5736 return false;
5737 }
5738
5739 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005740 * Implement this method to handle hover events.
5741 * <p>
5742 * Hover events are pointer events with action {@link MotionEvent#ACTION_HOVER_ENTER},
5743 * {@link MotionEvent#ACTION_HOVER_MOVE}, or {@link MotionEvent#ACTION_HOVER_EXIT}.
5744 * </p><p>
5745 * The view receives hover enter as the pointer enters the bounds of the view and hover
5746 * exit as the pointer exits the bound of the view or just before the pointer goes down
Romain Guy5c22a8c2011-05-13 11:48:45 -07005747 * (which implies that {@link #onTouchEvent(MotionEvent)} will be called soon).
Jeff Browna032cc02011-03-07 16:56:21 -08005748 * </p><p>
5749 * If the view would like to handle the hover event itself and prevent its children
5750 * from receiving hover, it should return true from this method. If this method returns
5751 * true and a child has already received a hover enter event, the child will
5752 * automatically receive a hover exit event.
5753 * </p><p>
5754 * The default implementation sets the hovered state of the view if the view is
5755 * clickable.
5756 * </p>
5757 *
5758 * @param event The motion event that describes the hover.
5759 * @return True if this view handled the hover event and does not want its children
5760 * to receive the hover event.
5761 */
5762 public boolean onHoverEvent(MotionEvent event) {
Jeff Browna032cc02011-03-07 16:56:21 -08005763 switch (event.getAction()) {
5764 case MotionEvent.ACTION_HOVER_ENTER:
5765 setHovered(true);
5766 break;
5767
5768 case MotionEvent.ACTION_HOVER_EXIT:
5769 setHovered(false);
5770 break;
5771 }
5772
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005773 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08005774 }
5775
5776 /**
5777 * Returns true if the view is currently hovered.
5778 *
5779 * @return True if the view is currently hovered.
5780 */
5781 public boolean isHovered() {
5782 return (mPrivateFlags & HOVERED) != 0;
5783 }
5784
5785 /**
5786 * Sets whether the view is currently hovered.
5787 *
5788 * @param hovered True if the view is hovered.
5789 */
5790 public void setHovered(boolean hovered) {
5791 if (hovered) {
5792 if ((mPrivateFlags & HOVERED) == 0) {
5793 mPrivateFlags |= HOVERED;
5794 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005795 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Jeff Browna032cc02011-03-07 16:56:21 -08005796 }
5797 } else {
5798 if ((mPrivateFlags & HOVERED) != 0) {
5799 mPrivateFlags &= ~HOVERED;
5800 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005801 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Jeff Browna032cc02011-03-07 16:56:21 -08005802 }
5803 }
5804 }
5805
5806 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005807 * Implement this method to handle touch screen motion events.
5808 *
5809 * @param event The motion event.
5810 * @return True if the event was handled, false otherwise.
5811 */
5812 public boolean onTouchEvent(MotionEvent event) {
5813 final int viewFlags = mViewFlags;
5814
5815 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07005816 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
5817 mPrivateFlags &= ~PRESSED;
5818 refreshDrawableState();
5819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005820 // A disabled view that is clickable still consumes the touch
5821 // events, it just doesn't respond to them.
5822 return (((viewFlags & CLICKABLE) == CLICKABLE ||
5823 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
5824 }
5825
5826 if (mTouchDelegate != null) {
5827 if (mTouchDelegate.onTouchEvent(event)) {
5828 return true;
5829 }
5830 }
5831
5832 if (((viewFlags & CLICKABLE) == CLICKABLE ||
5833 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
5834 switch (event.getAction()) {
5835 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08005836 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
5837 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005838 // take focus if we don't have it already and we should in
5839 // touch mode.
5840 boolean focusTaken = false;
5841 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
5842 focusTaken = requestFocus();
5843 }
5844
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08005845 if (prepressed) {
5846 // The button is being released before we actually
5847 // showed it as pressed. Make it show the pressed
5848 // state now (before scheduling the click) to ensure
5849 // the user sees it.
5850 mPrivateFlags |= PRESSED;
5851 refreshDrawableState();
5852 }
Joe Malin32736f02011-01-19 16:14:20 -08005853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005854 if (!mHasPerformedLongPress) {
5855 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005856 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005857
5858 // Only perform take click actions if we were in the pressed state
5859 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08005860 // Use a Runnable and post this rather than calling
5861 // performClick directly. This lets other visual state
5862 // of the view update before click actions start.
5863 if (mPerformClick == null) {
5864 mPerformClick = new PerformClick();
5865 }
5866 if (!post(mPerformClick)) {
5867 performClick();
5868 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005869 }
5870 }
5871
5872 if (mUnsetPressedState == null) {
5873 mUnsetPressedState = new UnsetPressedState();
5874 }
5875
Adam Powelle14579b2009-12-16 18:39:52 -08005876 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08005877 postDelayed(mUnsetPressedState,
5878 ViewConfiguration.getPressedStateDuration());
5879 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005880 // If the post failed, unpress right now
5881 mUnsetPressedState.run();
5882 }
Adam Powelle14579b2009-12-16 18:39:52 -08005883 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005884 }
5885 break;
5886
5887 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08005888 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005889
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005890 if (performButtonActionOnTouchDown(event)) {
5891 break;
5892 }
5893
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005894 // Walk up the hierarchy to determine if we're inside a scrolling container.
5895 boolean isInScrollingContainer = false;
5896 ViewParent p = getParent();
5897 while (p != null && p instanceof ViewGroup) {
5898 if (((ViewGroup) p).shouldDelayChildPressedState()) {
5899 isInScrollingContainer = true;
5900 break;
5901 }
5902 p = p.getParent();
5903 }
5904
5905 // For views inside a scrolling container, delay the pressed feedback for
5906 // a short period in case this is a scroll.
5907 if (isInScrollingContainer) {
5908 mPrivateFlags |= PREPRESSED;
5909 if (mPendingCheckForTap == null) {
5910 mPendingCheckForTap = new CheckForTap();
5911 }
5912 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
5913 } else {
5914 // Not inside a scrolling container, so show the feedback right away
5915 mPrivateFlags |= PRESSED;
5916 refreshDrawableState();
5917 checkForLongClick(0);
5918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005919 break;
5920
5921 case MotionEvent.ACTION_CANCEL:
5922 mPrivateFlags &= ~PRESSED;
5923 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08005924 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005925 break;
5926
5927 case MotionEvent.ACTION_MOVE:
5928 final int x = (int) event.getX();
5929 final int y = (int) event.getY();
5930
5931 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07005932 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005933 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08005934 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005935 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08005936 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05005937 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005938
5939 // Need to switch from pressed to not pressed
5940 mPrivateFlags &= ~PRESSED;
5941 refreshDrawableState();
5942 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005943 }
5944 break;
5945 }
5946 return true;
5947 }
5948
5949 return false;
5950 }
5951
5952 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05005953 * Remove the longpress detection timer.
5954 */
5955 private void removeLongPressCallback() {
5956 if (mPendingCheckForLongPress != null) {
5957 removeCallbacks(mPendingCheckForLongPress);
5958 }
5959 }
Adam Powell3cb8b632011-01-21 15:34:14 -08005960
5961 /**
5962 * Remove the pending click action
5963 */
5964 private void removePerformClickCallback() {
5965 if (mPerformClick != null) {
5966 removeCallbacks(mPerformClick);
5967 }
5968 }
5969
Adam Powelle14579b2009-12-16 18:39:52 -08005970 /**
Romain Guya440b002010-02-24 15:57:54 -08005971 * Remove the prepress detection timer.
5972 */
5973 private void removeUnsetPressCallback() {
5974 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
5975 setPressed(false);
5976 removeCallbacks(mUnsetPressedState);
5977 }
5978 }
5979
5980 /**
Adam Powelle14579b2009-12-16 18:39:52 -08005981 * Remove the tap detection timer.
5982 */
5983 private void removeTapCallback() {
5984 if (mPendingCheckForTap != null) {
5985 mPrivateFlags &= ~PREPRESSED;
5986 removeCallbacks(mPendingCheckForTap);
5987 }
5988 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005989
5990 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005991 * Cancels a pending long press. Your subclass can use this if you
5992 * want the context menu to come up if the user presses and holds
5993 * at the same place, but you don't want it to come up if they press
5994 * and then move around enough to cause scrolling.
5995 */
5996 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005997 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08005998
5999 /*
6000 * The prepressed state handled by the tap callback is a display
6001 * construct, but the tap callback will post a long press callback
6002 * less its own timeout. Remove it here.
6003 */
6004 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006005 }
6006
6007 /**
6008 * Sets the TouchDelegate for this View.
6009 */
6010 public void setTouchDelegate(TouchDelegate delegate) {
6011 mTouchDelegate = delegate;
6012 }
6013
6014 /**
6015 * Gets the TouchDelegate for this View.
6016 */
6017 public TouchDelegate getTouchDelegate() {
6018 return mTouchDelegate;
6019 }
6020
6021 /**
6022 * Set flags controlling behavior of this view.
6023 *
6024 * @param flags Constant indicating the value which should be set
6025 * @param mask Constant indicating the bit range that should be changed
6026 */
6027 void setFlags(int flags, int mask) {
6028 int old = mViewFlags;
6029 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6030
6031 int changed = mViewFlags ^ old;
6032 if (changed == 0) {
6033 return;
6034 }
6035 int privateFlags = mPrivateFlags;
6036
6037 /* Check if the FOCUSABLE bit has changed */
6038 if (((changed & FOCUSABLE_MASK) != 0) &&
6039 ((privateFlags & HAS_BOUNDS) !=0)) {
6040 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6041 && ((privateFlags & FOCUSED) != 0)) {
6042 /* Give up focus if we are no longer focusable */
6043 clearFocus();
6044 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6045 && ((privateFlags & FOCUSED) == 0)) {
6046 /*
6047 * Tell the view system that we are now available to take focus
6048 * if no one else already has it.
6049 */
6050 if (mParent != null) mParent.focusableViewAvailable(this);
6051 }
6052 }
6053
6054 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6055 if ((changed & VISIBILITY_MASK) != 0) {
6056 /*
6057 * If this view is becoming visible, set the DRAWN flag so that
6058 * the next invalidate() will not be skipped.
6059 */
6060 mPrivateFlags |= DRAWN;
6061
6062 needGlobalAttributesUpdate(true);
6063
6064 // a view becoming visible is worth notifying the parent
6065 // about in case nothing has focus. even if this specific view
6066 // isn't focusable, it may contain something that is, so let
6067 // the root view try to give this focus if nothing else does.
6068 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6069 mParent.focusableViewAvailable(this);
6070 }
6071 }
6072 }
6073
6074 /* Check if the GONE bit has changed */
6075 if ((changed & GONE) != 0) {
6076 needGlobalAttributesUpdate(false);
6077 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006078 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006079
Romain Guyecd80ee2009-12-03 17:13:02 -08006080 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6081 if (hasFocus()) clearFocus();
6082 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006083 }
6084 if (mAttachInfo != null) {
6085 mAttachInfo.mViewVisibilityChanged = true;
6086 }
6087 }
6088
6089 /* Check if the VISIBLE bit has changed */
6090 if ((changed & INVISIBLE) != 0) {
6091 needGlobalAttributesUpdate(false);
Romain Guy0fd89bf2011-01-26 15:41:30 -08006092 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006093
6094 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6095 // root view becoming invisible shouldn't clear focus
6096 if (getRootView() != this) {
6097 clearFocus();
6098 }
6099 }
6100 if (mAttachInfo != null) {
6101 mAttachInfo.mViewVisibilityChanged = true;
6102 }
6103 }
6104
Adam Powell326d8082009-12-09 15:10:07 -08006105 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006106 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006107 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6108 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006109 }
Adam Powell326d8082009-12-09 15:10:07 -08006110 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6111 }
6112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006113 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6114 destroyDrawingCache();
6115 }
6116
6117 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6118 destroyDrawingCache();
6119 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006120 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006121 }
6122
6123 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6124 destroyDrawingCache();
6125 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6126 }
6127
6128 if ((changed & DRAW_MASK) != 0) {
6129 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6130 if (mBGDrawable != null) {
6131 mPrivateFlags &= ~SKIP_DRAW;
6132 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6133 } else {
6134 mPrivateFlags |= SKIP_DRAW;
6135 }
6136 } else {
6137 mPrivateFlags &= ~SKIP_DRAW;
6138 }
6139 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006140 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006141 }
6142
6143 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006144 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006145 mParent.recomputeViewAttributes(this);
6146 }
6147 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006148
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006149 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006150 requestLayout();
6151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006152 }
6153
6154 /**
6155 * Change the view's z order in the tree, so it's on top of other sibling
6156 * views
6157 */
6158 public void bringToFront() {
6159 if (mParent != null) {
6160 mParent.bringChildToFront(this);
6161 }
6162 }
6163
6164 /**
6165 * This is called in response to an internal scroll in this view (i.e., the
6166 * view scrolled its own contents). This is typically as a result of
6167 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6168 * called.
6169 *
6170 * @param l Current horizontal scroll origin.
6171 * @param t Current vertical scroll origin.
6172 * @param oldl Previous horizontal scroll origin.
6173 * @param oldt Previous vertical scroll origin.
6174 */
6175 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
6176 mBackgroundSizeChanged = true;
6177
6178 final AttachInfo ai = mAttachInfo;
6179 if (ai != null) {
6180 ai.mViewScrollChanged = true;
6181 }
6182 }
6183
6184 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006185 * Interface definition for a callback to be invoked when the layout bounds of a view
6186 * changes due to layout processing.
6187 */
6188 public interface OnLayoutChangeListener {
6189 /**
6190 * Called when the focus state of a view has changed.
6191 *
6192 * @param v The view whose state has changed.
6193 * @param left The new value of the view's left property.
6194 * @param top The new value of the view's top property.
6195 * @param right The new value of the view's right property.
6196 * @param bottom The new value of the view's bottom property.
6197 * @param oldLeft The previous value of the view's left property.
6198 * @param oldTop The previous value of the view's top property.
6199 * @param oldRight The previous value of the view's right property.
6200 * @param oldBottom The previous value of the view's bottom property.
6201 */
6202 void onLayoutChange(View v, int left, int top, int right, int bottom,
6203 int oldLeft, int oldTop, int oldRight, int oldBottom);
6204 }
6205
6206 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006207 * This is called during layout when the size of this view has changed. If
6208 * you were just added to the view hierarchy, you're called with the old
6209 * values of 0.
6210 *
6211 * @param w Current width of this view.
6212 * @param h Current height of this view.
6213 * @param oldw Old width of this view.
6214 * @param oldh Old height of this view.
6215 */
6216 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6217 }
6218
6219 /**
6220 * Called by draw to draw the child views. This may be overridden
6221 * by derived classes to gain control just before its children are drawn
6222 * (but after its own view has been drawn).
6223 * @param canvas the canvas on which to draw the view
6224 */
6225 protected void dispatchDraw(Canvas canvas) {
6226 }
6227
6228 /**
6229 * Gets the parent of this view. Note that the parent is a
6230 * ViewParent and not necessarily a View.
6231 *
6232 * @return Parent of this view.
6233 */
6234 public final ViewParent getParent() {
6235 return mParent;
6236 }
6237
6238 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006239 * Set the horizontal scrolled position of your view. This will cause a call to
6240 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6241 * invalidated.
6242 * @param value the x position to scroll to
6243 */
6244 public void setScrollX(int value) {
6245 scrollTo(value, mScrollY);
6246 }
6247
6248 /**
6249 * Set the vertical scrolled position of your view. This will cause a call to
6250 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6251 * invalidated.
6252 * @param value the y position to scroll to
6253 */
6254 public void setScrollY(int value) {
6255 scrollTo(mScrollX, value);
6256 }
6257
6258 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006259 * Return the scrolled left position of this view. This is the left edge of
6260 * the displayed part of your view. You do not need to draw any pixels
6261 * farther left, since those are outside of the frame of your view on
6262 * screen.
6263 *
6264 * @return The left edge of the displayed part of your view, in pixels.
6265 */
6266 public final int getScrollX() {
6267 return mScrollX;
6268 }
6269
6270 /**
6271 * Return the scrolled top position of this view. This is the top edge of
6272 * the displayed part of your view. You do not need to draw any pixels above
6273 * it, since those are outside of the frame of your view on screen.
6274 *
6275 * @return The top edge of the displayed part of your view, in pixels.
6276 */
6277 public final int getScrollY() {
6278 return mScrollY;
6279 }
6280
6281 /**
6282 * Return the width of the your view.
6283 *
6284 * @return The width of your view, in pixels.
6285 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006286 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006287 public final int getWidth() {
6288 return mRight - mLeft;
6289 }
6290
6291 /**
6292 * Return the height of your view.
6293 *
6294 * @return The height of your view, in pixels.
6295 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006296 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006297 public final int getHeight() {
6298 return mBottom - mTop;
6299 }
6300
6301 /**
6302 * Return the visible drawing bounds of your view. Fills in the output
6303 * rectangle with the values from getScrollX(), getScrollY(),
6304 * getWidth(), and getHeight().
6305 *
6306 * @param outRect The (scrolled) drawing bounds of the view.
6307 */
6308 public void getDrawingRect(Rect outRect) {
6309 outRect.left = mScrollX;
6310 outRect.top = mScrollY;
6311 outRect.right = mScrollX + (mRight - mLeft);
6312 outRect.bottom = mScrollY + (mBottom - mTop);
6313 }
6314
6315 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006316 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6317 * raw width component (that is the result is masked by
6318 * {@link #MEASURED_SIZE_MASK}).
6319 *
6320 * @return The raw measured width of this view.
6321 */
6322 public final int getMeasuredWidth() {
6323 return mMeasuredWidth & MEASURED_SIZE_MASK;
6324 }
6325
6326 /**
6327 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006328 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006329 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006330 * This should be used during measurement and layout calculations only. Use
6331 * {@link #getWidth()} to see how wide a view is after layout.
6332 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006333 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006334 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006335 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006336 return mMeasuredWidth;
6337 }
6338
6339 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006340 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6341 * raw width component (that is the result is masked by
6342 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006343 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006344 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006345 */
6346 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006347 return mMeasuredHeight & MEASURED_SIZE_MASK;
6348 }
6349
6350 /**
6351 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006352 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006353 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6354 * This should be used during measurement and layout calculations only. Use
6355 * {@link #getHeight()} to see how wide a view is after layout.
6356 *
6357 * @return The measured width of this view as a bit mask.
6358 */
6359 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006360 return mMeasuredHeight;
6361 }
6362
6363 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006364 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6365 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6366 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6367 * and the height component is at the shifted bits
6368 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6369 */
6370 public final int getMeasuredState() {
6371 return (mMeasuredWidth&MEASURED_STATE_MASK)
6372 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6373 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6374 }
6375
6376 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006377 * The transform matrix of this view, which is calculated based on the current
6378 * roation, scale, and pivot properties.
6379 *
6380 * @see #getRotation()
6381 * @see #getScaleX()
6382 * @see #getScaleY()
6383 * @see #getPivotX()
6384 * @see #getPivotY()
6385 * @return The current transform matrix for the view
6386 */
6387 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07006388 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07006389 return mMatrix;
6390 }
6391
6392 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006393 * Utility function to determine if the value is far enough away from zero to be
6394 * considered non-zero.
6395 * @param value A floating point value to check for zero-ness
6396 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6397 */
6398 private static boolean nonzero(float value) {
6399 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6400 }
6401
6402 /**
Jeff Brown86671742010-09-30 20:00:15 -07006403 * Returns true if the transform matrix is the identity matrix.
6404 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006405 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006406 * @return True if the transform matrix is the identity matrix, false otherwise.
6407 */
Jeff Brown86671742010-09-30 20:00:15 -07006408 final boolean hasIdentityMatrix() {
6409 updateMatrix();
6410 return mMatrixIsIdentity;
6411 }
6412
6413 /**
6414 * Recomputes the transform matrix if necessary.
6415 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006416 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07006417 if (mMatrixDirty) {
6418 // transform-related properties have changed since the last time someone
6419 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006420
6421 // Figure out if we need to update the pivot point
6422 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006423 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006424 mPrevWidth = mRight - mLeft;
6425 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08006426 mPivotX = mPrevWidth / 2f;
6427 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07006428 }
6429 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006430 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07006431 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
6432 mMatrix.setTranslate(mTranslationX, mTranslationY);
6433 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
6434 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
6435 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07006436 if (mCamera == null) {
6437 mCamera = new Camera();
6438 matrix3D = new Matrix();
6439 }
6440 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07006441 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08006442 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07006443 mCamera.getMatrix(matrix3D);
6444 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006445 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07006446 mMatrix.postConcat(matrix3D);
6447 mCamera.restore();
6448 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006449 mMatrixDirty = false;
6450 mMatrixIsIdentity = mMatrix.isIdentity();
6451 mInverseMatrixDirty = true;
6452 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006453 }
6454
6455 /**
6456 * Utility method to retrieve the inverse of the current mMatrix property.
6457 * We cache the matrix to avoid recalculating it when transform properties
6458 * have not changed.
6459 *
6460 * @return The inverse of the current matrix of this view.
6461 */
Jeff Brown86671742010-09-30 20:00:15 -07006462 final Matrix getInverseMatrix() {
6463 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07006464 if (mInverseMatrixDirty) {
6465 if (mInverseMatrix == null) {
6466 mInverseMatrix = new Matrix();
6467 }
6468 mMatrix.invert(mInverseMatrix);
6469 mInverseMatrixDirty = false;
6470 }
6471 return mInverseMatrix;
6472 }
6473
6474 /**
Romain Guya5364ee2011-02-24 14:46:04 -08006475 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
6476 * views are drawn) from the camera to this view. The camera's distance
6477 * affects 3D transformations, for instance rotations around the X and Y
6478 * axis. If the rotationX or rotationY properties are changed and this view is
6479 * large (more than half the size of the screen), it is recommended to always
6480 * use a camera distance that's greater than the height (X axis rotation) or
6481 * the width (Y axis rotation) of this view.</p>
6482 *
6483 * <p>The distance of the camera from the view plane can have an affect on the
6484 * perspective distortion of the view when it is rotated around the x or y axis.
6485 * For example, a large distance will result in a large viewing angle, and there
6486 * will not be much perspective distortion of the view as it rotates. A short
6487 * distance may cause much more perspective distortion upon rotation, and can
6488 * also result in some drawing artifacts if the rotated view ends up partially
6489 * behind the camera (which is why the recommendation is to use a distance at
6490 * least as far as the size of the view, if the view is to be rotated.)</p>
6491 *
6492 * <p>The distance is expressed in "depth pixels." The default distance depends
6493 * on the screen density. For instance, on a medium density display, the
6494 * default distance is 1280. On a high density display, the default distance
6495 * is 1920.</p>
6496 *
6497 * <p>If you want to specify a distance that leads to visually consistent
6498 * results across various densities, use the following formula:</p>
6499 * <pre>
6500 * float scale = context.getResources().getDisplayMetrics().density;
6501 * view.setCameraDistance(distance * scale);
6502 * </pre>
6503 *
6504 * <p>The density scale factor of a high density display is 1.5,
6505 * and 1920 = 1280 * 1.5.</p>
6506 *
6507 * @param distance The distance in "depth pixels", if negative the opposite
6508 * value is used
6509 *
6510 * @see #setRotationX(float)
6511 * @see #setRotationY(float)
6512 */
6513 public void setCameraDistance(float distance) {
6514 invalidateParentCaches();
6515 invalidate(false);
6516
6517 final float dpi = mResources.getDisplayMetrics().densityDpi;
6518 if (mCamera == null) {
6519 mCamera = new Camera();
6520 matrix3D = new Matrix();
6521 }
6522
6523 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6524 mMatrixDirty = true;
6525
6526 invalidate(false);
6527 }
6528
6529 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006530 * The degrees that the view is rotated around the pivot point.
6531 *
Romain Guya5364ee2011-02-24 14:46:04 -08006532 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006533 * @see #getPivotX()
6534 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006535 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006536 * @return The degrees of rotation.
6537 */
6538 public float getRotation() {
6539 return mRotation;
6540 }
6541
6542 /**
Chet Haase897247b2010-09-09 14:54:47 -07006543 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6544 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006545 *
6546 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006547 *
6548 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006549 * @see #getPivotX()
6550 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006551 * @see #setRotationX(float)
6552 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006553 *
6554 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006555 */
6556 public void setRotation(float rotation) {
6557 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006558 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006559 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006560 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006561 mRotation = rotation;
6562 mMatrixDirty = true;
6563 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006564 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006565 }
6566 }
6567
6568 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006569 * The degrees that the view is rotated around the vertical axis through the pivot point.
6570 *
6571 * @see #getPivotX()
6572 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006573 * @see #setRotationY(float)
6574 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006575 * @return The degrees of Y rotation.
6576 */
6577 public float getRotationY() {
6578 return mRotationY;
6579 }
6580
6581 /**
Chet Haase897247b2010-09-09 14:54:47 -07006582 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6583 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6584 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006585 *
6586 * When rotating large views, it is recommended to adjust the camera distance
6587 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006588 *
6589 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006590 *
6591 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006592 * @see #getPivotX()
6593 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006594 * @see #setRotation(float)
6595 * @see #setRotationX(float)
6596 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006597 *
6598 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006599 */
6600 public void setRotationY(float rotationY) {
6601 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006602 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006603 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006604 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006605 mRotationY = rotationY;
6606 mMatrixDirty = true;
6607 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006608 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006609 }
6610 }
6611
6612 /**
6613 * The degrees that the view is rotated around the horizontal axis through the pivot point.
6614 *
6615 * @see #getPivotX()
6616 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006617 * @see #setRotationX(float)
6618 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006619 * @return The degrees of X rotation.
6620 */
6621 public float getRotationX() {
6622 return mRotationX;
6623 }
6624
6625 /**
Chet Haase897247b2010-09-09 14:54:47 -07006626 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6627 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6628 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006629 *
6630 * When rotating large views, it is recommended to adjust the camera distance
6631 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006632 *
6633 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006634 *
6635 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006636 * @see #getPivotX()
6637 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006638 * @see #setRotation(float)
6639 * @see #setRotationY(float)
6640 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006641 *
6642 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006643 */
6644 public void setRotationX(float rotationX) {
6645 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006646 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006647 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006648 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006649 mRotationX = rotationX;
6650 mMatrixDirty = true;
6651 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006652 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006653 }
6654 }
6655
6656 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006657 * The amount that the view is scaled in x around the pivot point, as a proportion of
6658 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6659 *
Joe Onorato93162322010-09-16 15:42:01 -04006660 * <p>By default, this is 1.0f.
6661 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006662 * @see #getPivotX()
6663 * @see #getPivotY()
6664 * @return The scaling factor.
6665 */
6666 public float getScaleX() {
6667 return mScaleX;
6668 }
6669
6670 /**
6671 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6672 * the view's unscaled width. A value of 1 means that no scaling is applied.
6673 *
6674 * @param scaleX The scaling factor.
6675 * @see #getPivotX()
6676 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006677 *
6678 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006679 */
6680 public void setScaleX(float scaleX) {
6681 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006682 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006683 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006684 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006685 mScaleX = scaleX;
6686 mMatrixDirty = true;
6687 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006688 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006689 }
6690 }
6691
6692 /**
6693 * The amount that the view is scaled in y around the pivot point, as a proportion of
6694 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6695 *
Joe Onorato93162322010-09-16 15:42:01 -04006696 * <p>By default, this is 1.0f.
6697 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006698 * @see #getPivotX()
6699 * @see #getPivotY()
6700 * @return The scaling factor.
6701 */
6702 public float getScaleY() {
6703 return mScaleY;
6704 }
6705
6706 /**
6707 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
6708 * the view's unscaled width. A value of 1 means that no scaling is applied.
6709 *
6710 * @param scaleY The scaling factor.
6711 * @see #getPivotX()
6712 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006713 *
6714 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07006715 */
6716 public void setScaleY(float scaleY) {
6717 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006718 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006719 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006720 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006721 mScaleY = scaleY;
6722 mMatrixDirty = true;
6723 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006724 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006725 }
6726 }
6727
6728 /**
6729 * The x location of the point around which the view is {@link #setRotation(float) rotated}
6730 * and {@link #setScaleX(float) scaled}.
6731 *
6732 * @see #getRotation()
6733 * @see #getScaleX()
6734 * @see #getScaleY()
6735 * @see #getPivotY()
6736 * @return The x location of the pivot point.
6737 */
6738 public float getPivotX() {
6739 return mPivotX;
6740 }
6741
6742 /**
6743 * Sets the x location of the point around which the view is
6744 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07006745 * By default, the pivot point is centered on the object.
6746 * Setting this property disables this behavior and causes the view to use only the
6747 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006748 *
6749 * @param pivotX The x location of the pivot point.
6750 * @see #getRotation()
6751 * @see #getScaleX()
6752 * @see #getScaleY()
6753 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006754 *
6755 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07006756 */
6757 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006758 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006759 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006760 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006761 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006762 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006763 mPivotX = pivotX;
6764 mMatrixDirty = true;
6765 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006766 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006767 }
6768 }
6769
6770 /**
6771 * The y location of the point around which the view is {@link #setRotation(float) rotated}
6772 * and {@link #setScaleY(float) scaled}.
6773 *
6774 * @see #getRotation()
6775 * @see #getScaleX()
6776 * @see #getScaleY()
6777 * @see #getPivotY()
6778 * @return The y location of the pivot point.
6779 */
6780 public float getPivotY() {
6781 return mPivotY;
6782 }
6783
6784 /**
6785 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07006786 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
6787 * Setting this property disables this behavior and causes the view to use only the
6788 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006789 *
6790 * @param pivotY The y location of the pivot point.
6791 * @see #getRotation()
6792 * @see #getScaleX()
6793 * @see #getScaleY()
6794 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006795 *
6796 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07006797 */
6798 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006799 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006800 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006801 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006802 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006803 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006804 mPivotY = pivotY;
6805 mMatrixDirty = true;
6806 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006807 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006808 }
6809 }
6810
6811 /**
6812 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
6813 * completely transparent and 1 means the view is completely opaque.
6814 *
Joe Onorato93162322010-09-16 15:42:01 -04006815 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07006816 * @return The opacity of the view.
6817 */
6818 public float getAlpha() {
6819 return mAlpha;
6820 }
6821
6822 /**
Romain Guy171c5922011-01-06 10:04:23 -08006823 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
6824 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08006825 *
Romain Guy171c5922011-01-06 10:04:23 -08006826 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
6827 * responsible for applying the opacity itself. Otherwise, calling this method is
6828 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08006829 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07006830 *
6831 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08006832 *
Joe Malin32736f02011-01-19 16:14:20 -08006833 * @see #setLayerType(int, android.graphics.Paint)
6834 *
Chet Haase73066682010-11-29 15:55:32 -08006835 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07006836 */
6837 public void setAlpha(float alpha) {
6838 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006839 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07006840 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07006841 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006842 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006843 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07006844 } else {
Romain Guya3496a92010-10-12 11:53:24 -07006845 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006846 invalidate(false);
6847 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006848 }
6849
6850 /**
Chet Haasea00f3862011-02-22 06:34:40 -08006851 * Faster version of setAlpha() which performs the same steps except there are
6852 * no calls to invalidate(). The caller of this function should perform proper invalidation
6853 * on the parent and this object. The return value indicates whether the subclass handles
6854 * alpha (the return value for onSetAlpha()).
6855 *
6856 * @param alpha The new value for the alpha property
6857 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
6858 */
6859 boolean setAlphaNoInvalidation(float alpha) {
6860 mAlpha = alpha;
6861 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
6862 if (subclassHandlesAlpha) {
6863 mPrivateFlags |= ALPHA_SET;
6864 } else {
6865 mPrivateFlags &= ~ALPHA_SET;
6866 }
6867 return subclassHandlesAlpha;
6868 }
6869
6870 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006871 * Top position of this view relative to its parent.
6872 *
6873 * @return The top of this view, in pixels.
6874 */
6875 @ViewDebug.CapturedViewProperty
6876 public final int getTop() {
6877 return mTop;
6878 }
6879
6880 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006881 * Sets the top position of this view relative to its parent. This method is meant to be called
6882 * by the layout system and should not generally be called otherwise, because the property
6883 * may be changed at any time by the layout.
6884 *
6885 * @param top The top of this view, in pixels.
6886 */
6887 public final void setTop(int top) {
6888 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07006889 updateMatrix();
6890 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006891 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006892 int minTop;
6893 int yLoc;
6894 if (top < mTop) {
6895 minTop = top;
6896 yLoc = top - mTop;
6897 } else {
6898 minTop = mTop;
6899 yLoc = 0;
6900 }
Chet Haasee9140a72011-02-16 16:23:29 -08006901 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006902 }
6903 } else {
6904 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006905 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006906 }
6907
Chet Haaseed032702010-10-01 14:05:54 -07006908 int width = mRight - mLeft;
6909 int oldHeight = mBottom - mTop;
6910
Chet Haase21cd1382010-09-01 17:42:29 -07006911 mTop = top;
6912
Chet Haaseed032702010-10-01 14:05:54 -07006913 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6914
Chet Haase21cd1382010-09-01 17:42:29 -07006915 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006916 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6917 // A change in dimension means an auto-centered pivot point changes, too
6918 mMatrixDirty = true;
6919 }
Chet Haase21cd1382010-09-01 17:42:29 -07006920 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006921 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006922 }
Chet Haase55dbb652010-12-21 20:15:08 -08006923 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006924 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006925 }
6926 }
6927
6928 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006929 * Bottom position of this view relative to its parent.
6930 *
6931 * @return The bottom of this view, in pixels.
6932 */
6933 @ViewDebug.CapturedViewProperty
6934 public final int getBottom() {
6935 return mBottom;
6936 }
6937
6938 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08006939 * True if this view has changed since the last time being drawn.
6940 *
6941 * @return The dirty state of this view.
6942 */
6943 public boolean isDirty() {
6944 return (mPrivateFlags & DIRTY_MASK) != 0;
6945 }
6946
6947 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006948 * Sets the bottom position of this view relative to its parent. This method is meant to be
6949 * called by the layout system and should not generally be called otherwise, because the
6950 * property may be changed at any time by the layout.
6951 *
6952 * @param bottom The bottom of this view, in pixels.
6953 */
6954 public final void setBottom(int bottom) {
6955 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07006956 updateMatrix();
6957 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006958 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006959 int maxBottom;
6960 if (bottom < mBottom) {
6961 maxBottom = mBottom;
6962 } else {
6963 maxBottom = bottom;
6964 }
Chet Haasee9140a72011-02-16 16:23:29 -08006965 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006966 }
6967 } else {
6968 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006969 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006970 }
6971
Chet Haaseed032702010-10-01 14:05:54 -07006972 int width = mRight - mLeft;
6973 int oldHeight = mBottom - mTop;
6974
Chet Haase21cd1382010-09-01 17:42:29 -07006975 mBottom = bottom;
6976
Chet Haaseed032702010-10-01 14:05:54 -07006977 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6978
Chet Haase21cd1382010-09-01 17:42:29 -07006979 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006980 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6981 // A change in dimension means an auto-centered pivot point changes, too
6982 mMatrixDirty = true;
6983 }
Chet Haase21cd1382010-09-01 17:42:29 -07006984 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006985 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006986 }
Chet Haase55dbb652010-12-21 20:15:08 -08006987 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006988 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006989 }
6990 }
6991
6992 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006993 * Left position of this view relative to its parent.
6994 *
6995 * @return The left edge of this view, in pixels.
6996 */
6997 @ViewDebug.CapturedViewProperty
6998 public final int getLeft() {
6999 return mLeft;
7000 }
7001
7002 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007003 * Sets the left position of this view relative to its parent. This method is meant to be called
7004 * by the layout system and should not generally be called otherwise, because the property
7005 * may be changed at any time by the layout.
7006 *
7007 * @param left The bottom of this view, in pixels.
7008 */
7009 public final void setLeft(int left) {
7010 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007011 updateMatrix();
7012 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007013 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007014 int minLeft;
7015 int xLoc;
7016 if (left < mLeft) {
7017 minLeft = left;
7018 xLoc = left - mLeft;
7019 } else {
7020 minLeft = mLeft;
7021 xLoc = 0;
7022 }
Chet Haasee9140a72011-02-16 16:23:29 -08007023 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007024 }
7025 } else {
7026 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007027 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007028 }
7029
Chet Haaseed032702010-10-01 14:05:54 -07007030 int oldWidth = mRight - mLeft;
7031 int height = mBottom - mTop;
7032
Chet Haase21cd1382010-09-01 17:42:29 -07007033 mLeft = left;
7034
Chet Haaseed032702010-10-01 14:05:54 -07007035 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7036
Chet Haase21cd1382010-09-01 17:42:29 -07007037 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007038 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7039 // A change in dimension means an auto-centered pivot point changes, too
7040 mMatrixDirty = true;
7041 }
Chet Haase21cd1382010-09-01 17:42:29 -07007042 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007043 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007044 }
Chet Haase55dbb652010-12-21 20:15:08 -08007045 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007046 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007047 }
7048 }
7049
7050 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007051 * Right position of this view relative to its parent.
7052 *
7053 * @return The right edge of this view, in pixels.
7054 */
7055 @ViewDebug.CapturedViewProperty
7056 public final int getRight() {
7057 return mRight;
7058 }
7059
7060 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007061 * Sets the right position of this view relative to its parent. This method is meant to be called
7062 * by the layout system and should not generally be called otherwise, because the property
7063 * may be changed at any time by the layout.
7064 *
7065 * @param right The bottom of this view, in pixels.
7066 */
7067 public final void setRight(int right) {
7068 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007069 updateMatrix();
7070 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007071 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007072 int maxRight;
7073 if (right < mRight) {
7074 maxRight = mRight;
7075 } else {
7076 maxRight = right;
7077 }
Chet Haasee9140a72011-02-16 16:23:29 -08007078 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007079 }
7080 } else {
7081 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007082 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007083 }
7084
Chet Haaseed032702010-10-01 14:05:54 -07007085 int oldWidth = mRight - mLeft;
7086 int height = mBottom - mTop;
7087
Chet Haase21cd1382010-09-01 17:42:29 -07007088 mRight = right;
7089
Chet Haaseed032702010-10-01 14:05:54 -07007090 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7091
Chet Haase21cd1382010-09-01 17:42:29 -07007092 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007093 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7094 // A change in dimension means an auto-centered pivot point changes, too
7095 mMatrixDirty = true;
7096 }
Chet Haase21cd1382010-09-01 17:42:29 -07007097 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007098 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007099 }
Chet Haase55dbb652010-12-21 20:15:08 -08007100 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007101 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007102 }
7103 }
7104
7105 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007106 * The visual x position of this view, in pixels. This is equivalent to the
7107 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007108 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007109 *
Chet Haasedf030d22010-07-30 17:22:38 -07007110 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007111 */
Chet Haasedf030d22010-07-30 17:22:38 -07007112 public float getX() {
7113 return mLeft + mTranslationX;
7114 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007115
Chet Haasedf030d22010-07-30 17:22:38 -07007116 /**
7117 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7118 * {@link #setTranslationX(float) translationX} property to be the difference between
7119 * the x value passed in and the current {@link #getLeft() left} property.
7120 *
7121 * @param x The visual x position of this view, in pixels.
7122 */
7123 public void setX(float x) {
7124 setTranslationX(x - mLeft);
7125 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007126
Chet Haasedf030d22010-07-30 17:22:38 -07007127 /**
7128 * The visual y position of this view, in pixels. This is equivalent to the
7129 * {@link #setTranslationY(float) translationY} property plus the current
7130 * {@link #getTop() top} property.
7131 *
7132 * @return The visual y position of this view, in pixels.
7133 */
7134 public float getY() {
7135 return mTop + mTranslationY;
7136 }
7137
7138 /**
7139 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7140 * {@link #setTranslationY(float) translationY} property to be the difference between
7141 * the y value passed in and the current {@link #getTop() top} property.
7142 *
7143 * @param y The visual y position of this view, in pixels.
7144 */
7145 public void setY(float y) {
7146 setTranslationY(y - mTop);
7147 }
7148
7149
7150 /**
7151 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7152 * This position is post-layout, in addition to wherever the object's
7153 * layout placed it.
7154 *
7155 * @return The horizontal position of this view relative to its left position, in pixels.
7156 */
7157 public float getTranslationX() {
7158 return mTranslationX;
7159 }
7160
7161 /**
7162 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7163 * This effectively positions the object post-layout, in addition to wherever the object's
7164 * layout placed it.
7165 *
7166 * @param translationX The horizontal position of this view relative to its left position,
7167 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007168 *
7169 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007170 */
7171 public void setTranslationX(float translationX) {
7172 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007173 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007174 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007175 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007176 mTranslationX = translationX;
7177 mMatrixDirty = true;
7178 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007179 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007180 }
7181 }
7182
7183 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007184 * The horizontal location of this view relative to its {@link #getTop() top} position.
7185 * This position is post-layout, in addition to wherever the object's
7186 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007187 *
Chet Haasedf030d22010-07-30 17:22:38 -07007188 * @return The vertical position of this view relative to its top position,
7189 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007190 */
Chet Haasedf030d22010-07-30 17:22:38 -07007191 public float getTranslationY() {
7192 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07007193 }
7194
7195 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007196 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7197 * This effectively positions the object post-layout, in addition to wherever the object's
7198 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007199 *
Chet Haasedf030d22010-07-30 17:22:38 -07007200 * @param translationY The vertical position of this view relative to its top position,
7201 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007202 *
7203 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007204 */
Chet Haasedf030d22010-07-30 17:22:38 -07007205 public void setTranslationY(float translationY) {
7206 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007207 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007208 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007209 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007210 mTranslationY = translationY;
7211 mMatrixDirty = true;
7212 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007213 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007214 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007215 }
7216
7217 /**
Romain Guyda489792011-02-03 01:05:15 -08007218 * @hide
7219 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007220 public void setFastTranslationX(float x) {
7221 mTranslationX = x;
7222 mMatrixDirty = true;
7223 }
7224
7225 /**
7226 * @hide
7227 */
7228 public void setFastTranslationY(float y) {
7229 mTranslationY = y;
7230 mMatrixDirty = true;
7231 }
7232
7233 /**
7234 * @hide
7235 */
Romain Guyda489792011-02-03 01:05:15 -08007236 public void setFastX(float x) {
7237 mTranslationX = x - mLeft;
7238 mMatrixDirty = true;
7239 }
7240
7241 /**
7242 * @hide
7243 */
7244 public void setFastY(float y) {
7245 mTranslationY = y - mTop;
7246 mMatrixDirty = true;
7247 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007248
Romain Guyda489792011-02-03 01:05:15 -08007249 /**
7250 * @hide
7251 */
7252 public void setFastScaleX(float x) {
7253 mScaleX = x;
7254 mMatrixDirty = true;
7255 }
7256
7257 /**
7258 * @hide
7259 */
7260 public void setFastScaleY(float y) {
7261 mScaleY = y;
7262 mMatrixDirty = true;
7263 }
7264
7265 /**
7266 * @hide
7267 */
7268 public void setFastAlpha(float alpha) {
7269 mAlpha = alpha;
7270 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007271
Romain Guyda489792011-02-03 01:05:15 -08007272 /**
7273 * @hide
7274 */
7275 public void setFastRotationY(float y) {
7276 mRotationY = y;
7277 mMatrixDirty = true;
7278 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007279
Romain Guyda489792011-02-03 01:05:15 -08007280 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007281 * Hit rectangle in parent's coordinates
7282 *
7283 * @param outRect The hit rectangle of the view.
7284 */
7285 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007286 updateMatrix();
7287 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007288 outRect.set(mLeft, mTop, mRight, mBottom);
7289 } else {
7290 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07007291 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07007292 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007293 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7294 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007295 }
7296 }
7297
7298 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007299 * Determines whether the given point, in local coordinates is inside the view.
7300 */
7301 /*package*/ final boolean pointInView(float localX, float localY) {
7302 return localX >= 0 && localX < (mRight - mLeft)
7303 && localY >= 0 && localY < (mBottom - mTop);
7304 }
7305
7306 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007307 * Utility method to determine whether the given point, in local coordinates,
7308 * is inside the view, where the area of the view is expanded by the slop factor.
7309 * This method is called while processing touch-move events to determine if the event
7310 * is still within the view.
7311 */
7312 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007313 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007314 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007315 }
7316
7317 /**
7318 * When a view has focus and the user navigates away from it, the next view is searched for
7319 * starting from the rectangle filled in by this method.
7320 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007321 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7322 * of the view. However, if your view maintains some idea of internal selection,
7323 * such as a cursor, or a selected row or column, you should override this method and
7324 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007325 *
7326 * @param r The rectangle to fill in, in this view's coordinates.
7327 */
7328 public void getFocusedRect(Rect r) {
7329 getDrawingRect(r);
7330 }
7331
7332 /**
7333 * If some part of this view is not clipped by any of its parents, then
7334 * return that area in r in global (root) coordinates. To convert r to local
7335 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7336 * -globalOffset.y)) If the view is completely clipped or translated out,
7337 * return false.
7338 *
7339 * @param r If true is returned, r holds the global coordinates of the
7340 * visible portion of this view.
7341 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7342 * between this view and its root. globalOffet may be null.
7343 * @return true if r is non-empty (i.e. part of the view is visible at the
7344 * root level.
7345 */
7346 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7347 int width = mRight - mLeft;
7348 int height = mBottom - mTop;
7349 if (width > 0 && height > 0) {
7350 r.set(0, 0, width, height);
7351 if (globalOffset != null) {
7352 globalOffset.set(-mScrollX, -mScrollY);
7353 }
7354 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7355 }
7356 return false;
7357 }
7358
7359 public final boolean getGlobalVisibleRect(Rect r) {
7360 return getGlobalVisibleRect(r, null);
7361 }
7362
7363 public final boolean getLocalVisibleRect(Rect r) {
7364 Point offset = new Point();
7365 if (getGlobalVisibleRect(r, offset)) {
7366 r.offset(-offset.x, -offset.y); // make r local
7367 return true;
7368 }
7369 return false;
7370 }
7371
7372 /**
7373 * Offset this view's vertical location by the specified number of pixels.
7374 *
7375 * @param offset the number of pixels to offset the view by
7376 */
7377 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007378 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007379 updateMatrix();
7380 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007381 final ViewParent p = mParent;
7382 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007383 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007384 int minTop;
7385 int maxBottom;
7386 int yLoc;
7387 if (offset < 0) {
7388 minTop = mTop + offset;
7389 maxBottom = mBottom;
7390 yLoc = offset;
7391 } else {
7392 minTop = mTop;
7393 maxBottom = mBottom + offset;
7394 yLoc = 0;
7395 }
7396 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
7397 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007398 }
7399 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007400 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007401 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007402
Chet Haasec3aa3612010-06-17 08:50:37 -07007403 mTop += offset;
7404 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007405
Chet Haasec3aa3612010-06-17 08:50:37 -07007406 if (!mMatrixIsIdentity) {
7407 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007408 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007409 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007410 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007412 }
7413
7414 /**
7415 * Offset this view's horizontal location by the specified amount of pixels.
7416 *
7417 * @param offset the numer of pixels to offset the view by
7418 */
7419 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007420 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007421 updateMatrix();
7422 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007423 final ViewParent p = mParent;
7424 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007425 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007426 int minLeft;
7427 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007428 if (offset < 0) {
7429 minLeft = mLeft + offset;
7430 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007431 } else {
7432 minLeft = mLeft;
7433 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007434 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007435 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07007436 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007437 }
7438 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007439 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007440 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007441
Chet Haasec3aa3612010-06-17 08:50:37 -07007442 mLeft += offset;
7443 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007444
Chet Haasec3aa3612010-06-17 08:50:37 -07007445 if (!mMatrixIsIdentity) {
7446 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007447 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007448 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007449 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007450 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007451 }
7452
7453 /**
7454 * Get the LayoutParams associated with this view. All views should have
7455 * layout parameters. These supply parameters to the <i>parent</i> of this
7456 * view specifying how it should be arranged. There are many subclasses of
7457 * ViewGroup.LayoutParams, and these correspond to the different subclasses
7458 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08007459 *
7460 * This method may return null if this View is not attached to a parent
7461 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
7462 * was not invoked successfully. When a View is attached to a parent
7463 * ViewGroup, this method must not return null.
7464 *
7465 * @return The LayoutParams associated with this view, or null if no
7466 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007467 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07007468 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007469 public ViewGroup.LayoutParams getLayoutParams() {
7470 return mLayoutParams;
7471 }
7472
7473 /**
7474 * Set the layout parameters associated with this view. These supply
7475 * parameters to the <i>parent</i> of this view specifying how it should be
7476 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
7477 * correspond to the different subclasses of ViewGroup that are responsible
7478 * for arranging their children.
7479 *
Romain Guy01c174b2011-02-22 11:51:06 -08007480 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007481 */
7482 public void setLayoutParams(ViewGroup.LayoutParams params) {
7483 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007484 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007485 }
7486 mLayoutParams = params;
7487 requestLayout();
7488 }
7489
7490 /**
7491 * Set the scrolled position of your view. This will cause a call to
7492 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7493 * invalidated.
7494 * @param x the x position to scroll to
7495 * @param y the y position to scroll to
7496 */
7497 public void scrollTo(int x, int y) {
7498 if (mScrollX != x || mScrollY != y) {
7499 int oldX = mScrollX;
7500 int oldY = mScrollY;
7501 mScrollX = x;
7502 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007503 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007504 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007505 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007506 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007508 }
7509 }
7510
7511 /**
7512 * Move the scrolled position of your view. This will cause a call to
7513 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7514 * invalidated.
7515 * @param x the amount of pixels to scroll by horizontally
7516 * @param y the amount of pixels to scroll by vertically
7517 */
7518 public void scrollBy(int x, int y) {
7519 scrollTo(mScrollX + x, mScrollY + y);
7520 }
7521
7522 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007523 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7524 * animation to fade the scrollbars out after a default delay. If a subclass
7525 * provides animated scrolling, the start delay should equal the duration
7526 * of the scrolling animation.</p>
7527 *
7528 * <p>The animation starts only if at least one of the scrollbars is
7529 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7530 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7531 * this method returns true, and false otherwise. If the animation is
7532 * started, this method calls {@link #invalidate()}; in that case the
7533 * caller should not call {@link #invalidate()}.</p>
7534 *
7535 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007536 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007537 *
7538 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7539 * and {@link #scrollTo(int, int)}.</p>
7540 *
7541 * @return true if the animation is played, false otherwise
7542 *
7543 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007544 * @see #scrollBy(int, int)
7545 * @see #scrollTo(int, int)
7546 * @see #isHorizontalScrollBarEnabled()
7547 * @see #isVerticalScrollBarEnabled()
7548 * @see #setHorizontalScrollBarEnabled(boolean)
7549 * @see #setVerticalScrollBarEnabled(boolean)
7550 */
7551 protected boolean awakenScrollBars() {
7552 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007553 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007554 }
7555
7556 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007557 * Trigger the scrollbars to draw.
7558 * This method differs from awakenScrollBars() only in its default duration.
7559 * initialAwakenScrollBars() will show the scroll bars for longer than
7560 * usual to give the user more of a chance to notice them.
7561 *
7562 * @return true if the animation is played, false otherwise.
7563 */
7564 private boolean initialAwakenScrollBars() {
7565 return mScrollCache != null &&
7566 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7567 }
7568
7569 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007570 * <p>
7571 * Trigger the scrollbars to draw. When invoked this method starts an
7572 * animation to fade the scrollbars out after a fixed delay. If a subclass
7573 * provides animated scrolling, the start delay should equal the duration of
7574 * the scrolling animation.
7575 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007576 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007577 * <p>
7578 * The animation starts only if at least one of the scrollbars is enabled,
7579 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7580 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7581 * this method returns true, and false otherwise. If the animation is
7582 * started, this method calls {@link #invalidate()}; in that case the caller
7583 * should not call {@link #invalidate()}.
7584 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007585 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007586 * <p>
7587 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007588 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007589 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007590 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007591 * @param startDelay the delay, in milliseconds, after which the animation
7592 * should start; when the delay is 0, the animation starts
7593 * immediately
7594 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007595 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007596 * @see #scrollBy(int, int)
7597 * @see #scrollTo(int, int)
7598 * @see #isHorizontalScrollBarEnabled()
7599 * @see #isVerticalScrollBarEnabled()
7600 * @see #setHorizontalScrollBarEnabled(boolean)
7601 * @see #setVerticalScrollBarEnabled(boolean)
7602 */
7603 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007604 return awakenScrollBars(startDelay, true);
7605 }
Joe Malin32736f02011-01-19 16:14:20 -08007606
Mike Cleron290947b2009-09-29 18:34:32 -07007607 /**
7608 * <p>
7609 * Trigger the scrollbars to draw. When invoked this method starts an
7610 * animation to fade the scrollbars out after a fixed delay. If a subclass
7611 * provides animated scrolling, the start delay should equal the duration of
7612 * the scrolling animation.
7613 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007614 *
Mike Cleron290947b2009-09-29 18:34:32 -07007615 * <p>
7616 * The animation starts only if at least one of the scrollbars is enabled,
7617 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7618 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7619 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08007620 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07007621 * is set to true; in that case the caller
7622 * should not call {@link #invalidate()}.
7623 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007624 *
Mike Cleron290947b2009-09-29 18:34:32 -07007625 * <p>
7626 * This method should be invoked everytime a subclass directly updates the
7627 * scroll parameters.
7628 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007629 *
Mike Cleron290947b2009-09-29 18:34:32 -07007630 * @param startDelay the delay, in milliseconds, after which the animation
7631 * should start; when the delay is 0, the animation starts
7632 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007633 *
Mike Cleron290947b2009-09-29 18:34:32 -07007634 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007635 *
Mike Cleron290947b2009-09-29 18:34:32 -07007636 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007637 *
Mike Cleron290947b2009-09-29 18:34:32 -07007638 * @see #scrollBy(int, int)
7639 * @see #scrollTo(int, int)
7640 * @see #isHorizontalScrollBarEnabled()
7641 * @see #isVerticalScrollBarEnabled()
7642 * @see #setHorizontalScrollBarEnabled(boolean)
7643 * @see #setVerticalScrollBarEnabled(boolean)
7644 */
7645 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007646 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007647
Mike Cleronf116bf82009-09-27 19:14:12 -07007648 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7649 return false;
7650 }
7651
7652 if (scrollCache.scrollBar == null) {
7653 scrollCache.scrollBar = new ScrollBarDrawable();
7654 }
7655
7656 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7657
Mike Cleron290947b2009-09-29 18:34:32 -07007658 if (invalidate) {
7659 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007660 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007661 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007662
7663 if (scrollCache.state == ScrollabilityCache.OFF) {
7664 // FIXME: this is copied from WindowManagerService.
7665 // We should get this value from the system when it
7666 // is possible to do so.
7667 final int KEY_REPEAT_FIRST_DELAY = 750;
7668 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7669 }
7670
7671 // Tell mScrollCache when we should start fading. This may
7672 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007673 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007674 scrollCache.fadeStartTime = fadeStartTime;
7675 scrollCache.state = ScrollabilityCache.ON;
7676
7677 // Schedule our fader to run, unscheduling any old ones first
7678 if (mAttachInfo != null) {
7679 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7680 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7681 }
7682
7683 return true;
7684 }
7685
7686 return false;
7687 }
7688
7689 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007690 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07007691 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
7692 * in the future. This must be called from a UI thread. To call from a non-UI
7693 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007694 *
7695 * WARNING: This method is destructive to dirty.
7696 * @param dirty the rectangle representing the bounds of the dirty region
7697 */
7698 public void invalidate(Rect dirty) {
7699 if (ViewDebug.TRACE_HIERARCHY) {
7700 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7701 }
7702
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007703 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007704 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7705 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007706 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007707 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007708 final ViewParent p = mParent;
7709 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007710 //noinspection PointlessBooleanExpression,ConstantConditions
7711 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7712 if (p != null && ai != null && ai.mHardwareAccelerated) {
7713 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007714 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007715 p.invalidateChild(this, null);
7716 return;
7717 }
Romain Guyaf636eb2010-12-09 17:47:21 -08007718 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007719 if (p != null && ai != null) {
7720 final int scrollX = mScrollX;
7721 final int scrollY = mScrollY;
7722 final Rect r = ai.mTmpInvalRect;
7723 r.set(dirty.left - scrollX, dirty.top - scrollY,
7724 dirty.right - scrollX, dirty.bottom - scrollY);
7725 mParent.invalidateChild(this, r);
7726 }
7727 }
7728 }
7729
7730 /**
7731 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
7732 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007733 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
7734 * will be called at some point in the future. This must be called from
7735 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007736 * @param l the left position of the dirty region
7737 * @param t the top position of the dirty region
7738 * @param r the right position of the dirty region
7739 * @param b the bottom position of the dirty region
7740 */
7741 public void invalidate(int l, int t, int r, int b) {
7742 if (ViewDebug.TRACE_HIERARCHY) {
7743 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7744 }
7745
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007746 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007747 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7748 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007749 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007750 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007751 final ViewParent p = mParent;
7752 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007753 //noinspection PointlessBooleanExpression,ConstantConditions
7754 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7755 if (p != null && ai != null && ai.mHardwareAccelerated) {
7756 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007757 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007758 p.invalidateChild(this, null);
7759 return;
7760 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007761 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007762 if (p != null && ai != null && l < r && t < b) {
7763 final int scrollX = mScrollX;
7764 final int scrollY = mScrollY;
7765 final Rect tmpr = ai.mTmpInvalRect;
7766 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
7767 p.invalidateChild(this, tmpr);
7768 }
7769 }
7770 }
7771
7772 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07007773 * Invalidate the whole view. If the view is visible,
7774 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
7775 * the future. This must be called from a UI thread. To call from a non-UI thread,
7776 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007777 */
7778 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07007779 invalidate(true);
7780 }
Joe Malin32736f02011-01-19 16:14:20 -08007781
Chet Haaseed032702010-10-01 14:05:54 -07007782 /**
7783 * This is where the invalidate() work actually happens. A full invalidate()
7784 * causes the drawing cache to be invalidated, but this function can be called with
7785 * invalidateCache set to false to skip that invalidation step for cases that do not
7786 * need it (for example, a component that remains at the same dimensions with the same
7787 * content).
7788 *
7789 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
7790 * well. This is usually true for a full invalidate, but may be set to false if the
7791 * View's contents or dimensions have not changed.
7792 */
Romain Guy849d0a32011-02-01 17:20:48 -08007793 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007794 if (ViewDebug.TRACE_HIERARCHY) {
7795 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7796 }
7797
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007798 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08007799 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08007800 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
7801 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07007802 mPrivateFlags &= ~DRAWN;
7803 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08007804 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07007805 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7806 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007807 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07007808 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08007809 //noinspection PointlessBooleanExpression,ConstantConditions
7810 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7811 if (p != null && ai != null && ai.mHardwareAccelerated) {
7812 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007813 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007814 p.invalidateChild(this, null);
7815 return;
7816 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007817 }
Michael Jurkaebefea42010-11-15 16:04:01 -08007818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007819 if (p != null && ai != null) {
7820 final Rect r = ai.mTmpInvalRect;
7821 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7822 // Don't call invalidate -- we don't want to internally scroll
7823 // our own bounds
7824 p.invalidateChild(this, r);
7825 }
7826 }
7827 }
7828
7829 /**
Romain Guyda489792011-02-03 01:05:15 -08007830 * @hide
7831 */
7832 public void fastInvalidate() {
7833 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
7834 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7835 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
7836 if (mParent instanceof View) {
7837 ((View) mParent).mPrivateFlags |= INVALIDATED;
7838 }
7839 mPrivateFlags &= ~DRAWN;
7840 mPrivateFlags |= INVALIDATED;
7841 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07007842 if (mParent != null && mAttachInfo != null) {
7843 if (mAttachInfo.mHardwareAccelerated) {
7844 mParent.invalidateChild(this, null);
7845 } else {
7846 final Rect r = mAttachInfo.mTmpInvalRect;
7847 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7848 // Don't call invalidate -- we don't want to internally scroll
7849 // our own bounds
7850 mParent.invalidateChild(this, r);
7851 }
Romain Guyda489792011-02-03 01:05:15 -08007852 }
7853 }
7854 }
7855
7856 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08007857 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08007858 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7859 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08007860 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
7861 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08007862 *
7863 * @hide
7864 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08007865 protected void invalidateParentCaches() {
7866 if (mParent instanceof View) {
7867 ((View) mParent).mPrivateFlags |= INVALIDATED;
7868 }
7869 }
Joe Malin32736f02011-01-19 16:14:20 -08007870
Romain Guy0fd89bf2011-01-26 15:41:30 -08007871 /**
7872 * Used to indicate that the parent of this view should be invalidated. This functionality
7873 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7874 * which is necessary when various parent-managed properties of the view change, such as
7875 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
7876 * an invalidation event to the parent.
7877 *
7878 * @hide
7879 */
7880 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08007881 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007882 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08007883 }
7884 }
7885
7886 /**
Romain Guy24443ea2009-05-11 11:56:30 -07007887 * Indicates whether this View is opaque. An opaque View guarantees that it will
7888 * draw all the pixels overlapping its bounds using a fully opaque color.
7889 *
7890 * Subclasses of View should override this method whenever possible to indicate
7891 * whether an instance is opaque. Opaque Views are treated in a special way by
7892 * the View hierarchy, possibly allowing it to perform optimizations during
7893 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07007894 *
Romain Guy24443ea2009-05-11 11:56:30 -07007895 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07007896 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007897 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07007898 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07007899 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
7900 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07007901 }
7902
Adam Powell20232d02010-12-08 21:08:53 -08007903 /**
7904 * @hide
7905 */
7906 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07007907 // Opaque if:
7908 // - Has a background
7909 // - Background is opaque
7910 // - Doesn't have scrollbars or scrollbars are inside overlay
7911
7912 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
7913 mPrivateFlags |= OPAQUE_BACKGROUND;
7914 } else {
7915 mPrivateFlags &= ~OPAQUE_BACKGROUND;
7916 }
7917
7918 final int flags = mViewFlags;
7919 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
7920 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
7921 mPrivateFlags |= OPAQUE_SCROLLBARS;
7922 } else {
7923 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
7924 }
7925 }
7926
7927 /**
7928 * @hide
7929 */
7930 protected boolean hasOpaqueScrollbars() {
7931 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07007932 }
7933
7934 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007935 * @return A handler associated with the thread running the View. This
7936 * handler can be used to pump events in the UI events queue.
7937 */
7938 public Handler getHandler() {
7939 if (mAttachInfo != null) {
7940 return mAttachInfo.mHandler;
7941 }
7942 return null;
7943 }
7944
7945 /**
7946 * Causes the Runnable to be added to the message queue.
7947 * The runnable will be run on the user interface thread.
7948 *
7949 * @param action The Runnable that will be executed.
7950 *
7951 * @return Returns true if the Runnable was successfully placed in to the
7952 * message queue. Returns false on failure, usually because the
7953 * looper processing the message queue is exiting.
7954 */
7955 public boolean post(Runnable action) {
7956 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007957 AttachInfo attachInfo = mAttachInfo;
7958 if (attachInfo != null) {
7959 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007960 } else {
7961 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007962 ViewAncestor.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007963 return true;
7964 }
7965
7966 return handler.post(action);
7967 }
7968
7969 /**
7970 * Causes the Runnable to be added to the message queue, to be run
7971 * after the specified amount of time elapses.
7972 * The runnable will be run on the user interface thread.
7973 *
7974 * @param action The Runnable that will be executed.
7975 * @param delayMillis The delay (in milliseconds) until the Runnable
7976 * will be executed.
7977 *
7978 * @return true if the Runnable was successfully placed in to the
7979 * message queue. Returns false on failure, usually because the
7980 * looper processing the message queue is exiting. Note that a
7981 * result of true does not mean the Runnable will be processed --
7982 * if the looper is quit before the delivery time of the message
7983 * occurs then the message will be dropped.
7984 */
7985 public boolean postDelayed(Runnable action, long delayMillis) {
7986 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007987 AttachInfo attachInfo = mAttachInfo;
7988 if (attachInfo != null) {
7989 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007990 } else {
7991 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007992 ViewAncestor.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007993 return true;
7994 }
7995
7996 return handler.postDelayed(action, delayMillis);
7997 }
7998
7999 /**
8000 * Removes the specified Runnable from the message queue.
8001 *
8002 * @param action The Runnable to remove from the message handling queue
8003 *
8004 * @return true if this view could ask the Handler to remove the Runnable,
8005 * false otherwise. When the returned value is true, the Runnable
8006 * may or may not have been actually removed from the message queue
8007 * (for instance, if the Runnable was not in the queue already.)
8008 */
8009 public boolean removeCallbacks(Runnable action) {
8010 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008011 AttachInfo attachInfo = mAttachInfo;
8012 if (attachInfo != null) {
8013 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008014 } else {
8015 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008016 ViewAncestor.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008017 return true;
8018 }
8019
8020 handler.removeCallbacks(action);
8021 return true;
8022 }
8023
8024 /**
8025 * Cause an invalidate to happen on a subsequent cycle through the event loop.
8026 * Use this to invalidate the View from a non-UI thread.
8027 *
8028 * @see #invalidate()
8029 */
8030 public void postInvalidate() {
8031 postInvalidateDelayed(0);
8032 }
8033
8034 /**
8035 * Cause an invalidate of the specified area to happen on a subsequent cycle
8036 * through the event loop. Use this to invalidate the View from a non-UI thread.
8037 *
8038 * @param left The left coordinate of the rectangle to invalidate.
8039 * @param top The top coordinate of the rectangle to invalidate.
8040 * @param right The right coordinate of the rectangle to invalidate.
8041 * @param bottom The bottom coordinate of the rectangle to invalidate.
8042 *
8043 * @see #invalidate(int, int, int, int)
8044 * @see #invalidate(Rect)
8045 */
8046 public void postInvalidate(int left, int top, int right, int bottom) {
8047 postInvalidateDelayed(0, left, top, right, bottom);
8048 }
8049
8050 /**
8051 * Cause an invalidate to happen on a subsequent cycle through the event
8052 * loop. Waits for the specified amount of time.
8053 *
8054 * @param delayMilliseconds the duration in milliseconds to delay the
8055 * invalidation by
8056 */
8057 public void postInvalidateDelayed(long delayMilliseconds) {
8058 // We try only with the AttachInfo because there's no point in invalidating
8059 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008060 AttachInfo attachInfo = mAttachInfo;
8061 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008062 Message msg = Message.obtain();
8063 msg.what = AttachInfo.INVALIDATE_MSG;
8064 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008065 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008066 }
8067 }
8068
8069 /**
8070 * Cause an invalidate of the specified area to happen on a subsequent cycle
8071 * through the event loop. Waits for the specified amount of time.
8072 *
8073 * @param delayMilliseconds the duration in milliseconds to delay the
8074 * invalidation by
8075 * @param left The left coordinate of the rectangle to invalidate.
8076 * @param top The top coordinate of the rectangle to invalidate.
8077 * @param right The right coordinate of the rectangle to invalidate.
8078 * @param bottom The bottom coordinate of the rectangle to invalidate.
8079 */
8080 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8081 int right, int bottom) {
8082
8083 // We try only with the AttachInfo because there's no point in invalidating
8084 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008085 AttachInfo attachInfo = mAttachInfo;
8086 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008087 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8088 info.target = this;
8089 info.left = left;
8090 info.top = top;
8091 info.right = right;
8092 info.bottom = bottom;
8093
8094 final Message msg = Message.obtain();
8095 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8096 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008097 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008098 }
8099 }
8100
8101 /**
8102 * Called by a parent to request that a child update its values for mScrollX
8103 * and mScrollY if necessary. This will typically be done if the child is
8104 * animating a scroll using a {@link android.widget.Scroller Scroller}
8105 * object.
8106 */
8107 public void computeScroll() {
8108 }
8109
8110 /**
8111 * <p>Indicate whether the horizontal edges are faded when the view is
8112 * scrolled horizontally.</p>
8113 *
8114 * @return true if the horizontal edges should are faded on scroll, false
8115 * otherwise
8116 *
8117 * @see #setHorizontalFadingEdgeEnabled(boolean)
8118 * @attr ref android.R.styleable#View_fadingEdge
8119 */
8120 public boolean isHorizontalFadingEdgeEnabled() {
8121 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8122 }
8123
8124 /**
8125 * <p>Define whether the horizontal edges should be faded when this view
8126 * is scrolled horizontally.</p>
8127 *
8128 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8129 * be faded when the view is scrolled
8130 * horizontally
8131 *
8132 * @see #isHorizontalFadingEdgeEnabled()
8133 * @attr ref android.R.styleable#View_fadingEdge
8134 */
8135 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8136 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8137 if (horizontalFadingEdgeEnabled) {
8138 initScrollCache();
8139 }
8140
8141 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8142 }
8143 }
8144
8145 /**
8146 * <p>Indicate whether the vertical edges are faded when the view is
8147 * scrolled horizontally.</p>
8148 *
8149 * @return true if the vertical edges should are faded on scroll, false
8150 * otherwise
8151 *
8152 * @see #setVerticalFadingEdgeEnabled(boolean)
8153 * @attr ref android.R.styleable#View_fadingEdge
8154 */
8155 public boolean isVerticalFadingEdgeEnabled() {
8156 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8157 }
8158
8159 /**
8160 * <p>Define whether the vertical edges should be faded when this view
8161 * is scrolled vertically.</p>
8162 *
8163 * @param verticalFadingEdgeEnabled true if the vertical edges should
8164 * be faded when the view is scrolled
8165 * vertically
8166 *
8167 * @see #isVerticalFadingEdgeEnabled()
8168 * @attr ref android.R.styleable#View_fadingEdge
8169 */
8170 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8171 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8172 if (verticalFadingEdgeEnabled) {
8173 initScrollCache();
8174 }
8175
8176 mViewFlags ^= FADING_EDGE_VERTICAL;
8177 }
8178 }
8179
8180 /**
8181 * Returns the strength, or intensity, of the top faded edge. The strength is
8182 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8183 * returns 0.0 or 1.0 but no value in between.
8184 *
8185 * Subclasses should override this method to provide a smoother fade transition
8186 * when scrolling occurs.
8187 *
8188 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8189 */
8190 protected float getTopFadingEdgeStrength() {
8191 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8192 }
8193
8194 /**
8195 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8196 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8197 * returns 0.0 or 1.0 but no value in between.
8198 *
8199 * Subclasses should override this method to provide a smoother fade transition
8200 * when scrolling occurs.
8201 *
8202 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8203 */
8204 protected float getBottomFadingEdgeStrength() {
8205 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8206 computeVerticalScrollRange() ? 1.0f : 0.0f;
8207 }
8208
8209 /**
8210 * Returns the strength, or intensity, of the left faded edge. The strength is
8211 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8212 * returns 0.0 or 1.0 but no value in between.
8213 *
8214 * Subclasses should override this method to provide a smoother fade transition
8215 * when scrolling occurs.
8216 *
8217 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8218 */
8219 protected float getLeftFadingEdgeStrength() {
8220 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8221 }
8222
8223 /**
8224 * Returns the strength, or intensity, of the right faded edge. The strength is
8225 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8226 * returns 0.0 or 1.0 but no value in between.
8227 *
8228 * Subclasses should override this method to provide a smoother fade transition
8229 * when scrolling occurs.
8230 *
8231 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8232 */
8233 protected float getRightFadingEdgeStrength() {
8234 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8235 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8236 }
8237
8238 /**
8239 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8240 * scrollbar is not drawn by default.</p>
8241 *
8242 * @return true if the horizontal scrollbar should be painted, false
8243 * otherwise
8244 *
8245 * @see #setHorizontalScrollBarEnabled(boolean)
8246 */
8247 public boolean isHorizontalScrollBarEnabled() {
8248 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8249 }
8250
8251 /**
8252 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8253 * scrollbar is not drawn by default.</p>
8254 *
8255 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8256 * be painted
8257 *
8258 * @see #isHorizontalScrollBarEnabled()
8259 */
8260 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8261 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8262 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008263 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008264 recomputePadding();
8265 }
8266 }
8267
8268 /**
8269 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
8270 * scrollbar is not drawn by default.</p>
8271 *
8272 * @return true if the vertical scrollbar should be painted, false
8273 * otherwise
8274 *
8275 * @see #setVerticalScrollBarEnabled(boolean)
8276 */
8277 public boolean isVerticalScrollBarEnabled() {
8278 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
8279 }
8280
8281 /**
8282 * <p>Define whether the vertical scrollbar should be drawn or not. The
8283 * scrollbar is not drawn by default.</p>
8284 *
8285 * @param verticalScrollBarEnabled true if the vertical scrollbar should
8286 * be painted
8287 *
8288 * @see #isVerticalScrollBarEnabled()
8289 */
8290 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
8291 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
8292 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008293 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008294 recomputePadding();
8295 }
8296 }
8297
Adam Powell20232d02010-12-08 21:08:53 -08008298 /**
8299 * @hide
8300 */
8301 protected void recomputePadding() {
8302 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008303 }
Joe Malin32736f02011-01-19 16:14:20 -08008304
Mike Cleronfe81d382009-09-28 14:22:16 -07008305 /**
8306 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08008307 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008308 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08008309 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008310 */
8311 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
8312 initScrollCache();
8313 final ScrollabilityCache scrollabilityCache = mScrollCache;
8314 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008315 if (fadeScrollbars) {
8316 scrollabilityCache.state = ScrollabilityCache.OFF;
8317 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07008318 scrollabilityCache.state = ScrollabilityCache.ON;
8319 }
8320 }
Joe Malin32736f02011-01-19 16:14:20 -08008321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008322 /**
Joe Malin32736f02011-01-19 16:14:20 -08008323 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008324 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08008325 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008326 * @return true if scrollbar fading is enabled
8327 */
8328 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08008329 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008330 }
Joe Malin32736f02011-01-19 16:14:20 -08008331
Mike Cleron52f0a642009-09-28 18:21:37 -07008332 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008333 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
8334 * inset. When inset, they add to the padding of the view. And the scrollbars
8335 * can be drawn inside the padding area or on the edge of the view. For example,
8336 * if a view has a background drawable and you want to draw the scrollbars
8337 * inside the padding specified by the drawable, you can use
8338 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
8339 * appear at the edge of the view, ignoring the padding, then you can use
8340 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
8341 * @param style the style of the scrollbars. Should be one of
8342 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
8343 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
8344 * @see #SCROLLBARS_INSIDE_OVERLAY
8345 * @see #SCROLLBARS_INSIDE_INSET
8346 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8347 * @see #SCROLLBARS_OUTSIDE_INSET
8348 */
8349 public void setScrollBarStyle(int style) {
8350 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
8351 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07008352 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008353 recomputePadding();
8354 }
8355 }
8356
8357 /**
8358 * <p>Returns the current scrollbar style.</p>
8359 * @return the current scrollbar style
8360 * @see #SCROLLBARS_INSIDE_OVERLAY
8361 * @see #SCROLLBARS_INSIDE_INSET
8362 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8363 * @see #SCROLLBARS_OUTSIDE_INSET
8364 */
8365 public int getScrollBarStyle() {
8366 return mViewFlags & SCROLLBARS_STYLE_MASK;
8367 }
8368
8369 /**
8370 * <p>Compute the horizontal range that the horizontal scrollbar
8371 * represents.</p>
8372 *
8373 * <p>The range is expressed in arbitrary units that must be the same as the
8374 * units used by {@link #computeHorizontalScrollExtent()} and
8375 * {@link #computeHorizontalScrollOffset()}.</p>
8376 *
8377 * <p>The default range is the drawing width of this view.</p>
8378 *
8379 * @return the total horizontal range represented by the horizontal
8380 * scrollbar
8381 *
8382 * @see #computeHorizontalScrollExtent()
8383 * @see #computeHorizontalScrollOffset()
8384 * @see android.widget.ScrollBarDrawable
8385 */
8386 protected int computeHorizontalScrollRange() {
8387 return getWidth();
8388 }
8389
8390 /**
8391 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
8392 * within the horizontal range. This value is used to compute the position
8393 * of the thumb within the scrollbar's track.</p>
8394 *
8395 * <p>The range is expressed in arbitrary units that must be the same as the
8396 * units used by {@link #computeHorizontalScrollRange()} and
8397 * {@link #computeHorizontalScrollExtent()}.</p>
8398 *
8399 * <p>The default offset is the scroll offset of this view.</p>
8400 *
8401 * @return the horizontal offset of the scrollbar's thumb
8402 *
8403 * @see #computeHorizontalScrollRange()
8404 * @see #computeHorizontalScrollExtent()
8405 * @see android.widget.ScrollBarDrawable
8406 */
8407 protected int computeHorizontalScrollOffset() {
8408 return mScrollX;
8409 }
8410
8411 /**
8412 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
8413 * within the horizontal range. This value is used to compute the length
8414 * of the thumb within the scrollbar's track.</p>
8415 *
8416 * <p>The range is expressed in arbitrary units that must be the same as the
8417 * units used by {@link #computeHorizontalScrollRange()} and
8418 * {@link #computeHorizontalScrollOffset()}.</p>
8419 *
8420 * <p>The default extent is the drawing width of this view.</p>
8421 *
8422 * @return the horizontal extent of the scrollbar's thumb
8423 *
8424 * @see #computeHorizontalScrollRange()
8425 * @see #computeHorizontalScrollOffset()
8426 * @see android.widget.ScrollBarDrawable
8427 */
8428 protected int computeHorizontalScrollExtent() {
8429 return getWidth();
8430 }
8431
8432 /**
8433 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
8434 *
8435 * <p>The range is expressed in arbitrary units that must be the same as the
8436 * units used by {@link #computeVerticalScrollExtent()} and
8437 * {@link #computeVerticalScrollOffset()}.</p>
8438 *
8439 * @return the total vertical range represented by the vertical scrollbar
8440 *
8441 * <p>The default range is the drawing height of this view.</p>
8442 *
8443 * @see #computeVerticalScrollExtent()
8444 * @see #computeVerticalScrollOffset()
8445 * @see android.widget.ScrollBarDrawable
8446 */
8447 protected int computeVerticalScrollRange() {
8448 return getHeight();
8449 }
8450
8451 /**
8452 * <p>Compute the vertical offset of the vertical scrollbar's thumb
8453 * within the horizontal range. This value is used to compute the position
8454 * of the thumb within the scrollbar's track.</p>
8455 *
8456 * <p>The range is expressed in arbitrary units that must be the same as the
8457 * units used by {@link #computeVerticalScrollRange()} and
8458 * {@link #computeVerticalScrollExtent()}.</p>
8459 *
8460 * <p>The default offset is the scroll offset of this view.</p>
8461 *
8462 * @return the vertical offset of the scrollbar's thumb
8463 *
8464 * @see #computeVerticalScrollRange()
8465 * @see #computeVerticalScrollExtent()
8466 * @see android.widget.ScrollBarDrawable
8467 */
8468 protected int computeVerticalScrollOffset() {
8469 return mScrollY;
8470 }
8471
8472 /**
8473 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
8474 * within the vertical range. This value is used to compute the length
8475 * of the thumb within the scrollbar's track.</p>
8476 *
8477 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08008478 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008479 * {@link #computeVerticalScrollOffset()}.</p>
8480 *
8481 * <p>The default extent is the drawing height of this view.</p>
8482 *
8483 * @return the vertical extent of the scrollbar's thumb
8484 *
8485 * @see #computeVerticalScrollRange()
8486 * @see #computeVerticalScrollOffset()
8487 * @see android.widget.ScrollBarDrawable
8488 */
8489 protected int computeVerticalScrollExtent() {
8490 return getHeight();
8491 }
8492
8493 /**
Adam Powell69159442011-06-13 17:53:06 -07008494 * Check if this view can be scrolled horizontally in a certain direction.
8495 *
8496 * @param direction Negative to check scrolling left, positive to check scrolling right.
8497 * @return true if this view can be scrolled in the specified direction, false otherwise.
8498 */
8499 public boolean canScrollHorizontally(int direction) {
8500 final int offset = computeHorizontalScrollOffset();
8501 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
8502 if (range == 0) return false;
8503 if (direction < 0) {
8504 return offset > 0;
8505 } else {
8506 return offset < range - 1;
8507 }
8508 }
8509
8510 /**
8511 * Check if this view can be scrolled vertically in a certain direction.
8512 *
8513 * @param direction Negative to check scrolling up, positive to check scrolling down.
8514 * @return true if this view can be scrolled in the specified direction, false otherwise.
8515 */
8516 public boolean canScrollVertically(int direction) {
8517 final int offset = computeVerticalScrollOffset();
8518 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
8519 if (range == 0) return false;
8520 if (direction < 0) {
8521 return offset > 0;
8522 } else {
8523 return offset < range - 1;
8524 }
8525 }
8526
8527 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008528 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8529 * scrollbars are painted only if they have been awakened first.</p>
8530 *
8531 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008532 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008533 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008534 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008535 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008536 // scrollbars are drawn only when the animation is running
8537 final ScrollabilityCache cache = mScrollCache;
8538 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008539
Mike Cleronf116bf82009-09-27 19:14:12 -07008540 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008541
Mike Cleronf116bf82009-09-27 19:14:12 -07008542 if (state == ScrollabilityCache.OFF) {
8543 return;
8544 }
Joe Malin32736f02011-01-19 16:14:20 -08008545
Mike Cleronf116bf82009-09-27 19:14:12 -07008546 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008547
Mike Cleronf116bf82009-09-27 19:14:12 -07008548 if (state == ScrollabilityCache.FADING) {
8549 // We're fading -- get our fade interpolation
8550 if (cache.interpolatorValues == null) {
8551 cache.interpolatorValues = new float[1];
8552 }
Joe Malin32736f02011-01-19 16:14:20 -08008553
Mike Cleronf116bf82009-09-27 19:14:12 -07008554 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008555
Mike Cleronf116bf82009-09-27 19:14:12 -07008556 // Stops the animation if we're done
8557 if (cache.scrollBarInterpolator.timeToValues(values) ==
8558 Interpolator.Result.FREEZE_END) {
8559 cache.state = ScrollabilityCache.OFF;
8560 } else {
8561 cache.scrollBar.setAlpha(Math.round(values[0]));
8562 }
Joe Malin32736f02011-01-19 16:14:20 -08008563
8564 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008565 // drawing. We only want this when we're fading so that
8566 // we prevent excessive redraws
8567 invalidate = true;
8568 } else {
8569 // We're just on -- but we may have been fading before so
8570 // reset alpha
8571 cache.scrollBar.setAlpha(255);
8572 }
8573
Joe Malin32736f02011-01-19 16:14:20 -08008574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008575 final int viewFlags = mViewFlags;
8576
8577 final boolean drawHorizontalScrollBar =
8578 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8579 final boolean drawVerticalScrollBar =
8580 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8581 && !isVerticalScrollBarHidden();
8582
8583 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8584 final int width = mRight - mLeft;
8585 final int height = mBottom - mTop;
8586
8587 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008588
Mike Reede8853fc2009-09-04 14:01:48 -04008589 final int scrollX = mScrollX;
8590 final int scrollY = mScrollY;
8591 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8592
Mike Cleronf116bf82009-09-27 19:14:12 -07008593 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008595 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008596 int size = scrollBar.getSize(false);
8597 if (size <= 0) {
8598 size = cache.scrollBarSize;
8599 }
8600
Mike Cleronf116bf82009-09-27 19:14:12 -07008601 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008602 computeHorizontalScrollOffset(),
8603 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008604 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008605 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008606 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008607 left = scrollX + (mPaddingLeft & inside);
8608 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8609 bottom = top + size;
8610 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8611 if (invalidate) {
8612 invalidate(left, top, right, bottom);
8613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008614 }
8615
8616 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008617 int size = scrollBar.getSize(true);
8618 if (size <= 0) {
8619 size = cache.scrollBarSize;
8620 }
8621
Mike Reede8853fc2009-09-04 14:01:48 -04008622 scrollBar.setParameters(computeVerticalScrollRange(),
8623 computeVerticalScrollOffset(),
8624 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008625 switch (mVerticalScrollbarPosition) {
8626 default:
8627 case SCROLLBAR_POSITION_DEFAULT:
8628 case SCROLLBAR_POSITION_RIGHT:
8629 left = scrollX + width - size - (mUserPaddingRight & inside);
8630 break;
8631 case SCROLLBAR_POSITION_LEFT:
8632 left = scrollX + (mUserPaddingLeft & inside);
8633 break;
8634 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008635 top = scrollY + (mPaddingTop & inside);
8636 right = left + size;
8637 bottom = scrollY + height - (mUserPaddingBottom & inside);
8638 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8639 if (invalidate) {
8640 invalidate(left, top, right, bottom);
8641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008642 }
8643 }
8644 }
8645 }
Romain Guy8506ab42009-06-11 17:35:47 -07008646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008647 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008648 * 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 -08008649 * FastScroller is visible.
8650 * @return whether to temporarily hide the vertical scrollbar
8651 * @hide
8652 */
8653 protected boolean isVerticalScrollBarHidden() {
8654 return false;
8655 }
8656
8657 /**
8658 * <p>Draw the horizontal scrollbar if
8659 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
8660 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008661 * @param canvas the canvas on which to draw the scrollbar
8662 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008663 *
8664 * @see #isHorizontalScrollBarEnabled()
8665 * @see #computeHorizontalScrollRange()
8666 * @see #computeHorizontalScrollExtent()
8667 * @see #computeHorizontalScrollOffset()
8668 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008669 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008670 */
Romain Guy8fb95422010-08-17 18:38:51 -07008671 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8672 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008673 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008674 scrollBar.draw(canvas);
8675 }
Mike Reede8853fc2009-09-04 14:01:48 -04008676
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008677 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008678 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8679 * returns true.</p>
8680 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008681 * @param canvas the canvas on which to draw the scrollbar
8682 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008683 *
8684 * @see #isVerticalScrollBarEnabled()
8685 * @see #computeVerticalScrollRange()
8686 * @see #computeVerticalScrollExtent()
8687 * @see #computeVerticalScrollOffset()
8688 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008689 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008690 */
Romain Guy8fb95422010-08-17 18:38:51 -07008691 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
8692 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04008693 scrollBar.setBounds(l, t, r, b);
8694 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008695 }
8696
8697 /**
8698 * Implement this to do your drawing.
8699 *
8700 * @param canvas the canvas on which the background will be drawn
8701 */
8702 protected void onDraw(Canvas canvas) {
8703 }
8704
8705 /*
8706 * Caller is responsible for calling requestLayout if necessary.
8707 * (This allows addViewInLayout to not request a new layout.)
8708 */
8709 void assignParent(ViewParent parent) {
8710 if (mParent == null) {
8711 mParent = parent;
8712 } else if (parent == null) {
8713 mParent = null;
8714 } else {
8715 throw new RuntimeException("view " + this + " being added, but"
8716 + " it already has a parent");
8717 }
8718 }
8719
8720 /**
8721 * This is called when the view is attached to a window. At this point it
8722 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008723 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
8724 * however it may be called any time before the first onDraw -- including
8725 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008726 *
8727 * @see #onDetachedFromWindow()
8728 */
8729 protected void onAttachedToWindow() {
8730 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
8731 mParent.requestTransparentRegion(this);
8732 }
Adam Powell8568c3a2010-04-19 14:26:11 -07008733 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
8734 initialAwakenScrollBars();
8735 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
8736 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08008737 jumpDrawablesToCurrentState();
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008738 resolveLayoutDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07008739 if (isFocused()) {
8740 InputMethodManager imm = InputMethodManager.peekInstance();
8741 imm.focusIn(this);
8742 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008743 }
Cibu Johny86666632010-02-22 13:01:02 -08008744
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008745 /**
8746 * Resolving the layout direction. LTR is set initially.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07008747 * We are supposing here that the parent directionality will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008748 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008749 private void resolveLayoutDirection() {
Cibu Johny86666632010-02-22 13:01:02 -08008750 mPrivateFlags2 &= ~RESOLVED_LAYOUT_RTL;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008751 switch (getLayoutDirection()) {
8752 case LAYOUT_DIRECTION_INHERIT:
Cibu Johny86666632010-02-22 13:01:02 -08008753 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07008754 if (mParent != null &&
8755 mParent instanceof ViewGroup &&
8756 ((ViewGroup) mParent).getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Cibu Johny86666632010-02-22 13:01:02 -08008757 mPrivateFlags2 |= RESOLVED_LAYOUT_RTL;
8758 }
8759 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008760 case LAYOUT_DIRECTION_RTL:
Cibu Johny86666632010-02-22 13:01:02 -08008761 mPrivateFlags2 |= RESOLVED_LAYOUT_RTL;
8762 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07008763 case LAYOUT_DIRECTION_LOCALE:
8764 if(isLayoutDirectionRtl(Locale.getDefault())) {
8765 mPrivateFlags2 |= RESOLVED_LAYOUT_RTL;
8766 }
8767 break;
8768 default:
8769 // Nothing to do, LTR by default
8770 }
8771 }
8772
8773 /**
8774 * Check if a Locale is corresponding to a RTL script.
8775 *
8776 * @param locale Locale to check
8777 * @return true if a Locale is corresponding to a RTL script.
8778 */
8779 private static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07008780 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
8781 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008782 }
8783
8784 /**
8785 * This is called when the view is detached from a window. At this point it
8786 * no longer has a surface for drawing.
8787 *
8788 * @see #onAttachedToWindow()
8789 */
8790 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08008791 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08008792
Romain Guya440b002010-02-24 15:57:54 -08008793 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05008794 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08008795 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08008796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008797 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08008798
8799 if (mHardwareLayer != null) {
8800 mHardwareLayer.destroy();
8801 mHardwareLayer = null;
8802 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008803
Chet Haasedaf98e92011-01-10 14:10:36 -08008804 if (mDisplayList != null) {
8805 mDisplayList.invalidate();
8806 }
8807
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008808 if (mAttachInfo != null) {
8809 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
8810 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
8811 }
8812
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08008813 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008814 }
8815
8816 /**
8817 * @return The number of times this view has been attached to a window
8818 */
8819 protected int getWindowAttachCount() {
8820 return mWindowAttachCount;
8821 }
8822
8823 /**
8824 * Retrieve a unique token identifying the window this view is attached to.
8825 * @return Return the window's token for use in
8826 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
8827 */
8828 public IBinder getWindowToken() {
8829 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
8830 }
8831
8832 /**
8833 * Retrieve a unique token identifying the top-level "real" window of
8834 * the window that this view is attached to. That is, this is like
8835 * {@link #getWindowToken}, except if the window this view in is a panel
8836 * window (attached to another containing window), then the token of
8837 * the containing window is returned instead.
8838 *
8839 * @return Returns the associated window token, either
8840 * {@link #getWindowToken()} or the containing window's token.
8841 */
8842 public IBinder getApplicationWindowToken() {
8843 AttachInfo ai = mAttachInfo;
8844 if (ai != null) {
8845 IBinder appWindowToken = ai.mPanelParentWindowToken;
8846 if (appWindowToken == null) {
8847 appWindowToken = ai.mWindowToken;
8848 }
8849 return appWindowToken;
8850 }
8851 return null;
8852 }
8853
8854 /**
8855 * Retrieve private session object this view hierarchy is using to
8856 * communicate with the window manager.
8857 * @return the session object to communicate with the window manager
8858 */
8859 /*package*/ IWindowSession getWindowSession() {
8860 return mAttachInfo != null ? mAttachInfo.mSession : null;
8861 }
8862
8863 /**
8864 * @param info the {@link android.view.View.AttachInfo} to associated with
8865 * this view
8866 */
8867 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
8868 //System.out.println("Attached! " + this);
8869 mAttachInfo = info;
8870 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008871 // We will need to evaluate the drawable state at least once.
8872 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008873 if (mFloatingTreeObserver != null) {
8874 info.mTreeObserver.merge(mFloatingTreeObserver);
8875 mFloatingTreeObserver = null;
8876 }
8877 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
8878 mAttachInfo.mScrollContainers.add(this);
8879 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
8880 }
8881 performCollectViewAttributes(visibility);
8882 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08008883
8884 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8885 mOnAttachStateChangeListeners;
8886 if (listeners != null && listeners.size() > 0) {
8887 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8888 // perform the dispatching. The iterator is a safe guard against listeners that
8889 // could mutate the list by calling the various add/remove methods. This prevents
8890 // the array from being modified while we iterate it.
8891 for (OnAttachStateChangeListener listener : listeners) {
8892 listener.onViewAttachedToWindow(this);
8893 }
8894 }
8895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008896 int vis = info.mWindowVisibility;
8897 if (vis != GONE) {
8898 onWindowVisibilityChanged(vis);
8899 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008900 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
8901 // If nobody has evaluated the drawable state yet, then do it now.
8902 refreshDrawableState();
8903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008904 }
8905
8906 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008907 AttachInfo info = mAttachInfo;
8908 if (info != null) {
8909 int vis = info.mWindowVisibility;
8910 if (vis != GONE) {
8911 onWindowVisibilityChanged(GONE);
8912 }
8913 }
8914
8915 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08008916
Adam Powell4afd62b2011-02-18 15:02:18 -08008917 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8918 mOnAttachStateChangeListeners;
8919 if (listeners != null && listeners.size() > 0) {
8920 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8921 // perform the dispatching. The iterator is a safe guard against listeners that
8922 // could mutate the list by calling the various add/remove methods. This prevents
8923 // the array from being modified while we iterate it.
8924 for (OnAttachStateChangeListener listener : listeners) {
8925 listener.onViewDetachedFromWindow(this);
8926 }
8927 }
8928
Romain Guy01d5edc2011-01-28 11:28:53 -08008929 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008930 mAttachInfo.mScrollContainers.remove(this);
8931 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
8932 }
Romain Guy01d5edc2011-01-28 11:28:53 -08008933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008934 mAttachInfo = null;
8935 }
8936
8937 /**
8938 * Store this view hierarchy's frozen state into the given container.
8939 *
8940 * @param container The SparseArray in which to save the view's state.
8941 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008942 * @see #restoreHierarchyState(android.util.SparseArray)
8943 * @see #dispatchSaveInstanceState(android.util.SparseArray)
8944 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008945 */
8946 public void saveHierarchyState(SparseArray<Parcelable> container) {
8947 dispatchSaveInstanceState(container);
8948 }
8949
8950 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008951 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
8952 * this view and its children. May be overridden to modify how freezing happens to a
8953 * 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 -08008954 *
8955 * @param container The SparseArray in which to save the view's state.
8956 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008957 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
8958 * @see #saveHierarchyState(android.util.SparseArray)
8959 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008960 */
8961 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
8962 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
8963 mPrivateFlags &= ~SAVE_STATE_CALLED;
8964 Parcelable state = onSaveInstanceState();
8965 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8966 throw new IllegalStateException(
8967 "Derived class did not call super.onSaveInstanceState()");
8968 }
8969 if (state != null) {
8970 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
8971 // + ": " + state);
8972 container.put(mID, state);
8973 }
8974 }
8975 }
8976
8977 /**
8978 * Hook allowing a view to generate a representation of its internal state
8979 * that can later be used to create a new instance with that same state.
8980 * This state should only contain information that is not persistent or can
8981 * not be reconstructed later. For example, you will never store your
8982 * current position on screen because that will be computed again when a
8983 * new instance of the view is placed in its view hierarchy.
8984 * <p>
8985 * Some examples of things you may store here: the current cursor position
8986 * in a text view (but usually not the text itself since that is stored in a
8987 * content provider or other persistent storage), the currently selected
8988 * item in a list view.
8989 *
8990 * @return Returns a Parcelable object containing the view's current dynamic
8991 * state, or null if there is nothing interesting to save. The
8992 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008993 * @see #onRestoreInstanceState(android.os.Parcelable)
8994 * @see #saveHierarchyState(android.util.SparseArray)
8995 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008996 * @see #setSaveEnabled(boolean)
8997 */
8998 protected Parcelable onSaveInstanceState() {
8999 mPrivateFlags |= SAVE_STATE_CALLED;
9000 return BaseSavedState.EMPTY_STATE;
9001 }
9002
9003 /**
9004 * Restore this view hierarchy's frozen state from the given container.
9005 *
9006 * @param container The SparseArray which holds previously frozen states.
9007 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009008 * @see #saveHierarchyState(android.util.SparseArray)
9009 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9010 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009011 */
9012 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9013 dispatchRestoreInstanceState(container);
9014 }
9015
9016 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009017 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9018 * state for this view and its children. May be overridden to modify how restoring
9019 * happens to a view's children; for example, some views may want to not store state
9020 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009021 *
9022 * @param container The SparseArray which holds previously saved state.
9023 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009024 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9025 * @see #restoreHierarchyState(android.util.SparseArray)
9026 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009027 */
9028 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9029 if (mID != NO_ID) {
9030 Parcelable state = container.get(mID);
9031 if (state != null) {
9032 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9033 // + ": " + state);
9034 mPrivateFlags &= ~SAVE_STATE_CALLED;
9035 onRestoreInstanceState(state);
9036 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9037 throw new IllegalStateException(
9038 "Derived class did not call super.onRestoreInstanceState()");
9039 }
9040 }
9041 }
9042 }
9043
9044 /**
9045 * Hook allowing a view to re-apply a representation of its internal state that had previously
9046 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9047 * null state.
9048 *
9049 * @param state The frozen state that had previously been returned by
9050 * {@link #onSaveInstanceState}.
9051 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009052 * @see #onSaveInstanceState()
9053 * @see #restoreHierarchyState(android.util.SparseArray)
9054 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009055 */
9056 protected void onRestoreInstanceState(Parcelable state) {
9057 mPrivateFlags |= SAVE_STATE_CALLED;
9058 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009059 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9060 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009061 + "when two views of different type have the same id in the same hierarchy. "
9062 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009063 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009064 }
9065 }
9066
9067 /**
9068 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9069 *
9070 * @return the drawing start time in milliseconds
9071 */
9072 public long getDrawingTime() {
9073 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9074 }
9075
9076 /**
9077 * <p>Enables or disables the duplication of the parent's state into this view. When
9078 * duplication is enabled, this view gets its drawable state from its parent rather
9079 * than from its own internal properties.</p>
9080 *
9081 * <p>Note: in the current implementation, setting this property to true after the
9082 * view was added to a ViewGroup might have no effect at all. This property should
9083 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9084 *
9085 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9086 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009087 *
Gilles Debunnefb817032011-01-13 13:52:49 -08009088 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
9089 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009090 *
9091 * @param enabled True to enable duplication of the parent's drawable state, false
9092 * to disable it.
9093 *
9094 * @see #getDrawableState()
9095 * @see #isDuplicateParentStateEnabled()
9096 */
9097 public void setDuplicateParentStateEnabled(boolean enabled) {
9098 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
9099 }
9100
9101 /**
9102 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
9103 *
9104 * @return True if this view's drawable state is duplicated from the parent,
9105 * false otherwise
9106 *
9107 * @see #getDrawableState()
9108 * @see #setDuplicateParentStateEnabled(boolean)
9109 */
9110 public boolean isDuplicateParentStateEnabled() {
9111 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
9112 }
9113
9114 /**
Romain Guy171c5922011-01-06 10:04:23 -08009115 * <p>Specifies the type of layer backing this view. The layer can be
9116 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
9117 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009118 *
Romain Guy171c5922011-01-06 10:04:23 -08009119 * <p>A layer is associated with an optional {@link android.graphics.Paint}
9120 * instance that controls how the layer is composed on screen. The following
9121 * properties of the paint are taken into account when composing the layer:</p>
9122 * <ul>
9123 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
9124 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
9125 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
9126 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08009127 *
Romain Guy171c5922011-01-06 10:04:23 -08009128 * <p>If this view has an alpha value set to < 1.0 by calling
9129 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
9130 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
9131 * equivalent to setting a hardware layer on this view and providing a paint with
9132 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08009133 *
Romain Guy171c5922011-01-06 10:04:23 -08009134 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
9135 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
9136 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009137 *
Romain Guy171c5922011-01-06 10:04:23 -08009138 * @param layerType The ype of layer to use with this view, must be one of
9139 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9140 * {@link #LAYER_TYPE_HARDWARE}
9141 * @param paint The paint used to compose the layer. This argument is optional
9142 * and can be null. It is ignored when the layer type is
9143 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08009144 *
9145 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08009146 * @see #LAYER_TYPE_NONE
9147 * @see #LAYER_TYPE_SOFTWARE
9148 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08009149 * @see #setAlpha(float)
9150 *
Romain Guy171c5922011-01-06 10:04:23 -08009151 * @attr ref android.R.styleable#View_layerType
9152 */
9153 public void setLayerType(int layerType, Paint paint) {
9154 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08009155 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08009156 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
9157 }
Chet Haasedaf98e92011-01-10 14:10:36 -08009158
Romain Guyd6cd5722011-01-17 14:42:41 -08009159 if (layerType == mLayerType) {
9160 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
9161 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009162 invalidateParentCaches();
9163 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08009164 }
9165 return;
9166 }
Romain Guy171c5922011-01-06 10:04:23 -08009167
9168 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08009169 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -07009170 case LAYER_TYPE_HARDWARE:
9171 if (mHardwareLayer != null) {
9172 mHardwareLayer.destroy();
9173 mHardwareLayer = null;
9174 }
9175 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -08009176 case LAYER_TYPE_SOFTWARE:
9177 if (mDrawingCache != null) {
9178 mDrawingCache.recycle();
9179 mDrawingCache = null;
9180 }
Joe Malin32736f02011-01-19 16:14:20 -08009181
Romain Guy6c319ca2011-01-11 14:29:25 -08009182 if (mUnscaledDrawingCache != null) {
9183 mUnscaledDrawingCache.recycle();
9184 mUnscaledDrawingCache = null;
9185 }
9186 break;
Romain Guy6c319ca2011-01-11 14:29:25 -08009187 default:
9188 break;
Romain Guy171c5922011-01-06 10:04:23 -08009189 }
9190
9191 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08009192 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
9193 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
9194 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08009195
Romain Guy0fd89bf2011-01-26 15:41:30 -08009196 invalidateParentCaches();
9197 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08009198 }
9199
9200 /**
9201 * Indicates what type of layer is currently associated with this view. By default
9202 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
9203 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
9204 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08009205 *
Romain Guy171c5922011-01-06 10:04:23 -08009206 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9207 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08009208 *
9209 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08009210 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08009211 * @see #LAYER_TYPE_NONE
9212 * @see #LAYER_TYPE_SOFTWARE
9213 * @see #LAYER_TYPE_HARDWARE
9214 */
9215 public int getLayerType() {
9216 return mLayerType;
9217 }
Joe Malin32736f02011-01-19 16:14:20 -08009218
Romain Guy6c319ca2011-01-11 14:29:25 -08009219 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08009220 * Forces this view's layer to be created and this view to be rendered
9221 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
9222 * invoking this method will have no effect.
9223 *
9224 * This method can for instance be used to render a view into its layer before
9225 * starting an animation. If this view is complex, rendering into the layer
9226 * before starting the animation will avoid skipping frames.
9227 *
9228 * @throws IllegalStateException If this view is not attached to a window
9229 *
9230 * @see #setLayerType(int, android.graphics.Paint)
9231 */
9232 public void buildLayer() {
9233 if (mLayerType == LAYER_TYPE_NONE) return;
9234
9235 if (mAttachInfo == null) {
9236 throw new IllegalStateException("This view must be attached to a window first");
9237 }
9238
9239 switch (mLayerType) {
9240 case LAYER_TYPE_HARDWARE:
9241 getHardwareLayer();
9242 break;
9243 case LAYER_TYPE_SOFTWARE:
9244 buildDrawingCache(true);
9245 break;
9246 }
9247 }
9248
9249 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08009250 * <p>Returns a hardware layer that can be used to draw this view again
9251 * without executing its draw method.</p>
9252 *
9253 * @return A HardwareLayer ready to render, or null if an error occurred.
9254 */
Romain Guy5e7f7662011-01-24 22:35:56 -08009255 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08009256 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
9257 return null;
9258 }
9259
9260 final int width = mRight - mLeft;
9261 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08009262
Romain Guy6c319ca2011-01-11 14:29:25 -08009263 if (width == 0 || height == 0) {
9264 return null;
9265 }
9266
9267 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
9268 if (mHardwareLayer == null) {
9269 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
9270 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08009271 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009272 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
9273 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08009274 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009275 }
9276
Romain Guy59a12ca2011-06-09 17:48:21 -07009277 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -08009278 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
9279 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009280 try {
9281 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08009282 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08009283 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009284
Chet Haase88172fe2011-03-07 17:36:33 -08009285 final int restoreCount = canvas.save();
9286
Romain Guy6c319ca2011-01-11 14:29:25 -08009287 computeScroll();
9288 canvas.translate(-mScrollX, -mScrollY);
9289
Romain Guy6c319ca2011-01-11 14:29:25 -08009290 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009291
Romain Guy6c319ca2011-01-11 14:29:25 -08009292 // Fast path for layouts with no backgrounds
9293 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9294 mPrivateFlags &= ~DIRTY_MASK;
9295 dispatchDraw(canvas);
9296 } else {
9297 draw(canvas);
9298 }
Joe Malin32736f02011-01-19 16:14:20 -08009299
Chet Haase88172fe2011-03-07 17:36:33 -08009300 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08009301 } finally {
9302 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08009303 mHardwareLayer.end(currentCanvas);
9304 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009305 }
9306 }
9307
9308 return mHardwareLayer;
9309 }
Romain Guy171c5922011-01-06 10:04:23 -08009310
9311 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009312 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
9313 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
9314 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
9315 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
9316 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
9317 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009318 *
Romain Guy171c5922011-01-06 10:04:23 -08009319 * <p>Enabling the drawing cache is similar to
9320 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08009321 * acceleration is turned off. When hardware acceleration is turned on, enabling the
9322 * drawing cache has no effect on rendering because the system uses a different mechanism
9323 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
9324 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
9325 * for information on how to enable software and hardware layers.</p>
9326 *
9327 * <p>This API can be used to manually generate
9328 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
9329 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009330 *
9331 * @param enabled true to enable the drawing cache, false otherwise
9332 *
9333 * @see #isDrawingCacheEnabled()
9334 * @see #getDrawingCache()
9335 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08009336 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009337 */
9338 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009339 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009340 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
9341 }
9342
9343 /**
9344 * <p>Indicates whether the drawing cache is enabled for this view.</p>
9345 *
9346 * @return true if the drawing cache is enabled
9347 *
9348 * @see #setDrawingCacheEnabled(boolean)
9349 * @see #getDrawingCache()
9350 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009351 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009352 public boolean isDrawingCacheEnabled() {
9353 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
9354 }
9355
9356 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08009357 * Debugging utility which recursively outputs the dirty state of a view and its
9358 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08009359 *
Chet Haasedaf98e92011-01-10 14:10:36 -08009360 * @hide
9361 */
Romain Guy676b1732011-02-14 14:45:33 -08009362 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08009363 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
9364 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
9365 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
9366 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
9367 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
9368 if (clear) {
9369 mPrivateFlags &= clearMask;
9370 }
9371 if (this instanceof ViewGroup) {
9372 ViewGroup parent = (ViewGroup) this;
9373 final int count = parent.getChildCount();
9374 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08009375 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08009376 child.outputDirtyFlags(indent + " ", clear, clearMask);
9377 }
9378 }
9379 }
9380
9381 /**
9382 * This method is used by ViewGroup to cause its children to restore or recreate their
9383 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
9384 * to recreate its own display list, which would happen if it went through the normal
9385 * draw/dispatchDraw mechanisms.
9386 *
9387 * @hide
9388 */
9389 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08009390
9391 /**
9392 * A view that is not attached or hardware accelerated cannot create a display list.
9393 * This method checks these conditions and returns the appropriate result.
9394 *
9395 * @return true if view has the ability to create a display list, false otherwise.
9396 *
9397 * @hide
9398 */
9399 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08009400 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08009401 }
Joe Malin32736f02011-01-19 16:14:20 -08009402
Chet Haasedaf98e92011-01-10 14:10:36 -08009403 /**
Romain Guyb051e892010-09-28 19:09:36 -07009404 * <p>Returns a display list that can be used to draw this view again
9405 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009406 *
Romain Guyb051e892010-09-28 19:09:36 -07009407 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08009408 *
9409 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07009410 */
Chet Haasedaf98e92011-01-10 14:10:36 -08009411 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08009412 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07009413 return null;
9414 }
9415
Chet Haasedaf98e92011-01-10 14:10:36 -08009416 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
9417 mDisplayList == null || !mDisplayList.isValid() ||
9418 mRecreateDisplayList)) {
9419 // Don't need to recreate the display list, just need to tell our
9420 // children to restore/recreate theirs
9421 if (mDisplayList != null && mDisplayList.isValid() &&
9422 !mRecreateDisplayList) {
9423 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9424 mPrivateFlags &= ~DIRTY_MASK;
9425 dispatchGetDisplayList();
9426
9427 return mDisplayList;
9428 }
9429
9430 // If we got here, we're recreating it. Mark it as such to ensure that
9431 // we copy in child display lists into ours in drawChild()
9432 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08009433 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009434 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
9435 // If we're creating a new display list, make sure our parent gets invalidated
9436 // since they will need to recreate their display list to account for this
9437 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08009438 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08009439 }
Romain Guyb051e892010-09-28 19:09:36 -07009440
9441 final HardwareCanvas canvas = mDisplayList.start();
9442 try {
9443 int width = mRight - mLeft;
9444 int height = mBottom - mTop;
9445
9446 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08009447 // The dirty rect should always be null for a display list
9448 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07009449
Chet Haase88172fe2011-03-07 17:36:33 -08009450 final int restoreCount = canvas.save();
9451
Chet Haasedaf98e92011-01-10 14:10:36 -08009452 computeScroll();
9453 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009454 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009455
Romain Guyb051e892010-09-28 19:09:36 -07009456 // Fast path for layouts with no backgrounds
9457 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9458 mPrivateFlags &= ~DIRTY_MASK;
9459 dispatchDraw(canvas);
9460 } else {
9461 draw(canvas);
9462 }
Joe Malin32736f02011-01-19 16:14:20 -08009463
Chet Haase88172fe2011-03-07 17:36:33 -08009464 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07009465 } finally {
9466 canvas.onPostDraw();
9467
9468 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07009469 }
Chet Haase77785f92011-01-25 23:22:09 -08009470 } else {
9471 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9472 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07009473 }
9474
9475 return mDisplayList;
9476 }
9477
9478 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009479 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009480 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009481 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009482 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009483 * @see #getDrawingCache(boolean)
9484 */
9485 public Bitmap getDrawingCache() {
9486 return getDrawingCache(false);
9487 }
9488
9489 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009490 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
9491 * is null when caching is disabled. If caching is enabled and the cache is not ready,
9492 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
9493 * draw from the cache when the cache is enabled. To benefit from the cache, you must
9494 * request the drawing cache by calling this method and draw it on screen if the
9495 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009496 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009497 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9498 * this method will create a bitmap of the same size as this view. Because this bitmap
9499 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9500 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9501 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9502 * size than the view. This implies that your application must be able to handle this
9503 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009504 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009505 * @param autoScale Indicates whether the generated bitmap should be scaled based on
9506 * the current density of the screen when the application is in compatibility
9507 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009508 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009509 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009510 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009511 * @see #setDrawingCacheEnabled(boolean)
9512 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07009513 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009514 * @see #destroyDrawingCache()
9515 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009516 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009517 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
9518 return null;
9519 }
9520 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009521 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009522 }
Romain Guy02890fd2010-08-06 17:58:44 -07009523 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009524 }
9525
9526 /**
9527 * <p>Frees the resources used by the drawing cache. If you call
9528 * {@link #buildDrawingCache()} manually without calling
9529 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9530 * should cleanup the cache with this method afterwards.</p>
9531 *
9532 * @see #setDrawingCacheEnabled(boolean)
9533 * @see #buildDrawingCache()
9534 * @see #getDrawingCache()
9535 */
9536 public void destroyDrawingCache() {
9537 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009538 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009539 mDrawingCache = null;
9540 }
Romain Guyfbd8f692009-06-26 14:51:58 -07009541 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009542 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07009543 mUnscaledDrawingCache = null;
9544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009545 }
9546
9547 /**
9548 * Setting a solid background color for the drawing cache's bitmaps will improve
9549 * perfromance and memory usage. Note, though that this should only be used if this
9550 * view will always be drawn on top of a solid color.
9551 *
9552 * @param color The background color to use for the drawing cache's bitmap
9553 *
9554 * @see #setDrawingCacheEnabled(boolean)
9555 * @see #buildDrawingCache()
9556 * @see #getDrawingCache()
9557 */
9558 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08009559 if (color != mDrawingCacheBackgroundColor) {
9560 mDrawingCacheBackgroundColor = color;
9561 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009563 }
9564
9565 /**
9566 * @see #setDrawingCacheBackgroundColor(int)
9567 *
9568 * @return The background color to used for the drawing cache's bitmap
9569 */
9570 public int getDrawingCacheBackgroundColor() {
9571 return mDrawingCacheBackgroundColor;
9572 }
9573
9574 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009575 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009576 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009577 * @see #buildDrawingCache(boolean)
9578 */
9579 public void buildDrawingCache() {
9580 buildDrawingCache(false);
9581 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08009582
Romain Guyfbd8f692009-06-26 14:51:58 -07009583 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009584 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
9585 *
9586 * <p>If you call {@link #buildDrawingCache()} manually without calling
9587 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9588 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009589 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009590 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9591 * this method will create a bitmap of the same size as this view. Because this bitmap
9592 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9593 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9594 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9595 * size than the view. This implies that your application must be able to handle this
9596 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009597 *
Romain Guy0d9275e2010-10-26 14:22:30 -07009598 * <p>You should avoid calling this method when hardware acceleration is enabled. If
9599 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08009600 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07009601 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009602 *
9603 * @see #getDrawingCache()
9604 * @see #destroyDrawingCache()
9605 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009606 public void buildDrawingCache(boolean autoScale) {
9607 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07009608 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009609 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009610
9611 if (ViewDebug.TRACE_HIERARCHY) {
9612 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
9613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009614
Romain Guy8506ab42009-06-11 17:35:47 -07009615 int width = mRight - mLeft;
9616 int height = mBottom - mTop;
9617
9618 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07009619 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07009620
Romain Guye1123222009-06-29 14:24:56 -07009621 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009622 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
9623 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07009624 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009625
9626 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07009627 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08009628 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009629
9630 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07009631 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08009632 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009633 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
9634 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08009635 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009636 return;
9637 }
9638
9639 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07009640 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009641
9642 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009643 Bitmap.Config quality;
9644 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08009645 // Never pick ARGB_4444 because it looks awful
9646 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009647 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
9648 case DRAWING_CACHE_QUALITY_AUTO:
9649 quality = Bitmap.Config.ARGB_8888;
9650 break;
9651 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08009652 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009653 break;
9654 case DRAWING_CACHE_QUALITY_HIGH:
9655 quality = Bitmap.Config.ARGB_8888;
9656 break;
9657 default:
9658 quality = Bitmap.Config.ARGB_8888;
9659 break;
9660 }
9661 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07009662 // Optimization for translucent windows
9663 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08009664 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009665 }
9666
9667 // Try to cleanup memory
9668 if (bitmap != null) bitmap.recycle();
9669
9670 try {
9671 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07009672 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07009673 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07009674 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009675 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07009676 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009677 }
Adam Powell26153a32010-11-08 15:22:27 -08009678 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009679 } catch (OutOfMemoryError e) {
9680 // If there is not enough memory to create the bitmap cache, just
9681 // ignore the issue as bitmap caches are not required to draw the
9682 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07009683 if (autoScale) {
9684 mDrawingCache = null;
9685 } else {
9686 mUnscaledDrawingCache = null;
9687 }
Romain Guy0211a0a2011-02-14 16:34:59 -08009688 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009689 return;
9690 }
9691
9692 clear = drawingCacheBackgroundColor != 0;
9693 }
9694
9695 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009696 if (attachInfo != null) {
9697 canvas = attachInfo.mCanvas;
9698 if (canvas == null) {
9699 canvas = new Canvas();
9700 }
9701 canvas.setBitmap(bitmap);
9702 // Temporarily clobber the cached Canvas in case one of our children
9703 // is also using a drawing cache. Without this, the children would
9704 // steal the canvas by attaching their own bitmap to it and bad, bad
9705 // thing would happen (invisible views, corrupted drawings, etc.)
9706 attachInfo.mCanvas = null;
9707 } else {
9708 // This case should hopefully never or seldom happen
9709 canvas = new Canvas(bitmap);
9710 }
9711
9712 if (clear) {
9713 bitmap.eraseColor(drawingCacheBackgroundColor);
9714 }
9715
9716 computeScroll();
9717 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08009718
Romain Guye1123222009-06-29 14:24:56 -07009719 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009720 final float scale = attachInfo.mApplicationScale;
9721 canvas.scale(scale, scale);
9722 }
Joe Malin32736f02011-01-19 16:14:20 -08009723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009724 canvas.translate(-mScrollX, -mScrollY);
9725
Romain Guy5bcdff42009-05-14 21:27:18 -07009726 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08009727 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
9728 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07009729 mPrivateFlags |= DRAWING_CACHE_VALID;
9730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009731
9732 // Fast path for layouts with no backgrounds
9733 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9734 if (ViewDebug.TRACE_HIERARCHY) {
9735 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9736 }
Romain Guy5bcdff42009-05-14 21:27:18 -07009737 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009738 dispatchDraw(canvas);
9739 } else {
9740 draw(canvas);
9741 }
9742
9743 canvas.restoreToCount(restoreCount);
9744
9745 if (attachInfo != null) {
9746 // Restore the cached Canvas for our siblings
9747 attachInfo.mCanvas = canvas;
9748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009749 }
9750 }
9751
9752 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009753 * Create a snapshot of the view into a bitmap. We should probably make
9754 * some form of this public, but should think about the API.
9755 */
Romain Guy223ff5c2010-03-02 17:07:47 -08009756 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009757 int width = mRight - mLeft;
9758 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009759
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009760 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07009761 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009762 width = (int) ((width * scale) + 0.5f);
9763 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08009764
Romain Guy8c11e312009-09-14 15:15:30 -07009765 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009766 if (bitmap == null) {
9767 throw new OutOfMemoryError();
9768 }
9769
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009770 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08009771
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009772 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009773 if (attachInfo != null) {
9774 canvas = attachInfo.mCanvas;
9775 if (canvas == null) {
9776 canvas = new Canvas();
9777 }
9778 canvas.setBitmap(bitmap);
9779 // Temporarily clobber the cached Canvas in case one of our children
9780 // is also using a drawing cache. Without this, the children would
9781 // steal the canvas by attaching their own bitmap to it and bad, bad
9782 // things would happen (invisible views, corrupted drawings, etc.)
9783 attachInfo.mCanvas = null;
9784 } else {
9785 // This case should hopefully never or seldom happen
9786 canvas = new Canvas(bitmap);
9787 }
9788
Romain Guy5bcdff42009-05-14 21:27:18 -07009789 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009790 bitmap.eraseColor(backgroundColor);
9791 }
9792
9793 computeScroll();
9794 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009795 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009796 canvas.translate(-mScrollX, -mScrollY);
9797
Romain Guy5bcdff42009-05-14 21:27:18 -07009798 // Temporarily remove the dirty mask
9799 int flags = mPrivateFlags;
9800 mPrivateFlags &= ~DIRTY_MASK;
9801
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009802 // Fast path for layouts with no backgrounds
9803 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9804 dispatchDraw(canvas);
9805 } else {
9806 draw(canvas);
9807 }
9808
Romain Guy5bcdff42009-05-14 21:27:18 -07009809 mPrivateFlags = flags;
9810
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009811 canvas.restoreToCount(restoreCount);
9812
9813 if (attachInfo != null) {
9814 // Restore the cached Canvas for our siblings
9815 attachInfo.mCanvas = canvas;
9816 }
Romain Guy8506ab42009-06-11 17:35:47 -07009817
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009818 return bitmap;
9819 }
9820
9821 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009822 * Indicates whether this View is currently in edit mode. A View is usually
9823 * in edit mode when displayed within a developer tool. For instance, if
9824 * this View is being drawn by a visual user interface builder, this method
9825 * should return true.
9826 *
9827 * Subclasses should check the return value of this method to provide
9828 * different behaviors if their normal behavior might interfere with the
9829 * host environment. For instance: the class spawns a thread in its
9830 * constructor, the drawing code relies on device-specific features, etc.
9831 *
9832 * This method is usually checked in the drawing code of custom widgets.
9833 *
9834 * @return True if this View is in edit mode, false otherwise.
9835 */
9836 public boolean isInEditMode() {
9837 return false;
9838 }
9839
9840 /**
9841 * If the View draws content inside its padding and enables fading edges,
9842 * it needs to support padding offsets. Padding offsets are added to the
9843 * fading edges to extend the length of the fade so that it covers pixels
9844 * drawn inside the padding.
9845 *
9846 * Subclasses of this class should override this method if they need
9847 * to draw content inside the padding.
9848 *
9849 * @return True if padding offset must be applied, false otherwise.
9850 *
9851 * @see #getLeftPaddingOffset()
9852 * @see #getRightPaddingOffset()
9853 * @see #getTopPaddingOffset()
9854 * @see #getBottomPaddingOffset()
9855 *
9856 * @since CURRENT
9857 */
9858 protected boolean isPaddingOffsetRequired() {
9859 return false;
9860 }
9861
9862 /**
9863 * Amount by which to extend the left fading region. Called only when
9864 * {@link #isPaddingOffsetRequired()} returns true.
9865 *
9866 * @return The left padding offset in pixels.
9867 *
9868 * @see #isPaddingOffsetRequired()
9869 *
9870 * @since CURRENT
9871 */
9872 protected int getLeftPaddingOffset() {
9873 return 0;
9874 }
9875
9876 /**
9877 * Amount by which to extend the right fading region. Called only when
9878 * {@link #isPaddingOffsetRequired()} returns true.
9879 *
9880 * @return The right padding offset in pixels.
9881 *
9882 * @see #isPaddingOffsetRequired()
9883 *
9884 * @since CURRENT
9885 */
9886 protected int getRightPaddingOffset() {
9887 return 0;
9888 }
9889
9890 /**
9891 * Amount by which to extend the top fading region. Called only when
9892 * {@link #isPaddingOffsetRequired()} returns true.
9893 *
9894 * @return The top padding offset in pixels.
9895 *
9896 * @see #isPaddingOffsetRequired()
9897 *
9898 * @since CURRENT
9899 */
9900 protected int getTopPaddingOffset() {
9901 return 0;
9902 }
9903
9904 /**
9905 * Amount by which to extend the bottom fading region. Called only when
9906 * {@link #isPaddingOffsetRequired()} returns true.
9907 *
9908 * @return The bottom padding offset in pixels.
9909 *
9910 * @see #isPaddingOffsetRequired()
9911 *
9912 * @since CURRENT
9913 */
9914 protected int getBottomPaddingOffset() {
9915 return 0;
9916 }
9917
9918 /**
Romain Guy2bffd262010-09-12 17:40:02 -07009919 * <p>Indicates whether this view is attached to an hardware accelerated
9920 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009921 *
Romain Guy2bffd262010-09-12 17:40:02 -07009922 * <p>Even if this method returns true, it does not mean that every call
9923 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
9924 * accelerated {@link android.graphics.Canvas}. For instance, if this view
9925 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
9926 * window is hardware accelerated,
9927 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
9928 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009929 *
Romain Guy2bffd262010-09-12 17:40:02 -07009930 * @return True if the view is attached to a window and the window is
9931 * hardware accelerated; false in any other case.
9932 */
9933 public boolean isHardwareAccelerated() {
9934 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
9935 }
Joe Malin32736f02011-01-19 16:14:20 -08009936
Romain Guy2bffd262010-09-12 17:40:02 -07009937 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009938 * Manually render this view (and all of its children) to the given Canvas.
9939 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009940 * called. When implementing a view, implement
9941 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
9942 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009943 *
9944 * @param canvas The Canvas to which the View is rendered.
9945 */
9946 public void draw(Canvas canvas) {
9947 if (ViewDebug.TRACE_HIERARCHY) {
9948 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9949 }
9950
Romain Guy5bcdff42009-05-14 21:27:18 -07009951 final int privateFlags = mPrivateFlags;
9952 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
9953 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
9954 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07009955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009956 /*
9957 * Draw traversal performs several drawing steps which must be executed
9958 * in the appropriate order:
9959 *
9960 * 1. Draw the background
9961 * 2. If necessary, save the canvas' layers to prepare for fading
9962 * 3. Draw view's content
9963 * 4. Draw children
9964 * 5. If necessary, draw the fading edges and restore layers
9965 * 6. Draw decorations (scrollbars for instance)
9966 */
9967
9968 // Step 1, draw the background, if needed
9969 int saveCount;
9970
Romain Guy24443ea2009-05-11 11:56:30 -07009971 if (!dirtyOpaque) {
9972 final Drawable background = mBGDrawable;
9973 if (background != null) {
9974 final int scrollX = mScrollX;
9975 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009976
Romain Guy24443ea2009-05-11 11:56:30 -07009977 if (mBackgroundSizeChanged) {
9978 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
9979 mBackgroundSizeChanged = false;
9980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009981
Romain Guy24443ea2009-05-11 11:56:30 -07009982 if ((scrollX | scrollY) == 0) {
9983 background.draw(canvas);
9984 } else {
9985 canvas.translate(scrollX, scrollY);
9986 background.draw(canvas);
9987 canvas.translate(-scrollX, -scrollY);
9988 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009989 }
9990 }
9991
9992 // skip step 2 & 5 if possible (common case)
9993 final int viewFlags = mViewFlags;
9994 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
9995 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
9996 if (!verticalEdges && !horizontalEdges) {
9997 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009998 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009999
10000 // Step 4, draw the children
10001 dispatchDraw(canvas);
10002
10003 // Step 6, draw decorations (scrollbars)
10004 onDrawScrollBars(canvas);
10005
10006 // we're done...
10007 return;
10008 }
10009
10010 /*
10011 * Here we do the full fledged routine...
10012 * (this is an uncommon case where speed matters less,
10013 * this is why we repeat some of the tests that have been
10014 * done above)
10015 */
10016
10017 boolean drawTop = false;
10018 boolean drawBottom = false;
10019 boolean drawLeft = false;
10020 boolean drawRight = false;
10021
10022 float topFadeStrength = 0.0f;
10023 float bottomFadeStrength = 0.0f;
10024 float leftFadeStrength = 0.0f;
10025 float rightFadeStrength = 0.0f;
10026
10027 // Step 2, save the canvas' layers
10028 int paddingLeft = mPaddingLeft;
10029 int paddingTop = mPaddingTop;
10030
10031 final boolean offsetRequired = isPaddingOffsetRequired();
10032 if (offsetRequired) {
10033 paddingLeft += getLeftPaddingOffset();
10034 paddingTop += getTopPaddingOffset();
10035 }
10036
10037 int left = mScrollX + paddingLeft;
10038 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
10039 int top = mScrollY + paddingTop;
10040 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
10041
10042 if (offsetRequired) {
10043 right += getRightPaddingOffset();
10044 bottom += getBottomPaddingOffset();
10045 }
10046
10047 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010048 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
10049 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010050
10051 // clip the fade length if top and bottom fades overlap
10052 // overlapping fades produce odd-looking artifacts
10053 if (verticalEdges && (top + length > bottom - length)) {
10054 length = (bottom - top) / 2;
10055 }
10056
10057 // also clip horizontal fades if necessary
10058 if (horizontalEdges && (left + length > right - length)) {
10059 length = (right - left) / 2;
10060 }
10061
10062 if (verticalEdges) {
10063 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010064 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010065 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010066 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010067 }
10068
10069 if (horizontalEdges) {
10070 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010071 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010072 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010073 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010074 }
10075
10076 saveCount = canvas.getSaveCount();
10077
10078 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070010079 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010080 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
10081
10082 if (drawTop) {
10083 canvas.saveLayer(left, top, right, top + length, null, flags);
10084 }
10085
10086 if (drawBottom) {
10087 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
10088 }
10089
10090 if (drawLeft) {
10091 canvas.saveLayer(left, top, left + length, bottom, null, flags);
10092 }
10093
10094 if (drawRight) {
10095 canvas.saveLayer(right - length, top, right, bottom, null, flags);
10096 }
10097 } else {
10098 scrollabilityCache.setFadeColor(solidColor);
10099 }
10100
10101 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010102 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010103
10104 // Step 4, draw the children
10105 dispatchDraw(canvas);
10106
10107 // Step 5, draw the fade effect and restore layers
10108 final Paint p = scrollabilityCache.paint;
10109 final Matrix matrix = scrollabilityCache.matrix;
10110 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010111
10112 if (drawTop) {
10113 matrix.setScale(1, fadeHeight * topFadeStrength);
10114 matrix.postTranslate(left, top);
10115 fade.setLocalMatrix(matrix);
10116 canvas.drawRect(left, top, right, top + length, p);
10117 }
10118
10119 if (drawBottom) {
10120 matrix.setScale(1, fadeHeight * bottomFadeStrength);
10121 matrix.postRotate(180);
10122 matrix.postTranslate(left, bottom);
10123 fade.setLocalMatrix(matrix);
10124 canvas.drawRect(left, bottom - length, right, bottom, p);
10125 }
10126
10127 if (drawLeft) {
10128 matrix.setScale(1, fadeHeight * leftFadeStrength);
10129 matrix.postRotate(-90);
10130 matrix.postTranslate(left, top);
10131 fade.setLocalMatrix(matrix);
10132 canvas.drawRect(left, top, left + length, bottom, p);
10133 }
10134
10135 if (drawRight) {
10136 matrix.setScale(1, fadeHeight * rightFadeStrength);
10137 matrix.postRotate(90);
10138 matrix.postTranslate(right, top);
10139 fade.setLocalMatrix(matrix);
10140 canvas.drawRect(right - length, top, right, bottom, p);
10141 }
10142
10143 canvas.restoreToCount(saveCount);
10144
10145 // Step 6, draw decorations (scrollbars)
10146 onDrawScrollBars(canvas);
10147 }
10148
10149 /**
10150 * Override this if your view is known to always be drawn on top of a solid color background,
10151 * and needs to draw fading edges. Returning a non-zero color enables the view system to
10152 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
10153 * should be set to 0xFF.
10154 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010155 * @see #setVerticalFadingEdgeEnabled(boolean)
10156 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010157 *
10158 * @return The known solid color background for this view, or 0 if the color may vary
10159 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010160 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010161 public int getSolidColor() {
10162 return 0;
10163 }
10164
10165 /**
10166 * Build a human readable string representation of the specified view flags.
10167 *
10168 * @param flags the view flags to convert to a string
10169 * @return a String representing the supplied flags
10170 */
10171 private static String printFlags(int flags) {
10172 String output = "";
10173 int numFlags = 0;
10174 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
10175 output += "TAKES_FOCUS";
10176 numFlags++;
10177 }
10178
10179 switch (flags & VISIBILITY_MASK) {
10180 case INVISIBLE:
10181 if (numFlags > 0) {
10182 output += " ";
10183 }
10184 output += "INVISIBLE";
10185 // USELESS HERE numFlags++;
10186 break;
10187 case GONE:
10188 if (numFlags > 0) {
10189 output += " ";
10190 }
10191 output += "GONE";
10192 // USELESS HERE numFlags++;
10193 break;
10194 default:
10195 break;
10196 }
10197 return output;
10198 }
10199
10200 /**
10201 * Build a human readable string representation of the specified private
10202 * view flags.
10203 *
10204 * @param privateFlags the private view flags to convert to a string
10205 * @return a String representing the supplied flags
10206 */
10207 private static String printPrivateFlags(int privateFlags) {
10208 String output = "";
10209 int numFlags = 0;
10210
10211 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
10212 output += "WANTS_FOCUS";
10213 numFlags++;
10214 }
10215
10216 if ((privateFlags & FOCUSED) == FOCUSED) {
10217 if (numFlags > 0) {
10218 output += " ";
10219 }
10220 output += "FOCUSED";
10221 numFlags++;
10222 }
10223
10224 if ((privateFlags & SELECTED) == SELECTED) {
10225 if (numFlags > 0) {
10226 output += " ";
10227 }
10228 output += "SELECTED";
10229 numFlags++;
10230 }
10231
10232 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
10233 if (numFlags > 0) {
10234 output += " ";
10235 }
10236 output += "IS_ROOT_NAMESPACE";
10237 numFlags++;
10238 }
10239
10240 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
10241 if (numFlags > 0) {
10242 output += " ";
10243 }
10244 output += "HAS_BOUNDS";
10245 numFlags++;
10246 }
10247
10248 if ((privateFlags & DRAWN) == DRAWN) {
10249 if (numFlags > 0) {
10250 output += " ";
10251 }
10252 output += "DRAWN";
10253 // USELESS HERE numFlags++;
10254 }
10255 return output;
10256 }
10257
10258 /**
10259 * <p>Indicates whether or not this view's layout will be requested during
10260 * the next hierarchy layout pass.</p>
10261 *
10262 * @return true if the layout will be forced during next layout pass
10263 */
10264 public boolean isLayoutRequested() {
10265 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
10266 }
10267
10268 /**
10269 * Assign a size and position to a view and all of its
10270 * descendants
10271 *
10272 * <p>This is the second phase of the layout mechanism.
10273 * (The first is measuring). In this phase, each parent calls
10274 * layout on all of its children to position them.
10275 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080010276 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010277 *
Chet Haase9c087442011-01-12 16:20:16 -080010278 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010279 * Derived classes with children should override
10280 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080010281 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010282 *
10283 * @param l Left position, relative to parent
10284 * @param t Top position, relative to parent
10285 * @param r Right position, relative to parent
10286 * @param b Bottom position, relative to parent
10287 */
Romain Guy5429e1d2010-09-07 12:38:00 -070010288 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080010289 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070010290 int oldL = mLeft;
10291 int oldT = mTop;
10292 int oldB = mBottom;
10293 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010294 boolean changed = setFrame(l, t, r, b);
10295 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
10296 if (ViewDebug.TRACE_HIERARCHY) {
10297 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
10298 }
10299
10300 onLayout(changed, l, t, r, b);
10301 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070010302
10303 if (mOnLayoutChangeListeners != null) {
10304 ArrayList<OnLayoutChangeListener> listenersCopy =
10305 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
10306 int numListeners = listenersCopy.size();
10307 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070010308 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070010309 }
10310 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010311 }
10312 mPrivateFlags &= ~FORCE_LAYOUT;
10313 }
10314
10315 /**
10316 * Called from layout when this view should
10317 * assign a size and position to each of its children.
10318 *
10319 * Derived classes with children should override
10320 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070010321 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010322 * @param changed This is a new size or position for this view
10323 * @param left Left position, relative to parent
10324 * @param top Top position, relative to parent
10325 * @param right Right position, relative to parent
10326 * @param bottom Bottom position, relative to parent
10327 */
10328 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
10329 }
10330
10331 /**
10332 * Assign a size and position to this view.
10333 *
10334 * This is called from layout.
10335 *
10336 * @param left Left position, relative to parent
10337 * @param top Top position, relative to parent
10338 * @param right Right position, relative to parent
10339 * @param bottom Bottom position, relative to parent
10340 * @return true if the new size and position are different than the
10341 * previous ones
10342 * {@hide}
10343 */
10344 protected boolean setFrame(int left, int top, int right, int bottom) {
10345 boolean changed = false;
10346
10347 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070010348 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010349 + right + "," + bottom + ")");
10350 }
10351
10352 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
10353 changed = true;
10354
10355 // Remember our drawn bit
10356 int drawn = mPrivateFlags & DRAWN;
10357
10358 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -080010359 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010360
10361
10362 int oldWidth = mRight - mLeft;
10363 int oldHeight = mBottom - mTop;
10364
10365 mLeft = left;
10366 mTop = top;
10367 mRight = right;
10368 mBottom = bottom;
10369
10370 mPrivateFlags |= HAS_BOUNDS;
10371
10372 int newWidth = right - left;
10373 int newHeight = bottom - top;
10374
10375 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010376 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
10377 // A change in dimension means an auto-centered pivot point changes, too
10378 mMatrixDirty = true;
10379 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010380 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
10381 }
10382
10383 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
10384 // If we are visible, force the DRAWN bit to on so that
10385 // this invalidate will go through (at least to our parent).
10386 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010387 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010388 // the DRAWN bit.
10389 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010390 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -080010391 // parent display list may need to be recreated based on a change in the bounds
10392 // of any child
10393 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010394 }
10395
10396 // Reset drawn bit to original value (invalidate turns it off)
10397 mPrivateFlags |= drawn;
10398
10399 mBackgroundSizeChanged = true;
10400 }
10401 return changed;
10402 }
10403
10404 /**
10405 * Finalize inflating a view from XML. This is called as the last phase
10406 * of inflation, after all child views have been added.
10407 *
10408 * <p>Even if the subclass overrides onFinishInflate, they should always be
10409 * sure to call the super method, so that we get called.
10410 */
10411 protected void onFinishInflate() {
10412 }
10413
10414 /**
10415 * Returns the resources associated with this view.
10416 *
10417 * @return Resources object.
10418 */
10419 public Resources getResources() {
10420 return mResources;
10421 }
10422
10423 /**
10424 * Invalidates the specified Drawable.
10425 *
10426 * @param drawable the drawable to invalidate
10427 */
10428 public void invalidateDrawable(Drawable drawable) {
10429 if (verifyDrawable(drawable)) {
10430 final Rect dirty = drawable.getBounds();
10431 final int scrollX = mScrollX;
10432 final int scrollY = mScrollY;
10433
10434 invalidate(dirty.left + scrollX, dirty.top + scrollY,
10435 dirty.right + scrollX, dirty.bottom + scrollY);
10436 }
10437 }
10438
10439 /**
10440 * Schedules an action on a drawable to occur at a specified time.
10441 *
10442 * @param who the recipient of the action
10443 * @param what the action to run on the drawable
10444 * @param when the time at which the action must occur. Uses the
10445 * {@link SystemClock#uptimeMillis} timebase.
10446 */
10447 public void scheduleDrawable(Drawable who, Runnable what, long when) {
10448 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10449 mAttachInfo.mHandler.postAtTime(what, who, when);
10450 }
10451 }
10452
10453 /**
10454 * Cancels a scheduled action on a drawable.
10455 *
10456 * @param who the recipient of the action
10457 * @param what the action to cancel
10458 */
10459 public void unscheduleDrawable(Drawable who, Runnable what) {
10460 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10461 mAttachInfo.mHandler.removeCallbacks(what, who);
10462 }
10463 }
10464
10465 /**
10466 * Unschedule any events associated with the given Drawable. This can be
10467 * used when selecting a new Drawable into a view, so that the previous
10468 * one is completely unscheduled.
10469 *
10470 * @param who The Drawable to unschedule.
10471 *
10472 * @see #drawableStateChanged
10473 */
10474 public void unscheduleDrawable(Drawable who) {
10475 if (mAttachInfo != null) {
10476 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
10477 }
10478 }
10479
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070010480 /**
10481 * Return the layout direction of a given Drawable.
10482 *
10483 * @param who the Drawable to query
10484 *
10485 * @hide
10486 */
10487 public int getResolvedLayoutDirection(Drawable who) {
10488 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070010489 }
10490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010491 /**
10492 * If your view subclass is displaying its own Drawable objects, it should
10493 * override this function and return true for any Drawable it is
10494 * displaying. This allows animations for those drawables to be
10495 * scheduled.
10496 *
10497 * <p>Be sure to call through to the super class when overriding this
10498 * function.
10499 *
10500 * @param who The Drawable to verify. Return true if it is one you are
10501 * displaying, else return the result of calling through to the
10502 * super class.
10503 *
10504 * @return boolean If true than the Drawable is being displayed in the
10505 * view; else false and it is not allowed to animate.
10506 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010507 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
10508 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010509 */
10510 protected boolean verifyDrawable(Drawable who) {
10511 return who == mBGDrawable;
10512 }
10513
10514 /**
10515 * This function is called whenever the state of the view changes in such
10516 * a way that it impacts the state of drawables being shown.
10517 *
10518 * <p>Be sure to call through to the superclass when overriding this
10519 * function.
10520 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010521 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010522 */
10523 protected void drawableStateChanged() {
10524 Drawable d = mBGDrawable;
10525 if (d != null && d.isStateful()) {
10526 d.setState(getDrawableState());
10527 }
10528 }
10529
10530 /**
10531 * Call this to force a view to update its drawable state. This will cause
10532 * drawableStateChanged to be called on this view. Views that are interested
10533 * in the new state should call getDrawableState.
10534 *
10535 * @see #drawableStateChanged
10536 * @see #getDrawableState
10537 */
10538 public void refreshDrawableState() {
10539 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
10540 drawableStateChanged();
10541
10542 ViewParent parent = mParent;
10543 if (parent != null) {
10544 parent.childDrawableStateChanged(this);
10545 }
10546 }
10547
10548 /**
10549 * Return an array of resource IDs of the drawable states representing the
10550 * current state of the view.
10551 *
10552 * @return The current drawable state
10553 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010554 * @see Drawable#setState(int[])
10555 * @see #drawableStateChanged()
10556 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010557 */
10558 public final int[] getDrawableState() {
10559 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
10560 return mDrawableState;
10561 } else {
10562 mDrawableState = onCreateDrawableState(0);
10563 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
10564 return mDrawableState;
10565 }
10566 }
10567
10568 /**
10569 * Generate the new {@link android.graphics.drawable.Drawable} state for
10570 * this view. This is called by the view
10571 * system when the cached Drawable state is determined to be invalid. To
10572 * retrieve the current state, you should use {@link #getDrawableState}.
10573 *
10574 * @param extraSpace if non-zero, this is the number of extra entries you
10575 * would like in the returned array in which you can place your own
10576 * states.
10577 *
10578 * @return Returns an array holding the current {@link Drawable} state of
10579 * the view.
10580 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010581 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010582 */
10583 protected int[] onCreateDrawableState(int extraSpace) {
10584 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
10585 mParent instanceof View) {
10586 return ((View) mParent).onCreateDrawableState(extraSpace);
10587 }
10588
10589 int[] drawableState;
10590
10591 int privateFlags = mPrivateFlags;
10592
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010593 int viewStateIndex = 0;
10594 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
10595 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
10596 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070010597 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010598 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
10599 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070010600 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
10601 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010602 // This is set if HW acceleration is requested, even if the current
10603 // process doesn't allow it. This is just to allow app preview
10604 // windows to better match their app.
10605 viewStateIndex |= VIEW_STATE_ACCELERATED;
10606 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070010607 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010608
Christopher Tate3d4bf172011-03-28 16:16:46 -070010609 final int privateFlags2 = mPrivateFlags2;
10610 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
10611 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
10612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010613 drawableState = VIEW_STATE_SETS[viewStateIndex];
10614
10615 //noinspection ConstantIfStatement
10616 if (false) {
10617 Log.i("View", "drawableStateIndex=" + viewStateIndex);
10618 Log.i("View", toString()
10619 + " pressed=" + ((privateFlags & PRESSED) != 0)
10620 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
10621 + " fo=" + hasFocus()
10622 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010623 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010624 + ": " + Arrays.toString(drawableState));
10625 }
10626
10627 if (extraSpace == 0) {
10628 return drawableState;
10629 }
10630
10631 final int[] fullState;
10632 if (drawableState != null) {
10633 fullState = new int[drawableState.length + extraSpace];
10634 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
10635 } else {
10636 fullState = new int[extraSpace];
10637 }
10638
10639 return fullState;
10640 }
10641
10642 /**
10643 * Merge your own state values in <var>additionalState</var> into the base
10644 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070010645 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010646 *
10647 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070010648 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010649 * own additional state values.
10650 *
10651 * @param additionalState The additional state values you would like
10652 * added to <var>baseState</var>; this array is not modified.
10653 *
10654 * @return As a convenience, the <var>baseState</var> array you originally
10655 * passed into the function is returned.
10656 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010657 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010658 */
10659 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
10660 final int N = baseState.length;
10661 int i = N - 1;
10662 while (i >= 0 && baseState[i] == 0) {
10663 i--;
10664 }
10665 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
10666 return baseState;
10667 }
10668
10669 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070010670 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
10671 * on all Drawable objects associated with this view.
10672 */
10673 public void jumpDrawablesToCurrentState() {
10674 if (mBGDrawable != null) {
10675 mBGDrawable.jumpToCurrentState();
10676 }
10677 }
10678
10679 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010680 * Sets the background color for this view.
10681 * @param color the color of the background
10682 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010683 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010684 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070010685 if (mBGDrawable instanceof ColorDrawable) {
10686 ((ColorDrawable) mBGDrawable).setColor(color);
10687 } else {
10688 setBackgroundDrawable(new ColorDrawable(color));
10689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010690 }
10691
10692 /**
10693 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070010694 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010695 * @param resid The identifier of the resource.
10696 * @attr ref android.R.styleable#View_background
10697 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010698 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010699 public void setBackgroundResource(int resid) {
10700 if (resid != 0 && resid == mBackgroundResource) {
10701 return;
10702 }
10703
10704 Drawable d= null;
10705 if (resid != 0) {
10706 d = mResources.getDrawable(resid);
10707 }
10708 setBackgroundDrawable(d);
10709
10710 mBackgroundResource = resid;
10711 }
10712
10713 /**
10714 * Set the background to a given Drawable, or remove the background. If the
10715 * background has padding, this View's padding is set to the background's
10716 * padding. However, when a background is removed, this View's padding isn't
10717 * touched. If setting the padding is desired, please use
10718 * {@link #setPadding(int, int, int, int)}.
10719 *
10720 * @param d The Drawable to use as the background, or null to remove the
10721 * background
10722 */
10723 public void setBackgroundDrawable(Drawable d) {
10724 boolean requestLayout = false;
10725
10726 mBackgroundResource = 0;
10727
10728 /*
10729 * Regardless of whether we're setting a new background or not, we want
10730 * to clear the previous drawable.
10731 */
10732 if (mBGDrawable != null) {
10733 mBGDrawable.setCallback(null);
10734 unscheduleDrawable(mBGDrawable);
10735 }
10736
10737 if (d != null) {
10738 Rect padding = sThreadLocal.get();
10739 if (padding == null) {
10740 padding = new Rect();
10741 sThreadLocal.set(padding);
10742 }
10743 if (d.getPadding(padding)) {
10744 setPadding(padding.left, padding.top, padding.right, padding.bottom);
10745 }
10746
10747 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
10748 // if it has a different minimum size, we should layout again
10749 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
10750 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
10751 requestLayout = true;
10752 }
10753
10754 d.setCallback(this);
10755 if (d.isStateful()) {
10756 d.setState(getDrawableState());
10757 }
10758 d.setVisible(getVisibility() == VISIBLE, false);
10759 mBGDrawable = d;
10760
10761 if ((mPrivateFlags & SKIP_DRAW) != 0) {
10762 mPrivateFlags &= ~SKIP_DRAW;
10763 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
10764 requestLayout = true;
10765 }
10766 } else {
10767 /* Remove the background */
10768 mBGDrawable = null;
10769
10770 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
10771 /*
10772 * This view ONLY drew the background before and we're removing
10773 * the background, so now it won't draw anything
10774 * (hence we SKIP_DRAW)
10775 */
10776 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
10777 mPrivateFlags |= SKIP_DRAW;
10778 }
10779
10780 /*
10781 * When the background is set, we try to apply its padding to this
10782 * View. When the background is removed, we don't touch this View's
10783 * padding. This is noted in the Javadocs. Hence, we don't need to
10784 * requestLayout(), the invalidate() below is sufficient.
10785 */
10786
10787 // The old background's minimum size could have affected this
10788 // View's layout, so let's requestLayout
10789 requestLayout = true;
10790 }
10791
Romain Guy8f1344f52009-05-15 16:03:59 -070010792 computeOpaqueFlags();
10793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010794 if (requestLayout) {
10795 requestLayout();
10796 }
10797
10798 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010799 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010800 }
10801
10802 /**
10803 * Gets the background drawable
10804 * @return The drawable used as the background for this view, if any.
10805 */
10806 public Drawable getBackground() {
10807 return mBGDrawable;
10808 }
10809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010810 /**
10811 * Sets the padding. The view may add on the space required to display
10812 * the scrollbars, depending on the style and visibility of the scrollbars.
10813 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
10814 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
10815 * from the values set in this call.
10816 *
10817 * @attr ref android.R.styleable#View_padding
10818 * @attr ref android.R.styleable#View_paddingBottom
10819 * @attr ref android.R.styleable#View_paddingLeft
10820 * @attr ref android.R.styleable#View_paddingRight
10821 * @attr ref android.R.styleable#View_paddingTop
10822 * @param left the left padding in pixels
10823 * @param top the top padding in pixels
10824 * @param right the right padding in pixels
10825 * @param bottom the bottom padding in pixels
10826 */
10827 public void setPadding(int left, int top, int right, int bottom) {
10828 boolean changed = false;
10829
Adam Powell20232d02010-12-08 21:08:53 -080010830 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010831 mUserPaddingRight = right;
10832 mUserPaddingBottom = bottom;
10833
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010834 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070010835
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010836 // Common case is there are no scroll bars.
10837 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010838 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080010839 // TODO Determine what to do with SCROLLBAR_POSITION_DEFAULT based on RTL settings.
10840 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010841 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080010842 switch (mVerticalScrollbarPosition) {
10843 case SCROLLBAR_POSITION_DEFAULT:
10844 case SCROLLBAR_POSITION_RIGHT:
10845 right += offset;
10846 break;
10847 case SCROLLBAR_POSITION_LEFT:
10848 left += offset;
10849 break;
10850 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010851 }
Adam Powell20232d02010-12-08 21:08:53 -080010852 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010853 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
10854 ? 0 : getHorizontalScrollbarHeight();
10855 }
10856 }
Romain Guy8506ab42009-06-11 17:35:47 -070010857
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010858 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010859 changed = true;
10860 mPaddingLeft = left;
10861 }
10862 if (mPaddingTop != top) {
10863 changed = true;
10864 mPaddingTop = top;
10865 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010866 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010867 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010868 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010869 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010870 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010871 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010872 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010873 }
10874
10875 if (changed) {
10876 requestLayout();
10877 }
10878 }
10879
10880 /**
10881 * Returns the top padding of this view.
10882 *
10883 * @return the top padding in pixels
10884 */
10885 public int getPaddingTop() {
10886 return mPaddingTop;
10887 }
10888
10889 /**
10890 * Returns the bottom padding of this view. If there are inset and enabled
10891 * scrollbars, this value may include the space required to display the
10892 * scrollbars as well.
10893 *
10894 * @return the bottom padding in pixels
10895 */
10896 public int getPaddingBottom() {
10897 return mPaddingBottom;
10898 }
10899
10900 /**
10901 * Returns the left padding of this view. If there are inset and enabled
10902 * scrollbars, this value may include the space required to display the
10903 * scrollbars as well.
10904 *
10905 * @return the left padding in pixels
10906 */
10907 public int getPaddingLeft() {
10908 return mPaddingLeft;
10909 }
10910
10911 /**
10912 * Returns the right padding of this view. If there are inset and enabled
10913 * scrollbars, this value may include the space required to display the
10914 * scrollbars as well.
10915 *
10916 * @return the right padding in pixels
10917 */
10918 public int getPaddingRight() {
10919 return mPaddingRight;
10920 }
10921
10922 /**
10923 * Changes the selection state of this view. A view can be selected or not.
10924 * Note that selection is not the same as focus. Views are typically
10925 * selected in the context of an AdapterView like ListView or GridView;
10926 * the selected view is the view that is highlighted.
10927 *
10928 * @param selected true if the view must be selected, false otherwise
10929 */
10930 public void setSelected(boolean selected) {
10931 if (((mPrivateFlags & SELECTED) != 0) != selected) {
10932 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070010933 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080010934 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010935 refreshDrawableState();
10936 dispatchSetSelected(selected);
10937 }
10938 }
10939
10940 /**
10941 * Dispatch setSelected to all of this View's children.
10942 *
10943 * @see #setSelected(boolean)
10944 *
10945 * @param selected The new selected state
10946 */
10947 protected void dispatchSetSelected(boolean selected) {
10948 }
10949
10950 /**
10951 * Indicates the selection state of this view.
10952 *
10953 * @return true if the view is selected, false otherwise
10954 */
10955 @ViewDebug.ExportedProperty
10956 public boolean isSelected() {
10957 return (mPrivateFlags & SELECTED) != 0;
10958 }
10959
10960 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010961 * Changes the activated state of this view. A view can be activated or not.
10962 * Note that activation is not the same as selection. Selection is
10963 * a transient property, representing the view (hierarchy) the user is
10964 * currently interacting with. Activation is a longer-term state that the
10965 * user can move views in and out of. For example, in a list view with
10966 * single or multiple selection enabled, the views in the current selection
10967 * set are activated. (Um, yeah, we are deeply sorry about the terminology
10968 * here.) The activated state is propagated down to children of the view it
10969 * is set on.
10970 *
10971 * @param activated true if the view must be activated, false otherwise
10972 */
10973 public void setActivated(boolean activated) {
10974 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
10975 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010976 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010977 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070010978 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010979 }
10980 }
10981
10982 /**
10983 * Dispatch setActivated to all of this View's children.
10984 *
10985 * @see #setActivated(boolean)
10986 *
10987 * @param activated The new activated state
10988 */
10989 protected void dispatchSetActivated(boolean activated) {
10990 }
10991
10992 /**
10993 * Indicates the activation state of this view.
10994 *
10995 * @return true if the view is activated, false otherwise
10996 */
10997 @ViewDebug.ExportedProperty
10998 public boolean isActivated() {
10999 return (mPrivateFlags & ACTIVATED) != 0;
11000 }
11001
11002 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011003 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
11004 * observer can be used to get notifications when global events, like
11005 * layout, happen.
11006 *
11007 * The returned ViewTreeObserver observer is not guaranteed to remain
11008 * valid for the lifetime of this View. If the caller of this method keeps
11009 * a long-lived reference to ViewTreeObserver, it should always check for
11010 * the return value of {@link ViewTreeObserver#isAlive()}.
11011 *
11012 * @return The ViewTreeObserver for this view's hierarchy.
11013 */
11014 public ViewTreeObserver getViewTreeObserver() {
11015 if (mAttachInfo != null) {
11016 return mAttachInfo.mTreeObserver;
11017 }
11018 if (mFloatingTreeObserver == null) {
11019 mFloatingTreeObserver = new ViewTreeObserver();
11020 }
11021 return mFloatingTreeObserver;
11022 }
11023
11024 /**
11025 * <p>Finds the topmost view in the current view hierarchy.</p>
11026 *
11027 * @return the topmost view containing this view
11028 */
11029 public View getRootView() {
11030 if (mAttachInfo != null) {
11031 final View v = mAttachInfo.mRootView;
11032 if (v != null) {
11033 return v;
11034 }
11035 }
Romain Guy8506ab42009-06-11 17:35:47 -070011036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011037 View parent = this;
11038
11039 while (parent.mParent != null && parent.mParent instanceof View) {
11040 parent = (View) parent.mParent;
11041 }
11042
11043 return parent;
11044 }
11045
11046 /**
11047 * <p>Computes the coordinates of this view on the screen. The argument
11048 * must be an array of two integers. After the method returns, the array
11049 * contains the x and y location in that order.</p>
11050 *
11051 * @param location an array of two integers in which to hold the coordinates
11052 */
11053 public void getLocationOnScreen(int[] location) {
11054 getLocationInWindow(location);
11055
11056 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070011057 if (info != null) {
11058 location[0] += info.mWindowLeft;
11059 location[1] += info.mWindowTop;
11060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011061 }
11062
11063 /**
11064 * <p>Computes the coordinates of this view in its window. The argument
11065 * must be an array of two integers. After the method returns, the array
11066 * contains the x and y location in that order.</p>
11067 *
11068 * @param location an array of two integers in which to hold the coordinates
11069 */
11070 public void getLocationInWindow(int[] location) {
11071 if (location == null || location.length < 2) {
11072 throw new IllegalArgumentException("location must be an array of "
11073 + "two integers");
11074 }
11075
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011076 location[0] = mLeft + (int) (mTranslationX + 0.5f);
11077 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011078
11079 ViewParent viewParent = mParent;
11080 while (viewParent instanceof View) {
11081 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011082 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
11083 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011084 viewParent = view.mParent;
11085 }
Romain Guy8506ab42009-06-11 17:35:47 -070011086
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011087 if (viewParent instanceof ViewAncestor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011088 // *cough*
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011089 final ViewAncestor vr = (ViewAncestor)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011090 location[1] -= vr.mCurScrollY;
11091 }
11092 }
11093
11094 /**
11095 * {@hide}
11096 * @param id the id of the view to be found
11097 * @return the view of the specified id, null if cannot be found
11098 */
11099 protected View findViewTraversal(int id) {
11100 if (id == mID) {
11101 return this;
11102 }
11103 return null;
11104 }
11105
11106 /**
11107 * {@hide}
11108 * @param tag the tag of the view to be found
11109 * @return the view of specified tag, null if cannot be found
11110 */
11111 protected View findViewWithTagTraversal(Object tag) {
11112 if (tag != null && tag.equals(mTag)) {
11113 return this;
11114 }
11115 return null;
11116 }
11117
11118 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011119 * {@hide}
11120 * @param predicate The predicate to evaluate.
11121 * @return The first view that matches the predicate or null.
11122 */
11123 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
11124 if (predicate.apply(this)) {
11125 return this;
11126 }
11127 return null;
11128 }
11129
11130 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011131 * Look for a child view with the given id. If this view has the given
11132 * id, return this view.
11133 *
11134 * @param id The id to search for.
11135 * @return The view that has the given id in the hierarchy or null
11136 */
11137 public final View findViewById(int id) {
11138 if (id < 0) {
11139 return null;
11140 }
11141 return findViewTraversal(id);
11142 }
11143
11144 /**
11145 * Look for a child view with the given tag. If this view has the given
11146 * tag, return this view.
11147 *
11148 * @param tag The tag to search for, using "tag.equals(getTag())".
11149 * @return The View that has the given tag in the hierarchy or null
11150 */
11151 public final View findViewWithTag(Object tag) {
11152 if (tag == null) {
11153 return null;
11154 }
11155 return findViewWithTagTraversal(tag);
11156 }
11157
11158 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011159 * {@hide}
11160 * Look for a child view that matches the specified predicate.
11161 * If this view matches the predicate, return this view.
11162 *
11163 * @param predicate The predicate to evaluate.
11164 * @return The first view that matches the predicate or null.
11165 */
11166 public final View findViewByPredicate(Predicate<View> predicate) {
11167 return findViewByPredicateTraversal(predicate);
11168 }
11169
11170 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011171 * Sets the identifier for this view. The identifier does not have to be
11172 * unique in this view's hierarchy. The identifier should be a positive
11173 * number.
11174 *
11175 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070011176 * @see #getId()
11177 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011178 *
11179 * @param id a number used to identify the view
11180 *
11181 * @attr ref android.R.styleable#View_id
11182 */
11183 public void setId(int id) {
11184 mID = id;
11185 }
11186
11187 /**
11188 * {@hide}
11189 *
11190 * @param isRoot true if the view belongs to the root namespace, false
11191 * otherwise
11192 */
11193 public void setIsRootNamespace(boolean isRoot) {
11194 if (isRoot) {
11195 mPrivateFlags |= IS_ROOT_NAMESPACE;
11196 } else {
11197 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
11198 }
11199 }
11200
11201 /**
11202 * {@hide}
11203 *
11204 * @return true if the view belongs to the root namespace, false otherwise
11205 */
11206 public boolean isRootNamespace() {
11207 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
11208 }
11209
11210 /**
11211 * Returns this view's identifier.
11212 *
11213 * @return a positive integer used to identify the view or {@link #NO_ID}
11214 * if the view has no ID
11215 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011216 * @see #setId(int)
11217 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011218 * @attr ref android.R.styleable#View_id
11219 */
11220 @ViewDebug.CapturedViewProperty
11221 public int getId() {
11222 return mID;
11223 }
11224
11225 /**
11226 * Returns this view's tag.
11227 *
11228 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070011229 *
11230 * @see #setTag(Object)
11231 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011232 */
11233 @ViewDebug.ExportedProperty
11234 public Object getTag() {
11235 return mTag;
11236 }
11237
11238 /**
11239 * Sets the tag associated with this view. A tag can be used to mark
11240 * a view in its hierarchy and does not have to be unique within the
11241 * hierarchy. Tags can also be used to store data within a view without
11242 * resorting to another data structure.
11243 *
11244 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070011245 *
11246 * @see #getTag()
11247 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011248 */
11249 public void setTag(final Object tag) {
11250 mTag = tag;
11251 }
11252
11253 /**
Romain Guyd90a3312009-05-06 14:54:28 -070011254 * Returns the tag associated with this view and the specified key.
11255 *
11256 * @param key The key identifying the tag
11257 *
11258 * @return the Object stored in this view as a tag
11259 *
11260 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070011261 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070011262 */
11263 public Object getTag(int key) {
11264 SparseArray<Object> tags = null;
11265 synchronized (sTagsLock) {
11266 if (sTags != null) {
11267 tags = sTags.get(this);
11268 }
11269 }
11270
11271 if (tags != null) return tags.get(key);
11272 return null;
11273 }
11274
11275 /**
11276 * Sets a tag associated with this view and a key. A tag can be used
11277 * to mark a view in its hierarchy and does not have to be unique within
11278 * the hierarchy. Tags can also be used to store data within a view
11279 * without resorting to another data structure.
11280 *
11281 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070011282 * application to ensure it is unique (see the <a
11283 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
11284 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070011285 * the Android framework or not associated with any package will cause
11286 * an {@link IllegalArgumentException} to be thrown.
11287 *
11288 * @param key The key identifying the tag
11289 * @param tag An Object to tag the view with
11290 *
11291 * @throws IllegalArgumentException If they specified key is not valid
11292 *
11293 * @see #setTag(Object)
11294 * @see #getTag(int)
11295 */
11296 public void setTag(int key, final Object tag) {
11297 // If the package id is 0x00 or 0x01, it's either an undefined package
11298 // or a framework id
11299 if ((key >>> 24) < 2) {
11300 throw new IllegalArgumentException("The key must be an application-specific "
11301 + "resource id.");
11302 }
11303
11304 setTagInternal(this, key, tag);
11305 }
11306
11307 /**
11308 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
11309 * framework id.
11310 *
11311 * @hide
11312 */
11313 public void setTagInternal(int key, Object tag) {
11314 if ((key >>> 24) != 0x1) {
11315 throw new IllegalArgumentException("The key must be a framework-specific "
11316 + "resource id.");
11317 }
11318
Romain Guy8506ab42009-06-11 17:35:47 -070011319 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070011320 }
11321
11322 private static void setTagInternal(View view, int key, Object tag) {
11323 SparseArray<Object> tags = null;
11324 synchronized (sTagsLock) {
11325 if (sTags == null) {
11326 sTags = new WeakHashMap<View, SparseArray<Object>>();
11327 } else {
11328 tags = sTags.get(view);
11329 }
11330 }
11331
11332 if (tags == null) {
11333 tags = new SparseArray<Object>(2);
11334 synchronized (sTagsLock) {
11335 sTags.put(view, tags);
11336 }
11337 }
11338
11339 tags.put(key, tag);
11340 }
11341
11342 /**
Romain Guy13922e02009-05-12 17:56:14 -070011343 * @param consistency The type of consistency. See ViewDebug for more information.
11344 *
11345 * @hide
11346 */
11347 protected boolean dispatchConsistencyCheck(int consistency) {
11348 return onConsistencyCheck(consistency);
11349 }
11350
11351 /**
11352 * Method that subclasses should implement to check their consistency. The type of
11353 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070011354 *
Romain Guy13922e02009-05-12 17:56:14 -070011355 * @param consistency The type of consistency. See ViewDebug for more information.
11356 *
11357 * @throws IllegalStateException if the view is in an inconsistent state.
11358 *
11359 * @hide
11360 */
11361 protected boolean onConsistencyCheck(int consistency) {
11362 boolean result = true;
11363
11364 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
11365 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
11366
11367 if (checkLayout) {
11368 if (getParent() == null) {
11369 result = false;
11370 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11371 "View " + this + " does not have a parent.");
11372 }
11373
11374 if (mAttachInfo == null) {
11375 result = false;
11376 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11377 "View " + this + " is not attached to a window.");
11378 }
11379 }
11380
11381 if (checkDrawing) {
11382 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
11383 // from their draw() method
11384
11385 if ((mPrivateFlags & DRAWN) != DRAWN &&
11386 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
11387 result = false;
11388 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11389 "View " + this + " was invalidated but its drawing cache is valid.");
11390 }
11391 }
11392
11393 return result;
11394 }
11395
11396 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011397 * Prints information about this view in the log output, with the tag
11398 * {@link #VIEW_LOG_TAG}.
11399 *
11400 * @hide
11401 */
11402 public void debug() {
11403 debug(0);
11404 }
11405
11406 /**
11407 * Prints information about this view in the log output, with the tag
11408 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
11409 * indentation defined by the <code>depth</code>.
11410 *
11411 * @param depth the indentation level
11412 *
11413 * @hide
11414 */
11415 protected void debug(int depth) {
11416 String output = debugIndent(depth - 1);
11417
11418 output += "+ " + this;
11419 int id = getId();
11420 if (id != -1) {
11421 output += " (id=" + id + ")";
11422 }
11423 Object tag = getTag();
11424 if (tag != null) {
11425 output += " (tag=" + tag + ")";
11426 }
11427 Log.d(VIEW_LOG_TAG, output);
11428
11429 if ((mPrivateFlags & FOCUSED) != 0) {
11430 output = debugIndent(depth) + " FOCUSED";
11431 Log.d(VIEW_LOG_TAG, output);
11432 }
11433
11434 output = debugIndent(depth);
11435 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
11436 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
11437 + "} ";
11438 Log.d(VIEW_LOG_TAG, output);
11439
11440 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
11441 || mPaddingBottom != 0) {
11442 output = debugIndent(depth);
11443 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
11444 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
11445 Log.d(VIEW_LOG_TAG, output);
11446 }
11447
11448 output = debugIndent(depth);
11449 output += "mMeasureWidth=" + mMeasuredWidth +
11450 " mMeasureHeight=" + mMeasuredHeight;
11451 Log.d(VIEW_LOG_TAG, output);
11452
11453 output = debugIndent(depth);
11454 if (mLayoutParams == null) {
11455 output += "BAD! no layout params";
11456 } else {
11457 output = mLayoutParams.debug(output);
11458 }
11459 Log.d(VIEW_LOG_TAG, output);
11460
11461 output = debugIndent(depth);
11462 output += "flags={";
11463 output += View.printFlags(mViewFlags);
11464 output += "}";
11465 Log.d(VIEW_LOG_TAG, output);
11466
11467 output = debugIndent(depth);
11468 output += "privateFlags={";
11469 output += View.printPrivateFlags(mPrivateFlags);
11470 output += "}";
11471 Log.d(VIEW_LOG_TAG, output);
11472 }
11473
11474 /**
11475 * Creates an string of whitespaces used for indentation.
11476 *
11477 * @param depth the indentation level
11478 * @return a String containing (depth * 2 + 3) * 2 white spaces
11479 *
11480 * @hide
11481 */
11482 protected static String debugIndent(int depth) {
11483 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
11484 for (int i = 0; i < (depth * 2) + 3; i++) {
11485 spaces.append(' ').append(' ');
11486 }
11487 return spaces.toString();
11488 }
11489
11490 /**
11491 * <p>Return the offset of the widget's text baseline from the widget's top
11492 * boundary. If this widget does not support baseline alignment, this
11493 * method returns -1. </p>
11494 *
11495 * @return the offset of the baseline within the widget's bounds or -1
11496 * if baseline alignment is not supported
11497 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011498 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011499 public int getBaseline() {
11500 return -1;
11501 }
11502
11503 /**
11504 * Call this when something has changed which has invalidated the
11505 * layout of this view. This will schedule a layout pass of the view
11506 * tree.
11507 */
11508 public void requestLayout() {
11509 if (ViewDebug.TRACE_HIERARCHY) {
11510 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
11511 }
11512
11513 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011514 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011515
11516 if (mParent != null && !mParent.isLayoutRequested()) {
11517 mParent.requestLayout();
11518 }
11519 }
11520
11521 /**
11522 * Forces this view to be laid out during the next layout pass.
11523 * This method does not call requestLayout() or forceLayout()
11524 * on the parent.
11525 */
11526 public void forceLayout() {
11527 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011528 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011529 }
11530
11531 /**
11532 * <p>
11533 * This is called to find out how big a view should be. The parent
11534 * supplies constraint information in the width and height parameters.
11535 * </p>
11536 *
11537 * <p>
11538 * The actual mesurement work of a view is performed in
11539 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
11540 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
11541 * </p>
11542 *
11543 *
11544 * @param widthMeasureSpec Horizontal space requirements as imposed by the
11545 * parent
11546 * @param heightMeasureSpec Vertical space requirements as imposed by the
11547 * parent
11548 *
11549 * @see #onMeasure(int, int)
11550 */
11551 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
11552 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
11553 widthMeasureSpec != mOldWidthMeasureSpec ||
11554 heightMeasureSpec != mOldHeightMeasureSpec) {
11555
11556 // first clears the measured dimension flag
11557 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
11558
11559 if (ViewDebug.TRACE_HIERARCHY) {
11560 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
11561 }
11562
11563 // measure ourselves, this should set the measured dimension flag back
11564 onMeasure(widthMeasureSpec, heightMeasureSpec);
11565
11566 // flag not set, setMeasuredDimension() was not invoked, we raise
11567 // an exception to warn the developer
11568 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
11569 throw new IllegalStateException("onMeasure() did not set the"
11570 + " measured dimension by calling"
11571 + " setMeasuredDimension()");
11572 }
11573
11574 mPrivateFlags |= LAYOUT_REQUIRED;
11575 }
11576
11577 mOldWidthMeasureSpec = widthMeasureSpec;
11578 mOldHeightMeasureSpec = heightMeasureSpec;
11579 }
11580
11581 /**
11582 * <p>
11583 * Measure the view and its content to determine the measured width and the
11584 * measured height. This method is invoked by {@link #measure(int, int)} and
11585 * should be overriden by subclasses to provide accurate and efficient
11586 * measurement of their contents.
11587 * </p>
11588 *
11589 * <p>
11590 * <strong>CONTRACT:</strong> When overriding this method, you
11591 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
11592 * measured width and height of this view. Failure to do so will trigger an
11593 * <code>IllegalStateException</code>, thrown by
11594 * {@link #measure(int, int)}. Calling the superclass'
11595 * {@link #onMeasure(int, int)} is a valid use.
11596 * </p>
11597 *
11598 * <p>
11599 * The base class implementation of measure defaults to the background size,
11600 * unless a larger size is allowed by the MeasureSpec. Subclasses should
11601 * override {@link #onMeasure(int, int)} to provide better measurements of
11602 * their content.
11603 * </p>
11604 *
11605 * <p>
11606 * If this method is overridden, it is the subclass's responsibility to make
11607 * sure the measured height and width are at least the view's minimum height
11608 * and width ({@link #getSuggestedMinimumHeight()} and
11609 * {@link #getSuggestedMinimumWidth()}).
11610 * </p>
11611 *
11612 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
11613 * The requirements are encoded with
11614 * {@link android.view.View.MeasureSpec}.
11615 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
11616 * The requirements are encoded with
11617 * {@link android.view.View.MeasureSpec}.
11618 *
11619 * @see #getMeasuredWidth()
11620 * @see #getMeasuredHeight()
11621 * @see #setMeasuredDimension(int, int)
11622 * @see #getSuggestedMinimumHeight()
11623 * @see #getSuggestedMinimumWidth()
11624 * @see android.view.View.MeasureSpec#getMode(int)
11625 * @see android.view.View.MeasureSpec#getSize(int)
11626 */
11627 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
11628 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
11629 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
11630 }
11631
11632 /**
11633 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
11634 * measured width and measured height. Failing to do so will trigger an
11635 * exception at measurement time.</p>
11636 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080011637 * @param measuredWidth The measured width of this view. May be a complex
11638 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11639 * {@link #MEASURED_STATE_TOO_SMALL}.
11640 * @param measuredHeight The measured height of this view. May be a complex
11641 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11642 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011643 */
11644 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
11645 mMeasuredWidth = measuredWidth;
11646 mMeasuredHeight = measuredHeight;
11647
11648 mPrivateFlags |= MEASURED_DIMENSION_SET;
11649 }
11650
11651 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080011652 * Merge two states as returned by {@link #getMeasuredState()}.
11653 * @param curState The current state as returned from a view or the result
11654 * of combining multiple views.
11655 * @param newState The new view state to combine.
11656 * @return Returns a new integer reflecting the combination of the two
11657 * states.
11658 */
11659 public static int combineMeasuredStates(int curState, int newState) {
11660 return curState | newState;
11661 }
11662
11663 /**
11664 * Version of {@link #resolveSizeAndState(int, int, int)}
11665 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
11666 */
11667 public static int resolveSize(int size, int measureSpec) {
11668 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
11669 }
11670
11671 /**
11672 * Utility to reconcile a desired size and state, with constraints imposed
11673 * by a MeasureSpec. Will take the desired size, unless a different size
11674 * is imposed by the constraints. The returned value is a compound integer,
11675 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
11676 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
11677 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011678 *
11679 * @param size How big the view wants to be
11680 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080011681 * @return Size information bit mask as defined by
11682 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011683 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080011684 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011685 int result = size;
11686 int specMode = MeasureSpec.getMode(measureSpec);
11687 int specSize = MeasureSpec.getSize(measureSpec);
11688 switch (specMode) {
11689 case MeasureSpec.UNSPECIFIED:
11690 result = size;
11691 break;
11692 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080011693 if (specSize < size) {
11694 result = specSize | MEASURED_STATE_TOO_SMALL;
11695 } else {
11696 result = size;
11697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011698 break;
11699 case MeasureSpec.EXACTLY:
11700 result = specSize;
11701 break;
11702 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080011703 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011704 }
11705
11706 /**
11707 * Utility to return a default size. Uses the supplied size if the
11708 * MeasureSpec imposed no contraints. Will get larger if allowed
11709 * by the MeasureSpec.
11710 *
11711 * @param size Default size for this view
11712 * @param measureSpec Constraints imposed by the parent
11713 * @return The size this view should be.
11714 */
11715 public static int getDefaultSize(int size, int measureSpec) {
11716 int result = size;
11717 int specMode = MeasureSpec.getMode(measureSpec);
11718 int specSize = MeasureSpec.getSize(measureSpec);
11719
11720 switch (specMode) {
11721 case MeasureSpec.UNSPECIFIED:
11722 result = size;
11723 break;
11724 case MeasureSpec.AT_MOST:
11725 case MeasureSpec.EXACTLY:
11726 result = specSize;
11727 break;
11728 }
11729 return result;
11730 }
11731
11732 /**
11733 * Returns the suggested minimum height that the view should use. This
11734 * returns the maximum of the view's minimum height
11735 * and the background's minimum height
11736 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
11737 * <p>
11738 * When being used in {@link #onMeasure(int, int)}, the caller should still
11739 * ensure the returned height is within the requirements of the parent.
11740 *
11741 * @return The suggested minimum height of the view.
11742 */
11743 protected int getSuggestedMinimumHeight() {
11744 int suggestedMinHeight = mMinHeight;
11745
11746 if (mBGDrawable != null) {
11747 final int bgMinHeight = mBGDrawable.getMinimumHeight();
11748 if (suggestedMinHeight < bgMinHeight) {
11749 suggestedMinHeight = bgMinHeight;
11750 }
11751 }
11752
11753 return suggestedMinHeight;
11754 }
11755
11756 /**
11757 * Returns the suggested minimum width that the view should use. This
11758 * returns the maximum of the view's minimum width)
11759 * and the background's minimum width
11760 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
11761 * <p>
11762 * When being used in {@link #onMeasure(int, int)}, the caller should still
11763 * ensure the returned width is within the requirements of the parent.
11764 *
11765 * @return The suggested minimum width of the view.
11766 */
11767 protected int getSuggestedMinimumWidth() {
11768 int suggestedMinWidth = mMinWidth;
11769
11770 if (mBGDrawable != null) {
11771 final int bgMinWidth = mBGDrawable.getMinimumWidth();
11772 if (suggestedMinWidth < bgMinWidth) {
11773 suggestedMinWidth = bgMinWidth;
11774 }
11775 }
11776
11777 return suggestedMinWidth;
11778 }
11779
11780 /**
11781 * Sets the minimum height of the view. It is not guaranteed the view will
11782 * be able to achieve this minimum height (for example, if its parent layout
11783 * constrains it with less available height).
11784 *
11785 * @param minHeight The minimum height the view will try to be.
11786 */
11787 public void setMinimumHeight(int minHeight) {
11788 mMinHeight = minHeight;
11789 }
11790
11791 /**
11792 * Sets the minimum width of the view. It is not guaranteed the view will
11793 * be able to achieve this minimum width (for example, if its parent layout
11794 * constrains it with less available width).
11795 *
11796 * @param minWidth The minimum width the view will try to be.
11797 */
11798 public void setMinimumWidth(int minWidth) {
11799 mMinWidth = minWidth;
11800 }
11801
11802 /**
11803 * Get the animation currently associated with this view.
11804 *
11805 * @return The animation that is currently playing or
11806 * scheduled to play for this view.
11807 */
11808 public Animation getAnimation() {
11809 return mCurrentAnimation;
11810 }
11811
11812 /**
11813 * Start the specified animation now.
11814 *
11815 * @param animation the animation to start now
11816 */
11817 public void startAnimation(Animation animation) {
11818 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
11819 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011820 invalidateParentCaches();
11821 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011822 }
11823
11824 /**
11825 * Cancels any animations for this view.
11826 */
11827 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080011828 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080011829 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080011830 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011831 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011832 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011833 }
11834
11835 /**
11836 * Sets the next animation to play for this view.
11837 * If you want the animation to play immediately, use
11838 * startAnimation. This method provides allows fine-grained
11839 * control over the start time and invalidation, but you
11840 * must make sure that 1) the animation has a start time set, and
11841 * 2) the view will be invalidated when the animation is supposed to
11842 * start.
11843 *
11844 * @param animation The next animation, or null.
11845 */
11846 public void setAnimation(Animation animation) {
11847 mCurrentAnimation = animation;
11848 if (animation != null) {
11849 animation.reset();
11850 }
11851 }
11852
11853 /**
11854 * Invoked by a parent ViewGroup to notify the start of the animation
11855 * currently associated with this view. If you override this method,
11856 * always call super.onAnimationStart();
11857 *
11858 * @see #setAnimation(android.view.animation.Animation)
11859 * @see #getAnimation()
11860 */
11861 protected void onAnimationStart() {
11862 mPrivateFlags |= ANIMATION_STARTED;
11863 }
11864
11865 /**
11866 * Invoked by a parent ViewGroup to notify the end of the animation
11867 * currently associated with this view. If you override this method,
11868 * always call super.onAnimationEnd();
11869 *
11870 * @see #setAnimation(android.view.animation.Animation)
11871 * @see #getAnimation()
11872 */
11873 protected void onAnimationEnd() {
11874 mPrivateFlags &= ~ANIMATION_STARTED;
11875 }
11876
11877 /**
11878 * Invoked if there is a Transform that involves alpha. Subclass that can
11879 * draw themselves with the specified alpha should return true, and then
11880 * respect that alpha when their onDraw() is called. If this returns false
11881 * then the view may be redirected to draw into an offscreen buffer to
11882 * fulfill the request, which will look fine, but may be slower than if the
11883 * subclass handles it internally. The default implementation returns false.
11884 *
11885 * @param alpha The alpha (0..255) to apply to the view's drawing
11886 * @return true if the view can draw with the specified alpha.
11887 */
11888 protected boolean onSetAlpha(int alpha) {
11889 return false;
11890 }
11891
11892 /**
11893 * This is used by the RootView to perform an optimization when
11894 * the view hierarchy contains one or several SurfaceView.
11895 * SurfaceView is always considered transparent, but its children are not,
11896 * therefore all View objects remove themselves from the global transparent
11897 * region (passed as a parameter to this function).
11898 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011899 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011900 *
11901 * @return Returns true if the effective visibility of the view at this
11902 * point is opaque, regardless of the transparent region; returns false
11903 * if it is possible for underlying windows to be seen behind the view.
11904 *
11905 * {@hide}
11906 */
11907 public boolean gatherTransparentRegion(Region region) {
11908 final AttachInfo attachInfo = mAttachInfo;
11909 if (region != null && attachInfo != null) {
11910 final int pflags = mPrivateFlags;
11911 if ((pflags & SKIP_DRAW) == 0) {
11912 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
11913 // remove it from the transparent region.
11914 final int[] location = attachInfo.mTransparentLocation;
11915 getLocationInWindow(location);
11916 region.op(location[0], location[1], location[0] + mRight - mLeft,
11917 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
11918 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
11919 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
11920 // exists, so we remove the background drawable's non-transparent
11921 // parts from this transparent region.
11922 applyDrawableToTransparentRegion(mBGDrawable, region);
11923 }
11924 }
11925 return true;
11926 }
11927
11928 /**
11929 * Play a sound effect for this view.
11930 *
11931 * <p>The framework will play sound effects for some built in actions, such as
11932 * clicking, but you may wish to play these effects in your widget,
11933 * for instance, for internal navigation.
11934 *
11935 * <p>The sound effect will only be played if sound effects are enabled by the user, and
11936 * {@link #isSoundEffectsEnabled()} is true.
11937 *
11938 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
11939 */
11940 public void playSoundEffect(int soundConstant) {
11941 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
11942 return;
11943 }
11944 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
11945 }
11946
11947 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011948 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011949 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011950 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011951 *
11952 * <p>The framework will provide haptic feedback for some built in actions,
11953 * such as long presses, but you may wish to provide feedback for your
11954 * own widget.
11955 *
11956 * <p>The feedback will only be performed if
11957 * {@link #isHapticFeedbackEnabled()} is true.
11958 *
11959 * @param feedbackConstant One of the constants defined in
11960 * {@link HapticFeedbackConstants}
11961 */
11962 public boolean performHapticFeedback(int feedbackConstant) {
11963 return performHapticFeedback(feedbackConstant, 0);
11964 }
11965
11966 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011967 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011968 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011969 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011970 *
11971 * @param feedbackConstant One of the constants defined in
11972 * {@link HapticFeedbackConstants}
11973 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
11974 */
11975 public boolean performHapticFeedback(int feedbackConstant, int flags) {
11976 if (mAttachInfo == null) {
11977 return false;
11978 }
Romain Guyf607bdc2010-09-10 19:20:06 -070011979 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070011980 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011981 && !isHapticFeedbackEnabled()) {
11982 return false;
11983 }
Romain Guy812ccbe2010-06-01 14:07:24 -070011984 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
11985 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011986 }
11987
11988 /**
Joe Onorato664644d2011-01-23 17:53:23 -080011989 * Request that the visibility of the status bar be changed.
Scott Mainec6331b2011-05-24 16:55:56 -070011990 * @param visibility Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080011991 */
11992 public void setSystemUiVisibility(int visibility) {
11993 if (visibility != mSystemUiVisibility) {
11994 mSystemUiVisibility = visibility;
11995 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11996 mParent.recomputeViewAttributes(this);
11997 }
11998 }
11999 }
12000
12001 /**
12002 * Returns the status bar visibility that this view has requested.
Scott Mainec6331b2011-05-24 16:55:56 -070012003 * @return Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080012004 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080012005 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080012006 return mSystemUiVisibility;
12007 }
12008
Scott Mainec6331b2011-05-24 16:55:56 -070012009 /**
12010 * Set a listener to receive callbacks when the visibility of the system bar changes.
12011 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
12012 */
Joe Onorato664644d2011-01-23 17:53:23 -080012013 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
12014 mOnSystemUiVisibilityChangeListener = l;
12015 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12016 mParent.recomputeViewAttributes(this);
12017 }
12018 }
12019
12020 /**
12021 */
12022 public void dispatchSystemUiVisibilityChanged(int visibility) {
12023 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080012024 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080012025 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080012026 }
12027 }
12028
12029 /**
Joe Malin32736f02011-01-19 16:14:20 -080012030 * Creates an image that the system displays during the drag and drop
12031 * operation. This is called a &quot;drag shadow&quot;. The default implementation
12032 * for a DragShadowBuilder based on a View returns an image that has exactly the same
12033 * appearance as the given View. The default also positions the center of the drag shadow
12034 * directly under the touch point. If no View is provided (the constructor with no parameters
12035 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
12036 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
12037 * default is an invisible drag shadow.
12038 * <p>
12039 * You are not required to use the View you provide to the constructor as the basis of the
12040 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
12041 * anything you want as the drag shadow.
12042 * </p>
12043 * <p>
12044 * You pass a DragShadowBuilder object to the system when you start the drag. The system
12045 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
12046 * size and position of the drag shadow. It uses this data to construct a
12047 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
12048 * so that your application can draw the shadow image in the Canvas.
12049 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012050 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012051 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070012052 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070012053
12054 /**
Joe Malin32736f02011-01-19 16:14:20 -080012055 * Constructs a shadow image builder based on a View. By default, the resulting drag
12056 * shadow will have the same appearance and dimensions as the View, with the touch point
12057 * over the center of the View.
12058 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070012059 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012060 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070012061 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070012062 }
12063
Christopher Tate17ed60c2011-01-18 12:50:26 -080012064 /**
12065 * Construct a shadow builder object with no associated View. This
12066 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
12067 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
12068 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080012069 * reference to any View object. If they are not overridden, then the result is an
12070 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080012071 */
12072 public DragShadowBuilder() {
12073 mView = new WeakReference<View>(null);
12074 }
12075
12076 /**
12077 * Returns the View object that had been passed to the
12078 * {@link #View.DragShadowBuilder(View)}
12079 * constructor. If that View parameter was {@code null} or if the
12080 * {@link #View.DragShadowBuilder()}
12081 * constructor was used to instantiate the builder object, this method will return
12082 * null.
12083 *
12084 * @return The View object associate with this builder object.
12085 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070012086 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070012087 final public View getView() {
12088 return mView.get();
12089 }
12090
Christopher Tate2c095f32010-10-04 14:13:40 -070012091 /**
Joe Malin32736f02011-01-19 16:14:20 -080012092 * Provides the metrics for the shadow image. These include the dimensions of
12093 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070012094 * be centered under the touch location while dragging.
12095 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012096 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080012097 * same as the dimensions of the View itself and centers the shadow under
12098 * the touch point.
12099 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012100 *
Joe Malin32736f02011-01-19 16:14:20 -080012101 * @param shadowSize A {@link android.graphics.Point} containing the width and height
12102 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
12103 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
12104 * image.
12105 *
12106 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
12107 * shadow image that should be underneath the touch point during the drag and drop
12108 * operation. Your application must set {@link android.graphics.Point#x} to the
12109 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070012110 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012111 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070012112 final View view = mView.get();
12113 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012114 shadowSize.set(view.getWidth(), view.getHeight());
12115 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070012116 } else {
12117 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
12118 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012119 }
12120
12121 /**
Joe Malin32736f02011-01-19 16:14:20 -080012122 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
12123 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012124 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070012125 *
Joe Malin32736f02011-01-19 16:14:20 -080012126 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070012127 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012128 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070012129 final View view = mView.get();
12130 if (view != null) {
12131 view.draw(canvas);
12132 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012133 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070012134 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012135 }
12136 }
12137
12138 /**
Joe Malin32736f02011-01-19 16:14:20 -080012139 * Starts a drag and drop operation. When your application calls this method, it passes a
12140 * {@link android.view.View.DragShadowBuilder} object to the system. The
12141 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
12142 * to get metrics for the drag shadow, and then calls the object's
12143 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
12144 * <p>
12145 * Once the system has the drag shadow, it begins the drag and drop operation by sending
12146 * drag events to all the View objects in your application that are currently visible. It does
12147 * this either by calling the View object's drag listener (an implementation of
12148 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
12149 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
12150 * Both are passed a {@link android.view.DragEvent} object that has a
12151 * {@link android.view.DragEvent#getAction()} value of
12152 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
12153 * </p>
12154 * <p>
12155 * Your application can invoke startDrag() on any attached View object. The View object does not
12156 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
12157 * be related to the View the user selected for dragging.
12158 * </p>
12159 * @param data A {@link android.content.ClipData} object pointing to the data to be
12160 * transferred by the drag and drop operation.
12161 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
12162 * drag shadow.
12163 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
12164 * drop operation. This Object is put into every DragEvent object sent by the system during the
12165 * current drag.
12166 * <p>
12167 * myLocalState is a lightweight mechanism for the sending information from the dragged View
12168 * to the target Views. For example, it can contain flags that differentiate between a
12169 * a copy operation and a move operation.
12170 * </p>
12171 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
12172 * so the parameter should be set to 0.
12173 * @return {@code true} if the method completes successfully, or
12174 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
12175 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070012176 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012177 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012178 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070012179 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012180 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070012181 }
12182 boolean okay = false;
12183
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012184 Point shadowSize = new Point();
12185 Point shadowTouchPoint = new Point();
12186 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070012187
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012188 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
12189 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
12190 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070012191 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012192
Chris Tatea32dcf72010-10-14 12:13:50 -070012193 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012194 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
12195 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070012196 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012197 Surface surface = new Surface();
12198 try {
12199 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012200 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070012201 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070012202 + " surface=" + surface);
12203 if (token != null) {
12204 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070012205 try {
Chris Tate6b391282010-10-14 15:48:59 -070012206 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012207 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070012208 } finally {
12209 surface.unlockCanvasAndPost(canvas);
12210 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012211
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012212 final ViewAncestor root = getViewAncestor();
Christopher Tate407b4e92010-11-30 17:14:08 -080012213
12214 // Cache the local state object for delivery with DragEvents
12215 root.setLocalDragState(myLocalState);
12216
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012217 // repurpose 'shadowSize' for the last touch point
12218 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070012219
Christopher Tatea53146c2010-09-07 11:57:52 -070012220 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012221 shadowSize.x, shadowSize.y,
12222 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070012223 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070012224 }
12225 } catch (Exception e) {
12226 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
12227 surface.destroy();
12228 }
12229
12230 return okay;
12231 }
12232
Christopher Tatea53146c2010-09-07 11:57:52 -070012233 /**
Joe Malin32736f02011-01-19 16:14:20 -080012234 * Handles drag events sent by the system following a call to
12235 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
12236 *<p>
12237 * When the system calls this method, it passes a
12238 * {@link android.view.DragEvent} object. A call to
12239 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
12240 * in DragEvent. The method uses these to determine what is happening in the drag and drop
12241 * operation.
12242 * @param event The {@link android.view.DragEvent} sent by the system.
12243 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
12244 * in DragEvent, indicating the type of drag event represented by this object.
12245 * @return {@code true} if the method was successful, otherwise {@code false}.
12246 * <p>
12247 * The method should return {@code true} in response to an action type of
12248 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
12249 * operation.
12250 * </p>
12251 * <p>
12252 * The method should also return {@code true} in response to an action type of
12253 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
12254 * {@code false} if it didn't.
12255 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012256 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070012257 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070012258 return false;
12259 }
12260
12261 /**
Joe Malin32736f02011-01-19 16:14:20 -080012262 * Detects if this View is enabled and has a drag event listener.
12263 * If both are true, then it calls the drag event listener with the
12264 * {@link android.view.DragEvent} it received. If the drag event listener returns
12265 * {@code true}, then dispatchDragEvent() returns {@code true}.
12266 * <p>
12267 * For all other cases, the method calls the
12268 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
12269 * method and returns its result.
12270 * </p>
12271 * <p>
12272 * This ensures that a drag event is always consumed, even if the View does not have a drag
12273 * event listener. However, if the View has a listener and the listener returns true, then
12274 * onDragEvent() is not called.
12275 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012276 */
12277 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080012278 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070012279 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
12280 && mOnDragListener.onDrag(this, event)) {
12281 return true;
12282 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012283 return onDragEvent(event);
12284 }
12285
Christopher Tate3d4bf172011-03-28 16:16:46 -070012286 boolean canAcceptDrag() {
12287 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
12288 }
12289
Christopher Tatea53146c2010-09-07 11:57:52 -070012290 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070012291 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
12292 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070012293 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070012294 */
12295 public void onCloseSystemDialogs(String reason) {
12296 }
Joe Malin32736f02011-01-19 16:14:20 -080012297
Dianne Hackbornffa42482009-09-23 22:20:11 -070012298 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012299 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070012300 * update a Region being computed for
12301 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012302 * that any non-transparent parts of the Drawable are removed from the
12303 * given transparent region.
12304 *
12305 * @param dr The Drawable whose transparency is to be applied to the region.
12306 * @param region A Region holding the current transparency information,
12307 * where any parts of the region that are set are considered to be
12308 * transparent. On return, this region will be modified to have the
12309 * transparency information reduced by the corresponding parts of the
12310 * Drawable that are not transparent.
12311 * {@hide}
12312 */
12313 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
12314 if (DBG) {
12315 Log.i("View", "Getting transparent region for: " + this);
12316 }
12317 final Region r = dr.getTransparentRegion();
12318 final Rect db = dr.getBounds();
12319 final AttachInfo attachInfo = mAttachInfo;
12320 if (r != null && attachInfo != null) {
12321 final int w = getRight()-getLeft();
12322 final int h = getBottom()-getTop();
12323 if (db.left > 0) {
12324 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
12325 r.op(0, 0, db.left, h, Region.Op.UNION);
12326 }
12327 if (db.right < w) {
12328 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
12329 r.op(db.right, 0, w, h, Region.Op.UNION);
12330 }
12331 if (db.top > 0) {
12332 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
12333 r.op(0, 0, w, db.top, Region.Op.UNION);
12334 }
12335 if (db.bottom < h) {
12336 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
12337 r.op(0, db.bottom, w, h, Region.Op.UNION);
12338 }
12339 final int[] location = attachInfo.mTransparentLocation;
12340 getLocationInWindow(location);
12341 r.translate(location[0], location[1]);
12342 region.op(r, Region.Op.INTERSECT);
12343 } else {
12344 region.op(db, Region.Op.DIFFERENCE);
12345 }
12346 }
12347
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012348 private void checkForLongClick(int delayOffset) {
12349 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
12350 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012351
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012352 if (mPendingCheckForLongPress == null) {
12353 mPendingCheckForLongPress = new CheckForLongPress();
12354 }
12355 mPendingCheckForLongPress.rememberWindowAttachCount();
12356 postDelayed(mPendingCheckForLongPress,
12357 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012359 }
12360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012361 /**
12362 * Inflate a view from an XML resource. This convenience method wraps the {@link
12363 * LayoutInflater} class, which provides a full range of options for view inflation.
12364 *
12365 * @param context The Context object for your activity or application.
12366 * @param resource The resource ID to inflate
12367 * @param root A view group that will be the parent. Used to properly inflate the
12368 * layout_* parameters.
12369 * @see LayoutInflater
12370 */
12371 public static View inflate(Context context, int resource, ViewGroup root) {
12372 LayoutInflater factory = LayoutInflater.from(context);
12373 return factory.inflate(resource, root);
12374 }
Romain Guy33e72ae2010-07-17 12:40:29 -070012375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012376 /**
Adam Powell637d3372010-08-25 14:37:03 -070012377 * Scroll the view with standard behavior for scrolling beyond the normal
12378 * content boundaries. Views that call this method should override
12379 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
12380 * results of an over-scroll operation.
12381 *
12382 * Views can use this method to handle any touch or fling-based scrolling.
12383 *
12384 * @param deltaX Change in X in pixels
12385 * @param deltaY Change in Y in pixels
12386 * @param scrollX Current X scroll value in pixels before applying deltaX
12387 * @param scrollY Current Y scroll value in pixels before applying deltaY
12388 * @param scrollRangeX Maximum content scroll range along the X axis
12389 * @param scrollRangeY Maximum content scroll range along the Y axis
12390 * @param maxOverScrollX Number of pixels to overscroll by in either direction
12391 * along the X axis.
12392 * @param maxOverScrollY Number of pixels to overscroll by in either direction
12393 * along the Y axis.
12394 * @param isTouchEvent true if this scroll operation is the result of a touch event.
12395 * @return true if scrolling was clamped to an over-scroll boundary along either
12396 * axis, false otherwise.
12397 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012398 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070012399 protected boolean overScrollBy(int deltaX, int deltaY,
12400 int scrollX, int scrollY,
12401 int scrollRangeX, int scrollRangeY,
12402 int maxOverScrollX, int maxOverScrollY,
12403 boolean isTouchEvent) {
12404 final int overScrollMode = mOverScrollMode;
12405 final boolean canScrollHorizontal =
12406 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
12407 final boolean canScrollVertical =
12408 computeVerticalScrollRange() > computeVerticalScrollExtent();
12409 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
12410 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
12411 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
12412 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
12413
12414 int newScrollX = scrollX + deltaX;
12415 if (!overScrollHorizontal) {
12416 maxOverScrollX = 0;
12417 }
12418
12419 int newScrollY = scrollY + deltaY;
12420 if (!overScrollVertical) {
12421 maxOverScrollY = 0;
12422 }
12423
12424 // Clamp values if at the limits and record
12425 final int left = -maxOverScrollX;
12426 final int right = maxOverScrollX + scrollRangeX;
12427 final int top = -maxOverScrollY;
12428 final int bottom = maxOverScrollY + scrollRangeY;
12429
12430 boolean clampedX = false;
12431 if (newScrollX > right) {
12432 newScrollX = right;
12433 clampedX = true;
12434 } else if (newScrollX < left) {
12435 newScrollX = left;
12436 clampedX = true;
12437 }
12438
12439 boolean clampedY = false;
12440 if (newScrollY > bottom) {
12441 newScrollY = bottom;
12442 clampedY = true;
12443 } else if (newScrollY < top) {
12444 newScrollY = top;
12445 clampedY = true;
12446 }
12447
12448 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
12449
12450 return clampedX || clampedY;
12451 }
12452
12453 /**
12454 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
12455 * respond to the results of an over-scroll operation.
12456 *
12457 * @param scrollX New X scroll value in pixels
12458 * @param scrollY New Y scroll value in pixels
12459 * @param clampedX True if scrollX was clamped to an over-scroll boundary
12460 * @param clampedY True if scrollY was clamped to an over-scroll boundary
12461 */
12462 protected void onOverScrolled(int scrollX, int scrollY,
12463 boolean clampedX, boolean clampedY) {
12464 // Intentionally empty.
12465 }
12466
12467 /**
12468 * Returns the over-scroll mode for this view. The result will be
12469 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12470 * (allow over-scrolling only if the view content is larger than the container),
12471 * or {@link #OVER_SCROLL_NEVER}.
12472 *
12473 * @return This view's over-scroll mode.
12474 */
12475 public int getOverScrollMode() {
12476 return mOverScrollMode;
12477 }
12478
12479 /**
12480 * Set the over-scroll mode for this view. Valid over-scroll modes are
12481 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12482 * (allow over-scrolling only if the view content is larger than the container),
12483 * or {@link #OVER_SCROLL_NEVER}.
12484 *
12485 * Setting the over-scroll mode of a view will have an effect only if the
12486 * view is capable of scrolling.
12487 *
12488 * @param overScrollMode The new over-scroll mode for this view.
12489 */
12490 public void setOverScrollMode(int overScrollMode) {
12491 if (overScrollMode != OVER_SCROLL_ALWAYS &&
12492 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
12493 overScrollMode != OVER_SCROLL_NEVER) {
12494 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
12495 }
12496 mOverScrollMode = overScrollMode;
12497 }
12498
12499 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012500 * Gets a scale factor that determines the distance the view should scroll
12501 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
12502 * @return The vertical scroll scale factor.
12503 * @hide
12504 */
12505 protected float getVerticalScrollFactor() {
12506 if (mVerticalScrollFactor == 0) {
12507 TypedValue outValue = new TypedValue();
12508 if (!mContext.getTheme().resolveAttribute(
12509 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
12510 throw new IllegalStateException(
12511 "Expected theme to define listPreferredItemHeight.");
12512 }
12513 mVerticalScrollFactor = outValue.getDimension(
12514 mContext.getResources().getDisplayMetrics());
12515 }
12516 return mVerticalScrollFactor;
12517 }
12518
12519 /**
12520 * Gets a scale factor that determines the distance the view should scroll
12521 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
12522 * @return The horizontal scroll scale factor.
12523 * @hide
12524 */
12525 protected float getHorizontalScrollFactor() {
12526 // TODO: Should use something else.
12527 return getVerticalScrollFactor();
12528 }
12529
Chet Haaseb39f0512011-05-24 14:36:40 -070012530 //
12531 // Properties
12532 //
12533 /**
12534 * A Property wrapper around the <code>alpha</code> functionality handled by the
12535 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
12536 */
12537 static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
12538 @Override
12539 public void setValue(View object, float value) {
12540 object.setAlpha(value);
12541 }
12542
12543 @Override
12544 public Float get(View object) {
12545 return object.getAlpha();
12546 }
12547 };
12548
12549 /**
12550 * A Property wrapper around the <code>translationX</code> functionality handled by the
12551 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
12552 */
12553 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
12554 @Override
12555 public void setValue(View object, float value) {
12556 object.setTranslationX(value);
12557 }
12558
12559 @Override
12560 public Float get(View object) {
12561 return object.getTranslationX();
12562 }
12563 };
12564
12565 /**
12566 * A Property wrapper around the <code>translationY</code> functionality handled by the
12567 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
12568 */
12569 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
12570 @Override
12571 public void setValue(View object, float value) {
12572 object.setTranslationY(value);
12573 }
12574
12575 @Override
12576 public Float get(View object) {
12577 return object.getTranslationY();
12578 }
12579 };
12580
12581 /**
12582 * A Property wrapper around the <code>x</code> functionality handled by the
12583 * {@link View#setX(float)} and {@link View#getX()} methods.
12584 */
12585 public static Property<View, Float> X = new FloatProperty<View>("x") {
12586 @Override
12587 public void setValue(View object, float value) {
12588 object.setX(value);
12589 }
12590
12591 @Override
12592 public Float get(View object) {
12593 return object.getX();
12594 }
12595 };
12596
12597 /**
12598 * A Property wrapper around the <code>y</code> functionality handled by the
12599 * {@link View#setY(float)} and {@link View#getY()} methods.
12600 */
12601 public static Property<View, Float> Y = new FloatProperty<View>("y") {
12602 @Override
12603 public void setValue(View object, float value) {
12604 object.setY(value);
12605 }
12606
12607 @Override
12608 public Float get(View object) {
12609 return object.getY();
12610 }
12611 };
12612
12613 /**
12614 * A Property wrapper around the <code>rotation</code> functionality handled by the
12615 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
12616 */
12617 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
12618 @Override
12619 public void setValue(View object, float value) {
12620 object.setRotation(value);
12621 }
12622
12623 @Override
12624 public Float get(View object) {
12625 return object.getRotation();
12626 }
12627 };
12628
12629 /**
12630 * A Property wrapper around the <code>rotationX</code> functionality handled by the
12631 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
12632 */
12633 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
12634 @Override
12635 public void setValue(View object, float value) {
12636 object.setRotationX(value);
12637 }
12638
12639 @Override
12640 public Float get(View object) {
12641 return object.getRotationX();
12642 }
12643 };
12644
12645 /**
12646 * A Property wrapper around the <code>rotationY</code> functionality handled by the
12647 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
12648 */
12649 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
12650 @Override
12651 public void setValue(View object, float value) {
12652 object.setRotationY(value);
12653 }
12654
12655 @Override
12656 public Float get(View object) {
12657 return object.getRotationY();
12658 }
12659 };
12660
12661 /**
12662 * A Property wrapper around the <code>scaleX</code> functionality handled by the
12663 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
12664 */
12665 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
12666 @Override
12667 public void setValue(View object, float value) {
12668 object.setScaleX(value);
12669 }
12670
12671 @Override
12672 public Float get(View object) {
12673 return object.getScaleX();
12674 }
12675 };
12676
12677 /**
12678 * A Property wrapper around the <code>scaleY</code> functionality handled by the
12679 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
12680 */
12681 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
12682 @Override
12683 public void setValue(View object, float value) {
12684 object.setScaleY(value);
12685 }
12686
12687 @Override
12688 public Float get(View object) {
12689 return object.getScaleY();
12690 }
12691 };
12692
Jeff Brown33bbfd22011-02-24 20:55:35 -080012693 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012694 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
12695 * Each MeasureSpec represents a requirement for either the width or the height.
12696 * A MeasureSpec is comprised of a size and a mode. There are three possible
12697 * modes:
12698 * <dl>
12699 * <dt>UNSPECIFIED</dt>
12700 * <dd>
12701 * The parent has not imposed any constraint on the child. It can be whatever size
12702 * it wants.
12703 * </dd>
12704 *
12705 * <dt>EXACTLY</dt>
12706 * <dd>
12707 * The parent has determined an exact size for the child. The child is going to be
12708 * given those bounds regardless of how big it wants to be.
12709 * </dd>
12710 *
12711 * <dt>AT_MOST</dt>
12712 * <dd>
12713 * The child can be as large as it wants up to the specified size.
12714 * </dd>
12715 * </dl>
12716 *
12717 * MeasureSpecs are implemented as ints to reduce object allocation. This class
12718 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
12719 */
12720 public static class MeasureSpec {
12721 private static final int MODE_SHIFT = 30;
12722 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
12723
12724 /**
12725 * Measure specification mode: The parent has not imposed any constraint
12726 * on the child. It can be whatever size it wants.
12727 */
12728 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
12729
12730 /**
12731 * Measure specification mode: The parent has determined an exact size
12732 * for the child. The child is going to be given those bounds regardless
12733 * of how big it wants to be.
12734 */
12735 public static final int EXACTLY = 1 << MODE_SHIFT;
12736
12737 /**
12738 * Measure specification mode: The child can be as large as it wants up
12739 * to the specified size.
12740 */
12741 public static final int AT_MOST = 2 << MODE_SHIFT;
12742
12743 /**
12744 * Creates a measure specification based on the supplied size and mode.
12745 *
12746 * The mode must always be one of the following:
12747 * <ul>
12748 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
12749 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
12750 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
12751 * </ul>
12752 *
12753 * @param size the size of the measure specification
12754 * @param mode the mode of the measure specification
12755 * @return the measure specification based on size and mode
12756 */
12757 public static int makeMeasureSpec(int size, int mode) {
12758 return size + mode;
12759 }
12760
12761 /**
12762 * Extracts the mode from the supplied measure specification.
12763 *
12764 * @param measureSpec the measure specification to extract the mode from
12765 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
12766 * {@link android.view.View.MeasureSpec#AT_MOST} or
12767 * {@link android.view.View.MeasureSpec#EXACTLY}
12768 */
12769 public static int getMode(int measureSpec) {
12770 return (measureSpec & MODE_MASK);
12771 }
12772
12773 /**
12774 * Extracts the size from the supplied measure specification.
12775 *
12776 * @param measureSpec the measure specification to extract the size from
12777 * @return the size in pixels defined in the supplied measure specification
12778 */
12779 public static int getSize(int measureSpec) {
12780 return (measureSpec & ~MODE_MASK);
12781 }
12782
12783 /**
12784 * Returns a String representation of the specified measure
12785 * specification.
12786 *
12787 * @param measureSpec the measure specification to convert to a String
12788 * @return a String with the following format: "MeasureSpec: MODE SIZE"
12789 */
12790 public static String toString(int measureSpec) {
12791 int mode = getMode(measureSpec);
12792 int size = getSize(measureSpec);
12793
12794 StringBuilder sb = new StringBuilder("MeasureSpec: ");
12795
12796 if (mode == UNSPECIFIED)
12797 sb.append("UNSPECIFIED ");
12798 else if (mode == EXACTLY)
12799 sb.append("EXACTLY ");
12800 else if (mode == AT_MOST)
12801 sb.append("AT_MOST ");
12802 else
12803 sb.append(mode).append(" ");
12804
12805 sb.append(size);
12806 return sb.toString();
12807 }
12808 }
12809
12810 class CheckForLongPress implements Runnable {
12811
12812 private int mOriginalWindowAttachCount;
12813
12814 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070012815 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012816 && mOriginalWindowAttachCount == mWindowAttachCount) {
12817 if (performLongClick()) {
12818 mHasPerformedLongPress = true;
12819 }
12820 }
12821 }
12822
12823 public void rememberWindowAttachCount() {
12824 mOriginalWindowAttachCount = mWindowAttachCount;
12825 }
12826 }
Joe Malin32736f02011-01-19 16:14:20 -080012827
Adam Powelle14579b2009-12-16 18:39:52 -080012828 private final class CheckForTap implements Runnable {
12829 public void run() {
12830 mPrivateFlags &= ~PREPRESSED;
12831 mPrivateFlags |= PRESSED;
12832 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012833 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080012834 }
12835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012836
Adam Powella35d7682010-03-12 14:48:13 -080012837 private final class PerformClick implements Runnable {
12838 public void run() {
12839 performClick();
12840 }
12841 }
12842
Dianne Hackborn63042d62011-01-26 18:56:29 -080012843 /** @hide */
12844 public void hackTurnOffWindowResizeAnim(boolean off) {
12845 mAttachInfo.mTurnOffWindowResizeAnim = off;
12846 }
Joe Malin32736f02011-01-19 16:14:20 -080012847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012848 /**
Chet Haasea00f3862011-02-22 06:34:40 -080012849 * This method returns a ViewPropertyAnimator object, which can be used to animate
12850 * specific properties on this View.
12851 *
12852 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
12853 */
12854 public ViewPropertyAnimator animate() {
12855 if (mAnimator == null) {
12856 mAnimator = new ViewPropertyAnimator(this);
12857 }
12858 return mAnimator;
12859 }
12860
12861 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012862 * Interface definition for a callback to be invoked when a key event is
12863 * dispatched to this view. The callback will be invoked before the key
12864 * event is given to the view.
12865 */
12866 public interface OnKeyListener {
12867 /**
12868 * Called when a key is dispatched to a view. This allows listeners to
12869 * get a chance to respond before the target view.
12870 *
12871 * @param v The view the key has been dispatched to.
12872 * @param keyCode The code for the physical key that was pressed
12873 * @param event The KeyEvent object containing full information about
12874 * the event.
12875 * @return True if the listener has consumed the event, false otherwise.
12876 */
12877 boolean onKey(View v, int keyCode, KeyEvent event);
12878 }
12879
12880 /**
12881 * Interface definition for a callback to be invoked when a touch event is
12882 * dispatched to this view. The callback will be invoked before the touch
12883 * event is given to the view.
12884 */
12885 public interface OnTouchListener {
12886 /**
12887 * Called when a touch event is dispatched to a view. This allows listeners to
12888 * get a chance to respond before the target view.
12889 *
12890 * @param v The view the touch event has been dispatched to.
12891 * @param event The MotionEvent object containing full information about
12892 * the event.
12893 * @return True if the listener has consumed the event, false otherwise.
12894 */
12895 boolean onTouch(View v, MotionEvent event);
12896 }
12897
12898 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012899 * Interface definition for a callback to be invoked when a generic motion event is
12900 * dispatched to this view. The callback will be invoked before the generic motion
12901 * event is given to the view.
12902 */
12903 public interface OnGenericMotionListener {
12904 /**
12905 * Called when a generic motion event is dispatched to a view. This allows listeners to
12906 * get a chance to respond before the target view.
12907 *
12908 * @param v The view the generic motion event has been dispatched to.
12909 * @param event The MotionEvent object containing full information about
12910 * the event.
12911 * @return True if the listener has consumed the event, false otherwise.
12912 */
12913 boolean onGenericMotion(View v, MotionEvent event);
12914 }
12915
12916 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012917 * Interface definition for a callback to be invoked when a view has been clicked and held.
12918 */
12919 public interface OnLongClickListener {
12920 /**
12921 * Called when a view has been clicked and held.
12922 *
12923 * @param v The view that was clicked and held.
12924 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080012925 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012926 */
12927 boolean onLongClick(View v);
12928 }
12929
12930 /**
Chris Tate32affef2010-10-18 15:29:21 -070012931 * Interface definition for a callback to be invoked when a drag is being dispatched
12932 * to this view. The callback will be invoked before the hosting view's own
12933 * onDrag(event) method. If the listener wants to fall back to the hosting view's
12934 * onDrag(event) behavior, it should return 'false' from this callback.
12935 */
12936 public interface OnDragListener {
12937 /**
12938 * Called when a drag event is dispatched to a view. This allows listeners
12939 * to get a chance to override base View behavior.
12940 *
Joe Malin32736f02011-01-19 16:14:20 -080012941 * @param v The View that received the drag event.
12942 * @param event The {@link android.view.DragEvent} object for the drag event.
12943 * @return {@code true} if the drag event was handled successfully, or {@code false}
12944 * if the drag event was not handled. Note that {@code false} will trigger the View
12945 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070012946 */
12947 boolean onDrag(View v, DragEvent event);
12948 }
12949
12950 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012951 * Interface definition for a callback to be invoked when the focus state of
12952 * a view changed.
12953 */
12954 public interface OnFocusChangeListener {
12955 /**
12956 * Called when the focus state of a view has changed.
12957 *
12958 * @param v The view whose state has changed.
12959 * @param hasFocus The new focus state of v.
12960 */
12961 void onFocusChange(View v, boolean hasFocus);
12962 }
12963
12964 /**
12965 * Interface definition for a callback to be invoked when a view is clicked.
12966 */
12967 public interface OnClickListener {
12968 /**
12969 * Called when a view has been clicked.
12970 *
12971 * @param v The view that was clicked.
12972 */
12973 void onClick(View v);
12974 }
12975
12976 /**
12977 * Interface definition for a callback to be invoked when the context menu
12978 * for this view is being built.
12979 */
12980 public interface OnCreateContextMenuListener {
12981 /**
12982 * Called when the context menu for this view is being built. It is not
12983 * safe to hold onto the menu after this method returns.
12984 *
12985 * @param menu The context menu that is being built
12986 * @param v The view for which the context menu is being built
12987 * @param menuInfo Extra information about the item for which the
12988 * context menu should be shown. This information will vary
12989 * depending on the class of v.
12990 */
12991 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
12992 }
12993
Joe Onorato664644d2011-01-23 17:53:23 -080012994 /**
12995 * Interface definition for a callback to be invoked when the status bar changes
12996 * visibility.
12997 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012998 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080012999 */
13000 public interface OnSystemUiVisibilityChangeListener {
13001 /**
13002 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070013003 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080013004 *
13005 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
13006 */
13007 public void onSystemUiVisibilityChange(int visibility);
13008 }
13009
Adam Powell4afd62b2011-02-18 15:02:18 -080013010 /**
13011 * Interface definition for a callback to be invoked when this view is attached
13012 * or detached from its window.
13013 */
13014 public interface OnAttachStateChangeListener {
13015 /**
13016 * Called when the view is attached to a window.
13017 * @param v The view that was attached
13018 */
13019 public void onViewAttachedToWindow(View v);
13020 /**
13021 * Called when the view is detached from a window.
13022 * @param v The view that was detached
13023 */
13024 public void onViewDetachedFromWindow(View v);
13025 }
13026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013027 private final class UnsetPressedState implements Runnable {
13028 public void run() {
13029 setPressed(false);
13030 }
13031 }
13032
13033 /**
13034 * Base class for derived classes that want to save and restore their own
13035 * state in {@link android.view.View#onSaveInstanceState()}.
13036 */
13037 public static class BaseSavedState extends AbsSavedState {
13038 /**
13039 * Constructor used when reading from a parcel. Reads the state of the superclass.
13040 *
13041 * @param source
13042 */
13043 public BaseSavedState(Parcel source) {
13044 super(source);
13045 }
13046
13047 /**
13048 * Constructor called by derived classes when creating their SavedState objects
13049 *
13050 * @param superState The state of the superclass of this view
13051 */
13052 public BaseSavedState(Parcelable superState) {
13053 super(superState);
13054 }
13055
13056 public static final Parcelable.Creator<BaseSavedState> CREATOR =
13057 new Parcelable.Creator<BaseSavedState>() {
13058 public BaseSavedState createFromParcel(Parcel in) {
13059 return new BaseSavedState(in);
13060 }
13061
13062 public BaseSavedState[] newArray(int size) {
13063 return new BaseSavedState[size];
13064 }
13065 };
13066 }
13067
13068 /**
13069 * A set of information given to a view when it is attached to its parent
13070 * window.
13071 */
13072 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013073 interface Callbacks {
13074 void playSoundEffect(int effectId);
13075 boolean performHapticFeedback(int effectId, boolean always);
13076 }
13077
13078 /**
13079 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
13080 * to a Handler. This class contains the target (View) to invalidate and
13081 * the coordinates of the dirty rectangle.
13082 *
13083 * For performance purposes, this class also implements a pool of up to
13084 * POOL_LIMIT objects that get reused. This reduces memory allocations
13085 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013086 */
Romain Guyd928d682009-03-31 17:52:16 -070013087 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013088 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070013089 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
13090 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070013091 public InvalidateInfo newInstance() {
13092 return new InvalidateInfo();
13093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013094
Romain Guyd928d682009-03-31 17:52:16 -070013095 public void onAcquired(InvalidateInfo element) {
13096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013097
Romain Guyd928d682009-03-31 17:52:16 -070013098 public void onReleased(InvalidateInfo element) {
13099 }
13100 }, POOL_LIMIT)
13101 );
13102
13103 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013104 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013105
13106 View target;
13107
13108 int left;
13109 int top;
13110 int right;
13111 int bottom;
13112
Romain Guyd928d682009-03-31 17:52:16 -070013113 public void setNextPoolable(InvalidateInfo element) {
13114 mNext = element;
13115 }
13116
13117 public InvalidateInfo getNextPoolable() {
13118 return mNext;
13119 }
13120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013121 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070013122 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013123 }
13124
13125 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070013126 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013127 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013128
13129 public boolean isPooled() {
13130 return mIsPooled;
13131 }
13132
13133 public void setPooled(boolean isPooled) {
13134 mIsPooled = isPooled;
13135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013136 }
13137
13138 final IWindowSession mSession;
13139
13140 final IWindow mWindow;
13141
13142 final IBinder mWindowToken;
13143
13144 final Callbacks mRootCallbacks;
13145
Romain Guy59a12ca2011-06-09 17:48:21 -070013146 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080013147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013148 /**
13149 * The top view of the hierarchy.
13150 */
13151 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070013152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013153 IBinder mPanelParentWindowToken;
13154 Surface mSurface;
13155
Romain Guyb051e892010-09-28 19:09:36 -070013156 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013157 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070013158 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080013159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013160 /**
Romain Guy8506ab42009-06-11 17:35:47 -070013161 * Scale factor used by the compatibility mode
13162 */
13163 float mApplicationScale;
13164
13165 /**
13166 * Indicates whether the application is in compatibility mode
13167 */
13168 boolean mScalingRequired;
13169
13170 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013171 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080013172 */
13173 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080013174
Dianne Hackborn63042d62011-01-26 18:56:29 -080013175 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013176 * Left position of this view's window
13177 */
13178 int mWindowLeft;
13179
13180 /**
13181 * Top position of this view's window
13182 */
13183 int mWindowTop;
13184
13185 /**
Adam Powell26153a32010-11-08 15:22:27 -080013186 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070013187 */
Adam Powell26153a32010-11-08 15:22:27 -080013188 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070013189
13190 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013191 * For windows that are full-screen but using insets to layout inside
13192 * of the screen decorations, these are the current insets for the
13193 * content of the window.
13194 */
13195 final Rect mContentInsets = new Rect();
13196
13197 /**
13198 * For windows that are full-screen but using insets to layout inside
13199 * of the screen decorations, these are the current insets for the
13200 * actual visible parts of the window.
13201 */
13202 final Rect mVisibleInsets = new Rect();
13203
13204 /**
13205 * The internal insets given by this window. This value is
13206 * supplied by the client (through
13207 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
13208 * be given to the window manager when changed to be used in laying
13209 * out windows behind it.
13210 */
13211 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
13212 = new ViewTreeObserver.InternalInsetsInfo();
13213
13214 /**
13215 * All views in the window's hierarchy that serve as scroll containers,
13216 * used to determine if the window can be resized or must be panned
13217 * to adjust for a soft input area.
13218 */
13219 final ArrayList<View> mScrollContainers = new ArrayList<View>();
13220
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070013221 final KeyEvent.DispatcherState mKeyDispatchState
13222 = new KeyEvent.DispatcherState();
13223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013224 /**
13225 * Indicates whether the view's window currently has the focus.
13226 */
13227 boolean mHasWindowFocus;
13228
13229 /**
13230 * The current visibility of the window.
13231 */
13232 int mWindowVisibility;
13233
13234 /**
13235 * Indicates the time at which drawing started to occur.
13236 */
13237 long mDrawingTime;
13238
13239 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070013240 * Indicates whether or not ignoring the DIRTY_MASK flags.
13241 */
13242 boolean mIgnoreDirtyState;
13243
13244 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013245 * Indicates whether the view's window is currently in touch mode.
13246 */
13247 boolean mInTouchMode;
13248
13249 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013250 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013251 * the next time it performs a traversal
13252 */
13253 boolean mRecomputeGlobalAttributes;
13254
13255 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013256 * Set during a traveral if any views want to keep the screen on.
13257 */
13258 boolean mKeepScreenOn;
13259
13260 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013261 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
13262 */
13263 int mSystemUiVisibility;
13264
13265 /**
13266 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
13267 * attached.
13268 */
13269 boolean mHasSystemUiListeners;
13270
13271 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013272 * Set if the visibility of any views has changed.
13273 */
13274 boolean mViewVisibilityChanged;
13275
13276 /**
13277 * Set to true if a view has been scrolled.
13278 */
13279 boolean mViewScrollChanged;
13280
13281 /**
13282 * Global to the view hierarchy used as a temporary for dealing with
13283 * x/y points in the transparent region computations.
13284 */
13285 final int[] mTransparentLocation = new int[2];
13286
13287 /**
13288 * Global to the view hierarchy used as a temporary for dealing with
13289 * x/y points in the ViewGroup.invalidateChild implementation.
13290 */
13291 final int[] mInvalidateChildLocation = new int[2];
13292
Chet Haasec3aa3612010-06-17 08:50:37 -070013293
13294 /**
13295 * Global to the view hierarchy used as a temporary for dealing with
13296 * x/y location when view is transformed.
13297 */
13298 final float[] mTmpTransformLocation = new float[2];
13299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013300 /**
13301 * The view tree observer used to dispatch global events like
13302 * layout, pre-draw, touch mode change, etc.
13303 */
13304 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
13305
13306 /**
13307 * A Canvas used by the view hierarchy to perform bitmap caching.
13308 */
13309 Canvas mCanvas;
13310
13311 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013312 * A Handler supplied by a view's {@link android.view.ViewAncestor}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013313 * handler can be used to pump events in the UI events queue.
13314 */
13315 final Handler mHandler;
13316
13317 /**
13318 * Identifier for messages requesting the view to be invalidated.
13319 * Such messages should be sent to {@link #mHandler}.
13320 */
13321 static final int INVALIDATE_MSG = 0x1;
13322
13323 /**
13324 * Identifier for messages requesting the view to invalidate a region.
13325 * Such messages should be sent to {@link #mHandler}.
13326 */
13327 static final int INVALIDATE_RECT_MSG = 0x2;
13328
13329 /**
13330 * Temporary for use in computing invalidate rectangles while
13331 * calling up the hierarchy.
13332 */
13333 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070013334
13335 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070013336 * Temporary for use in computing hit areas with transformed views
13337 */
13338 final RectF mTmpTransformRect = new RectF();
13339
13340 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070013341 * Temporary list for use in collecting focusable descendents of a view.
13342 */
13343 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
13344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013345 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013346 * The id of the window for accessibility purposes.
13347 */
13348 int mAccessibilityWindowId = View.NO_ID;
13349
13350 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013351 * Creates a new set of attachment information with the specified
13352 * events handler and thread.
13353 *
13354 * @param handler the events handler the view must use
13355 */
13356 AttachInfo(IWindowSession session, IWindow window,
13357 Handler handler, Callbacks effectPlayer) {
13358 mSession = session;
13359 mWindow = window;
13360 mWindowToken = window.asBinder();
13361 mHandler = handler;
13362 mRootCallbacks = effectPlayer;
13363 }
13364 }
13365
13366 /**
13367 * <p>ScrollabilityCache holds various fields used by a View when scrolling
13368 * is supported. This avoids keeping too many unused fields in most
13369 * instances of View.</p>
13370 */
Mike Cleronf116bf82009-09-27 19:14:12 -070013371 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080013372
Mike Cleronf116bf82009-09-27 19:14:12 -070013373 /**
13374 * Scrollbars are not visible
13375 */
13376 public static final int OFF = 0;
13377
13378 /**
13379 * Scrollbars are visible
13380 */
13381 public static final int ON = 1;
13382
13383 /**
13384 * Scrollbars are fading away
13385 */
13386 public static final int FADING = 2;
13387
13388 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080013389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013390 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070013391 public int scrollBarDefaultDelayBeforeFade;
13392 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013393
13394 public int scrollBarSize;
13395 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070013396 public float[] interpolatorValues;
13397 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013398
13399 public final Paint paint;
13400 public final Matrix matrix;
13401 public Shader shader;
13402
Mike Cleronf116bf82009-09-27 19:14:12 -070013403 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
13404
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013405 private static final float[] OPAQUE = { 255 };
13406 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080013407
Mike Cleronf116bf82009-09-27 19:14:12 -070013408 /**
13409 * When fading should start. This time moves into the future every time
13410 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
13411 */
13412 public long fadeStartTime;
13413
13414
13415 /**
13416 * The current state of the scrollbars: ON, OFF, or FADING
13417 */
13418 public int state = OFF;
13419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013420 private int mLastColor;
13421
Mike Cleronf116bf82009-09-27 19:14:12 -070013422 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013423 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
13424 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070013425 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
13426 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013427
13428 paint = new Paint();
13429 matrix = new Matrix();
13430 // use use a height of 1, and then wack the matrix each time we
13431 // actually use it.
13432 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070013433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013434 paint.setShader(shader);
13435 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070013436 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013437 }
Romain Guy8506ab42009-06-11 17:35:47 -070013438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013439 public void setFadeColor(int color) {
13440 if (color != 0 && color != mLastColor) {
13441 mLastColor = color;
13442 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070013443
Romain Guye55e1a72009-08-27 10:42:26 -070013444 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
13445 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070013446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013447 paint.setShader(shader);
13448 // Restore the default transfer mode (src_over)
13449 paint.setXfermode(null);
13450 }
13451 }
Joe Malin32736f02011-01-19 16:14:20 -080013452
Mike Cleronf116bf82009-09-27 19:14:12 -070013453 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070013454 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070013455 if (now >= fadeStartTime) {
13456
13457 // the animation fades the scrollbars out by changing
13458 // the opacity (alpha) from fully opaque to fully
13459 // transparent
13460 int nextFrame = (int) now;
13461 int framesCount = 0;
13462
13463 Interpolator interpolator = scrollBarInterpolator;
13464
13465 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013466 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070013467
13468 // End transparent
13469 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013470 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070013471
13472 state = FADING;
13473
13474 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080013475 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070013476 }
13477 }
13478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013479 }
13480}