blob: e58a605175d17e7c786de167d0eb5d4b7376265a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Chet Haaseb39f0512011-05-24 14:36:40 -070019import android.util.FloatProperty;
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -070020import android.util.LocaleUtil;
Chet Haaseb39f0512011-05-24 14:36:40 -070021import android.util.Property;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080022import com.android.internal.R;
23import com.android.internal.util.Predicate;
24import com.android.internal.view.menu.MenuBuilder;
25
Christopher Tatea53146c2010-09-07 11:57:52 -070026import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080028import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.res.Resources;
30import android.content.res.TypedArray;
31import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070032import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.graphics.Canvas;
Mike Cleronf116bf82009-09-27 19:14:12 -070034import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.graphics.LinearGradient;
36import android.graphics.Matrix;
37import android.graphics.Paint;
38import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070039import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.PorterDuff;
41import android.graphics.PorterDuffXfermode;
42import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070043import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.graphics.Region;
45import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.graphics.drawable.ColorDrawable;
47import android.graphics.drawable.Drawable;
48import android.os.Handler;
49import android.os.IBinder;
50import android.os.Message;
51import android.os.Parcel;
52import android.os.Parcelable;
53import android.os.RemoteException;
54import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070057import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070059import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.util.Pools;
61import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080062import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070064import android.view.accessibility.AccessibilityEvent;
65import android.view.accessibility.AccessibilityEventSource;
66import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070067import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070069import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070070import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.inputmethod.InputConnection;
72import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.widget.ScrollBarDrawable;
74
Christopher Tatea0374192010-10-05 13:06:41 -070075import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070076import java.lang.reflect.InvocationTargetException;
77import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import java.util.ArrayList;
79import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070080import java.util.Locale;
Romain Guyd90a3312009-05-06 14:54:28 -070081import java.util.WeakHashMap;
Adam Powell4afd62b2011-02-18 15:02:18 -080082import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
84/**
85 * <p>
86 * This class represents the basic building block for user interface components. A View
87 * occupies a rectangular area on the screen and is responsible for drawing and
88 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070089 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
91 * are invisible containers that hold other Views (or other ViewGroups) and define
92 * their layout properties.
93 * </p>
94 *
95 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070096 * <p>For an introduction to using this class to develop your
97 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -070099 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
101 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
102 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
103 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
104 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
105 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
106 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
107 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
108 * </p>
109 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700110 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 * <a name="Using"></a>
112 * <h3>Using Views</h3>
113 * <p>
114 * All of the views in a window are arranged in a single tree. You can add views
115 * either from code or by specifying a tree of views in one or more XML layout
116 * files. There are many specialized subclasses of views that act as controls or
117 * are capable of displaying text, images, or other content.
118 * </p>
119 * <p>
120 * Once you have created a tree of views, there are typically a few types of
121 * common operations you may wish to perform:
122 * <ul>
123 * <li><strong>Set properties:</strong> for example setting the text of a
124 * {@link android.widget.TextView}. The available properties and the methods
125 * that set them will vary among the different subclasses of views. Note that
126 * properties that are known at build time can be set in the XML layout
127 * files.</li>
128 * <li><strong>Set focus:</strong> The framework will handled moving focus in
129 * response to user input. To force focus to a specific view, call
130 * {@link #requestFocus}.</li>
131 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
132 * that will be notified when something interesting happens to the view. For
133 * example, all views will let you set a listener to be notified when the view
134 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700135 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
136 * Other view subclasses offer more specialized listeners. For example, a Button
137 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700139 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * </ul>
141 * </p>
142 * <p><em>
143 * Note: The Android framework is responsible for measuring, laying out and
144 * drawing views. You should not call methods that perform these actions on
145 * views yourself unless you are actually implementing a
146 * {@link android.view.ViewGroup}.
147 * </em></p>
148 *
149 * <a name="Lifecycle"></a>
150 * <h3>Implementing a Custom View</h3>
151 *
152 * <p>
153 * To implement a custom view, you will usually begin by providing overrides for
154 * some of the standard methods that the framework calls on all views. You do
155 * not need to override all of these methods. In fact, you can start by just
156 * overriding {@link #onDraw(android.graphics.Canvas)}.
157 * <table border="2" width="85%" align="center" cellpadding="5">
158 * <thead>
159 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
160 * </thead>
161 *
162 * <tbody>
163 * <tr>
164 * <td rowspan="2">Creation</td>
165 * <td>Constructors</td>
166 * <td>There is a form of the constructor that are called when the view
167 * is created from code and a form that is called when the view is
168 * inflated from a layout file. The second form should parse and apply
169 * any attributes defined in the layout file.
170 * </td>
171 * </tr>
172 * <tr>
173 * <td><code>{@link #onFinishInflate()}</code></td>
174 * <td>Called after a view and all of its children has been inflated
175 * from XML.</td>
176 * </tr>
177 *
178 * <tr>
179 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700180 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <td>Called to determine the size requirements for this view and all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700186 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * <td>Called when this view should assign a size and position to all
188 * of its children.
189 * </td>
190 * </tr>
191 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700192 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * <td>Called when the size of this view has changed.
194 * </td>
195 * </tr>
196 *
197 * <tr>
198 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when the view should render its content.
201 * </td>
202 * </tr>
203 *
204 * <tr>
205 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700206 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * <td>Called when a new key event occurs.
208 * </td>
209 * </tr>
210 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700211 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 * <td>Called when a key up event occurs.
213 * </td>
214 * </tr>
215 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700216 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 * <td>Called when a trackball motion event occurs.
218 * </td>
219 * </tr>
220 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700221 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <td>Called when a touch screen motion event occurs.
223 * </td>
224 * </tr>
225 *
226 * <tr>
227 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700234 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * <td>Called when the window containing the view gains or loses focus.
236 * </td>
237 * </tr>
238 *
239 * <tr>
240 * <td rowspan="3">Attaching</td>
241 * <td><code>{@link #onAttachedToWindow()}</code></td>
242 * <td>Called when the view is attached to a window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
247 * <td><code>{@link #onDetachedFromWindow}</code></td>
248 * <td>Called when the view is detached from its window.
249 * </td>
250 * </tr>
251 *
252 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700253 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * <td>Called when the visibility of the window containing the view
255 * has changed.
256 * </td>
257 * </tr>
258 * </tbody>
259 *
260 * </table>
261 * </p>
262 *
263 * <a name="IDs"></a>
264 * <h3>IDs</h3>
265 * Views may have an integer id associated with them. These ids are typically
266 * assigned in the layout XML files, and are used to find specific views within
267 * the view tree. A common pattern is to:
268 * <ul>
269 * <li>Define a Button in the layout file and assign it a unique ID.
270 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700271 * &lt;Button
272 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 * android:layout_width="wrap_content"
274 * android:layout_height="wrap_content"
275 * android:text="@string/my_button_text"/&gt;
276 * </pre></li>
277 * <li>From the onCreate method of an Activity, find the Button
278 * <pre class="prettyprint">
279 * Button myButton = (Button) findViewById(R.id.my_button);
280 * </pre></li>
281 * </ul>
282 * <p>
283 * View IDs need not be unique throughout the tree, but it is good practice to
284 * ensure that they are at least unique within the part of the tree you are
285 * searching.
286 * </p>
287 *
288 * <a name="Position"></a>
289 * <h3>Position</h3>
290 * <p>
291 * The geometry of a view is that of a rectangle. A view has a location,
292 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
293 * two dimensions, expressed as a width and a height. The unit for location
294 * and dimensions is the pixel.
295 * </p>
296 *
297 * <p>
298 * It is possible to retrieve the location of a view by invoking the methods
299 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
300 * coordinate of the rectangle representing the view. The latter returns the
301 * top, or Y, coordinate of the rectangle representing the view. These methods
302 * both return the location of the view relative to its parent. For instance,
303 * when getLeft() returns 20, that means the view is located 20 pixels to the
304 * right of the left edge of its direct parent.
305 * </p>
306 *
307 * <p>
308 * In addition, several convenience methods are offered to avoid unnecessary
309 * computations, namely {@link #getRight()} and {@link #getBottom()}.
310 * These methods return the coordinates of the right and bottom edges of the
311 * rectangle representing the view. For instance, calling {@link #getRight()}
312 * is similar to the following computation: <code>getLeft() + getWidth()</code>
313 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
314 * </p>
315 *
316 * <a name="SizePaddingMargins"></a>
317 * <h3>Size, padding and margins</h3>
318 * <p>
319 * The size of a view is expressed with a width and a height. A view actually
320 * possess two pairs of width and height values.
321 * </p>
322 *
323 * <p>
324 * The first pair is known as <em>measured width</em> and
325 * <em>measured height</em>. These dimensions define how big a view wants to be
326 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
327 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
328 * and {@link #getMeasuredHeight()}.
329 * </p>
330 *
331 * <p>
332 * The second pair is simply known as <em>width</em> and <em>height</em>, or
333 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
334 * dimensions define the actual size of the view on screen, at drawing time and
335 * after layout. These values may, but do not have to, be different from the
336 * measured width and height. The width and height can be obtained by calling
337 * {@link #getWidth()} and {@link #getHeight()}.
338 * </p>
339 *
340 * <p>
341 * To measure its dimensions, a view takes into account its padding. The padding
342 * is expressed in pixels for the left, top, right and bottom parts of the view.
343 * Padding can be used to offset the content of the view by a specific amount of
344 * pixels. For instance, a left padding of 2 will push the view's content by
345 * 2 pixels to the right of the left edge. Padding can be set using the
346 * {@link #setPadding(int, int, int, int)} method and queried by calling
347 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
Fabrice Di Megliod8703a92011-06-16 18:54:08 -0700348 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * </p>
350 *
351 * <p>
352 * Even though a view can define a padding, it does not provide any support for
353 * margins. However, view groups provide such a support. Refer to
354 * {@link android.view.ViewGroup} and
355 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
356 * </p>
357 *
358 * <a name="Layout"></a>
359 * <h3>Layout</h3>
360 * <p>
361 * Layout is a two pass process: a measure pass and a layout pass. The measuring
362 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
363 * of the view tree. Each view pushes dimension specifications down the tree
364 * during the recursion. At the end of the measure pass, every view has stored
365 * its measurements. The second pass happens in
366 * {@link #layout(int,int,int,int)} and is also top-down. During
367 * this pass each parent is responsible for positioning all of its children
368 * using the sizes computed in the measure pass.
369 * </p>
370 *
371 * <p>
372 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
373 * {@link #getMeasuredHeight()} values must be set, along with those for all of
374 * that view's descendants. A view's measured width and measured height values
375 * must respect the constraints imposed by the view's parents. This guarantees
376 * that at the end of the measure pass, all parents accept all of their
377 * children's measurements. A parent view may call measure() more than once on
378 * its children. For example, the parent may measure each child once with
379 * unspecified dimensions to find out how big they want to be, then call
380 * measure() on them again with actual numbers if the sum of all the children's
381 * unconstrained sizes is too big or too small.
382 * </p>
383 *
384 * <p>
385 * The measure pass uses two classes to communicate dimensions. The
386 * {@link MeasureSpec} class is used by views to tell their parents how they
387 * want to be measured and positioned. The base LayoutParams class just
388 * describes how big the view wants to be for both width and height. For each
389 * dimension, it can specify one of:
390 * <ul>
391 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800392 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 * (minus padding)
394 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
395 * enclose its content (plus padding).
396 * </ul>
397 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
398 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
399 * an X and Y value.
400 * </p>
401 *
402 * <p>
403 * MeasureSpecs are used to push requirements down the tree from parent to
404 * child. A MeasureSpec can be in one of three modes:
405 * <ul>
406 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
407 * of a child view. For example, a LinearLayout may call measure() on its child
408 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
409 * tall the child view wants to be given a width of 240 pixels.
410 * <li>EXACTLY: This is used by the parent to impose an exact size on the
411 * child. The child must use this size, and guarantee that all of its
412 * descendants will fit within this size.
413 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
414 * child. The child must gurantee that it and all of its descendants will fit
415 * within this size.
416 * </ul>
417 * </p>
418 *
419 * <p>
420 * To intiate a layout, call {@link #requestLayout}. This method is typically
421 * called by a view on itself when it believes that is can no longer fit within
422 * its current bounds.
423 * </p>
424 *
425 * <a name="Drawing"></a>
426 * <h3>Drawing</h3>
427 * <p>
428 * Drawing is handled by walking the tree and rendering each view that
429 * intersects the the invalid region. Because the tree is traversed in-order,
430 * this means that parents will draw before (i.e., behind) their children, with
431 * siblings drawn in the order they appear in the tree.
432 * If you set a background drawable for a View, then the View will draw it for you
433 * before calling back to its <code>onDraw()</code> method.
434 * </p>
435 *
436 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700437 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 * </p>
439 *
440 * <p>
441 * To force a view to draw, call {@link #invalidate()}.
442 * </p>
443 *
444 * <a name="EventHandlingThreading"></a>
445 * <h3>Event Handling and Threading</h3>
446 * <p>
447 * The basic cycle of a view is as follows:
448 * <ol>
449 * <li>An event comes in and is dispatched to the appropriate view. The view
450 * handles the event and notifies any listeners.</li>
451 * <li>If in the course of processing the event, the view's bounds may need
452 * to be changed, the view will call {@link #requestLayout()}.</li>
453 * <li>Similarly, if in the course of processing the event the view's appearance
454 * may need to be changed, the view will call {@link #invalidate()}.</li>
455 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
456 * the framework will take care of measuring, laying out, and drawing the tree
457 * as appropriate.</li>
458 * </ol>
459 * </p>
460 *
461 * <p><em>Note: The entire view tree is single threaded. You must always be on
462 * the UI thread when calling any method on any view.</em>
463 * If you are doing work on other threads and want to update the state of a view
464 * from that thread, you should use a {@link Handler}.
465 * </p>
466 *
467 * <a name="FocusHandling"></a>
468 * <h3>Focus Handling</h3>
469 * <p>
470 * The framework will handle routine focus movement in response to user input.
471 * This includes changing the focus as views are removed or hidden, or as new
472 * views become available. Views indicate their willingness to take focus
473 * through the {@link #isFocusable} method. To change whether a view can take
474 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
475 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
476 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
477 * </p>
478 * <p>
479 * Focus movement is based on an algorithm which finds the nearest neighbor in a
480 * given direction. In rare cases, the default algorithm may not match the
481 * intended behavior of the developer. In these situations, you can provide
482 * explicit overrides by using these XML attributes in the layout file:
483 * <pre>
484 * nextFocusDown
485 * nextFocusLeft
486 * nextFocusRight
487 * nextFocusUp
488 * </pre>
489 * </p>
490 *
491 *
492 * <p>
493 * To get a particular view to take focus, call {@link #requestFocus()}.
494 * </p>
495 *
496 * <a name="TouchMode"></a>
497 * <h3>Touch Mode</h3>
498 * <p>
499 * When a user is navigating a user interface via directional keys such as a D-pad, it is
500 * necessary to give focus to actionable items such as buttons so the user can see
501 * what will take input. If the device has touch capabilities, however, and the user
502 * begins interacting with the interface by touching it, it is no longer necessary to
503 * always highlight, or give focus to, a particular view. This motivates a mode
504 * for interaction named 'touch mode'.
505 * </p>
506 * <p>
507 * For a touch capable device, once the user touches the screen, the device
508 * will enter touch mode. From this point onward, only views for which
509 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
510 * Other views that are touchable, like buttons, will not take focus when touched; they will
511 * only fire the on click listeners.
512 * </p>
513 * <p>
514 * Any time a user hits a directional key, such as a D-pad direction, the view device will
515 * exit touch mode, and find a view to take focus, so that the user may resume interacting
516 * with the user interface without touching the screen again.
517 * </p>
518 * <p>
519 * The touch mode state is maintained across {@link android.app.Activity}s. Call
520 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
521 * </p>
522 *
523 * <a name="Scrolling"></a>
524 * <h3>Scrolling</h3>
525 * <p>
526 * The framework provides basic support for views that wish to internally
527 * scroll their content. This includes keeping track of the X and Y scroll
528 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800529 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700530 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 * </p>
532 *
533 * <a name="Tags"></a>
534 * <h3>Tags</h3>
535 * <p>
536 * Unlike IDs, tags are not used to identify views. Tags are essentially an
537 * extra piece of information that can be associated with a view. They are most
538 * often used as a convenience to store data related to views in the views
539 * themselves rather than by putting them in a separate structure.
540 * </p>
541 *
542 * <a name="Animation"></a>
543 * <h3>Animation</h3>
544 * <p>
545 * You can attach an {@link Animation} object to a view using
546 * {@link #setAnimation(Animation)} or
547 * {@link #startAnimation(Animation)}. The animation can alter the scale,
548 * rotation, translation and alpha of a view over time. If the animation is
549 * attached to a view that has children, the animation will affect the entire
550 * subtree rooted by that node. When an animation is started, the framework will
551 * take care of redrawing the appropriate views until the animation completes.
552 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800553 * <p>
554 * Starting with Android 3.0, the preferred way of animating views is to use the
555 * {@link android.animation} package APIs.
556 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 *
Jeff Brown85a31762010-09-01 17:01:00 -0700558 * <a name="Security"></a>
559 * <h3>Security</h3>
560 * <p>
561 * Sometimes it is essential that an application be able to verify that an action
562 * is being performed with the full knowledge and consent of the user, such as
563 * granting a permission request, making a purchase or clicking on an advertisement.
564 * Unfortunately, a malicious application could try to spoof the user into
565 * performing these actions, unaware, by concealing the intended purpose of the view.
566 * As a remedy, the framework offers a touch filtering mechanism that can be used to
567 * improve the security of views that provide access to sensitive functionality.
568 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700569 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800570 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700571 * will discard touches that are received whenever the view's window is obscured by
572 * another visible window. As a result, the view will not receive touches whenever a
573 * toast, dialog or other window appears above the view's window.
574 * </p><p>
575 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700576 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
577 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700578 * </p>
579 *
Romain Guy171c5922011-01-06 10:04:23 -0800580 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700581 * @attr ref android.R.styleable#View_background
582 * @attr ref android.R.styleable#View_clickable
583 * @attr ref android.R.styleable#View_contentDescription
584 * @attr ref android.R.styleable#View_drawingCacheQuality
585 * @attr ref android.R.styleable#View_duplicateParentState
586 * @attr ref android.R.styleable#View_id
587 * @attr ref android.R.styleable#View_fadingEdge
588 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700589 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700591 * @attr ref android.R.styleable#View_isScrollContainer
592 * @attr ref android.R.styleable#View_focusable
593 * @attr ref android.R.styleable#View_focusableInTouchMode
594 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
595 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800596 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700597 * @attr ref android.R.styleable#View_longClickable
598 * @attr ref android.R.styleable#View_minHeight
599 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 * @attr ref android.R.styleable#View_nextFocusDown
601 * @attr ref android.R.styleable#View_nextFocusLeft
602 * @attr ref android.R.styleable#View_nextFocusRight
603 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700604 * @attr ref android.R.styleable#View_onClick
605 * @attr ref android.R.styleable#View_padding
606 * @attr ref android.R.styleable#View_paddingBottom
607 * @attr ref android.R.styleable#View_paddingLeft
608 * @attr ref android.R.styleable#View_paddingRight
609 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Megliod8703a92011-06-16 18:54:08 -0700610 * @attr ref android.R.styleable#View_paddingStart
611 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700612 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800613 * @attr ref android.R.styleable#View_rotation
614 * @attr ref android.R.styleable#View_rotationX
615 * @attr ref android.R.styleable#View_rotationY
616 * @attr ref android.R.styleable#View_scaleX
617 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 * @attr ref android.R.styleable#View_scrollX
619 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700620 * @attr ref android.R.styleable#View_scrollbarSize
621 * @attr ref android.R.styleable#View_scrollbarStyle
622 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700623 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
624 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
626 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 * @attr ref android.R.styleable#View_scrollbarThumbVertical
628 * @attr ref android.R.styleable#View_scrollbarTrackVertical
629 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
630 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700631 * @attr ref android.R.styleable#View_soundEffectsEnabled
632 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800633 * @attr ref android.R.styleable#View_transformPivotX
634 * @attr ref android.R.styleable#View_transformPivotY
635 * @attr ref android.R.styleable#View_translationX
636 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700637 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 *
639 * @see android.view.ViewGroup
640 */
Fabrice Di Meglio6a036402011-05-23 14:43:23 -0700641public class View implements Drawable.Callback2, KeyEvent.Callback, AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 private static final boolean DBG = false;
643
644 /**
645 * The logging tag used by this class with android.util.Log.
646 */
647 protected static final String VIEW_LOG_TAG = "View";
648
649 /**
650 * Used to mark a View that has no ID.
651 */
652 public static final int NO_ID = -1;
653
654 /**
655 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
656 * calling setFlags.
657 */
658 private static final int NOT_FOCUSABLE = 0x00000000;
659
660 /**
661 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
662 * setFlags.
663 */
664 private static final int FOCUSABLE = 0x00000001;
665
666 /**
667 * Mask for use with setFlags indicating bits used for focus.
668 */
669 private static final int FOCUSABLE_MASK = 0x00000001;
670
671 /**
672 * This view will adjust its padding to fit sytem windows (e.g. status bar)
673 */
674 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
675
676 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700677 * This view is visible. Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 */
679 public static final int VISIBLE = 0x00000000;
680
681 /**
682 * This view is invisible, but it still takes up space for layout purposes.
Romain Guy5c22a8c2011-05-13 11:48:45 -0700683 * Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 */
685 public static final int INVISIBLE = 0x00000004;
686
687 /**
688 * This view is invisible, and it doesn't take any space for layout
Romain Guy5c22a8c2011-05-13 11:48:45 -0700689 * purposes. Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 */
691 public static final int GONE = 0x00000008;
692
693 /**
694 * Mask for use with setFlags indicating bits used for visibility.
695 * {@hide}
696 */
697 static final int VISIBILITY_MASK = 0x0000000C;
698
699 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
700
701 /**
702 * This view is enabled. Intrepretation varies by subclass.
703 * Use with ENABLED_MASK when calling setFlags.
704 * {@hide}
705 */
706 static final int ENABLED = 0x00000000;
707
708 /**
709 * This view is disabled. Intrepretation varies by subclass.
710 * Use with ENABLED_MASK when calling setFlags.
711 * {@hide}
712 */
713 static final int DISABLED = 0x00000020;
714
715 /**
716 * Mask for use with setFlags indicating bits used for indicating whether
717 * this view is enabled
718 * {@hide}
719 */
720 static final int ENABLED_MASK = 0x00000020;
721
722 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700723 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
724 * called and further optimizations will be performed. It is okay to have
725 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 * {@hide}
727 */
728 static final int WILL_NOT_DRAW = 0x00000080;
729
730 /**
731 * Mask for use with setFlags indicating bits used for indicating whether
732 * this view is will draw
733 * {@hide}
734 */
735 static final int DRAW_MASK = 0x00000080;
736
737 /**
738 * <p>This view doesn't show scrollbars.</p>
739 * {@hide}
740 */
741 static final int SCROLLBARS_NONE = 0x00000000;
742
743 /**
744 * <p>This view shows horizontal scrollbars.</p>
745 * {@hide}
746 */
747 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
748
749 /**
750 * <p>This view shows vertical scrollbars.</p>
751 * {@hide}
752 */
753 static final int SCROLLBARS_VERTICAL = 0x00000200;
754
755 /**
756 * <p>Mask for use with setFlags indicating bits used for indicating which
757 * scrollbars are enabled.</p>
758 * {@hide}
759 */
760 static final int SCROLLBARS_MASK = 0x00000300;
761
Jeff Brown85a31762010-09-01 17:01:00 -0700762 /**
763 * Indicates that the view should filter touches when its window is obscured.
764 * Refer to the class comments for more information about this security feature.
765 * {@hide}
766 */
767 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
768
769 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770
771 /**
772 * <p>This view doesn't show fading edges.</p>
773 * {@hide}
774 */
775 static final int FADING_EDGE_NONE = 0x00000000;
776
777 /**
778 * <p>This view shows horizontal fading edges.</p>
779 * {@hide}
780 */
781 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
782
783 /**
784 * <p>This view shows vertical fading edges.</p>
785 * {@hide}
786 */
787 static final int FADING_EDGE_VERTICAL = 0x00002000;
788
789 /**
790 * <p>Mask for use with setFlags indicating bits used for indicating which
791 * fading edges are enabled.</p>
792 * {@hide}
793 */
794 static final int FADING_EDGE_MASK = 0x00003000;
795
796 /**
797 * <p>Indicates this view can be clicked. When clickable, a View reacts
798 * to clicks by notifying the OnClickListener.<p>
799 * {@hide}
800 */
801 static final int CLICKABLE = 0x00004000;
802
803 /**
804 * <p>Indicates this view is caching its drawing into a bitmap.</p>
805 * {@hide}
806 */
807 static final int DRAWING_CACHE_ENABLED = 0x00008000;
808
809 /**
810 * <p>Indicates that no icicle should be saved for this view.<p>
811 * {@hide}
812 */
813 static final int SAVE_DISABLED = 0x000010000;
814
815 /**
816 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
817 * property.</p>
818 * {@hide}
819 */
820 static final int SAVE_DISABLED_MASK = 0x000010000;
821
822 /**
823 * <p>Indicates that no drawing cache should ever be created for this view.<p>
824 * {@hide}
825 */
826 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
827
828 /**
829 * <p>Indicates this view can take / keep focus when int touch mode.</p>
830 * {@hide}
831 */
832 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
833
834 /**
835 * <p>Enables low quality mode for the drawing cache.</p>
836 */
837 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
838
839 /**
840 * <p>Enables high quality mode for the drawing cache.</p>
841 */
842 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
843
844 /**
845 * <p>Enables automatic quality mode for the drawing cache.</p>
846 */
847 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
848
849 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
850 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
851 };
852
853 /**
854 * <p>Mask for use with setFlags indicating bits used for the cache
855 * quality property.</p>
856 * {@hide}
857 */
858 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
859
860 /**
861 * <p>
862 * Indicates this view can be long clicked. When long clickable, a View
863 * reacts to long clicks by notifying the OnLongClickListener or showing a
864 * context menu.
865 * </p>
866 * {@hide}
867 */
868 static final int LONG_CLICKABLE = 0x00200000;
869
870 /**
871 * <p>Indicates that this view gets its drawable states from its direct parent
872 * and ignores its original internal states.</p>
873 *
874 * @hide
875 */
876 static final int DUPLICATE_PARENT_STATE = 0x00400000;
877
878 /**
879 * The scrollbar style to display the scrollbars inside the content area,
880 * without increasing the padding. The scrollbars will be overlaid with
881 * translucency on the view's content.
882 */
883 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
884
885 /**
886 * The scrollbar style to display the scrollbars inside the padded area,
887 * increasing the padding of the view. The scrollbars will not overlap the
888 * content area of the view.
889 */
890 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
891
892 /**
893 * The scrollbar style to display the scrollbars at the edge of the view,
894 * without increasing the padding. The scrollbars will be overlaid with
895 * translucency.
896 */
897 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
898
899 /**
900 * The scrollbar style to display the scrollbars at the edge of the view,
901 * increasing the padding of the view. The scrollbars will only overlap the
902 * background, if any.
903 */
904 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
905
906 /**
907 * Mask to check if the scrollbar style is overlay or inset.
908 * {@hide}
909 */
910 static final int SCROLLBARS_INSET_MASK = 0x01000000;
911
912 /**
913 * Mask to check if the scrollbar style is inside or outside.
914 * {@hide}
915 */
916 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
917
918 /**
919 * Mask for scrollbar style.
920 * {@hide}
921 */
922 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
923
924 /**
925 * View flag indicating that the screen should remain on while the
926 * window containing this view is visible to the user. This effectively
927 * takes care of automatically setting the WindowManager's
928 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
929 */
930 public static final int KEEP_SCREEN_ON = 0x04000000;
931
932 /**
933 * View flag indicating whether this view should have sound effects enabled
934 * for events such as clicking and touching.
935 */
936 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
937
938 /**
939 * View flag indicating whether this view should have haptic feedback
940 * enabled for events such as long presses.
941 */
942 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
943
944 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700945 * <p>Indicates that the view hierarchy should stop saving state when
946 * it reaches this view. If state saving is initiated immediately at
947 * the view, it will be allowed.
948 * {@hide}
949 */
950 static final int PARENT_SAVE_DISABLED = 0x20000000;
951
952 /**
953 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
954 * {@hide}
955 */
956 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
957
958 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800959 * Horizontal direction of this view is from Left to Right.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700960 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800961 * {@hide}
962 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700963 public static final int LAYOUT_DIRECTION_LTR = 0x00000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800964
965 /**
966 * Horizontal direction of this view is from Right to Left.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700967 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800968 * {@hide}
969 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700970 public static final int LAYOUT_DIRECTION_RTL = 0x40000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800971
972 /**
973 * Horizontal direction of this view is inherited from its parent.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700974 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800975 * {@hide}
976 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700977 public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800978
979 /**
980 * Horizontal direction of this view is from deduced from the default language
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700981 * script for the locale. Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800982 * {@hide}
983 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700984 public static final int LAYOUT_DIRECTION_LOCALE = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800985
986 /**
987 * Mask for use with setFlags indicating bits used for horizontalDirection.
988 * {@hide}
989 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700990 static final int LAYOUT_DIRECTION_MASK = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800991
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700992 /*
993 * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct
994 * flag value.
995 * {@hide}
996 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700997 private static final int[] LAYOUT_DIRECTION_FLAGS = {LAYOUT_DIRECTION_LTR,
998 LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE};
Cibu Johny7632cb92010-02-22 13:01:02 -0800999
1000 /**
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001001 * Default horizontalDirection.
1002 * {@hide}
1003 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07001004 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001005
1006 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001007 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1008 * should add all focusable Views regardless if they are focusable in touch mode.
1009 */
1010 public static final int FOCUSABLES_ALL = 0x00000000;
1011
1012 /**
1013 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1014 * should add only Views focusable in touch mode.
1015 */
1016 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1017
1018 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001019 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 * item.
1021 */
1022 public static final int FOCUS_BACKWARD = 0x00000001;
1023
1024 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001025 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 * item.
1027 */
1028 public static final int FOCUS_FORWARD = 0x00000002;
1029
1030 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001031 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 */
1033 public static final int FOCUS_LEFT = 0x00000011;
1034
1035 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001036 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 */
1038 public static final int FOCUS_UP = 0x00000021;
1039
1040 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001041 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
1043 public static final int FOCUS_RIGHT = 0x00000042;
1044
1045 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001046 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 */
1048 public static final int FOCUS_DOWN = 0x00000082;
1049
1050 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001051 * Bits of {@link #getMeasuredWidthAndState()} and
1052 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1053 */
1054 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1055
1056 /**
1057 * Bits of {@link #getMeasuredWidthAndState()} and
1058 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1059 */
1060 public static final int MEASURED_STATE_MASK = 0xff000000;
1061
1062 /**
1063 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1064 * for functions that combine both width and height into a single int,
1065 * such as {@link #getMeasuredState()} and the childState argument of
1066 * {@link #resolveSizeAndState(int, int, int)}.
1067 */
1068 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1069
1070 /**
1071 * Bit of {@link #getMeasuredWidthAndState()} and
1072 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1073 * is smaller that the space the view would like to have.
1074 */
1075 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1076
1077 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 * Base View state sets
1079 */
1080 // Singles
1081 /**
1082 * Indicates the view has no states set. States are used with
1083 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1084 * view depending on its state.
1085 *
1086 * @see android.graphics.drawable.Drawable
1087 * @see #getDrawableState()
1088 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001089 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 /**
1091 * Indicates the view is enabled. States are used with
1092 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1093 * view depending on its state.
1094 *
1095 * @see android.graphics.drawable.Drawable
1096 * @see #getDrawableState()
1097 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001098 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 /**
1100 * Indicates the view is focused. States are used with
1101 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1102 * view depending on its state.
1103 *
1104 * @see android.graphics.drawable.Drawable
1105 * @see #getDrawableState()
1106 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001107 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 /**
1109 * Indicates the view is selected. States are used with
1110 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1111 * view depending on its state.
1112 *
1113 * @see android.graphics.drawable.Drawable
1114 * @see #getDrawableState()
1115 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001116 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 /**
1118 * Indicates the view is pressed. States are used with
1119 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1120 * view depending on its state.
1121 *
1122 * @see android.graphics.drawable.Drawable
1123 * @see #getDrawableState()
1124 * @hide
1125 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001126 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 /**
1128 * Indicates the view's window has focus. States are used with
1129 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1130 * view depending on its state.
1131 *
1132 * @see android.graphics.drawable.Drawable
1133 * @see #getDrawableState()
1134 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001135 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 // Doubles
1137 /**
1138 * Indicates the view is enabled and has the focus.
1139 *
1140 * @see #ENABLED_STATE_SET
1141 * @see #FOCUSED_STATE_SET
1142 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001143 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 /**
1145 * Indicates the view is enabled and selected.
1146 *
1147 * @see #ENABLED_STATE_SET
1148 * @see #SELECTED_STATE_SET
1149 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001150 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 /**
1152 * Indicates the view is enabled and that its window has focus.
1153 *
1154 * @see #ENABLED_STATE_SET
1155 * @see #WINDOW_FOCUSED_STATE_SET
1156 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001157 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 /**
1159 * Indicates the view is focused and selected.
1160 *
1161 * @see #FOCUSED_STATE_SET
1162 * @see #SELECTED_STATE_SET
1163 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001164 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 /**
1166 * Indicates the view has the focus and that its window has the focus.
1167 *
1168 * @see #FOCUSED_STATE_SET
1169 * @see #WINDOW_FOCUSED_STATE_SET
1170 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001171 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 /**
1173 * Indicates the view is selected and that its window has the focus.
1174 *
1175 * @see #SELECTED_STATE_SET
1176 * @see #WINDOW_FOCUSED_STATE_SET
1177 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001178 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 // Triples
1180 /**
1181 * Indicates the view is enabled, focused and selected.
1182 *
1183 * @see #ENABLED_STATE_SET
1184 * @see #FOCUSED_STATE_SET
1185 * @see #SELECTED_STATE_SET
1186 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001187 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 /**
1189 * Indicates the view is enabled, focused and its window has the focus.
1190 *
1191 * @see #ENABLED_STATE_SET
1192 * @see #FOCUSED_STATE_SET
1193 * @see #WINDOW_FOCUSED_STATE_SET
1194 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001195 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 /**
1197 * Indicates the view is enabled, selected and its window has the focus.
1198 *
1199 * @see #ENABLED_STATE_SET
1200 * @see #SELECTED_STATE_SET
1201 * @see #WINDOW_FOCUSED_STATE_SET
1202 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001203 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 /**
1205 * Indicates the view is focused, selected and its window has the focus.
1206 *
1207 * @see #FOCUSED_STATE_SET
1208 * @see #SELECTED_STATE_SET
1209 * @see #WINDOW_FOCUSED_STATE_SET
1210 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001211 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 /**
1213 * Indicates the view is enabled, focused, selected and its window
1214 * has the focus.
1215 *
1216 * @see #ENABLED_STATE_SET
1217 * @see #FOCUSED_STATE_SET
1218 * @see #SELECTED_STATE_SET
1219 * @see #WINDOW_FOCUSED_STATE_SET
1220 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001221 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 /**
1223 * Indicates the view is pressed and its window has the focus.
1224 *
1225 * @see #PRESSED_STATE_SET
1226 * @see #WINDOW_FOCUSED_STATE_SET
1227 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001228 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 /**
1230 * Indicates the view is pressed and selected.
1231 *
1232 * @see #PRESSED_STATE_SET
1233 * @see #SELECTED_STATE_SET
1234 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001235 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 /**
1237 * Indicates the view is pressed, selected and its window has the focus.
1238 *
1239 * @see #PRESSED_STATE_SET
1240 * @see #SELECTED_STATE_SET
1241 * @see #WINDOW_FOCUSED_STATE_SET
1242 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001243 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 /**
1245 * Indicates the view is pressed and focused.
1246 *
1247 * @see #PRESSED_STATE_SET
1248 * @see #FOCUSED_STATE_SET
1249 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001250 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 /**
1252 * Indicates the view is pressed, focused and its window has the focus.
1253 *
1254 * @see #PRESSED_STATE_SET
1255 * @see #FOCUSED_STATE_SET
1256 * @see #WINDOW_FOCUSED_STATE_SET
1257 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001258 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 /**
1260 * Indicates the view is pressed, focused and selected.
1261 *
1262 * @see #PRESSED_STATE_SET
1263 * @see #SELECTED_STATE_SET
1264 * @see #FOCUSED_STATE_SET
1265 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001266 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 /**
1268 * Indicates the view is pressed, focused, selected and its window has the focus.
1269 *
1270 * @see #PRESSED_STATE_SET
1271 * @see #FOCUSED_STATE_SET
1272 * @see #SELECTED_STATE_SET
1273 * @see #WINDOW_FOCUSED_STATE_SET
1274 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001275 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 /**
1277 * Indicates the view is pressed and enabled.
1278 *
1279 * @see #PRESSED_STATE_SET
1280 * @see #ENABLED_STATE_SET
1281 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001282 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 /**
1284 * Indicates the view is pressed, enabled and its window has the focus.
1285 *
1286 * @see #PRESSED_STATE_SET
1287 * @see #ENABLED_STATE_SET
1288 * @see #WINDOW_FOCUSED_STATE_SET
1289 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001290 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 /**
1292 * Indicates the view is pressed, enabled and selected.
1293 *
1294 * @see #PRESSED_STATE_SET
1295 * @see #ENABLED_STATE_SET
1296 * @see #SELECTED_STATE_SET
1297 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001298 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 /**
1300 * Indicates the view is pressed, enabled, selected and its window has the
1301 * focus.
1302 *
1303 * @see #PRESSED_STATE_SET
1304 * @see #ENABLED_STATE_SET
1305 * @see #SELECTED_STATE_SET
1306 * @see #WINDOW_FOCUSED_STATE_SET
1307 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001308 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 /**
1310 * Indicates the view is pressed, enabled and focused.
1311 *
1312 * @see #PRESSED_STATE_SET
1313 * @see #ENABLED_STATE_SET
1314 * @see #FOCUSED_STATE_SET
1315 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001316 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 /**
1318 * Indicates the view is pressed, enabled, focused and its window has the
1319 * focus.
1320 *
1321 * @see #PRESSED_STATE_SET
1322 * @see #ENABLED_STATE_SET
1323 * @see #FOCUSED_STATE_SET
1324 * @see #WINDOW_FOCUSED_STATE_SET
1325 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001326 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 /**
1328 * Indicates the view is pressed, enabled, focused and selected.
1329 *
1330 * @see #PRESSED_STATE_SET
1331 * @see #ENABLED_STATE_SET
1332 * @see #SELECTED_STATE_SET
1333 * @see #FOCUSED_STATE_SET
1334 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001335 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 /**
1337 * Indicates the view is pressed, enabled, focused, selected and its window
1338 * has the focus.
1339 *
1340 * @see #PRESSED_STATE_SET
1341 * @see #ENABLED_STATE_SET
1342 * @see #SELECTED_STATE_SET
1343 * @see #FOCUSED_STATE_SET
1344 * @see #WINDOW_FOCUSED_STATE_SET
1345 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001346 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347
1348 /**
1349 * The order here is very important to {@link #getDrawableState()}
1350 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001351 private static final int[][] VIEW_STATE_SETS;
1352
Romain Guyb051e892010-09-28 19:09:36 -07001353 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1354 static final int VIEW_STATE_SELECTED = 1 << 1;
1355 static final int VIEW_STATE_FOCUSED = 1 << 2;
1356 static final int VIEW_STATE_ENABLED = 1 << 3;
1357 static final int VIEW_STATE_PRESSED = 1 << 4;
1358 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001359 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001360 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001361 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1362 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001363
1364 static final int[] VIEW_STATE_IDS = new int[] {
1365 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1366 R.attr.state_selected, VIEW_STATE_SELECTED,
1367 R.attr.state_focused, VIEW_STATE_FOCUSED,
1368 R.attr.state_enabled, VIEW_STATE_ENABLED,
1369 R.attr.state_pressed, VIEW_STATE_PRESSED,
1370 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001371 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001372 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001373 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1374 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 };
1376
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001377 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001378 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1379 throw new IllegalStateException(
1380 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1381 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001382 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001383 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001385 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001386 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001387 orderedIds[i * 2] = viewState;
1388 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001389 }
1390 }
1391 }
Romain Guyb051e892010-09-28 19:09:36 -07001392 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1393 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1394 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001395 int numBits = Integer.bitCount(i);
1396 int[] set = new int[numBits];
1397 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001398 for (int j = 0; j < orderedIds.length; j += 2) {
1399 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001400 set[pos++] = orderedIds[j];
1401 }
1402 }
1403 VIEW_STATE_SETS[i] = set;
1404 }
1405
1406 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1407 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1408 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1409 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1410 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1411 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1412 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1413 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1414 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1416 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1418 | VIEW_STATE_FOCUSED];
1419 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1420 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1421 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1422 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1424 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1425 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1426 | VIEW_STATE_ENABLED];
1427 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1428 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1429 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1430 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1431 | VIEW_STATE_ENABLED];
1432 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1433 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1434 | VIEW_STATE_ENABLED];
1435 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1436 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1437 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1438
1439 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1440 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1441 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1442 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1444 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1445 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1446 | VIEW_STATE_PRESSED];
1447 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1449 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1451 | VIEW_STATE_PRESSED];
1452 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1453 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1454 | VIEW_STATE_PRESSED];
1455 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1457 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1458 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1459 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1460 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1462 | VIEW_STATE_PRESSED];
1463 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1464 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1465 | VIEW_STATE_PRESSED];
1466 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1467 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1468 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1469 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1470 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1471 | VIEW_STATE_PRESSED];
1472 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1473 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1474 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1475 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1476 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1477 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1478 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1479 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1480 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1481 | VIEW_STATE_PRESSED];
1482 }
1483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 * Temporary Rect currently for use in setBackground(). This will probably
1486 * be extended in the future to hold our own class with more than just
1487 * a Rect. :)
1488 */
1489 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001490
1491 /**
1492 * Map used to store views' tags.
1493 */
1494 private static WeakHashMap<View, SparseArray<Object>> sTags;
1495
1496 /**
1497 * Lock used to access sTags.
1498 */
1499 private static final Object sTagsLock = new Object();
1500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001502 * The next available accessiiblity id.
1503 */
1504 private static int sNextAccessibilityViewId;
1505
1506 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 * The animation currently associated with this view.
1508 * @hide
1509 */
1510 protected Animation mCurrentAnimation = null;
1511
1512 /**
1513 * Width as measured during measure pass.
1514 * {@hide}
1515 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001516 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001517 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518
1519 /**
1520 * Height as measured during measure pass.
1521 * {@hide}
1522 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001523 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001524 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525
1526 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001527 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1528 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1529 * its display list. This flag, used only when hw accelerated, allows us to clear the
1530 * flag while retaining this information until it's needed (at getDisplayList() time and
1531 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1532 *
1533 * {@hide}
1534 */
1535 boolean mRecreateDisplayList = false;
1536
1537 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 * The view's identifier.
1539 * {@hide}
1540 *
1541 * @see #setId(int)
1542 * @see #getId()
1543 */
1544 @ViewDebug.ExportedProperty(resolveId = true)
1545 int mID = NO_ID;
1546
1547 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001548 * The stable ID of this view for accessibility porposes.
1549 */
1550 int mAccessibilityViewId = NO_ID;
1551
1552 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 * The view's tag.
1554 * {@hide}
1555 *
1556 * @see #setTag(Object)
1557 * @see #getTag()
1558 */
1559 protected Object mTag;
1560
1561 // for mPrivateFlags:
1562 /** {@hide} */
1563 static final int WANTS_FOCUS = 0x00000001;
1564 /** {@hide} */
1565 static final int FOCUSED = 0x00000002;
1566 /** {@hide} */
1567 static final int SELECTED = 0x00000004;
1568 /** {@hide} */
1569 static final int IS_ROOT_NAMESPACE = 0x00000008;
1570 /** {@hide} */
1571 static final int HAS_BOUNDS = 0x00000010;
1572 /** {@hide} */
1573 static final int DRAWN = 0x00000020;
1574 /**
1575 * When this flag is set, this view is running an animation on behalf of its
1576 * children and should therefore not cancel invalidate requests, even if they
1577 * lie outside of this view's bounds.
1578 *
1579 * {@hide}
1580 */
1581 static final int DRAW_ANIMATION = 0x00000040;
1582 /** {@hide} */
1583 static final int SKIP_DRAW = 0x00000080;
1584 /** {@hide} */
1585 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1586 /** {@hide} */
1587 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1588 /** {@hide} */
1589 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1590 /** {@hide} */
1591 static final int MEASURED_DIMENSION_SET = 0x00000800;
1592 /** {@hide} */
1593 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001594 /** {@hide} */
1595 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596
1597 private static final int PRESSED = 0x00004000;
1598
1599 /** {@hide} */
1600 static final int DRAWING_CACHE_VALID = 0x00008000;
1601 /**
1602 * Flag used to indicate that this view should be drawn once more (and only once
1603 * more) after its animation has completed.
1604 * {@hide}
1605 */
1606 static final int ANIMATION_STARTED = 0x00010000;
1607
1608 private static final int SAVE_STATE_CALLED = 0x00020000;
1609
1610 /**
1611 * Indicates that the View returned true when onSetAlpha() was called and that
1612 * the alpha must be restored.
1613 * {@hide}
1614 */
1615 static final int ALPHA_SET = 0x00040000;
1616
1617 /**
1618 * Set by {@link #setScrollContainer(boolean)}.
1619 */
1620 static final int SCROLL_CONTAINER = 0x00080000;
1621
1622 /**
1623 * Set by {@link #setScrollContainer(boolean)}.
1624 */
1625 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1626
1627 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001628 * View flag indicating whether this view was invalidated (fully or partially.)
1629 *
1630 * @hide
1631 */
1632 static final int DIRTY = 0x00200000;
1633
1634 /**
1635 * View flag indicating whether this view was invalidated by an opaque
1636 * invalidate request.
1637 *
1638 * @hide
1639 */
1640 static final int DIRTY_OPAQUE = 0x00400000;
1641
1642 /**
1643 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1644 *
1645 * @hide
1646 */
1647 static final int DIRTY_MASK = 0x00600000;
1648
1649 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001650 * Indicates whether the background is opaque.
1651 *
1652 * @hide
1653 */
1654 static final int OPAQUE_BACKGROUND = 0x00800000;
1655
1656 /**
1657 * Indicates whether the scrollbars are opaque.
1658 *
1659 * @hide
1660 */
1661 static final int OPAQUE_SCROLLBARS = 0x01000000;
1662
1663 /**
1664 * Indicates whether the view is opaque.
1665 *
1666 * @hide
1667 */
1668 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001669
Adam Powelle14579b2009-12-16 18:39:52 -08001670 /**
1671 * Indicates a prepressed state;
1672 * the short time between ACTION_DOWN and recognizing
1673 * a 'real' press. Prepressed is used to recognize quick taps
1674 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001675 *
Adam Powelle14579b2009-12-16 18:39:52 -08001676 * @hide
1677 */
1678 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001679
Adam Powellc9fbaab2010-02-16 17:16:19 -08001680 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001681 * Indicates whether the view is temporarily detached.
1682 *
1683 * @hide
1684 */
1685 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001686
Adam Powell8568c3a2010-04-19 14:26:11 -07001687 /**
1688 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001689 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001690 * @hide
1691 */
1692 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001693
1694 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001695 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1696 * @hide
1697 */
1698 private static final int HOVERED = 0x10000000;
1699
1700 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001701 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1702 * for transform operations
1703 *
1704 * @hide
1705 */
Adam Powellf37df072010-09-17 16:22:49 -07001706 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001707
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001708 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001709 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001710
Chet Haasefd2b0022010-08-06 13:08:56 -07001711 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001712 * Indicates that this view was specifically invalidated, not just dirtied because some
1713 * child view was invalidated. The flag is used to determine when we need to recreate
1714 * a view's display list (as opposed to just returning a reference to its existing
1715 * display list).
1716 *
1717 * @hide
1718 */
1719 static final int INVALIDATED = 0x80000000;
1720
Christopher Tate3d4bf172011-03-28 16:16:46 -07001721 /* Masks for mPrivateFlags2 */
1722
1723 /**
1724 * Indicates that this view has reported that it can accept the current drag's content.
1725 * Cleared when the drag operation concludes.
1726 * @hide
1727 */
1728 static final int DRAG_CAN_ACCEPT = 0x00000001;
1729
1730 /**
1731 * Indicates that this view is currently directly under the drag location in a
1732 * drag-and-drop operation involving content that it can accept. Cleared when
1733 * the drag exits the view, or when the drag operation concludes.
1734 * @hide
1735 */
1736 static final int DRAG_HOVERED = 0x00000002;
1737
Cibu Johny86666632010-02-22 13:01:02 -08001738 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001739 * Indicates whether the view layout direction has been resolved and drawn to the
1740 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001741 *
1742 * @hide
1743 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001744 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1745
1746 /**
1747 * Indicates whether the view layout direction has been resolved.
1748 *
1749 * @hide
1750 */
1751 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1752
Cibu Johny86666632010-02-22 13:01:02 -08001753
Christopher Tate3d4bf172011-03-28 16:16:46 -07001754 /* End of masks for mPrivateFlags2 */
1755
1756 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1757
Chet Haasedaf98e92011-01-10 14:10:36 -08001758 /**
Adam Powell637d3372010-08-25 14:37:03 -07001759 * Always allow a user to over-scroll this view, provided it is a
1760 * view that can scroll.
1761 *
1762 * @see #getOverScrollMode()
1763 * @see #setOverScrollMode(int)
1764 */
1765 public static final int OVER_SCROLL_ALWAYS = 0;
1766
1767 /**
1768 * Allow a user to over-scroll this view only if the content is large
1769 * enough to meaningfully scroll, provided it is a view that can scroll.
1770 *
1771 * @see #getOverScrollMode()
1772 * @see #setOverScrollMode(int)
1773 */
1774 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1775
1776 /**
1777 * Never allow a user to over-scroll this view.
1778 *
1779 * @see #getOverScrollMode()
1780 * @see #setOverScrollMode(int)
1781 */
1782 public static final int OVER_SCROLL_NEVER = 2;
1783
1784 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001785 * View has requested the status bar to be visible (the default).
1786 *
Joe Malin32736f02011-01-19 16:14:20 -08001787 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001788 */
1789 public static final int STATUS_BAR_VISIBLE = 0;
1790
1791 /**
Jeff Brown05dc66a2011-03-02 14:41:58 -08001792 * View has requested the status bar to be hidden.
Joe Onorato664644d2011-01-23 17:53:23 -08001793 *
Joe Malin32736f02011-01-19 16:14:20 -08001794 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001795 */
1796 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1797
1798 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001799 * @hide
1800 *
1801 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1802 * out of the public fields to keep the undefined bits out of the developer's way.
1803 *
1804 * Flag to make the status bar not expandable. Unless you also
1805 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1806 */
1807 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1808
1809 /**
1810 * @hide
1811 *
1812 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1813 * out of the public fields to keep the undefined bits out of the developer's way.
1814 *
1815 * Flag to hide notification icons and scrolling ticker text.
1816 */
1817 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1818
1819 /**
1820 * @hide
1821 *
1822 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1823 * out of the public fields to keep the undefined bits out of the developer's way.
1824 *
1825 * Flag to disable incoming notification alerts. This will not block
1826 * icons, but it will block sound, vibrating and other visual or aural notifications.
1827 */
1828 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1829
1830 /**
1831 * @hide
1832 *
1833 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1834 * out of the public fields to keep the undefined bits out of the developer's way.
1835 *
1836 * Flag to hide only the scrolling ticker. Note that
1837 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1838 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1839 */
1840 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1841
1842 /**
1843 * @hide
1844 *
1845 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1846 * out of the public fields to keep the undefined bits out of the developer's way.
1847 *
1848 * Flag to hide the center system info area.
1849 */
1850 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1851
1852 /**
1853 * @hide
1854 *
1855 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1856 * out of the public fields to keep the undefined bits out of the developer's way.
1857 *
1858 * Flag to hide only the navigation buttons. Don't use this
1859 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001860 *
1861 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001862 */
1863 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1864
1865 /**
1866 * @hide
1867 *
1868 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1869 * out of the public fields to keep the undefined bits out of the developer's way.
1870 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001871 * Flag to hide only the back button. Don't use this
1872 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1873 */
1874 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1875
1876 /**
1877 * @hide
1878 *
1879 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1880 * out of the public fields to keep the undefined bits out of the developer's way.
1881 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001882 * Flag to hide only the clock. You might use this if your activity has
1883 * its own clock making the status bar's clock redundant.
1884 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001885 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1886
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001887 /**
1888 * @hide
1889 */
1890 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001891
1892 /**
Adam Powell637d3372010-08-25 14:37:03 -07001893 * Controls the over-scroll mode for this view.
1894 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1895 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1896 * and {@link #OVER_SCROLL_NEVER}.
1897 */
1898 private int mOverScrollMode;
1899
1900 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 * The parent this view is attached to.
1902 * {@hide}
1903 *
1904 * @see #getParent()
1905 */
1906 protected ViewParent mParent;
1907
1908 /**
1909 * {@hide}
1910 */
1911 AttachInfo mAttachInfo;
1912
1913 /**
1914 * {@hide}
1915 */
Romain Guy809a7f62009-05-14 15:44:42 -07001916 @ViewDebug.ExportedProperty(flagMapping = {
1917 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1918 name = "FORCE_LAYOUT"),
1919 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1920 name = "LAYOUT_REQUIRED"),
1921 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001922 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001923 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1924 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1925 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1926 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1927 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001929 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930
1931 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001932 * This view's request for the visibility of the status bar.
1933 * @hide
1934 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001935 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001936 int mSystemUiVisibility;
1937
1938 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 * Count of how many windows this view has been attached to.
1940 */
1941 int mWindowAttachCount;
1942
1943 /**
1944 * The layout parameters associated with this view and used by the parent
1945 * {@link android.view.ViewGroup} to determine how this view should be
1946 * laid out.
1947 * {@hide}
1948 */
1949 protected ViewGroup.LayoutParams mLayoutParams;
1950
1951 /**
1952 * The view flags hold various views states.
1953 * {@hide}
1954 */
1955 @ViewDebug.ExportedProperty
1956 int mViewFlags;
1957
1958 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001959 * The transform matrix for the View. This transform is calculated internally
1960 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1961 * is used by default. Do *not* use this variable directly; instead call
1962 * getMatrix(), which will automatically recalculate the matrix if necessary
1963 * to get the correct matrix based on the latest rotation and scale properties.
1964 */
1965 private final Matrix mMatrix = new Matrix();
1966
1967 /**
1968 * The transform matrix for the View. This transform is calculated internally
1969 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1970 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001971 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001972 * to get the correct matrix based on the latest rotation and scale properties.
1973 */
1974 private Matrix mInverseMatrix;
1975
1976 /**
1977 * An internal variable that tracks whether we need to recalculate the
1978 * transform matrix, based on whether the rotation or scaleX/Y properties
1979 * have changed since the matrix was last calculated.
1980 */
Chet Haasea00f3862011-02-22 06:34:40 -08001981 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07001982
1983 /**
1984 * An internal variable that tracks whether we need to recalculate the
1985 * transform matrix, based on whether the rotation or scaleX/Y properties
1986 * have changed since the matrix was last calculated.
1987 */
1988 private boolean mInverseMatrixDirty = true;
1989
1990 /**
1991 * A variable that tracks whether we need to recalculate the
1992 * transform matrix, based on whether the rotation or scaleX/Y properties
1993 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001994 * is only valid after a call to updateMatrix() or to a function that
1995 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001996 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001997 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001998
1999 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07002000 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2001 */
2002 private Camera mCamera = null;
2003
2004 /**
2005 * This matrix is used when computing the matrix for 3D rotations.
2006 */
2007 private Matrix matrix3D = null;
2008
2009 /**
2010 * These prev values are used to recalculate a centered pivot point when necessary. The
2011 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2012 * set), so thes values are only used then as well.
2013 */
2014 private int mPrevWidth = -1;
2015 private int mPrevHeight = -1;
2016
Joe Malin32736f02011-01-19 16:14:20 -08002017 private boolean mLastIsOpaque;
2018
Chet Haasefd2b0022010-08-06 13:08:56 -07002019 /**
2020 * Convenience value to check for float values that are close enough to zero to be considered
2021 * zero.
2022 */
Romain Guy2542d192010-08-18 11:47:12 -07002023 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002024
2025 /**
2026 * The degrees rotation around the vertical axis through the pivot point.
2027 */
2028 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002029 float mRotationY = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002030
2031 /**
2032 * The degrees rotation around the horizontal axis through the pivot point.
2033 */
2034 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002035 float mRotationX = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002036
2037 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002038 * The degrees rotation around the pivot point.
2039 */
2040 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002041 float mRotation = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002042
2043 /**
Chet Haasedf030d22010-07-30 17:22:38 -07002044 * The amount of translation of the object away from its left property (post-layout).
2045 */
2046 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002047 float mTranslationX = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002048
2049 /**
2050 * The amount of translation of the object away from its top property (post-layout).
2051 */
2052 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002053 float mTranslationY = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002054
2055 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002056 * The amount of scale in the x direction around the pivot point. A
2057 * value of 1 means no scaling is applied.
2058 */
2059 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002060 float mScaleX = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002061
2062 /**
2063 * The amount of scale in the y direction around the pivot point. A
2064 * value of 1 means no scaling is applied.
2065 */
2066 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002067 float mScaleY = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002068
2069 /**
2070 * The amount of scale in the x direction around the pivot point. A
2071 * value of 1 means no scaling is applied.
2072 */
2073 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002074 float mPivotX = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002075
2076 /**
2077 * The amount of scale in the y direction around the pivot point. A
2078 * value of 1 means no scaling is applied.
2079 */
2080 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002081 float mPivotY = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002082
2083 /**
2084 * The opacity of the View. This is a value from 0 to 1, where 0 means
2085 * completely transparent and 1 means completely opaque.
2086 */
2087 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002088 float mAlpha = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002089
2090 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 * The distance in pixels from the left edge of this view's parent
2092 * to the left edge of this view.
2093 * {@hide}
2094 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002095 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 protected int mLeft;
2097 /**
2098 * The distance in pixels from the left edge of this view's parent
2099 * to the right edge of this view.
2100 * {@hide}
2101 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002102 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 protected int mRight;
2104 /**
2105 * The distance in pixels from the top edge of this view's parent
2106 * to the top edge of this view.
2107 * {@hide}
2108 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002109 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 protected int mTop;
2111 /**
2112 * The distance in pixels from the top edge of this view's parent
2113 * to the bottom edge of this view.
2114 * {@hide}
2115 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002116 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 protected int mBottom;
2118
2119 /**
2120 * The offset, in pixels, by which the content of this view is scrolled
2121 * horizontally.
2122 * {@hide}
2123 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002124 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 protected int mScrollX;
2126 /**
2127 * The offset, in pixels, by which the content of this view is scrolled
2128 * vertically.
2129 * {@hide}
2130 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002131 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 protected int mScrollY;
2133
2134 /**
2135 * The left padding in pixels, that is the distance in pixels between the
2136 * left edge of this view and the left edge of its content.
2137 * {@hide}
2138 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002139 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 protected int mPaddingLeft;
2141 /**
2142 * The right padding in pixels, that is the distance in pixels between the
2143 * right edge of this view and the right edge of its content.
2144 * {@hide}
2145 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002146 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 protected int mPaddingRight;
2148 /**
2149 * The top padding in pixels, that is the distance in pixels between the
2150 * top edge of this view and the top edge of its content.
2151 * {@hide}
2152 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002153 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 protected int mPaddingTop;
2155 /**
2156 * The bottom padding in pixels, that is the distance in pixels between the
2157 * bottom edge of this view and the bottom edge of its content.
2158 * {@hide}
2159 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002160 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 protected int mPaddingBottom;
2162
2163 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002164 * Briefly describes the view and is primarily used for accessibility support.
2165 */
2166 private CharSequence mContentDescription;
2167
2168 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 * Cache the paddingRight set by the user to append to the scrollbar's size.
2170 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002171 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 int mUserPaddingRight;
2173
2174 /**
2175 * Cache the paddingBottom set by the user to append to the scrollbar's size.
2176 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002177 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 int mUserPaddingBottom;
2179
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002180 /**
Adam Powell20232d02010-12-08 21:08:53 -08002181 * Cache the paddingLeft set by the user to append to the scrollbar's size.
2182 */
2183 @ViewDebug.ExportedProperty(category = "padding")
2184 int mUserPaddingLeft;
2185
2186 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002187 * Cache if the user padding is relative.
2188 *
2189 */
2190 @ViewDebug.ExportedProperty(category = "padding")
2191 boolean mUserPaddingRelative;
2192
2193 /**
2194 * Cache the paddingStart set by the user to append to the scrollbar's size.
2195 *
2196 */
2197 @ViewDebug.ExportedProperty(category = "padding")
2198 int mUserPaddingStart;
2199
2200 /**
2201 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2202 *
2203 */
2204 @ViewDebug.ExportedProperty(category = "padding")
2205 int mUserPaddingEnd;
2206
2207 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002208 * @hide
2209 */
2210 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2211 /**
2212 * @hide
2213 */
2214 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215
2216 private Resources mResources = null;
2217
2218 private Drawable mBGDrawable;
2219
2220 private int mBackgroundResource;
2221 private boolean mBackgroundSizeChanged;
2222
2223 /**
2224 * Listener used to dispatch focus change events.
2225 * This field should be made private, so it is hidden from the SDK.
2226 * {@hide}
2227 */
2228 protected OnFocusChangeListener mOnFocusChangeListener;
2229
2230 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002231 * Listeners for layout change events.
2232 */
2233 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2234
2235 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002236 * Listeners for attach events.
2237 */
2238 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2239
2240 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 * Listener used to dispatch click events.
2242 * This field should be made private, so it is hidden from the SDK.
2243 * {@hide}
2244 */
2245 protected OnClickListener mOnClickListener;
2246
2247 /**
2248 * Listener used to dispatch long click events.
2249 * This field should be made private, so it is hidden from the SDK.
2250 * {@hide}
2251 */
2252 protected OnLongClickListener mOnLongClickListener;
2253
2254 /**
2255 * Listener used to build the context menu.
2256 * This field should be made private, so it is hidden from the SDK.
2257 * {@hide}
2258 */
2259 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2260
2261 private OnKeyListener mOnKeyListener;
2262
2263 private OnTouchListener mOnTouchListener;
2264
Jeff Brown33bbfd22011-02-24 20:55:35 -08002265 private OnGenericMotionListener mOnGenericMotionListener;
2266
Chris Tate32affef2010-10-18 15:29:21 -07002267 private OnDragListener mOnDragListener;
2268
Joe Onorato664644d2011-01-23 17:53:23 -08002269 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 /**
2272 * The application environment this view lives in.
2273 * This field should be made private, so it is hidden from the SDK.
2274 * {@hide}
2275 */
2276 protected Context mContext;
2277
2278 private ScrollabilityCache mScrollCache;
2279
2280 private int[] mDrawableState = null;
2281
Romain Guy0211a0a2011-02-14 16:34:59 -08002282 /**
2283 * Set to true when drawing cache is enabled and cannot be created.
2284 *
2285 * @hide
2286 */
2287 public boolean mCachingFailed;
2288
Romain Guy02890fd2010-08-06 17:58:44 -07002289 private Bitmap mDrawingCache;
2290 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002291 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002292 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293
2294 /**
2295 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2296 * the user may specify which view to go to next.
2297 */
2298 private int mNextFocusLeftId = View.NO_ID;
2299
2300 /**
2301 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2302 * the user may specify which view to go to next.
2303 */
2304 private int mNextFocusRightId = View.NO_ID;
2305
2306 /**
2307 * When this view has focus and the next focus is {@link #FOCUS_UP},
2308 * the user may specify which view to go to next.
2309 */
2310 private int mNextFocusUpId = View.NO_ID;
2311
2312 /**
2313 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2314 * the user may specify which view to go to next.
2315 */
2316 private int mNextFocusDownId = View.NO_ID;
2317
Jeff Brown4e6319b2010-12-13 10:36:51 -08002318 /**
2319 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2320 * the user may specify which view to go to next.
2321 */
2322 int mNextFocusForwardId = View.NO_ID;
2323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002325 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002326 private PerformClick mPerformClick;
Joe Malin32736f02011-01-19 16:14:20 -08002327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 private UnsetPressedState mUnsetPressedState;
2329
2330 /**
2331 * Whether the long press's action has been invoked. The tap's action is invoked on the
2332 * up event while a long press is invoked as soon as the long press duration is reached, so
2333 * a long press could be performed before the tap is checked, in which case the tap's action
2334 * should not be invoked.
2335 */
2336 private boolean mHasPerformedLongPress;
2337
2338 /**
2339 * The minimum height of the view. We'll try our best to have the height
2340 * of this view to at least this amount.
2341 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002342 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 private int mMinHeight;
2344
2345 /**
2346 * The minimum width of the view. We'll try our best to have the width
2347 * of this view to at least this amount.
2348 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002349 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 private int mMinWidth;
2351
2352 /**
2353 * The delegate to handle touch events that are physically in this view
2354 * but should be handled by another view.
2355 */
2356 private TouchDelegate mTouchDelegate = null;
2357
2358 /**
2359 * Solid color to use as a background when creating the drawing cache. Enables
2360 * the cache to use 16 bit bitmaps instead of 32 bit.
2361 */
2362 private int mDrawingCacheBackgroundColor = 0;
2363
2364 /**
2365 * Special tree observer used when mAttachInfo is null.
2366 */
2367 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002368
Adam Powelle14579b2009-12-16 18:39:52 -08002369 /**
2370 * Cache the touch slop from the context that created the view.
2371 */
2372 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002375 * Object that handles automatic animation of view properties.
2376 */
2377 private ViewPropertyAnimator mAnimator = null;
2378
2379 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002380 * Flag indicating that a drag can cross window boundaries. When
2381 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2382 * with this flag set, all visible applications will be able to participate
2383 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002384 *
2385 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002386 */
2387 public static final int DRAG_FLAG_GLOBAL = 1;
2388
2389 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002390 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2391 */
2392 private float mVerticalScrollFactor;
2393
2394 /**
Adam Powell20232d02010-12-08 21:08:53 -08002395 * Position of the vertical scroll bar.
2396 */
2397 private int mVerticalScrollbarPosition;
2398
2399 /**
2400 * Position the scroll bar at the default position as determined by the system.
2401 */
2402 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2403
2404 /**
2405 * Position the scroll bar along the left edge.
2406 */
2407 public static final int SCROLLBAR_POSITION_LEFT = 1;
2408
2409 /**
2410 * Position the scroll bar along the right edge.
2411 */
2412 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2413
2414 /**
Romain Guy171c5922011-01-06 10:04:23 -08002415 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002416 *
2417 * @see #getLayerType()
2418 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002419 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002420 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002421 */
2422 public static final int LAYER_TYPE_NONE = 0;
2423
2424 /**
2425 * <p>Indicates that the view has a software layer. A software layer is backed
2426 * by a bitmap and causes the view to be rendered using Android's software
2427 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002428 *
Romain Guy171c5922011-01-06 10:04:23 -08002429 * <p>Software layers have various usages:</p>
2430 * <p>When the application is not using hardware acceleration, a software layer
2431 * is useful to apply a specific color filter and/or blending mode and/or
2432 * translucency to a view and all its children.</p>
2433 * <p>When the application is using hardware acceleration, a software layer
2434 * is useful to render drawing primitives not supported by the hardware
2435 * accelerated pipeline. It can also be used to cache a complex view tree
2436 * into a texture and reduce the complexity of drawing operations. For instance,
2437 * when animating a complex view tree with a translation, a software layer can
2438 * be used to render the view tree only once.</p>
2439 * <p>Software layers should be avoided when the affected view tree updates
2440 * often. Every update will require to re-render the software layer, which can
2441 * potentially be slow (particularly when hardware acceleration is turned on
2442 * since the layer will have to be uploaded into a hardware texture after every
2443 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002444 *
2445 * @see #getLayerType()
2446 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002447 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002448 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002449 */
2450 public static final int LAYER_TYPE_SOFTWARE = 1;
2451
2452 /**
2453 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2454 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2455 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2456 * rendering pipeline, but only if hardware acceleration is turned on for the
2457 * view hierarchy. When hardware acceleration is turned off, hardware layers
2458 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002459 *
Romain Guy171c5922011-01-06 10:04:23 -08002460 * <p>A hardware layer is useful to apply a specific color filter and/or
2461 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002462 * <p>A hardware layer can be used to cache a complex view tree into a
2463 * texture and reduce the complexity of drawing operations. For instance,
2464 * when animating a complex view tree with a translation, a hardware layer can
2465 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002466 * <p>A hardware layer can also be used to increase the rendering quality when
2467 * rotation transformations are applied on a view. It can also be used to
2468 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002469 *
2470 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002471 * @see #setLayerType(int, android.graphics.Paint)
2472 * @see #LAYER_TYPE_NONE
2473 * @see #LAYER_TYPE_SOFTWARE
2474 */
2475 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002476
Romain Guy3aaff3a2011-01-12 14:18:47 -08002477 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2478 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2479 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2480 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2481 })
Romain Guy171c5922011-01-06 10:04:23 -08002482 int mLayerType = LAYER_TYPE_NONE;
2483 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002484 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002485
2486 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002487 * Consistency verifier for debugging purposes.
2488 * @hide
2489 */
2490 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2491 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2492 new InputEventConsistencyVerifier(this, 0) : null;
2493
2494 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 * Simple constructor to use when creating a view from code.
2496 *
2497 * @param context The Context the view is running in, through which it can
2498 * access the current theme, resources, etc.
2499 */
2500 public View(Context context) {
2501 mContext = context;
2502 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002503 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002504 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002505 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002506 mUserPaddingStart = -1;
2507 mUserPaddingEnd = -1;
2508 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 }
2510
2511 /**
2512 * Constructor that is called when inflating a view from XML. This is called
2513 * when a view is being constructed from an XML file, supplying attributes
2514 * that were specified in the XML file. This version uses a default style of
2515 * 0, so the only attribute values applied are those in the Context's Theme
2516 * and the given AttributeSet.
2517 *
2518 * <p>
2519 * The method onFinishInflate() will be called after all children have been
2520 * added.
2521 *
2522 * @param context The Context the view is running in, through which it can
2523 * access the current theme, resources, etc.
2524 * @param attrs The attributes of the XML tag that is inflating the view.
2525 * @see #View(Context, AttributeSet, int)
2526 */
2527 public View(Context context, AttributeSet attrs) {
2528 this(context, attrs, 0);
2529 }
2530
2531 /**
2532 * Perform inflation from XML and apply a class-specific base style. This
2533 * constructor of View allows subclasses to use their own base style when
2534 * they are inflating. For example, a Button class's constructor would call
2535 * this version of the super class constructor and supply
2536 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2537 * the theme's button style to modify all of the base view attributes (in
2538 * particular its background) as well as the Button class's attributes.
2539 *
2540 * @param context The Context the view is running in, through which it can
2541 * access the current theme, resources, etc.
2542 * @param attrs The attributes of the XML tag that is inflating the view.
2543 * @param defStyle The default style to apply to this view. If 0, no style
2544 * will be applied (beyond what is included in the theme). This may
2545 * either be an attribute resource, whose value will be retrieved
2546 * from the current theme, or an explicit style resource.
2547 * @see #View(Context, AttributeSet)
2548 */
2549 public View(Context context, AttributeSet attrs, int defStyle) {
2550 this(context);
2551
2552 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2553 defStyle, 0);
2554
2555 Drawable background = null;
2556
2557 int leftPadding = -1;
2558 int topPadding = -1;
2559 int rightPadding = -1;
2560 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002561 int startPadding = -1;
2562 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563
2564 int padding = -1;
2565
2566 int viewFlagValues = 0;
2567 int viewFlagMasks = 0;
2568
2569 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 int x = 0;
2572 int y = 0;
2573
Chet Haase73066682010-11-29 15:55:32 -08002574 float tx = 0;
2575 float ty = 0;
2576 float rotation = 0;
2577 float rotationX = 0;
2578 float rotationY = 0;
2579 float sx = 1f;
2580 float sy = 1f;
2581 boolean transformSet = false;
2582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2584
Adam Powell637d3372010-08-25 14:37:03 -07002585 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 final int N = a.getIndexCount();
2587 for (int i = 0; i < N; i++) {
2588 int attr = a.getIndex(i);
2589 switch (attr) {
2590 case com.android.internal.R.styleable.View_background:
2591 background = a.getDrawable(attr);
2592 break;
2593 case com.android.internal.R.styleable.View_padding:
2594 padding = a.getDimensionPixelSize(attr, -1);
2595 break;
2596 case com.android.internal.R.styleable.View_paddingLeft:
2597 leftPadding = a.getDimensionPixelSize(attr, -1);
2598 break;
2599 case com.android.internal.R.styleable.View_paddingTop:
2600 topPadding = a.getDimensionPixelSize(attr, -1);
2601 break;
2602 case com.android.internal.R.styleable.View_paddingRight:
2603 rightPadding = a.getDimensionPixelSize(attr, -1);
2604 break;
2605 case com.android.internal.R.styleable.View_paddingBottom:
2606 bottomPadding = a.getDimensionPixelSize(attr, -1);
2607 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002608 case com.android.internal.R.styleable.View_paddingStart:
2609 startPadding = a.getDimensionPixelSize(attr, -1);
2610 break;
2611 case com.android.internal.R.styleable.View_paddingEnd:
2612 endPadding = a.getDimensionPixelSize(attr, -1);
2613 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 case com.android.internal.R.styleable.View_scrollX:
2615 x = a.getDimensionPixelOffset(attr, 0);
2616 break;
2617 case com.android.internal.R.styleable.View_scrollY:
2618 y = a.getDimensionPixelOffset(attr, 0);
2619 break;
Chet Haase73066682010-11-29 15:55:32 -08002620 case com.android.internal.R.styleable.View_alpha:
2621 setAlpha(a.getFloat(attr, 1f));
2622 break;
2623 case com.android.internal.R.styleable.View_transformPivotX:
2624 setPivotX(a.getDimensionPixelOffset(attr, 0));
2625 break;
2626 case com.android.internal.R.styleable.View_transformPivotY:
2627 setPivotY(a.getDimensionPixelOffset(attr, 0));
2628 break;
2629 case com.android.internal.R.styleable.View_translationX:
2630 tx = a.getDimensionPixelOffset(attr, 0);
2631 transformSet = true;
2632 break;
2633 case com.android.internal.R.styleable.View_translationY:
2634 ty = a.getDimensionPixelOffset(attr, 0);
2635 transformSet = true;
2636 break;
2637 case com.android.internal.R.styleable.View_rotation:
2638 rotation = a.getFloat(attr, 0);
2639 transformSet = true;
2640 break;
2641 case com.android.internal.R.styleable.View_rotationX:
2642 rotationX = a.getFloat(attr, 0);
2643 transformSet = true;
2644 break;
2645 case com.android.internal.R.styleable.View_rotationY:
2646 rotationY = a.getFloat(attr, 0);
2647 transformSet = true;
2648 break;
2649 case com.android.internal.R.styleable.View_scaleX:
2650 sx = a.getFloat(attr, 1f);
2651 transformSet = true;
2652 break;
2653 case com.android.internal.R.styleable.View_scaleY:
2654 sy = a.getFloat(attr, 1f);
2655 transformSet = true;
2656 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 case com.android.internal.R.styleable.View_id:
2658 mID = a.getResourceId(attr, NO_ID);
2659 break;
2660 case com.android.internal.R.styleable.View_tag:
2661 mTag = a.getText(attr);
2662 break;
2663 case com.android.internal.R.styleable.View_fitsSystemWindows:
2664 if (a.getBoolean(attr, false)) {
2665 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2666 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2667 }
2668 break;
2669 case com.android.internal.R.styleable.View_focusable:
2670 if (a.getBoolean(attr, false)) {
2671 viewFlagValues |= FOCUSABLE;
2672 viewFlagMasks |= FOCUSABLE_MASK;
2673 }
2674 break;
2675 case com.android.internal.R.styleable.View_focusableInTouchMode:
2676 if (a.getBoolean(attr, false)) {
2677 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2678 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2679 }
2680 break;
2681 case com.android.internal.R.styleable.View_clickable:
2682 if (a.getBoolean(attr, false)) {
2683 viewFlagValues |= CLICKABLE;
2684 viewFlagMasks |= CLICKABLE;
2685 }
2686 break;
2687 case com.android.internal.R.styleable.View_longClickable:
2688 if (a.getBoolean(attr, false)) {
2689 viewFlagValues |= LONG_CLICKABLE;
2690 viewFlagMasks |= LONG_CLICKABLE;
2691 }
2692 break;
2693 case com.android.internal.R.styleable.View_saveEnabled:
2694 if (!a.getBoolean(attr, true)) {
2695 viewFlagValues |= SAVE_DISABLED;
2696 viewFlagMasks |= SAVE_DISABLED_MASK;
2697 }
2698 break;
2699 case com.android.internal.R.styleable.View_duplicateParentState:
2700 if (a.getBoolean(attr, false)) {
2701 viewFlagValues |= DUPLICATE_PARENT_STATE;
2702 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2703 }
2704 break;
2705 case com.android.internal.R.styleable.View_visibility:
2706 final int visibility = a.getInt(attr, 0);
2707 if (visibility != 0) {
2708 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2709 viewFlagMasks |= VISIBILITY_MASK;
2710 }
2711 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002712 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002713 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002714 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002715 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002716 final int layoutDirection = a.getInt(attr, -1);
2717 if (layoutDirection != -1) {
2718 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002719 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002720 // Set to default (LAYOUT_DIRECTION_INHERIT)
2721 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002722 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002723 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002724 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 case com.android.internal.R.styleable.View_drawingCacheQuality:
2726 final int cacheQuality = a.getInt(attr, 0);
2727 if (cacheQuality != 0) {
2728 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2729 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2730 }
2731 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002732 case com.android.internal.R.styleable.View_contentDescription:
2733 mContentDescription = a.getString(attr);
2734 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2736 if (!a.getBoolean(attr, true)) {
2737 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2738 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2739 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002740 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2742 if (!a.getBoolean(attr, true)) {
2743 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2744 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2745 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002746 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 case R.styleable.View_scrollbars:
2748 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2749 if (scrollbars != SCROLLBARS_NONE) {
2750 viewFlagValues |= scrollbars;
2751 viewFlagMasks |= SCROLLBARS_MASK;
2752 initializeScrollbars(a);
2753 }
2754 break;
2755 case R.styleable.View_fadingEdge:
2756 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2757 if (fadingEdge != FADING_EDGE_NONE) {
2758 viewFlagValues |= fadingEdge;
2759 viewFlagMasks |= FADING_EDGE_MASK;
2760 initializeFadingEdge(a);
2761 }
2762 break;
2763 case R.styleable.View_scrollbarStyle:
2764 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2765 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2766 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2767 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2768 }
2769 break;
2770 case R.styleable.View_isScrollContainer:
2771 setScrollContainer = true;
2772 if (a.getBoolean(attr, false)) {
2773 setScrollContainer(true);
2774 }
2775 break;
2776 case com.android.internal.R.styleable.View_keepScreenOn:
2777 if (a.getBoolean(attr, false)) {
2778 viewFlagValues |= KEEP_SCREEN_ON;
2779 viewFlagMasks |= KEEP_SCREEN_ON;
2780 }
2781 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002782 case R.styleable.View_filterTouchesWhenObscured:
2783 if (a.getBoolean(attr, false)) {
2784 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2785 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2786 }
2787 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 case R.styleable.View_nextFocusLeft:
2789 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2790 break;
2791 case R.styleable.View_nextFocusRight:
2792 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2793 break;
2794 case R.styleable.View_nextFocusUp:
2795 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2796 break;
2797 case R.styleable.View_nextFocusDown:
2798 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2799 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002800 case R.styleable.View_nextFocusForward:
2801 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2802 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 case R.styleable.View_minWidth:
2804 mMinWidth = a.getDimensionPixelSize(attr, 0);
2805 break;
2806 case R.styleable.View_minHeight:
2807 mMinHeight = a.getDimensionPixelSize(attr, 0);
2808 break;
Romain Guy9a817362009-05-01 10:57:14 -07002809 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002810 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002811 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002812 + "be used within a restricted context");
2813 }
2814
Romain Guy9a817362009-05-01 10:57:14 -07002815 final String handlerName = a.getString(attr);
2816 if (handlerName != null) {
2817 setOnClickListener(new OnClickListener() {
2818 private Method mHandler;
2819
2820 public void onClick(View v) {
2821 if (mHandler == null) {
2822 try {
2823 mHandler = getContext().getClass().getMethod(handlerName,
2824 View.class);
2825 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002826 int id = getId();
2827 String idText = id == NO_ID ? "" : " with id '"
2828 + getContext().getResources().getResourceEntryName(
2829 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002830 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002831 handlerName + "(View) in the activity "
2832 + getContext().getClass() + " for onClick handler"
2833 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002834 }
2835 }
2836
2837 try {
2838 mHandler.invoke(getContext(), View.this);
2839 } catch (IllegalAccessException e) {
2840 throw new IllegalStateException("Could not execute non "
2841 + "public method of the activity", e);
2842 } catch (InvocationTargetException e) {
2843 throw new IllegalStateException("Could not execute "
2844 + "method of the activity", e);
2845 }
2846 }
2847 });
2848 }
2849 break;
Adam Powell637d3372010-08-25 14:37:03 -07002850 case R.styleable.View_overScrollMode:
2851 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2852 break;
Adam Powell20232d02010-12-08 21:08:53 -08002853 case R.styleable.View_verticalScrollbarPosition:
2854 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2855 break;
Romain Guy171c5922011-01-06 10:04:23 -08002856 case R.styleable.View_layerType:
2857 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2858 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 }
2860 }
2861
Adam Powell637d3372010-08-25 14:37:03 -07002862 setOverScrollMode(overScrollMode);
2863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 if (background != null) {
2865 setBackgroundDrawable(background);
2866 }
2867
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002868 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
2869
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002870 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
2871 // layout direction). Those cached values will be used later during padding resolution.
2872 mUserPaddingStart = startPadding;
2873 mUserPaddingEnd = endPadding;
2874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 if (padding >= 0) {
2876 leftPadding = padding;
2877 topPadding = padding;
2878 rightPadding = padding;
2879 bottomPadding = padding;
2880 }
2881
2882 // If the user specified the padding (either with android:padding or
2883 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2884 // use the default padding or the padding from the background drawable
2885 // (stored at this point in mPadding*)
2886 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2887 topPadding >= 0 ? topPadding : mPaddingTop,
2888 rightPadding >= 0 ? rightPadding : mPaddingRight,
2889 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2890
2891 if (viewFlagMasks != 0) {
2892 setFlags(viewFlagValues, viewFlagMasks);
2893 }
2894
2895 // Needs to be called after mViewFlags is set
2896 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2897 recomputePadding();
2898 }
2899
2900 if (x != 0 || y != 0) {
2901 scrollTo(x, y);
2902 }
2903
Chet Haase73066682010-11-29 15:55:32 -08002904 if (transformSet) {
2905 setTranslationX(tx);
2906 setTranslationY(ty);
2907 setRotation(rotation);
2908 setRotationX(rotationX);
2909 setRotationY(rotationY);
2910 setScaleX(sx);
2911 setScaleY(sy);
2912 }
2913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2915 setScrollContainer(true);
2916 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002917
2918 computeOpaqueFlags();
2919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 a.recycle();
2921 }
2922
2923 /**
2924 * Non-public constructor for use in testing
2925 */
2926 View() {
2927 }
2928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 /**
2930 * <p>
2931 * Initializes the fading edges from a given set of styled attributes. This
2932 * method should be called by subclasses that need fading edges and when an
2933 * instance of these subclasses is created programmatically rather than
2934 * being inflated from XML. This method is automatically called when the XML
2935 * is inflated.
2936 * </p>
2937 *
2938 * @param a the styled attributes set to initialize the fading edges from
2939 */
2940 protected void initializeFadingEdge(TypedArray a) {
2941 initScrollCache();
2942
2943 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2944 R.styleable.View_fadingEdgeLength,
2945 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2946 }
2947
2948 /**
2949 * Returns the size of the vertical faded edges used to indicate that more
2950 * content in this view is visible.
2951 *
2952 * @return The size in pixels of the vertical faded edge or 0 if vertical
2953 * faded edges are not enabled for this view.
2954 * @attr ref android.R.styleable#View_fadingEdgeLength
2955 */
2956 public int getVerticalFadingEdgeLength() {
2957 if (isVerticalFadingEdgeEnabled()) {
2958 ScrollabilityCache cache = mScrollCache;
2959 if (cache != null) {
2960 return cache.fadingEdgeLength;
2961 }
2962 }
2963 return 0;
2964 }
2965
2966 /**
2967 * Set the size of the faded edge used to indicate that more content in this
2968 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07002969 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
2970 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
2971 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 *
2973 * @param length The size in pixels of the faded edge used to indicate that more
2974 * content in this view is visible.
2975 */
2976 public void setFadingEdgeLength(int length) {
2977 initScrollCache();
2978 mScrollCache.fadingEdgeLength = length;
2979 }
2980
2981 /**
2982 * Returns the size of the horizontal faded edges used to indicate that more
2983 * content in this view is visible.
2984 *
2985 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2986 * faded edges are not enabled for this view.
2987 * @attr ref android.R.styleable#View_fadingEdgeLength
2988 */
2989 public int getHorizontalFadingEdgeLength() {
2990 if (isHorizontalFadingEdgeEnabled()) {
2991 ScrollabilityCache cache = mScrollCache;
2992 if (cache != null) {
2993 return cache.fadingEdgeLength;
2994 }
2995 }
2996 return 0;
2997 }
2998
2999 /**
3000 * Returns the width of the vertical scrollbar.
3001 *
3002 * @return The width in pixels of the vertical scrollbar or 0 if there
3003 * is no vertical scrollbar.
3004 */
3005 public int getVerticalScrollbarWidth() {
3006 ScrollabilityCache cache = mScrollCache;
3007 if (cache != null) {
3008 ScrollBarDrawable scrollBar = cache.scrollBar;
3009 if (scrollBar != null) {
3010 int size = scrollBar.getSize(true);
3011 if (size <= 0) {
3012 size = cache.scrollBarSize;
3013 }
3014 return size;
3015 }
3016 return 0;
3017 }
3018 return 0;
3019 }
3020
3021 /**
3022 * Returns the height of the horizontal scrollbar.
3023 *
3024 * @return The height in pixels of the horizontal scrollbar or 0 if
3025 * there is no horizontal scrollbar.
3026 */
3027 protected int getHorizontalScrollbarHeight() {
3028 ScrollabilityCache cache = mScrollCache;
3029 if (cache != null) {
3030 ScrollBarDrawable scrollBar = cache.scrollBar;
3031 if (scrollBar != null) {
3032 int size = scrollBar.getSize(false);
3033 if (size <= 0) {
3034 size = cache.scrollBarSize;
3035 }
3036 return size;
3037 }
3038 return 0;
3039 }
3040 return 0;
3041 }
3042
3043 /**
3044 * <p>
3045 * Initializes the scrollbars from a given set of styled attributes. This
3046 * method should be called by subclasses that need scrollbars and when an
3047 * instance of these subclasses is created programmatically rather than
3048 * being inflated from XML. This method is automatically called when the XML
3049 * is inflated.
3050 * </p>
3051 *
3052 * @param a the styled attributes set to initialize the scrollbars from
3053 */
3054 protected void initializeScrollbars(TypedArray a) {
3055 initScrollCache();
3056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003058
Mike Cleronf116bf82009-09-27 19:14:12 -07003059 if (scrollabilityCache.scrollBar == null) {
3060 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3061 }
Joe Malin32736f02011-01-19 16:14:20 -08003062
Romain Guy8bda2482010-03-02 11:42:11 -08003063 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003064
Mike Cleronf116bf82009-09-27 19:14:12 -07003065 if (!fadeScrollbars) {
3066 scrollabilityCache.state = ScrollabilityCache.ON;
3067 }
3068 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003069
3070
Mike Cleronf116bf82009-09-27 19:14:12 -07003071 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3072 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3073 .getScrollBarFadeDuration());
3074 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3075 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3076 ViewConfiguration.getScrollDefaultDelay());
3077
Joe Malin32736f02011-01-19 16:14:20 -08003078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3080 com.android.internal.R.styleable.View_scrollbarSize,
3081 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3082
3083 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3084 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3085
3086 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3087 if (thumb != null) {
3088 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3089 }
3090
3091 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3092 false);
3093 if (alwaysDraw) {
3094 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3095 }
3096
3097 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3098 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3099
3100 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3101 if (thumb != null) {
3102 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3103 }
3104
3105 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3106 false);
3107 if (alwaysDraw) {
3108 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3109 }
3110
3111 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003112 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113 }
3114
3115 /**
3116 * <p>
3117 * Initalizes the scrollability cache if necessary.
3118 * </p>
3119 */
3120 private void initScrollCache() {
3121 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003122 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 }
3124 }
3125
3126 /**
Adam Powell20232d02010-12-08 21:08:53 -08003127 * Set the position of the vertical scroll bar. Should be one of
3128 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3129 * {@link #SCROLLBAR_POSITION_RIGHT}.
3130 *
3131 * @param position Where the vertical scroll bar should be positioned.
3132 */
3133 public void setVerticalScrollbarPosition(int position) {
3134 if (mVerticalScrollbarPosition != position) {
3135 mVerticalScrollbarPosition = position;
3136 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003137 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003138 }
3139 }
3140
3141 /**
3142 * @return The position where the vertical scroll bar will show, if applicable.
3143 * @see #setVerticalScrollbarPosition(int)
3144 */
3145 public int getVerticalScrollbarPosition() {
3146 return mVerticalScrollbarPosition;
3147 }
3148
3149 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 * Register a callback to be invoked when focus of this view changed.
3151 *
3152 * @param l The callback that will run.
3153 */
3154 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3155 mOnFocusChangeListener = l;
3156 }
3157
3158 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003159 * Add a listener that will be called when the bounds of the view change due to
3160 * layout processing.
3161 *
3162 * @param listener The listener that will be called when layout bounds change.
3163 */
3164 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3165 if (mOnLayoutChangeListeners == null) {
3166 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3167 }
3168 mOnLayoutChangeListeners.add(listener);
3169 }
3170
3171 /**
3172 * Remove a listener for layout changes.
3173 *
3174 * @param listener The listener for layout bounds change.
3175 */
3176 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3177 if (mOnLayoutChangeListeners == null) {
3178 return;
3179 }
3180 mOnLayoutChangeListeners.remove(listener);
3181 }
3182
3183 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003184 * Add a listener for attach state changes.
3185 *
3186 * This listener will be called whenever this view is attached or detached
3187 * from a window. Remove the listener using
3188 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3189 *
3190 * @param listener Listener to attach
3191 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3192 */
3193 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3194 if (mOnAttachStateChangeListeners == null) {
3195 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3196 }
3197 mOnAttachStateChangeListeners.add(listener);
3198 }
3199
3200 /**
3201 * Remove a listener for attach state changes. The listener will receive no further
3202 * notification of window attach/detach events.
3203 *
3204 * @param listener Listener to remove
3205 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3206 */
3207 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3208 if (mOnAttachStateChangeListeners == null) {
3209 return;
3210 }
3211 mOnAttachStateChangeListeners.remove(listener);
3212 }
3213
3214 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 * Returns the focus-change callback registered for this view.
3216 *
3217 * @return The callback, or null if one is not registered.
3218 */
3219 public OnFocusChangeListener getOnFocusChangeListener() {
3220 return mOnFocusChangeListener;
3221 }
3222
3223 /**
3224 * Register a callback to be invoked when this view is clicked. If this view is not
3225 * clickable, it becomes clickable.
3226 *
3227 * @param l The callback that will run
3228 *
3229 * @see #setClickable(boolean)
3230 */
3231 public void setOnClickListener(OnClickListener l) {
3232 if (!isClickable()) {
3233 setClickable(true);
3234 }
3235 mOnClickListener = l;
3236 }
3237
3238 /**
3239 * Register a callback to be invoked when this view is clicked and held. If this view is not
3240 * long clickable, it becomes long clickable.
3241 *
3242 * @param l The callback that will run
3243 *
3244 * @see #setLongClickable(boolean)
3245 */
3246 public void setOnLongClickListener(OnLongClickListener l) {
3247 if (!isLongClickable()) {
3248 setLongClickable(true);
3249 }
3250 mOnLongClickListener = l;
3251 }
3252
3253 /**
3254 * Register a callback to be invoked when the context menu for this view is
3255 * being built. If this view is not long clickable, it becomes long clickable.
3256 *
3257 * @param l The callback that will run
3258 *
3259 */
3260 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3261 if (!isLongClickable()) {
3262 setLongClickable(true);
3263 }
3264 mOnCreateContextMenuListener = l;
3265 }
3266
3267 /**
3268 * Call this view's OnClickListener, if it is defined.
3269 *
3270 * @return True there was an assigned OnClickListener that was called, false
3271 * otherwise is returned.
3272 */
3273 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003274 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 if (mOnClickListener != null) {
3277 playSoundEffect(SoundEffectConstants.CLICK);
3278 mOnClickListener.onClick(this);
3279 return true;
3280 }
3281
3282 return false;
3283 }
3284
3285 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003286 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3287 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003289 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003290 */
3291 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003292 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003294 boolean handled = false;
3295 if (mOnLongClickListener != null) {
3296 handled = mOnLongClickListener.onLongClick(View.this);
3297 }
3298 if (!handled) {
3299 handled = showContextMenu();
3300 }
3301 if (handled) {
3302 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3303 }
3304 return handled;
3305 }
3306
3307 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003308 * Performs button-related actions during a touch down event.
3309 *
3310 * @param event The event.
3311 * @return True if the down was consumed.
3312 *
3313 * @hide
3314 */
3315 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3316 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3317 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3318 return true;
3319 }
3320 }
3321 return false;
3322 }
3323
3324 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 * Bring up the context menu for this view.
3326 *
3327 * @return Whether a context menu was displayed.
3328 */
3329 public boolean showContextMenu() {
3330 return getParent().showContextMenuForChild(this);
3331 }
3332
3333 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003334 * Bring up the context menu for this view, referring to the item under the specified point.
3335 *
3336 * @param x The referenced x coordinate.
3337 * @param y The referenced y coordinate.
3338 * @param metaState The keyboard modifiers that were pressed.
3339 * @return Whether a context menu was displayed.
3340 *
3341 * @hide
3342 */
3343 public boolean showContextMenu(float x, float y, int metaState) {
3344 return showContextMenu();
3345 }
3346
3347 /**
Adam Powell6e346362010-07-23 10:18:23 -07003348 * Start an action mode.
3349 *
3350 * @param callback Callback that will control the lifecycle of the action mode
3351 * @return The new action mode if it is started, null otherwise
3352 *
3353 * @see ActionMode
3354 */
3355 public ActionMode startActionMode(ActionMode.Callback callback) {
3356 return getParent().startActionModeForChild(this, callback);
3357 }
3358
3359 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 * Register a callback to be invoked when a key is pressed in this view.
3361 * @param l the key listener to attach to this view
3362 */
3363 public void setOnKeyListener(OnKeyListener l) {
3364 mOnKeyListener = l;
3365 }
3366
3367 /**
3368 * Register a callback to be invoked when a touch event is sent to this view.
3369 * @param l the touch listener to attach to this view
3370 */
3371 public void setOnTouchListener(OnTouchListener l) {
3372 mOnTouchListener = l;
3373 }
3374
3375 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003376 * Register a callback to be invoked when a generic motion event is sent to this view.
3377 * @param l the generic motion listener to attach to this view
3378 */
3379 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3380 mOnGenericMotionListener = l;
3381 }
3382
3383 /**
Joe Malin32736f02011-01-19 16:14:20 -08003384 * Register a drag event listener callback object for this View. The parameter is
3385 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3386 * View, the system calls the
3387 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3388 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003389 */
3390 public void setOnDragListener(OnDragListener l) {
3391 mOnDragListener = l;
3392 }
3393
3394 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003395 * Give this view focus. This will cause
3396 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 *
3398 * Note: this does not check whether this {@link View} should get focus, it just
3399 * gives it focus no matter what. It should only be called internally by framework
3400 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3401 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003402 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3403 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 * focus moved when requestFocus() is called. It may not always
3405 * apply, in which case use the default View.FOCUS_DOWN.
3406 * @param previouslyFocusedRect The rectangle of the view that had focus
3407 * prior in this View's coordinate system.
3408 */
3409 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3410 if (DBG) {
3411 System.out.println(this + " requestFocus()");
3412 }
3413
3414 if ((mPrivateFlags & FOCUSED) == 0) {
3415 mPrivateFlags |= FOCUSED;
3416
3417 if (mParent != null) {
3418 mParent.requestChildFocus(this, this);
3419 }
3420
3421 onFocusChanged(true, direction, previouslyFocusedRect);
3422 refreshDrawableState();
3423 }
3424 }
3425
3426 /**
3427 * Request that a rectangle of this view be visible on the screen,
3428 * scrolling if necessary just enough.
3429 *
3430 * <p>A View should call this if it maintains some notion of which part
3431 * of its content is interesting. For example, a text editing view
3432 * should call this when its cursor moves.
3433 *
3434 * @param rectangle The rectangle.
3435 * @return Whether any parent scrolled.
3436 */
3437 public boolean requestRectangleOnScreen(Rect rectangle) {
3438 return requestRectangleOnScreen(rectangle, false);
3439 }
3440
3441 /**
3442 * Request that a rectangle of this view be visible on the screen,
3443 * scrolling if necessary just enough.
3444 *
3445 * <p>A View should call this if it maintains some notion of which part
3446 * of its content is interesting. For example, a text editing view
3447 * should call this when its cursor moves.
3448 *
3449 * <p>When <code>immediate</code> is set to true, scrolling will not be
3450 * animated.
3451 *
3452 * @param rectangle The rectangle.
3453 * @param immediate True to forbid animated scrolling, false otherwise
3454 * @return Whether any parent scrolled.
3455 */
3456 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3457 View child = this;
3458 ViewParent parent = mParent;
3459 boolean scrolled = false;
3460 while (parent != null) {
3461 scrolled |= parent.requestChildRectangleOnScreen(child,
3462 rectangle, immediate);
3463
3464 // offset rect so next call has the rectangle in the
3465 // coordinate system of its direct child.
3466 rectangle.offset(child.getLeft(), child.getTop());
3467 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3468
3469 if (!(parent instanceof View)) {
3470 break;
3471 }
Romain Guy8506ab42009-06-11 17:35:47 -07003472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003473 child = (View) parent;
3474 parent = child.getParent();
3475 }
3476 return scrolled;
3477 }
3478
3479 /**
3480 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003481 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 */
3483 public void clearFocus() {
3484 if (DBG) {
3485 System.out.println(this + " clearFocus()");
3486 }
3487
3488 if ((mPrivateFlags & FOCUSED) != 0) {
3489 mPrivateFlags &= ~FOCUSED;
3490
3491 if (mParent != null) {
3492 mParent.clearChildFocus(this);
3493 }
3494
3495 onFocusChanged(false, 0, null);
3496 refreshDrawableState();
3497 }
3498 }
3499
3500 /**
3501 * Called to clear the focus of a view that is about to be removed.
3502 * Doesn't call clearChildFocus, which prevents this view from taking
3503 * focus again before it has been removed from the parent
3504 */
3505 void clearFocusForRemoval() {
3506 if ((mPrivateFlags & FOCUSED) != 0) {
3507 mPrivateFlags &= ~FOCUSED;
3508
3509 onFocusChanged(false, 0, null);
3510 refreshDrawableState();
3511 }
3512 }
3513
3514 /**
3515 * Called internally by the view system when a new view is getting focus.
3516 * This is what clears the old focus.
3517 */
3518 void unFocus() {
3519 if (DBG) {
3520 System.out.println(this + " unFocus()");
3521 }
3522
3523 if ((mPrivateFlags & FOCUSED) != 0) {
3524 mPrivateFlags &= ~FOCUSED;
3525
3526 onFocusChanged(false, 0, null);
3527 refreshDrawableState();
3528 }
3529 }
3530
3531 /**
3532 * Returns true if this view has focus iteself, or is the ancestor of the
3533 * view that has focus.
3534 *
3535 * @return True if this view has or contains focus, false otherwise.
3536 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003537 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 public boolean hasFocus() {
3539 return (mPrivateFlags & FOCUSED) != 0;
3540 }
3541
3542 /**
3543 * Returns true if this view is focusable or if it contains a reachable View
3544 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3545 * is a View whose parents do not block descendants focus.
3546 *
3547 * Only {@link #VISIBLE} views are considered focusable.
3548 *
3549 * @return True if the view is focusable or if the view contains a focusable
3550 * View, false otherwise.
3551 *
3552 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3553 */
3554 public boolean hasFocusable() {
3555 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3556 }
3557
3558 /**
3559 * Called by the view system when the focus state of this view changes.
3560 * When the focus change event is caused by directional navigation, direction
3561 * and previouslyFocusedRect provide insight into where the focus is coming from.
3562 * When overriding, be sure to call up through to the super class so that
3563 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003564 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003565 * @param gainFocus True if the View has focus; false otherwise.
3566 * @param direction The direction focus has moved when requestFocus()
3567 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003568 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3569 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3570 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3572 * system, of the previously focused view. If applicable, this will be
3573 * passed in as finer grained information about where the focus is coming
3574 * from (in addition to direction). Will be <code>null</code> otherwise.
3575 */
3576 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003577 if (gainFocus) {
3578 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3579 }
3580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003581 InputMethodManager imm = InputMethodManager.peekInstance();
3582 if (!gainFocus) {
3583 if (isPressed()) {
3584 setPressed(false);
3585 }
3586 if (imm != null && mAttachInfo != null
3587 && mAttachInfo.mHasWindowFocus) {
3588 imm.focusOut(this);
3589 }
Romain Guya2431d02009-04-30 16:30:00 -07003590 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003591 } else if (imm != null && mAttachInfo != null
3592 && mAttachInfo.mHasWindowFocus) {
3593 imm.focusIn(this);
3594 }
Romain Guy8506ab42009-06-11 17:35:47 -07003595
Romain Guy0fd89bf2011-01-26 15:41:30 -08003596 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003597 if (mOnFocusChangeListener != null) {
3598 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3599 }
Joe Malin32736f02011-01-19 16:14:20 -08003600
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003601 if (mAttachInfo != null) {
3602 mAttachInfo.mKeyDispatchState.reset(this);
3603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003604 }
3605
3606 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003607 * Sends an accessibility event of the given type. If accessiiblity is
3608 * not enabled this method has no effect. The default implementation calls
3609 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3610 * to populate information about the event source (this View), then calls
3611 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3612 * populate the text content of the event source including its descendants,
3613 * and last calls
3614 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3615 * on its parent to resuest sending of the event to interested parties.
3616 *
3617 * @param eventType The type of the event to send.
3618 *
3619 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3620 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3621 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
svetoslavganov75986cf2009-05-14 22:28:01 -07003622 */
3623 public void sendAccessibilityEvent(int eventType) {
3624 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3625 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3626 }
3627 }
3628
3629 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003630 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3631 * takes as an argument an empty {@link AccessibilityEvent} and does not
3632 * perfrom a check whether accessibility is enabled.
3633 *
3634 * @param event The event to send.
3635 *
3636 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003637 */
3638 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003639 if (!isShown()) {
3640 return;
3641 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003642 onInitializeAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003643 dispatchPopulateAccessibilityEvent(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003644 // In the beginning we called #isShown(), so we know that getParent() is not null.
3645 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003646 }
3647
3648 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003649 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3650 * to its children for adding their text content to the event. Note that the
3651 * event text is populated in a separate dispatch path since we add to the
3652 * event not only the text of the source but also the text of all its descendants.
3653 * </p>
3654 * A typical implementation will call
3655 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3656 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3657 * on each child. Override this method if custom population of the event text
3658 * content is required.
svetoslavganov75986cf2009-05-14 22:28:01 -07003659 *
3660 * @param event The event.
3661 *
3662 * @return True if the event population was completed.
3663 */
3664 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003665 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003666 return false;
3667 }
3668
3669 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003670 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003671 * giving a chance to this View to populate the accessibility event with its
3672 * text content. While the implementation is free to modify other event
3673 * attributes this should be performed in
3674 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
3675 * <p>
3676 * Example: Adding formatted date string to an accessibility event in addition
3677 * to the text added by the super implementation.
3678 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003679 * public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3680 * super.onPopulateAccessibilityEvent(event);
3681 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
3682 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
3683 * mCurrentDate.getTimeInMillis(), flags);
3684 * event.getText().add(selectedDateUtterance);
3685 * }
3686 * </code></pre></p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003687 *
3688 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003689 *
3690 * @see #sendAccessibilityEvent(int)
3691 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003692 */
3693 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3694
3695 }
3696
3697 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003698 * Initializes an {@link AccessibilityEvent} with information about the
3699 * the type of the event and this View which is the event source. In other
3700 * words, the source of an accessibility event is the view whose state
3701 * change triggered firing the event.
3702 * <p>
3703 * Example: Setting the password property of an event in addition
3704 * to properties set by the super implementation.
3705 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003706 * public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3707 * super.onInitializeAccessibilityEvent(event);
3708 * event.setPassword(true);
3709 * }
3710 * </code></pre></p>
3711 * @param event The event to initialeze.
3712 *
3713 * @see #sendAccessibilityEvent(int)
3714 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3715 */
3716 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003717 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07003718 event.setClassName(getClass().getName());
3719 event.setPackageName(getContext().getPackageName());
3720 event.setEnabled(isEnabled());
3721 event.setContentDescription(mContentDescription);
3722
3723 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
3724 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3725 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
3726 event.setItemCount(focusablesTempList.size());
3727 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3728 focusablesTempList.clear();
3729 }
3730 }
3731
3732 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003733 * Returns an {@link AccessibilityNodeInfo} representing this view from the
3734 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
3735 * This method is responsible for obtaining an accessibility node info from a
3736 * pool of reusable instances and calling
3737 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
3738 * initialize the former.
3739 * <p>
3740 * Note: The client is responsible for recycling the obtained instance by calling
3741 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
3742 * </p>
3743 * @return A populated {@link AccessibilityNodeInfo}.
3744 *
3745 * @see AccessibilityNodeInfo
3746 */
3747 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
3748 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
3749 onInitializeAccessibilityNodeInfo(info);
3750 return info;
3751 }
3752
3753 /**
3754 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
3755 * The base implementation sets:
3756 * <ul>
3757 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003758 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
3759 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003760 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
3761 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
3762 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
3763 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
3764 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
3765 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
3766 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
3767 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
3768 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
3769 * </ul>
3770 * <p>
3771 * Subclasses should override this method, call the super implementation,
3772 * and set additional attributes.
3773 * </p>
3774 * @param info The instance to initialize.
3775 */
3776 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
3777 Rect bounds = mAttachInfo.mTmpInvalRect;
3778 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003779 info.setBoundsInParent(bounds);
3780
3781 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
3782 getLocationOnScreen(locationOnScreen);
3783 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
3784 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003785
3786 ViewParent parent = getParent();
3787 if (parent instanceof View) {
3788 View parentView = (View) parent;
3789 info.setParent(parentView);
3790 }
3791
3792 info.setPackageName(mContext.getPackageName());
3793 info.setClassName(getClass().getName());
3794 info.setContentDescription(getContentDescription());
3795
3796 info.setEnabled(isEnabled());
3797 info.setClickable(isClickable());
3798 info.setFocusable(isFocusable());
3799 info.setFocused(isFocused());
3800 info.setSelected(isSelected());
3801 info.setLongClickable(isLongClickable());
3802
3803 // TODO: These make sense only if we are in an AdapterView but all
3804 // views can be selected. Maybe from accessiiblity perspective
3805 // we should report as selectable view in an AdapterView.
3806 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
3807 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
3808
3809 if (isFocusable()) {
3810 if (isFocused()) {
3811 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
3812 } else {
3813 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
3814 }
3815 }
3816 }
3817
3818 /**
3819 * Gets the unique identifier of this view on the screen for accessibility purposes.
3820 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
3821 *
3822 * @return The view accessibility id.
3823 *
3824 * @hide
3825 */
3826 public int getAccessibilityViewId() {
3827 if (mAccessibilityViewId == NO_ID) {
3828 mAccessibilityViewId = sNextAccessibilityViewId++;
3829 }
3830 return mAccessibilityViewId;
3831 }
3832
3833 /**
3834 * Gets the unique identifier of the window in which this View reseides.
3835 *
3836 * @return The window accessibility id.
3837 *
3838 * @hide
3839 */
3840 public int getAccessibilityWindowId() {
3841 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
3842 }
3843
3844 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003845 * Gets the {@link View} description. It briefly describes the view and is
3846 * primarily used for accessibility support. Set this property to enable
3847 * better accessibility support for your application. This is especially
3848 * true for views that do not have textual representation (For example,
3849 * ImageButton).
3850 *
3851 * @return The content descriptiopn.
3852 *
3853 * @attr ref android.R.styleable#View_contentDescription
3854 */
3855 public CharSequence getContentDescription() {
3856 return mContentDescription;
3857 }
3858
3859 /**
3860 * Sets the {@link View} description. It briefly describes the view and is
3861 * primarily used for accessibility support. Set this property to enable
3862 * better accessibility support for your application. This is especially
3863 * true for views that do not have textual representation (For example,
3864 * ImageButton).
3865 *
3866 * @param contentDescription The content description.
3867 *
3868 * @attr ref android.R.styleable#View_contentDescription
3869 */
3870 public void setContentDescription(CharSequence contentDescription) {
3871 mContentDescription = contentDescription;
3872 }
3873
3874 /**
Romain Guya2431d02009-04-30 16:30:00 -07003875 * Invoked whenever this view loses focus, either by losing window focus or by losing
3876 * focus within its window. This method can be used to clear any state tied to the
3877 * focus. For instance, if a button is held pressed with the trackball and the window
3878 * loses focus, this method can be used to cancel the press.
3879 *
3880 * Subclasses of View overriding this method should always call super.onFocusLost().
3881 *
3882 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003883 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003884 *
3885 * @hide pending API council approval
3886 */
3887 protected void onFocusLost() {
3888 resetPressedState();
3889 }
3890
3891 private void resetPressedState() {
3892 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3893 return;
3894 }
3895
3896 if (isPressed()) {
3897 setPressed(false);
3898
3899 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05003900 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003901 }
3902 }
3903 }
3904
3905 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003906 * Returns true if this view has focus
3907 *
3908 * @return True if this view has focus, false otherwise.
3909 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003910 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003911 public boolean isFocused() {
3912 return (mPrivateFlags & FOCUSED) != 0;
3913 }
3914
3915 /**
3916 * Find the view in the hierarchy rooted at this view that currently has
3917 * focus.
3918 *
3919 * @return The view that currently has focus, or null if no focused view can
3920 * be found.
3921 */
3922 public View findFocus() {
3923 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
3924 }
3925
3926 /**
3927 * Change whether this view is one of the set of scrollable containers in
3928 * its window. This will be used to determine whether the window can
3929 * resize or must pan when a soft input area is open -- scrollable
3930 * containers allow the window to use resize mode since the container
3931 * will appropriately shrink.
3932 */
3933 public void setScrollContainer(boolean isScrollContainer) {
3934 if (isScrollContainer) {
3935 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
3936 mAttachInfo.mScrollContainers.add(this);
3937 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
3938 }
3939 mPrivateFlags |= SCROLL_CONTAINER;
3940 } else {
3941 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
3942 mAttachInfo.mScrollContainers.remove(this);
3943 }
3944 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
3945 }
3946 }
3947
3948 /**
3949 * Returns the quality of the drawing cache.
3950 *
3951 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3952 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3953 *
3954 * @see #setDrawingCacheQuality(int)
3955 * @see #setDrawingCacheEnabled(boolean)
3956 * @see #isDrawingCacheEnabled()
3957 *
3958 * @attr ref android.R.styleable#View_drawingCacheQuality
3959 */
3960 public int getDrawingCacheQuality() {
3961 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
3962 }
3963
3964 /**
3965 * Set the drawing cache quality of this view. This value is used only when the
3966 * drawing cache is enabled
3967 *
3968 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3969 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3970 *
3971 * @see #getDrawingCacheQuality()
3972 * @see #setDrawingCacheEnabled(boolean)
3973 * @see #isDrawingCacheEnabled()
3974 *
3975 * @attr ref android.R.styleable#View_drawingCacheQuality
3976 */
3977 public void setDrawingCacheQuality(int quality) {
3978 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
3979 }
3980
3981 /**
3982 * Returns whether the screen should remain on, corresponding to the current
3983 * value of {@link #KEEP_SCREEN_ON}.
3984 *
3985 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
3986 *
3987 * @see #setKeepScreenOn(boolean)
3988 *
3989 * @attr ref android.R.styleable#View_keepScreenOn
3990 */
3991 public boolean getKeepScreenOn() {
3992 return (mViewFlags & KEEP_SCREEN_ON) != 0;
3993 }
3994
3995 /**
3996 * Controls whether the screen should remain on, modifying the
3997 * value of {@link #KEEP_SCREEN_ON}.
3998 *
3999 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4000 *
4001 * @see #getKeepScreenOn()
4002 *
4003 * @attr ref android.R.styleable#View_keepScreenOn
4004 */
4005 public void setKeepScreenOn(boolean keepScreenOn) {
4006 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4007 }
4008
4009 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004010 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4011 * @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 -08004012 *
4013 * @attr ref android.R.styleable#View_nextFocusLeft
4014 */
4015 public int getNextFocusLeftId() {
4016 return mNextFocusLeftId;
4017 }
4018
4019 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004020 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4021 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4022 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023 *
4024 * @attr ref android.R.styleable#View_nextFocusLeft
4025 */
4026 public void setNextFocusLeftId(int nextFocusLeftId) {
4027 mNextFocusLeftId = nextFocusLeftId;
4028 }
4029
4030 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004031 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4032 * @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 -08004033 *
4034 * @attr ref android.R.styleable#View_nextFocusRight
4035 */
4036 public int getNextFocusRightId() {
4037 return mNextFocusRightId;
4038 }
4039
4040 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004041 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4042 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4043 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004044 *
4045 * @attr ref android.R.styleable#View_nextFocusRight
4046 */
4047 public void setNextFocusRightId(int nextFocusRightId) {
4048 mNextFocusRightId = nextFocusRightId;
4049 }
4050
4051 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004052 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4053 * @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 -08004054 *
4055 * @attr ref android.R.styleable#View_nextFocusUp
4056 */
4057 public int getNextFocusUpId() {
4058 return mNextFocusUpId;
4059 }
4060
4061 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004062 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4063 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4064 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004065 *
4066 * @attr ref android.R.styleable#View_nextFocusUp
4067 */
4068 public void setNextFocusUpId(int nextFocusUpId) {
4069 mNextFocusUpId = nextFocusUpId;
4070 }
4071
4072 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004073 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4074 * @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 -08004075 *
4076 * @attr ref android.R.styleable#View_nextFocusDown
4077 */
4078 public int getNextFocusDownId() {
4079 return mNextFocusDownId;
4080 }
4081
4082 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004083 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4084 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4085 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004086 *
4087 * @attr ref android.R.styleable#View_nextFocusDown
4088 */
4089 public void setNextFocusDownId(int nextFocusDownId) {
4090 mNextFocusDownId = nextFocusDownId;
4091 }
4092
4093 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004094 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4095 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4096 *
4097 * @attr ref android.R.styleable#View_nextFocusForward
4098 */
4099 public int getNextFocusForwardId() {
4100 return mNextFocusForwardId;
4101 }
4102
4103 /**
4104 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4105 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4106 * decide automatically.
4107 *
4108 * @attr ref android.R.styleable#View_nextFocusForward
4109 */
4110 public void setNextFocusForwardId(int nextFocusForwardId) {
4111 mNextFocusForwardId = nextFocusForwardId;
4112 }
4113
4114 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004115 * Returns the visibility of this view and all of its ancestors
4116 *
4117 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4118 */
4119 public boolean isShown() {
4120 View current = this;
4121 //noinspection ConstantConditions
4122 do {
4123 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4124 return false;
4125 }
4126 ViewParent parent = current.mParent;
4127 if (parent == null) {
4128 return false; // We are not attached to the view root
4129 }
4130 if (!(parent instanceof View)) {
4131 return true;
4132 }
4133 current = (View) parent;
4134 } while (current != null);
4135
4136 return false;
4137 }
4138
4139 /**
4140 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4141 * is set
4142 *
4143 * @param insets Insets for system windows
4144 *
4145 * @return True if this view applied the insets, false otherwise
4146 */
4147 protected boolean fitSystemWindows(Rect insets) {
4148 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4149 mPaddingLeft = insets.left;
4150 mPaddingTop = insets.top;
4151 mPaddingRight = insets.right;
4152 mPaddingBottom = insets.bottom;
4153 requestLayout();
4154 return true;
4155 }
4156 return false;
4157 }
4158
4159 /**
4160 * Returns the visibility status for this view.
4161 *
4162 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4163 * @attr ref android.R.styleable#View_visibility
4164 */
4165 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004166 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4167 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4168 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 })
4170 public int getVisibility() {
4171 return mViewFlags & VISIBILITY_MASK;
4172 }
4173
4174 /**
4175 * Set the enabled state of this view.
4176 *
4177 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4178 * @attr ref android.R.styleable#View_visibility
4179 */
4180 @RemotableViewMethod
4181 public void setVisibility(int visibility) {
4182 setFlags(visibility, VISIBILITY_MASK);
4183 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4184 }
4185
4186 /**
4187 * Returns the enabled status for this view. The interpretation of the
4188 * enabled state varies by subclass.
4189 *
4190 * @return True if this view is enabled, false otherwise.
4191 */
4192 @ViewDebug.ExportedProperty
4193 public boolean isEnabled() {
4194 return (mViewFlags & ENABLED_MASK) == ENABLED;
4195 }
4196
4197 /**
4198 * Set the enabled state of this view. The interpretation of the enabled
4199 * state varies by subclass.
4200 *
4201 * @param enabled True if this view is enabled, false otherwise.
4202 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004203 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004204 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004205 if (enabled == isEnabled()) return;
4206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004207 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4208
4209 /*
4210 * The View most likely has to change its appearance, so refresh
4211 * the drawable state.
4212 */
4213 refreshDrawableState();
4214
4215 // Invalidate too, since the default behavior for views is to be
4216 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004217 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004218 }
4219
4220 /**
4221 * Set whether this view can receive the focus.
4222 *
4223 * Setting this to false will also ensure that this view is not focusable
4224 * in touch mode.
4225 *
4226 * @param focusable If true, this view can receive the focus.
4227 *
4228 * @see #setFocusableInTouchMode(boolean)
4229 * @attr ref android.R.styleable#View_focusable
4230 */
4231 public void setFocusable(boolean focusable) {
4232 if (!focusable) {
4233 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4234 }
4235 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4236 }
4237
4238 /**
4239 * Set whether this view can receive focus while in touch mode.
4240 *
4241 * Setting this to true will also ensure that this view is focusable.
4242 *
4243 * @param focusableInTouchMode If true, this view can receive the focus while
4244 * in touch mode.
4245 *
4246 * @see #setFocusable(boolean)
4247 * @attr ref android.R.styleable#View_focusableInTouchMode
4248 */
4249 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4250 // Focusable in touch mode should always be set before the focusable flag
4251 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4252 // which, in touch mode, will not successfully request focus on this view
4253 // because the focusable in touch mode flag is not set
4254 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4255 if (focusableInTouchMode) {
4256 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4257 }
4258 }
4259
4260 /**
4261 * Set whether this view should have sound effects enabled for events such as
4262 * clicking and touching.
4263 *
4264 * <p>You may wish to disable sound effects for a view if you already play sounds,
4265 * for instance, a dial key that plays dtmf tones.
4266 *
4267 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4268 * @see #isSoundEffectsEnabled()
4269 * @see #playSoundEffect(int)
4270 * @attr ref android.R.styleable#View_soundEffectsEnabled
4271 */
4272 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4273 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4274 }
4275
4276 /**
4277 * @return whether this view should have sound effects enabled for events such as
4278 * clicking and touching.
4279 *
4280 * @see #setSoundEffectsEnabled(boolean)
4281 * @see #playSoundEffect(int)
4282 * @attr ref android.R.styleable#View_soundEffectsEnabled
4283 */
4284 @ViewDebug.ExportedProperty
4285 public boolean isSoundEffectsEnabled() {
4286 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4287 }
4288
4289 /**
4290 * Set whether this view should have haptic feedback for events such as
4291 * long presses.
4292 *
4293 * <p>You may wish to disable haptic feedback if your view already controls
4294 * its own haptic feedback.
4295 *
4296 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4297 * @see #isHapticFeedbackEnabled()
4298 * @see #performHapticFeedback(int)
4299 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4300 */
4301 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4302 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4303 }
4304
4305 /**
4306 * @return whether this view should have haptic feedback enabled for events
4307 * long presses.
4308 *
4309 * @see #setHapticFeedbackEnabled(boolean)
4310 * @see #performHapticFeedback(int)
4311 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4312 */
4313 @ViewDebug.ExportedProperty
4314 public boolean isHapticFeedbackEnabled() {
4315 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4316 }
4317
4318 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004319 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004320 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004321 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4322 * {@link #LAYOUT_DIRECTION_RTL},
4323 * {@link #LAYOUT_DIRECTION_INHERIT} or
4324 * {@link #LAYOUT_DIRECTION_LOCALE}.
4325 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004326 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004327 * @hide
4328 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004329 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004330 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4331 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4332 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4333 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004334 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004335 public int getLayoutDirection() {
4336 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004337 }
4338
4339 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004340 * Set the layout direction for this view. This will propagate a reset of layout direction
4341 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004342 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004343 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4344 * {@link #LAYOUT_DIRECTION_RTL},
4345 * {@link #LAYOUT_DIRECTION_INHERIT} or
4346 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004347 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004348 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004349 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004350 * @hide
4351 */
4352 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004353 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004354 if (getLayoutDirection() != layoutDirection) {
4355 resetLayoutDirectionResolution();
4356 // Setting the flag will also request a layout.
4357 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4358 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004359 }
4360
4361 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004362 * Returns the resolved layout direction for this view.
4363 *
4364 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4365 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4366 *
4367 * @hide
4368 */
4369 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4370 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4371 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4372 })
4373 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004374 resolveLayoutDirectionIfNeeded();
4375 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004376 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4377 }
4378
4379 /**
4380 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4381 * layout attribute and/or the inherited value from the parent.</p>
4382 *
4383 * @return true if the layout is right-to-left.
4384 *
4385 * @hide
4386 */
4387 @ViewDebug.ExportedProperty(category = "layout")
4388 public boolean isLayoutRtl() {
4389 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4390 }
4391
4392 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004393 * If this view doesn't do any drawing on its own, set this flag to
4394 * allow further optimizations. By default, this flag is not set on
4395 * View, but could be set on some View subclasses such as ViewGroup.
4396 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004397 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4398 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399 *
4400 * @param willNotDraw whether or not this View draw on its own
4401 */
4402 public void setWillNotDraw(boolean willNotDraw) {
4403 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4404 }
4405
4406 /**
4407 * Returns whether or not this View draws on its own.
4408 *
4409 * @return true if this view has nothing to draw, false otherwise
4410 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004411 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004412 public boolean willNotDraw() {
4413 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4414 }
4415
4416 /**
4417 * When a View's drawing cache is enabled, drawing is redirected to an
4418 * offscreen bitmap. Some views, like an ImageView, must be able to
4419 * bypass this mechanism if they already draw a single bitmap, to avoid
4420 * unnecessary usage of the memory.
4421 *
4422 * @param willNotCacheDrawing true if this view does not cache its
4423 * drawing, false otherwise
4424 */
4425 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4426 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4427 }
4428
4429 /**
4430 * Returns whether or not this View can cache its drawing or not.
4431 *
4432 * @return true if this view does not cache its drawing, false otherwise
4433 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004434 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004435 public boolean willNotCacheDrawing() {
4436 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4437 }
4438
4439 /**
4440 * Indicates whether this view reacts to click events or not.
4441 *
4442 * @return true if the view is clickable, false otherwise
4443 *
4444 * @see #setClickable(boolean)
4445 * @attr ref android.R.styleable#View_clickable
4446 */
4447 @ViewDebug.ExportedProperty
4448 public boolean isClickable() {
4449 return (mViewFlags & CLICKABLE) == CLICKABLE;
4450 }
4451
4452 /**
4453 * Enables or disables click events for this view. When a view
4454 * is clickable it will change its state to "pressed" on every click.
4455 * Subclasses should set the view clickable to visually react to
4456 * user's clicks.
4457 *
4458 * @param clickable true to make the view clickable, false otherwise
4459 *
4460 * @see #isClickable()
4461 * @attr ref android.R.styleable#View_clickable
4462 */
4463 public void setClickable(boolean clickable) {
4464 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4465 }
4466
4467 /**
4468 * Indicates whether this view reacts to long click events or not.
4469 *
4470 * @return true if the view is long clickable, false otherwise
4471 *
4472 * @see #setLongClickable(boolean)
4473 * @attr ref android.R.styleable#View_longClickable
4474 */
4475 public boolean isLongClickable() {
4476 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4477 }
4478
4479 /**
4480 * Enables or disables long click events for this view. When a view is long
4481 * clickable it reacts to the user holding down the button for a longer
4482 * duration than a tap. This event can either launch the listener or a
4483 * context menu.
4484 *
4485 * @param longClickable true to make the view long clickable, false otherwise
4486 * @see #isLongClickable()
4487 * @attr ref android.R.styleable#View_longClickable
4488 */
4489 public void setLongClickable(boolean longClickable) {
4490 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4491 }
4492
4493 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004494 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004495 *
4496 * @see #isClickable()
4497 * @see #setClickable(boolean)
4498 *
4499 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4500 * the View's internal state from a previously set "pressed" state.
4501 */
4502 public void setPressed(boolean pressed) {
4503 if (pressed) {
4504 mPrivateFlags |= PRESSED;
4505 } else {
4506 mPrivateFlags &= ~PRESSED;
4507 }
4508 refreshDrawableState();
4509 dispatchSetPressed(pressed);
4510 }
4511
4512 /**
4513 * Dispatch setPressed to all of this View's children.
4514 *
4515 * @see #setPressed(boolean)
4516 *
4517 * @param pressed The new pressed state
4518 */
4519 protected void dispatchSetPressed(boolean pressed) {
4520 }
4521
4522 /**
4523 * Indicates whether the view is currently in pressed state. Unless
4524 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4525 * the pressed state.
4526 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004527 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004528 * @see #isClickable()
4529 * @see #setClickable(boolean)
4530 *
4531 * @return true if the view is currently pressed, false otherwise
4532 */
4533 public boolean isPressed() {
4534 return (mPrivateFlags & PRESSED) == PRESSED;
4535 }
4536
4537 /**
4538 * Indicates whether this view will save its state (that is,
4539 * whether its {@link #onSaveInstanceState} method will be called).
4540 *
4541 * @return Returns true if the view state saving is enabled, else false.
4542 *
4543 * @see #setSaveEnabled(boolean)
4544 * @attr ref android.R.styleable#View_saveEnabled
4545 */
4546 public boolean isSaveEnabled() {
4547 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4548 }
4549
4550 /**
4551 * Controls whether the saving of this view's state is
4552 * enabled (that is, whether its {@link #onSaveInstanceState} method
4553 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004554 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004555 * for its state to be saved. This flag can only disable the
4556 * saving of this view; any child views may still have their state saved.
4557 *
4558 * @param enabled Set to false to <em>disable</em> state saving, or true
4559 * (the default) to allow it.
4560 *
4561 * @see #isSaveEnabled()
4562 * @see #setId(int)
4563 * @see #onSaveInstanceState()
4564 * @attr ref android.R.styleable#View_saveEnabled
4565 */
4566 public void setSaveEnabled(boolean enabled) {
4567 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4568 }
4569
Jeff Brown85a31762010-09-01 17:01:00 -07004570 /**
4571 * Gets whether the framework should discard touches when the view's
4572 * window is obscured by another visible window.
4573 * Refer to the {@link View} security documentation for more details.
4574 *
4575 * @return True if touch filtering is enabled.
4576 *
4577 * @see #setFilterTouchesWhenObscured(boolean)
4578 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4579 */
4580 @ViewDebug.ExportedProperty
4581 public boolean getFilterTouchesWhenObscured() {
4582 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4583 }
4584
4585 /**
4586 * Sets whether the framework should discard touches when the view's
4587 * window is obscured by another visible window.
4588 * Refer to the {@link View} security documentation for more details.
4589 *
4590 * @param enabled True if touch filtering should be enabled.
4591 *
4592 * @see #getFilterTouchesWhenObscured
4593 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4594 */
4595 public void setFilterTouchesWhenObscured(boolean enabled) {
4596 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4597 FILTER_TOUCHES_WHEN_OBSCURED);
4598 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004599
4600 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004601 * Indicates whether the entire hierarchy under this view will save its
4602 * state when a state saving traversal occurs from its parent. The default
4603 * is true; if false, these views will not be saved unless
4604 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4605 *
4606 * @return Returns true if the view state saving from parent is enabled, else false.
4607 *
4608 * @see #setSaveFromParentEnabled(boolean)
4609 */
4610 public boolean isSaveFromParentEnabled() {
4611 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4612 }
4613
4614 /**
4615 * Controls whether the entire hierarchy under this view will save its
4616 * state when a state saving traversal occurs from its parent. The default
4617 * is true; if false, these views will not be saved unless
4618 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4619 *
4620 * @param enabled Set to false to <em>disable</em> state saving, or true
4621 * (the default) to allow it.
4622 *
4623 * @see #isSaveFromParentEnabled()
4624 * @see #setId(int)
4625 * @see #onSaveInstanceState()
4626 */
4627 public void setSaveFromParentEnabled(boolean enabled) {
4628 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4629 }
4630
4631
4632 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004633 * Returns whether this View is able to take focus.
4634 *
4635 * @return True if this view can take focus, or false otherwise.
4636 * @attr ref android.R.styleable#View_focusable
4637 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004638 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004639 public final boolean isFocusable() {
4640 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4641 }
4642
4643 /**
4644 * When a view is focusable, it may not want to take focus when in touch mode.
4645 * For example, a button would like focus when the user is navigating via a D-pad
4646 * so that the user can click on it, but once the user starts touching the screen,
4647 * the button shouldn't take focus
4648 * @return Whether the view is focusable in touch mode.
4649 * @attr ref android.R.styleable#View_focusableInTouchMode
4650 */
4651 @ViewDebug.ExportedProperty
4652 public final boolean isFocusableInTouchMode() {
4653 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4654 }
4655
4656 /**
4657 * Find the nearest view in the specified direction that can take focus.
4658 * This does not actually give focus to that view.
4659 *
4660 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4661 *
4662 * @return The nearest focusable in the specified direction, or null if none
4663 * can be found.
4664 */
4665 public View focusSearch(int direction) {
4666 if (mParent != null) {
4667 return mParent.focusSearch(this, direction);
4668 } else {
4669 return null;
4670 }
4671 }
4672
4673 /**
4674 * This method is the last chance for the focused view and its ancestors to
4675 * respond to an arrow key. This is called when the focused view did not
4676 * consume the key internally, nor could the view system find a new view in
4677 * the requested direction to give focus to.
4678 *
4679 * @param focused The currently focused view.
4680 * @param direction The direction focus wants to move. One of FOCUS_UP,
4681 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4682 * @return True if the this view consumed this unhandled move.
4683 */
4684 public boolean dispatchUnhandledMove(View focused, int direction) {
4685 return false;
4686 }
4687
4688 /**
4689 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004690 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004691 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004692 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4693 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004694 * @return The user specified next view, or null if there is none.
4695 */
4696 View findUserSetNextFocus(View root, int direction) {
4697 switch (direction) {
4698 case FOCUS_LEFT:
4699 if (mNextFocusLeftId == View.NO_ID) return null;
4700 return findViewShouldExist(root, mNextFocusLeftId);
4701 case FOCUS_RIGHT:
4702 if (mNextFocusRightId == View.NO_ID) return null;
4703 return findViewShouldExist(root, mNextFocusRightId);
4704 case FOCUS_UP:
4705 if (mNextFocusUpId == View.NO_ID) return null;
4706 return findViewShouldExist(root, mNextFocusUpId);
4707 case FOCUS_DOWN:
4708 if (mNextFocusDownId == View.NO_ID) return null;
4709 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004710 case FOCUS_FORWARD:
4711 if (mNextFocusForwardId == View.NO_ID) return null;
4712 return findViewShouldExist(root, mNextFocusForwardId);
4713 case FOCUS_BACKWARD: {
4714 final int id = mID;
4715 return root.findViewByPredicate(new Predicate<View>() {
4716 @Override
4717 public boolean apply(View t) {
4718 return t.mNextFocusForwardId == id;
4719 }
4720 });
4721 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004722 }
4723 return null;
4724 }
4725
4726 private static View findViewShouldExist(View root, int childViewId) {
4727 View result = root.findViewById(childViewId);
4728 if (result == null) {
4729 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4730 + "by user for id " + childViewId);
4731 }
4732 return result;
4733 }
4734
4735 /**
4736 * Find and return all focusable views that are descendants of this view,
4737 * possibly including this view if it is focusable itself.
4738 *
4739 * @param direction The direction of the focus
4740 * @return A list of focusable views
4741 */
4742 public ArrayList<View> getFocusables(int direction) {
4743 ArrayList<View> result = new ArrayList<View>(24);
4744 addFocusables(result, direction);
4745 return result;
4746 }
4747
4748 /**
4749 * Add any focusable views that are descendants of this view (possibly
4750 * including this view if it is focusable itself) to views. If we are in touch mode,
4751 * only add views that are also focusable in touch mode.
4752 *
4753 * @param views Focusable views found so far
4754 * @param direction The direction of the focus
4755 */
4756 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004757 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004759
svetoslavganov75986cf2009-05-14 22:28:01 -07004760 /**
4761 * Adds any focusable views that are descendants of this view (possibly
4762 * including this view if it is focusable itself) to views. This method
4763 * adds all focusable views regardless if we are in touch mode or
4764 * only views focusable in touch mode if we are in touch mode depending on
4765 * the focusable mode paramater.
4766 *
4767 * @param views Focusable views found so far or null if all we are interested is
4768 * the number of focusables.
4769 * @param direction The direction of the focus.
4770 * @param focusableMode The type of focusables to be added.
4771 *
4772 * @see #FOCUSABLES_ALL
4773 * @see #FOCUSABLES_TOUCH_MODE
4774 */
4775 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4776 if (!isFocusable()) {
4777 return;
4778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004779
svetoslavganov75986cf2009-05-14 22:28:01 -07004780 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4781 isInTouchMode() && !isFocusableInTouchMode()) {
4782 return;
4783 }
4784
4785 if (views != null) {
4786 views.add(this);
4787 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004788 }
4789
4790 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004791 * Finds the Views that contain given text. The containment is case insensitive.
4792 * As View's text is considered any text content that View renders.
4793 *
4794 * @param outViews The output list of matching Views.
4795 * @param text The text to match against.
4796 */
4797 public void findViewsWithText(ArrayList<View> outViews, CharSequence text) {
4798 }
4799
4800 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004801 * Find and return all touchable views that are descendants of this view,
4802 * possibly including this view if it is touchable itself.
4803 *
4804 * @return A list of touchable views
4805 */
4806 public ArrayList<View> getTouchables() {
4807 ArrayList<View> result = new ArrayList<View>();
4808 addTouchables(result);
4809 return result;
4810 }
4811
4812 /**
4813 * Add any touchable views that are descendants of this view (possibly
4814 * including this view if it is touchable itself) to views.
4815 *
4816 * @param views Touchable views found so far
4817 */
4818 public void addTouchables(ArrayList<View> views) {
4819 final int viewFlags = mViewFlags;
4820
4821 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4822 && (viewFlags & ENABLED_MASK) == ENABLED) {
4823 views.add(this);
4824 }
4825 }
4826
4827 /**
4828 * Call this to try to give focus to a specific view or to one of its
4829 * descendants.
4830 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004831 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4832 * false), or if it is focusable and it is not focusable in touch mode
4833 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004834 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004835 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004836 * have focus, and you want your parent to look for the next one.
4837 *
4838 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4839 * {@link #FOCUS_DOWN} and <code>null</code>.
4840 *
4841 * @return Whether this view or one of its descendants actually took focus.
4842 */
4843 public final boolean requestFocus() {
4844 return requestFocus(View.FOCUS_DOWN);
4845 }
4846
4847
4848 /**
4849 * Call this to try to give focus to a specific view or to one of its
4850 * descendants and give it a hint about what direction focus is heading.
4851 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004852 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4853 * false), or if it is focusable and it is not focusable in touch mode
4854 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004855 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004856 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004857 * have focus, and you want your parent to look for the next one.
4858 *
4859 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4860 * <code>null</code> set for the previously focused rectangle.
4861 *
4862 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4863 * @return Whether this view or one of its descendants actually took focus.
4864 */
4865 public final boolean requestFocus(int direction) {
4866 return requestFocus(direction, null);
4867 }
4868
4869 /**
4870 * Call this to try to give focus to a specific view or to one of its descendants
4871 * and give it hints about the direction and a specific rectangle that the focus
4872 * is coming from. The rectangle can help give larger views a finer grained hint
4873 * about where focus is coming from, and therefore, where to show selection, or
4874 * forward focus change internally.
4875 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004876 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4877 * false), or if it is focusable and it is not focusable in touch mode
4878 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004879 *
4880 * A View will not take focus if it is not visible.
4881 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004882 * A View will not take focus if one of its parents has
4883 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
4884 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004885 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004886 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004887 * have focus, and you want your parent to look for the next one.
4888 *
4889 * You may wish to override this method if your custom {@link View} has an internal
4890 * {@link View} that it wishes to forward the request to.
4891 *
4892 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4893 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
4894 * to give a finer grained hint about where focus is coming from. May be null
4895 * if there is no hint.
4896 * @return Whether this view or one of its descendants actually took focus.
4897 */
4898 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
4899 // need to be focusable
4900 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
4901 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4902 return false;
4903 }
4904
4905 // need to be focusable in touch mode if in touch mode
4906 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004907 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
4908 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004909 }
4910
4911 // need to not have any parents blocking us
4912 if (hasAncestorThatBlocksDescendantFocus()) {
4913 return false;
4914 }
4915
4916 handleFocusGainInternal(direction, previouslyFocusedRect);
4917 return true;
4918 }
4919
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004920 /** Gets the ViewAncestor, or null if not attached. */
4921 /*package*/ ViewAncestor getViewAncestor() {
Christopher Tate2c095f32010-10-04 14:13:40 -07004922 View root = getRootView();
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004923 return root != null ? (ViewAncestor)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07004924 }
4925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004926 /**
4927 * Call this to try to give focus to a specific view or to one of its descendants. This is a
4928 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
4929 * touch mode to request focus when they are touched.
4930 *
4931 * @return Whether this view or one of its descendants actually took focus.
4932 *
4933 * @see #isInTouchMode()
4934 *
4935 */
4936 public final boolean requestFocusFromTouch() {
4937 // Leave touch mode if we need to
4938 if (isInTouchMode()) {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004939 ViewAncestor viewRoot = getViewAncestor();
Christopher Tate2c095f32010-10-04 14:13:40 -07004940 if (viewRoot != null) {
4941 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004942 }
4943 }
4944 return requestFocus(View.FOCUS_DOWN);
4945 }
4946
4947 /**
4948 * @return Whether any ancestor of this view blocks descendant focus.
4949 */
4950 private boolean hasAncestorThatBlocksDescendantFocus() {
4951 ViewParent ancestor = mParent;
4952 while (ancestor instanceof ViewGroup) {
4953 final ViewGroup vgAncestor = (ViewGroup) ancestor;
4954 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
4955 return true;
4956 } else {
4957 ancestor = vgAncestor.getParent();
4958 }
4959 }
4960 return false;
4961 }
4962
4963 /**
Romain Guya440b002010-02-24 15:57:54 -08004964 * @hide
4965 */
4966 public void dispatchStartTemporaryDetach() {
4967 onStartTemporaryDetach();
4968 }
4969
4970 /**
4971 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004972 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
4973 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08004974 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004975 */
4976 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08004977 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08004978 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08004979 }
4980
4981 /**
4982 * @hide
4983 */
4984 public void dispatchFinishTemporaryDetach() {
4985 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004986 }
Romain Guy8506ab42009-06-11 17:35:47 -07004987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004988 /**
4989 * Called after {@link #onStartTemporaryDetach} when the container is done
4990 * changing the view.
4991 */
4992 public void onFinishTemporaryDetach() {
4993 }
Romain Guy8506ab42009-06-11 17:35:47 -07004994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004995 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004996 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
4997 * for this view's window. Returns null if the view is not currently attached
4998 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07004999 * just use the standard high-level event callbacks like
5000 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005001 */
5002 public KeyEvent.DispatcherState getKeyDispatcherState() {
5003 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5004 }
Joe Malin32736f02011-01-19 16:14:20 -08005005
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005006 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005007 * Dispatch a key event before it is processed by any input method
5008 * associated with the view hierarchy. This can be used to intercept
5009 * key events in special situations before the IME consumes them; a
5010 * typical example would be handling the BACK key to update the application's
5011 * UI instead of allowing the IME to see it and close itself.
5012 *
5013 * @param event The key event to be dispatched.
5014 * @return True if the event was handled, false otherwise.
5015 */
5016 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5017 return onKeyPreIme(event.getKeyCode(), event);
5018 }
5019
5020 /**
5021 * Dispatch a key event to the next view on the focus path. This path runs
5022 * from the top of the view tree down to the currently focused view. If this
5023 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5024 * the next node down the focus path. This method also fires any key
5025 * listeners.
5026 *
5027 * @param event The key event to be dispatched.
5028 * @return True if the event was handled, false otherwise.
5029 */
5030 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005031 if (mInputEventConsistencyVerifier != null) {
5032 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5033 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005034
Jeff Brown21bc5c92011-02-28 18:27:14 -08005035 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005036 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005037 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5038 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
5039 return true;
5040 }
5041
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005042 if (event.dispatch(this, mAttachInfo != null
5043 ? mAttachInfo.mKeyDispatchState : null, this)) {
5044 return true;
5045 }
5046
5047 if (mInputEventConsistencyVerifier != null) {
5048 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5049 }
5050 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005051 }
5052
5053 /**
5054 * Dispatches a key shortcut event.
5055 *
5056 * @param event The key event to be dispatched.
5057 * @return True if the event was handled by the view, false otherwise.
5058 */
5059 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5060 return onKeyShortcut(event.getKeyCode(), event);
5061 }
5062
5063 /**
5064 * Pass the touch screen motion event down to the target view, or this
5065 * view if it is the target.
5066 *
5067 * @param event The motion event to be dispatched.
5068 * @return True if the event was handled by the view, false otherwise.
5069 */
5070 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005071 if (mInputEventConsistencyVerifier != null) {
5072 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5073 }
5074
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005075 if (onFilterTouchEventForSecurity(event)) {
5076 //noinspection SimplifiableIfStatement
5077 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
5078 mOnTouchListener.onTouch(this, event)) {
5079 return true;
5080 }
5081
5082 if (onTouchEvent(event)) {
5083 return true;
5084 }
Jeff Brown85a31762010-09-01 17:01:00 -07005085 }
5086
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005087 if (mInputEventConsistencyVerifier != null) {
5088 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005089 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005090 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005091 }
5092
5093 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005094 * Filter the touch event to apply security policies.
5095 *
5096 * @param event The motion event to be filtered.
5097 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005098 *
Jeff Brown85a31762010-09-01 17:01:00 -07005099 * @see #getFilterTouchesWhenObscured
5100 */
5101 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005102 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005103 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5104 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5105 // Window is obscured, drop this touch.
5106 return false;
5107 }
5108 return true;
5109 }
5110
5111 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005112 * Pass a trackball motion event down to the focused view.
5113 *
5114 * @param event The motion event to be dispatched.
5115 * @return True if the event was handled by the view, false otherwise.
5116 */
5117 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005118 if (mInputEventConsistencyVerifier != null) {
5119 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5120 }
5121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005122 //Log.i("view", "view=" + this + ", " + event.toString());
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005123 if (onTrackballEvent(event)) {
5124 return true;
5125 }
5126
5127 if (mInputEventConsistencyVerifier != null) {
5128 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5129 }
5130 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005131 }
5132
5133 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005134 * Dispatch a generic motion event.
5135 * <p>
5136 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5137 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005138 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005139 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005140 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005141 *
5142 * @param event The motion event to be dispatched.
5143 * @return True if the event was handled by the view, false otherwise.
5144 */
5145 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005146 if (mInputEventConsistencyVerifier != null) {
5147 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5148 }
5149
Jeff Browna032cc02011-03-07 16:56:21 -08005150 final int source = event.getSource();
5151 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5152 final int action = event.getAction();
5153 if (action == MotionEvent.ACTION_HOVER_ENTER
5154 || action == MotionEvent.ACTION_HOVER_MOVE
5155 || action == MotionEvent.ACTION_HOVER_EXIT) {
5156 if (dispatchHoverEvent(event)) {
5157 return true;
5158 }
5159 } else if (dispatchGenericPointerEvent(event)) {
5160 return true;
5161 }
5162 } else if (dispatchGenericFocusedEvent(event)) {
5163 return true;
5164 }
5165
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005166 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08005167 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5168 && mOnGenericMotionListener.onGenericMotion(this, event)) {
5169 return true;
5170 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005171
5172 if (onGenericMotionEvent(event)) {
5173 return true;
5174 }
5175
5176 if (mInputEventConsistencyVerifier != null) {
5177 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5178 }
5179 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005180 }
5181
5182 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005183 * Dispatch a hover event.
5184 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005185 * Do not call this method directly.
5186 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005187 * </p>
5188 *
5189 * @param event The motion event to be dispatched.
5190 * @return True if the event was handled by the view, false otherwise.
5191 * @hide
5192 */
5193 protected boolean dispatchHoverEvent(MotionEvent event) {
5194 return onHoverEvent(event);
5195 }
5196
5197 /**
5198 * Dispatch a generic motion event to the view under the first pointer.
5199 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005200 * Do not call this method directly.
5201 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005202 * </p>
5203 *
5204 * @param event The motion event to be dispatched.
5205 * @return True if the event was handled by the view, false otherwise.
5206 * @hide
5207 */
5208 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5209 return false;
5210 }
5211
5212 /**
5213 * Dispatch a generic motion event to the currently focused view.
5214 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005215 * Do not call this method directly.
5216 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005217 * </p>
5218 *
5219 * @param event The motion event to be dispatched.
5220 * @return True if the event was handled by the view, false otherwise.
5221 * @hide
5222 */
5223 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5224 return false;
5225 }
5226
5227 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005228 * Dispatch a pointer event.
5229 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005230 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5231 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5232 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005233 * and should not be expected to handle other pointing device features.
5234 * </p>
5235 *
5236 * @param event The motion event to be dispatched.
5237 * @return True if the event was handled by the view, false otherwise.
5238 * @hide
5239 */
5240 public final boolean dispatchPointerEvent(MotionEvent event) {
5241 if (event.isTouchEvent()) {
5242 return dispatchTouchEvent(event);
5243 } else {
5244 return dispatchGenericMotionEvent(event);
5245 }
5246 }
5247
5248 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005249 * Called when the window containing this view gains or loses window focus.
5250 * ViewGroups should override to route to their children.
5251 *
5252 * @param hasFocus True if the window containing this view now has focus,
5253 * false otherwise.
5254 */
5255 public void dispatchWindowFocusChanged(boolean hasFocus) {
5256 onWindowFocusChanged(hasFocus);
5257 }
5258
5259 /**
5260 * Called when the window containing this view gains or loses focus. Note
5261 * that this is separate from view focus: to receive key events, both
5262 * your view and its window must have focus. If a window is displayed
5263 * on top of yours that takes input focus, then your own window will lose
5264 * focus but the view focus will remain unchanged.
5265 *
5266 * @param hasWindowFocus True if the window containing this view now has
5267 * focus, false otherwise.
5268 */
5269 public void onWindowFocusChanged(boolean hasWindowFocus) {
5270 InputMethodManager imm = InputMethodManager.peekInstance();
5271 if (!hasWindowFocus) {
5272 if (isPressed()) {
5273 setPressed(false);
5274 }
5275 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5276 imm.focusOut(this);
5277 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005278 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005279 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005280 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005281 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5282 imm.focusIn(this);
5283 }
5284 refreshDrawableState();
5285 }
5286
5287 /**
5288 * Returns true if this view is in a window that currently has window focus.
5289 * Note that this is not the same as the view itself having focus.
5290 *
5291 * @return True if this view is in a window that currently has window focus.
5292 */
5293 public boolean hasWindowFocus() {
5294 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5295 }
5296
5297 /**
Adam Powell326d8082009-12-09 15:10:07 -08005298 * Dispatch a view visibility change down the view hierarchy.
5299 * ViewGroups should override to route to their children.
5300 * @param changedView The view whose visibility changed. Could be 'this' or
5301 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005302 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5303 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005304 */
5305 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5306 onVisibilityChanged(changedView, visibility);
5307 }
5308
5309 /**
5310 * Called when the visibility of the view or an ancestor of the view is changed.
5311 * @param changedView The view whose visibility changed. Could be 'this' or
5312 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005313 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5314 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005315 */
5316 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005317 if (visibility == VISIBLE) {
5318 if (mAttachInfo != null) {
5319 initialAwakenScrollBars();
5320 } else {
5321 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5322 }
5323 }
Adam Powell326d8082009-12-09 15:10:07 -08005324 }
5325
5326 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005327 * Dispatch a hint about whether this view is displayed. For instance, when
5328 * a View moves out of the screen, it might receives a display hint indicating
5329 * the view is not displayed. Applications should not <em>rely</em> on this hint
5330 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005331 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005332 * @param hint A hint about whether or not this view is displayed:
5333 * {@link #VISIBLE} or {@link #INVISIBLE}.
5334 */
5335 public void dispatchDisplayHint(int hint) {
5336 onDisplayHint(hint);
5337 }
5338
5339 /**
5340 * Gives this view a hint about whether is displayed or not. For instance, when
5341 * a View moves out of the screen, it might receives a display hint indicating
5342 * the view is not displayed. Applications should not <em>rely</em> on this hint
5343 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005344 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005345 * @param hint A hint about whether or not this view is displayed:
5346 * {@link #VISIBLE} or {@link #INVISIBLE}.
5347 */
5348 protected void onDisplayHint(int hint) {
5349 }
5350
5351 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005352 * Dispatch a window visibility change down the view hierarchy.
5353 * ViewGroups should override to route to their children.
5354 *
5355 * @param visibility The new visibility of the window.
5356 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005357 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005358 */
5359 public void dispatchWindowVisibilityChanged(int visibility) {
5360 onWindowVisibilityChanged(visibility);
5361 }
5362
5363 /**
5364 * Called when the window containing has change its visibility
5365 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5366 * that this tells you whether or not your window is being made visible
5367 * to the window manager; this does <em>not</em> tell you whether or not
5368 * your window is obscured by other windows on the screen, even if it
5369 * is itself visible.
5370 *
5371 * @param visibility The new visibility of the window.
5372 */
5373 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005374 if (visibility == VISIBLE) {
5375 initialAwakenScrollBars();
5376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005377 }
5378
5379 /**
5380 * Returns the current visibility of the window this view is attached to
5381 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5382 *
5383 * @return Returns the current visibility of the view's window.
5384 */
5385 public int getWindowVisibility() {
5386 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5387 }
5388
5389 /**
5390 * Retrieve the overall visible display size in which the window this view is
5391 * attached to has been positioned in. This takes into account screen
5392 * decorations above the window, for both cases where the window itself
5393 * is being position inside of them or the window is being placed under
5394 * then and covered insets are used for the window to position its content
5395 * inside. In effect, this tells you the available area where content can
5396 * be placed and remain visible to users.
5397 *
5398 * <p>This function requires an IPC back to the window manager to retrieve
5399 * the requested information, so should not be used in performance critical
5400 * code like drawing.
5401 *
5402 * @param outRect Filled in with the visible display frame. If the view
5403 * is not attached to a window, this is simply the raw display size.
5404 */
5405 public void getWindowVisibleDisplayFrame(Rect outRect) {
5406 if (mAttachInfo != null) {
5407 try {
5408 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5409 } catch (RemoteException e) {
5410 return;
5411 }
5412 // XXX This is really broken, and probably all needs to be done
5413 // in the window manager, and we need to know more about whether
5414 // we want the area behind or in front of the IME.
5415 final Rect insets = mAttachInfo.mVisibleInsets;
5416 outRect.left += insets.left;
5417 outRect.top += insets.top;
5418 outRect.right -= insets.right;
5419 outRect.bottom -= insets.bottom;
5420 return;
5421 }
5422 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005423 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005424 }
5425
5426 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005427 * Dispatch a notification about a resource configuration change down
5428 * the view hierarchy.
5429 * ViewGroups should override to route to their children.
5430 *
5431 * @param newConfig The new resource configuration.
5432 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005433 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005434 */
5435 public void dispatchConfigurationChanged(Configuration newConfig) {
5436 onConfigurationChanged(newConfig);
5437 }
5438
5439 /**
5440 * Called when the current configuration of the resources being used
5441 * by the application have changed. You can use this to decide when
5442 * to reload resources that can changed based on orientation and other
5443 * configuration characterstics. You only need to use this if you are
5444 * not relying on the normal {@link android.app.Activity} mechanism of
5445 * recreating the activity instance upon a configuration change.
5446 *
5447 * @param newConfig The new resource configuration.
5448 */
5449 protected void onConfigurationChanged(Configuration newConfig) {
5450 }
5451
5452 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005453 * Private function to aggregate all per-view attributes in to the view
5454 * root.
5455 */
5456 void dispatchCollectViewAttributes(int visibility) {
5457 performCollectViewAttributes(visibility);
5458 }
5459
5460 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005461 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005462 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5463 mAttachInfo.mKeepScreenOn = true;
5464 }
5465 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5466 if (mOnSystemUiVisibilityChangeListener != null) {
5467 mAttachInfo.mHasSystemUiListeners = true;
5468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005469 }
5470 }
5471
5472 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005473 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005474 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005475 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5476 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005477 ai.mRecomputeGlobalAttributes = true;
5478 }
5479 }
5480 }
5481
5482 /**
5483 * Returns whether the device is currently in touch mode. Touch mode is entered
5484 * once the user begins interacting with the device by touch, and affects various
5485 * things like whether focus is always visible to the user.
5486 *
5487 * @return Whether the device is in touch mode.
5488 */
5489 @ViewDebug.ExportedProperty
5490 public boolean isInTouchMode() {
5491 if (mAttachInfo != null) {
5492 return mAttachInfo.mInTouchMode;
5493 } else {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005494 return ViewAncestor.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005495 }
5496 }
5497
5498 /**
5499 * Returns the context the view is running in, through which it can
5500 * access the current theme, resources, etc.
5501 *
5502 * @return The view's Context.
5503 */
5504 @ViewDebug.CapturedViewProperty
5505 public final Context getContext() {
5506 return mContext;
5507 }
5508
5509 /**
5510 * Handle a key event before it is processed by any input method
5511 * associated with the view hierarchy. This can be used to intercept
5512 * key events in special situations before the IME consumes them; a
5513 * typical example would be handling the BACK key to update the application's
5514 * UI instead of allowing the IME to see it and close itself.
5515 *
5516 * @param keyCode The value in event.getKeyCode().
5517 * @param event Description of the key event.
5518 * @return If you handled the event, return true. If you want to allow the
5519 * event to be handled by the next receiver, return false.
5520 */
5521 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5522 return false;
5523 }
5524
5525 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005526 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5527 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005528 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5529 * is released, if the view is enabled and clickable.
5530 *
5531 * @param keyCode A key code that represents the button pressed, from
5532 * {@link android.view.KeyEvent}.
5533 * @param event The KeyEvent object that defines the button action.
5534 */
5535 public boolean onKeyDown(int keyCode, KeyEvent event) {
5536 boolean result = false;
5537
5538 switch (keyCode) {
5539 case KeyEvent.KEYCODE_DPAD_CENTER:
5540 case KeyEvent.KEYCODE_ENTER: {
5541 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5542 return true;
5543 }
5544 // Long clickable items don't necessarily have to be clickable
5545 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5546 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5547 (event.getRepeatCount() == 0)) {
5548 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005549 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005550 return true;
5551 }
5552 break;
5553 }
5554 }
5555 return result;
5556 }
5557
5558 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005559 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5560 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5561 * the event).
5562 */
5563 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5564 return false;
5565 }
5566
5567 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005568 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5569 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005570 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5571 * {@link KeyEvent#KEYCODE_ENTER} is released.
5572 *
5573 * @param keyCode A key code that represents the button pressed, from
5574 * {@link android.view.KeyEvent}.
5575 * @param event The KeyEvent object that defines the button action.
5576 */
5577 public boolean onKeyUp(int keyCode, KeyEvent event) {
5578 boolean result = false;
5579
5580 switch (keyCode) {
5581 case KeyEvent.KEYCODE_DPAD_CENTER:
5582 case KeyEvent.KEYCODE_ENTER: {
5583 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5584 return true;
5585 }
5586 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5587 setPressed(false);
5588
5589 if (!mHasPerformedLongPress) {
5590 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005591 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005592
5593 result = performClick();
5594 }
5595 }
5596 break;
5597 }
5598 }
5599 return result;
5600 }
5601
5602 /**
5603 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5604 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5605 * the event).
5606 *
5607 * @param keyCode A key code that represents the button pressed, from
5608 * {@link android.view.KeyEvent}.
5609 * @param repeatCount The number of times the action was made.
5610 * @param event The KeyEvent object that defines the button action.
5611 */
5612 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5613 return false;
5614 }
5615
5616 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005617 * Called on the focused view when a key shortcut event is not handled.
5618 * Override this method to implement local key shortcuts for the View.
5619 * Key shortcuts can also be implemented by setting the
5620 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005621 *
5622 * @param keyCode The value in event.getKeyCode().
5623 * @param event Description of the key event.
5624 * @return If you handled the event, return true. If you want to allow the
5625 * event to be handled by the next receiver, return false.
5626 */
5627 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5628 return false;
5629 }
5630
5631 /**
5632 * Check whether the called view is a text editor, in which case it
5633 * would make sense to automatically display a soft input window for
5634 * it. Subclasses should override this if they implement
5635 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005636 * a call on that method would return a non-null InputConnection, and
5637 * they are really a first-class editor that the user would normally
5638 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005639 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005640 * <p>The default implementation always returns false. This does
5641 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5642 * will not be called or the user can not otherwise perform edits on your
5643 * view; it is just a hint to the system that this is not the primary
5644 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005645 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005646 * @return Returns true if this view is a text editor, else false.
5647 */
5648 public boolean onCheckIsTextEditor() {
5649 return false;
5650 }
Romain Guy8506ab42009-06-11 17:35:47 -07005651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005652 /**
5653 * Create a new InputConnection for an InputMethod to interact
5654 * with the view. The default implementation returns null, since it doesn't
5655 * support input methods. You can override this to implement such support.
5656 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005657 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005658 * <p>When implementing this, you probably also want to implement
5659 * {@link #onCheckIsTextEditor()} to indicate you will return a
5660 * non-null InputConnection.
5661 *
5662 * @param outAttrs Fill in with attribute information about the connection.
5663 */
5664 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5665 return null;
5666 }
5667
5668 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005669 * Called by the {@link android.view.inputmethod.InputMethodManager}
5670 * when a view who is not the current
5671 * input connection target is trying to make a call on the manager. The
5672 * default implementation returns false; you can override this to return
5673 * true for certain views if you are performing InputConnection proxying
5674 * to them.
5675 * @param view The View that is making the InputMethodManager call.
5676 * @return Return true to allow the call, false to reject.
5677 */
5678 public boolean checkInputConnectionProxy(View view) {
5679 return false;
5680 }
Romain Guy8506ab42009-06-11 17:35:47 -07005681
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005682 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005683 * Show the context menu for this view. It is not safe to hold on to the
5684 * menu after returning from this method.
5685 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005686 * You should normally not overload this method. Overload
5687 * {@link #onCreateContextMenu(ContextMenu)} or define an
5688 * {@link OnCreateContextMenuListener} to add items to the context menu.
5689 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005690 * @param menu The context menu to populate
5691 */
5692 public void createContextMenu(ContextMenu menu) {
5693 ContextMenuInfo menuInfo = getContextMenuInfo();
5694
5695 // Sets the current menu info so all items added to menu will have
5696 // my extra info set.
5697 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5698
5699 onCreateContextMenu(menu);
5700 if (mOnCreateContextMenuListener != null) {
5701 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5702 }
5703
5704 // Clear the extra information so subsequent items that aren't mine don't
5705 // have my extra info.
5706 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5707
5708 if (mParent != null) {
5709 mParent.createContextMenu(menu);
5710 }
5711 }
5712
5713 /**
5714 * Views should implement this if they have extra information to associate
5715 * with the context menu. The return result is supplied as a parameter to
5716 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5717 * callback.
5718 *
5719 * @return Extra information about the item for which the context menu
5720 * should be shown. This information will vary across different
5721 * subclasses of View.
5722 */
5723 protected ContextMenuInfo getContextMenuInfo() {
5724 return null;
5725 }
5726
5727 /**
5728 * Views should implement this if the view itself is going to add items to
5729 * the context menu.
5730 *
5731 * @param menu the context menu to populate
5732 */
5733 protected void onCreateContextMenu(ContextMenu menu) {
5734 }
5735
5736 /**
5737 * Implement this method to handle trackball motion events. The
5738 * <em>relative</em> movement of the trackball since the last event
5739 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5740 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5741 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5742 * they will often be fractional values, representing the more fine-grained
5743 * movement information available from a trackball).
5744 *
5745 * @param event The motion event.
5746 * @return True if the event was handled, false otherwise.
5747 */
5748 public boolean onTrackballEvent(MotionEvent event) {
5749 return false;
5750 }
5751
5752 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005753 * Implement this method to handle generic motion events.
5754 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005755 * Generic motion events describe joystick movements, mouse hovers, track pad
5756 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005757 * {@link MotionEvent#getSource() source} of the motion event specifies
5758 * the class of input that was received. Implementations of this method
5759 * must examine the bits in the source before processing the event.
5760 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005761 * </p><p>
5762 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5763 * are delivered to the view under the pointer. All other generic motion events are
5764 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005765 * </p>
5766 * <code>
5767 * public boolean onGenericMotionEvent(MotionEvent event) {
5768 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005769 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5770 * // process the joystick movement...
5771 * return true;
5772 * }
5773 * }
5774 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5775 * switch (event.getAction()) {
5776 * case MotionEvent.ACTION_HOVER_MOVE:
5777 * // process the mouse hover movement...
5778 * return true;
5779 * case MotionEvent.ACTION_SCROLL:
5780 * // process the scroll wheel movement...
5781 * return true;
5782 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005783 * }
5784 * return super.onGenericMotionEvent(event);
5785 * }
5786 * </code>
5787 *
5788 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08005789 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08005790 */
5791 public boolean onGenericMotionEvent(MotionEvent event) {
5792 return false;
5793 }
5794
5795 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005796 * Implement this method to handle hover events.
5797 * <p>
5798 * Hover events are pointer events with action {@link MotionEvent#ACTION_HOVER_ENTER},
5799 * {@link MotionEvent#ACTION_HOVER_MOVE}, or {@link MotionEvent#ACTION_HOVER_EXIT}.
5800 * </p><p>
5801 * The view receives hover enter as the pointer enters the bounds of the view and hover
5802 * exit as the pointer exits the bound of the view or just before the pointer goes down
Romain Guy5c22a8c2011-05-13 11:48:45 -07005803 * (which implies that {@link #onTouchEvent(MotionEvent)} will be called soon).
Jeff Browna032cc02011-03-07 16:56:21 -08005804 * </p><p>
5805 * If the view would like to handle the hover event itself and prevent its children
5806 * from receiving hover, it should return true from this method. If this method returns
5807 * true and a child has already received a hover enter event, the child will
5808 * automatically receive a hover exit event.
5809 * </p><p>
5810 * The default implementation sets the hovered state of the view if the view is
5811 * clickable.
5812 * </p>
5813 *
5814 * @param event The motion event that describes the hover.
5815 * @return True if this view handled the hover event and does not want its children
5816 * to receive the hover event.
5817 */
5818 public boolean onHoverEvent(MotionEvent event) {
Jeff Browna032cc02011-03-07 16:56:21 -08005819 switch (event.getAction()) {
5820 case MotionEvent.ACTION_HOVER_ENTER:
5821 setHovered(true);
5822 break;
5823
5824 case MotionEvent.ACTION_HOVER_EXIT:
5825 setHovered(false);
5826 break;
5827 }
5828
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005829 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08005830 }
5831
5832 /**
5833 * Returns true if the view is currently hovered.
5834 *
5835 * @return True if the view is currently hovered.
5836 */
5837 public boolean isHovered() {
5838 return (mPrivateFlags & HOVERED) != 0;
5839 }
5840
5841 /**
5842 * Sets whether the view is currently hovered.
5843 *
5844 * @param hovered True if the view is hovered.
5845 */
5846 public void setHovered(boolean hovered) {
5847 if (hovered) {
5848 if ((mPrivateFlags & HOVERED) == 0) {
5849 mPrivateFlags |= HOVERED;
5850 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005851 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Jeff Browna032cc02011-03-07 16:56:21 -08005852 }
5853 } else {
5854 if ((mPrivateFlags & HOVERED) != 0) {
5855 mPrivateFlags &= ~HOVERED;
5856 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005857 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Jeff Browna032cc02011-03-07 16:56:21 -08005858 }
5859 }
5860 }
5861
5862 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005863 * Implement this method to handle touch screen motion events.
5864 *
5865 * @param event The motion event.
5866 * @return True if the event was handled, false otherwise.
5867 */
5868 public boolean onTouchEvent(MotionEvent event) {
5869 final int viewFlags = mViewFlags;
5870
5871 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07005872 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
5873 mPrivateFlags &= ~PRESSED;
5874 refreshDrawableState();
5875 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005876 // A disabled view that is clickable still consumes the touch
5877 // events, it just doesn't respond to them.
5878 return (((viewFlags & CLICKABLE) == CLICKABLE ||
5879 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
5880 }
5881
5882 if (mTouchDelegate != null) {
5883 if (mTouchDelegate.onTouchEvent(event)) {
5884 return true;
5885 }
5886 }
5887
5888 if (((viewFlags & CLICKABLE) == CLICKABLE ||
5889 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
5890 switch (event.getAction()) {
5891 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08005892 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
5893 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005894 // take focus if we don't have it already and we should in
5895 // touch mode.
5896 boolean focusTaken = false;
5897 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
5898 focusTaken = requestFocus();
5899 }
5900
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08005901 if (prepressed) {
5902 // The button is being released before we actually
5903 // showed it as pressed. Make it show the pressed
5904 // state now (before scheduling the click) to ensure
5905 // the user sees it.
5906 mPrivateFlags |= PRESSED;
5907 refreshDrawableState();
5908 }
Joe Malin32736f02011-01-19 16:14:20 -08005909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005910 if (!mHasPerformedLongPress) {
5911 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005912 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005913
5914 // Only perform take click actions if we were in the pressed state
5915 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08005916 // Use a Runnable and post this rather than calling
5917 // performClick directly. This lets other visual state
5918 // of the view update before click actions start.
5919 if (mPerformClick == null) {
5920 mPerformClick = new PerformClick();
5921 }
5922 if (!post(mPerformClick)) {
5923 performClick();
5924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005925 }
5926 }
5927
5928 if (mUnsetPressedState == null) {
5929 mUnsetPressedState = new UnsetPressedState();
5930 }
5931
Adam Powelle14579b2009-12-16 18:39:52 -08005932 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08005933 postDelayed(mUnsetPressedState,
5934 ViewConfiguration.getPressedStateDuration());
5935 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005936 // If the post failed, unpress right now
5937 mUnsetPressedState.run();
5938 }
Adam Powelle14579b2009-12-16 18:39:52 -08005939 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005940 }
5941 break;
5942
5943 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08005944 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005945
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005946 if (performButtonActionOnTouchDown(event)) {
5947 break;
5948 }
5949
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005950 // Walk up the hierarchy to determine if we're inside a scrolling container.
5951 boolean isInScrollingContainer = false;
5952 ViewParent p = getParent();
5953 while (p != null && p instanceof ViewGroup) {
5954 if (((ViewGroup) p).shouldDelayChildPressedState()) {
5955 isInScrollingContainer = true;
5956 break;
5957 }
5958 p = p.getParent();
5959 }
5960
5961 // For views inside a scrolling container, delay the pressed feedback for
5962 // a short period in case this is a scroll.
5963 if (isInScrollingContainer) {
5964 mPrivateFlags |= PREPRESSED;
5965 if (mPendingCheckForTap == null) {
5966 mPendingCheckForTap = new CheckForTap();
5967 }
5968 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
5969 } else {
5970 // Not inside a scrolling container, so show the feedback right away
5971 mPrivateFlags |= PRESSED;
5972 refreshDrawableState();
5973 checkForLongClick(0);
5974 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005975 break;
5976
5977 case MotionEvent.ACTION_CANCEL:
5978 mPrivateFlags &= ~PRESSED;
5979 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08005980 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005981 break;
5982
5983 case MotionEvent.ACTION_MOVE:
5984 final int x = (int) event.getX();
5985 final int y = (int) event.getY();
5986
5987 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07005988 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005989 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08005990 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005991 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08005992 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05005993 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005994
5995 // Need to switch from pressed to not pressed
5996 mPrivateFlags &= ~PRESSED;
5997 refreshDrawableState();
5998 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005999 }
6000 break;
6001 }
6002 return true;
6003 }
6004
6005 return false;
6006 }
6007
6008 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006009 * Remove the longpress detection timer.
6010 */
6011 private void removeLongPressCallback() {
6012 if (mPendingCheckForLongPress != null) {
6013 removeCallbacks(mPendingCheckForLongPress);
6014 }
6015 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006016
6017 /**
6018 * Remove the pending click action
6019 */
6020 private void removePerformClickCallback() {
6021 if (mPerformClick != null) {
6022 removeCallbacks(mPerformClick);
6023 }
6024 }
6025
Adam Powelle14579b2009-12-16 18:39:52 -08006026 /**
Romain Guya440b002010-02-24 15:57:54 -08006027 * Remove the prepress detection timer.
6028 */
6029 private void removeUnsetPressCallback() {
6030 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6031 setPressed(false);
6032 removeCallbacks(mUnsetPressedState);
6033 }
6034 }
6035
6036 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006037 * Remove the tap detection timer.
6038 */
6039 private void removeTapCallback() {
6040 if (mPendingCheckForTap != null) {
6041 mPrivateFlags &= ~PREPRESSED;
6042 removeCallbacks(mPendingCheckForTap);
6043 }
6044 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006045
6046 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006047 * Cancels a pending long press. Your subclass can use this if you
6048 * want the context menu to come up if the user presses and holds
6049 * at the same place, but you don't want it to come up if they press
6050 * and then move around enough to cause scrolling.
6051 */
6052 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006053 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006054
6055 /*
6056 * The prepressed state handled by the tap callback is a display
6057 * construct, but the tap callback will post a long press callback
6058 * less its own timeout. Remove it here.
6059 */
6060 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006061 }
6062
6063 /**
6064 * Sets the TouchDelegate for this View.
6065 */
6066 public void setTouchDelegate(TouchDelegate delegate) {
6067 mTouchDelegate = delegate;
6068 }
6069
6070 /**
6071 * Gets the TouchDelegate for this View.
6072 */
6073 public TouchDelegate getTouchDelegate() {
6074 return mTouchDelegate;
6075 }
6076
6077 /**
6078 * Set flags controlling behavior of this view.
6079 *
6080 * @param flags Constant indicating the value which should be set
6081 * @param mask Constant indicating the bit range that should be changed
6082 */
6083 void setFlags(int flags, int mask) {
6084 int old = mViewFlags;
6085 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6086
6087 int changed = mViewFlags ^ old;
6088 if (changed == 0) {
6089 return;
6090 }
6091 int privateFlags = mPrivateFlags;
6092
6093 /* Check if the FOCUSABLE bit has changed */
6094 if (((changed & FOCUSABLE_MASK) != 0) &&
6095 ((privateFlags & HAS_BOUNDS) !=0)) {
6096 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6097 && ((privateFlags & FOCUSED) != 0)) {
6098 /* Give up focus if we are no longer focusable */
6099 clearFocus();
6100 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6101 && ((privateFlags & FOCUSED) == 0)) {
6102 /*
6103 * Tell the view system that we are now available to take focus
6104 * if no one else already has it.
6105 */
6106 if (mParent != null) mParent.focusableViewAvailable(this);
6107 }
6108 }
6109
6110 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6111 if ((changed & VISIBILITY_MASK) != 0) {
6112 /*
6113 * If this view is becoming visible, set the DRAWN flag so that
6114 * the next invalidate() will not be skipped.
6115 */
6116 mPrivateFlags |= DRAWN;
6117
6118 needGlobalAttributesUpdate(true);
6119
6120 // a view becoming visible is worth notifying the parent
6121 // about in case nothing has focus. even if this specific view
6122 // isn't focusable, it may contain something that is, so let
6123 // the root view try to give this focus if nothing else does.
6124 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6125 mParent.focusableViewAvailable(this);
6126 }
6127 }
6128 }
6129
6130 /* Check if the GONE bit has changed */
6131 if ((changed & GONE) != 0) {
6132 needGlobalAttributesUpdate(false);
6133 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006134 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006135
Romain Guyecd80ee2009-12-03 17:13:02 -08006136 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6137 if (hasFocus()) clearFocus();
6138 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006139 }
6140 if (mAttachInfo != null) {
6141 mAttachInfo.mViewVisibilityChanged = true;
6142 }
6143 }
6144
6145 /* Check if the VISIBLE bit has changed */
6146 if ((changed & INVISIBLE) != 0) {
6147 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006148 /*
6149 * If this view is becoming invisible, set the DRAWN flag so that
6150 * the next invalidate() will not be skipped.
6151 */
6152 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006153
6154 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6155 // root view becoming invisible shouldn't clear focus
6156 if (getRootView() != this) {
6157 clearFocus();
6158 }
6159 }
6160 if (mAttachInfo != null) {
6161 mAttachInfo.mViewVisibilityChanged = true;
6162 }
6163 }
6164
Adam Powell326d8082009-12-09 15:10:07 -08006165 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006166 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006167 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6168 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006169 }
Adam Powell326d8082009-12-09 15:10:07 -08006170 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6171 }
6172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006173 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6174 destroyDrawingCache();
6175 }
6176
6177 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6178 destroyDrawingCache();
6179 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006180 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006181 }
6182
6183 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6184 destroyDrawingCache();
6185 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6186 }
6187
6188 if ((changed & DRAW_MASK) != 0) {
6189 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6190 if (mBGDrawable != null) {
6191 mPrivateFlags &= ~SKIP_DRAW;
6192 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6193 } else {
6194 mPrivateFlags |= SKIP_DRAW;
6195 }
6196 } else {
6197 mPrivateFlags &= ~SKIP_DRAW;
6198 }
6199 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006200 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006201 }
6202
6203 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006204 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006205 mParent.recomputeViewAttributes(this);
6206 }
6207 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006208
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006209 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006210 requestLayout();
6211 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006212 }
6213
6214 /**
6215 * Change the view's z order in the tree, so it's on top of other sibling
6216 * views
6217 */
6218 public void bringToFront() {
6219 if (mParent != null) {
6220 mParent.bringChildToFront(this);
6221 }
6222 }
6223
6224 /**
6225 * This is called in response to an internal scroll in this view (i.e., the
6226 * view scrolled its own contents). This is typically as a result of
6227 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6228 * called.
6229 *
6230 * @param l Current horizontal scroll origin.
6231 * @param t Current vertical scroll origin.
6232 * @param oldl Previous horizontal scroll origin.
6233 * @param oldt Previous vertical scroll origin.
6234 */
6235 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
6236 mBackgroundSizeChanged = true;
6237
6238 final AttachInfo ai = mAttachInfo;
6239 if (ai != null) {
6240 ai.mViewScrollChanged = true;
6241 }
6242 }
6243
6244 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006245 * Interface definition for a callback to be invoked when the layout bounds of a view
6246 * changes due to layout processing.
6247 */
6248 public interface OnLayoutChangeListener {
6249 /**
6250 * Called when the focus state of a view has changed.
6251 *
6252 * @param v The view whose state has changed.
6253 * @param left The new value of the view's left property.
6254 * @param top The new value of the view's top property.
6255 * @param right The new value of the view's right property.
6256 * @param bottom The new value of the view's bottom property.
6257 * @param oldLeft The previous value of the view's left property.
6258 * @param oldTop The previous value of the view's top property.
6259 * @param oldRight The previous value of the view's right property.
6260 * @param oldBottom The previous value of the view's bottom property.
6261 */
6262 void onLayoutChange(View v, int left, int top, int right, int bottom,
6263 int oldLeft, int oldTop, int oldRight, int oldBottom);
6264 }
6265
6266 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006267 * This is called during layout when the size of this view has changed. If
6268 * you were just added to the view hierarchy, you're called with the old
6269 * values of 0.
6270 *
6271 * @param w Current width of this view.
6272 * @param h Current height of this view.
6273 * @param oldw Old width of this view.
6274 * @param oldh Old height of this view.
6275 */
6276 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6277 }
6278
6279 /**
6280 * Called by draw to draw the child views. This may be overridden
6281 * by derived classes to gain control just before its children are drawn
6282 * (but after its own view has been drawn).
6283 * @param canvas the canvas on which to draw the view
6284 */
6285 protected void dispatchDraw(Canvas canvas) {
6286 }
6287
6288 /**
6289 * Gets the parent of this view. Note that the parent is a
6290 * ViewParent and not necessarily a View.
6291 *
6292 * @return Parent of this view.
6293 */
6294 public final ViewParent getParent() {
6295 return mParent;
6296 }
6297
6298 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006299 * Set the horizontal scrolled position of your view. This will cause a call to
6300 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6301 * invalidated.
6302 * @param value the x position to scroll to
6303 */
6304 public void setScrollX(int value) {
6305 scrollTo(value, mScrollY);
6306 }
6307
6308 /**
6309 * Set the vertical scrolled position of your view. This will cause a call to
6310 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6311 * invalidated.
6312 * @param value the y position to scroll to
6313 */
6314 public void setScrollY(int value) {
6315 scrollTo(mScrollX, value);
6316 }
6317
6318 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006319 * Return the scrolled left position of this view. This is the left edge of
6320 * the displayed part of your view. You do not need to draw any pixels
6321 * farther left, since those are outside of the frame of your view on
6322 * screen.
6323 *
6324 * @return The left edge of the displayed part of your view, in pixels.
6325 */
6326 public final int getScrollX() {
6327 return mScrollX;
6328 }
6329
6330 /**
6331 * Return the scrolled top position of this view. This is the top edge of
6332 * the displayed part of your view. You do not need to draw any pixels above
6333 * it, since those are outside of the frame of your view on screen.
6334 *
6335 * @return The top edge of the displayed part of your view, in pixels.
6336 */
6337 public final int getScrollY() {
6338 return mScrollY;
6339 }
6340
6341 /**
6342 * Return the width of the your view.
6343 *
6344 * @return The width of your view, in pixels.
6345 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006346 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006347 public final int getWidth() {
6348 return mRight - mLeft;
6349 }
6350
6351 /**
6352 * Return the height of your view.
6353 *
6354 * @return The height of your view, in pixels.
6355 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006356 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006357 public final int getHeight() {
6358 return mBottom - mTop;
6359 }
6360
6361 /**
6362 * Return the visible drawing bounds of your view. Fills in the output
6363 * rectangle with the values from getScrollX(), getScrollY(),
6364 * getWidth(), and getHeight().
6365 *
6366 * @param outRect The (scrolled) drawing bounds of the view.
6367 */
6368 public void getDrawingRect(Rect outRect) {
6369 outRect.left = mScrollX;
6370 outRect.top = mScrollY;
6371 outRect.right = mScrollX + (mRight - mLeft);
6372 outRect.bottom = mScrollY + (mBottom - mTop);
6373 }
6374
6375 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006376 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6377 * raw width component (that is the result is masked by
6378 * {@link #MEASURED_SIZE_MASK}).
6379 *
6380 * @return The raw measured width of this view.
6381 */
6382 public final int getMeasuredWidth() {
6383 return mMeasuredWidth & MEASURED_SIZE_MASK;
6384 }
6385
6386 /**
6387 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006388 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006389 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006390 * This should be used during measurement and layout calculations only. Use
6391 * {@link #getWidth()} to see how wide a view is after layout.
6392 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006393 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006394 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006395 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006396 return mMeasuredWidth;
6397 }
6398
6399 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006400 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6401 * raw width component (that is the result is masked by
6402 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006403 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006404 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006405 */
6406 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006407 return mMeasuredHeight & MEASURED_SIZE_MASK;
6408 }
6409
6410 /**
6411 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006412 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006413 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6414 * This should be used during measurement and layout calculations only. Use
6415 * {@link #getHeight()} to see how wide a view is after layout.
6416 *
6417 * @return The measured width of this view as a bit mask.
6418 */
6419 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006420 return mMeasuredHeight;
6421 }
6422
6423 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006424 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6425 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6426 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6427 * and the height component is at the shifted bits
6428 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6429 */
6430 public final int getMeasuredState() {
6431 return (mMeasuredWidth&MEASURED_STATE_MASK)
6432 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6433 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6434 }
6435
6436 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006437 * The transform matrix of this view, which is calculated based on the current
6438 * roation, scale, and pivot properties.
6439 *
6440 * @see #getRotation()
6441 * @see #getScaleX()
6442 * @see #getScaleY()
6443 * @see #getPivotX()
6444 * @see #getPivotY()
6445 * @return The current transform matrix for the view
6446 */
6447 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07006448 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07006449 return mMatrix;
6450 }
6451
6452 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006453 * Utility function to determine if the value is far enough away from zero to be
6454 * considered non-zero.
6455 * @param value A floating point value to check for zero-ness
6456 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6457 */
6458 private static boolean nonzero(float value) {
6459 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6460 }
6461
6462 /**
Jeff Brown86671742010-09-30 20:00:15 -07006463 * Returns true if the transform matrix is the identity matrix.
6464 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006465 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006466 * @return True if the transform matrix is the identity matrix, false otherwise.
6467 */
Jeff Brown86671742010-09-30 20:00:15 -07006468 final boolean hasIdentityMatrix() {
6469 updateMatrix();
6470 return mMatrixIsIdentity;
6471 }
6472
6473 /**
6474 * Recomputes the transform matrix if necessary.
6475 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006476 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07006477 if (mMatrixDirty) {
6478 // transform-related properties have changed since the last time someone
6479 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006480
6481 // Figure out if we need to update the pivot point
6482 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006483 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006484 mPrevWidth = mRight - mLeft;
6485 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08006486 mPivotX = mPrevWidth / 2f;
6487 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07006488 }
6489 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006490 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07006491 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
6492 mMatrix.setTranslate(mTranslationX, mTranslationY);
6493 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
6494 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
6495 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07006496 if (mCamera == null) {
6497 mCamera = new Camera();
6498 matrix3D = new Matrix();
6499 }
6500 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07006501 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08006502 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07006503 mCamera.getMatrix(matrix3D);
6504 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006505 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07006506 mMatrix.postConcat(matrix3D);
6507 mCamera.restore();
6508 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006509 mMatrixDirty = false;
6510 mMatrixIsIdentity = mMatrix.isIdentity();
6511 mInverseMatrixDirty = true;
6512 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006513 }
6514
6515 /**
6516 * Utility method to retrieve the inverse of the current mMatrix property.
6517 * We cache the matrix to avoid recalculating it when transform properties
6518 * have not changed.
6519 *
6520 * @return The inverse of the current matrix of this view.
6521 */
Jeff Brown86671742010-09-30 20:00:15 -07006522 final Matrix getInverseMatrix() {
6523 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07006524 if (mInverseMatrixDirty) {
6525 if (mInverseMatrix == null) {
6526 mInverseMatrix = new Matrix();
6527 }
6528 mMatrix.invert(mInverseMatrix);
6529 mInverseMatrixDirty = false;
6530 }
6531 return mInverseMatrix;
6532 }
6533
6534 /**
Romain Guya5364ee2011-02-24 14:46:04 -08006535 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
6536 * views are drawn) from the camera to this view. The camera's distance
6537 * affects 3D transformations, for instance rotations around the X and Y
6538 * axis. If the rotationX or rotationY properties are changed and this view is
6539 * large (more than half the size of the screen), it is recommended to always
6540 * use a camera distance that's greater than the height (X axis rotation) or
6541 * the width (Y axis rotation) of this view.</p>
6542 *
6543 * <p>The distance of the camera from the view plane can have an affect on the
6544 * perspective distortion of the view when it is rotated around the x or y axis.
6545 * For example, a large distance will result in a large viewing angle, and there
6546 * will not be much perspective distortion of the view as it rotates. A short
6547 * distance may cause much more perspective distortion upon rotation, and can
6548 * also result in some drawing artifacts if the rotated view ends up partially
6549 * behind the camera (which is why the recommendation is to use a distance at
6550 * least as far as the size of the view, if the view is to be rotated.)</p>
6551 *
6552 * <p>The distance is expressed in "depth pixels." The default distance depends
6553 * on the screen density. For instance, on a medium density display, the
6554 * default distance is 1280. On a high density display, the default distance
6555 * is 1920.</p>
6556 *
6557 * <p>If you want to specify a distance that leads to visually consistent
6558 * results across various densities, use the following formula:</p>
6559 * <pre>
6560 * float scale = context.getResources().getDisplayMetrics().density;
6561 * view.setCameraDistance(distance * scale);
6562 * </pre>
6563 *
6564 * <p>The density scale factor of a high density display is 1.5,
6565 * and 1920 = 1280 * 1.5.</p>
6566 *
6567 * @param distance The distance in "depth pixels", if negative the opposite
6568 * value is used
6569 *
6570 * @see #setRotationX(float)
6571 * @see #setRotationY(float)
6572 */
6573 public void setCameraDistance(float distance) {
6574 invalidateParentCaches();
6575 invalidate(false);
6576
6577 final float dpi = mResources.getDisplayMetrics().densityDpi;
6578 if (mCamera == null) {
6579 mCamera = new Camera();
6580 matrix3D = new Matrix();
6581 }
6582
6583 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6584 mMatrixDirty = true;
6585
6586 invalidate(false);
6587 }
6588
6589 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006590 * The degrees that the view is rotated around the pivot point.
6591 *
Romain Guya5364ee2011-02-24 14:46:04 -08006592 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006593 * @see #getPivotX()
6594 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006595 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006596 * @return The degrees of rotation.
6597 */
6598 public float getRotation() {
6599 return mRotation;
6600 }
6601
6602 /**
Chet Haase897247b2010-09-09 14:54:47 -07006603 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6604 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006605 *
6606 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006607 *
6608 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006609 * @see #getPivotX()
6610 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006611 * @see #setRotationX(float)
6612 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006613 *
6614 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006615 */
6616 public void setRotation(float rotation) {
6617 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006618 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006619 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006620 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006621 mRotation = rotation;
6622 mMatrixDirty = true;
6623 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006624 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006625 }
6626 }
6627
6628 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006629 * The degrees that the view is rotated around the vertical axis through the pivot point.
6630 *
6631 * @see #getPivotX()
6632 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006633 * @see #setRotationY(float)
6634 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006635 * @return The degrees of Y rotation.
6636 */
6637 public float getRotationY() {
6638 return mRotationY;
6639 }
6640
6641 /**
Chet Haase897247b2010-09-09 14:54:47 -07006642 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6643 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6644 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006645 *
6646 * When rotating large views, it is recommended to adjust the camera distance
6647 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006648 *
6649 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006650 *
6651 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006652 * @see #getPivotX()
6653 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006654 * @see #setRotation(float)
6655 * @see #setRotationX(float)
6656 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006657 *
6658 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006659 */
6660 public void setRotationY(float rotationY) {
6661 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006662 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006663 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006664 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006665 mRotationY = rotationY;
6666 mMatrixDirty = true;
6667 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006668 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006669 }
6670 }
6671
6672 /**
6673 * The degrees that the view is rotated around the horizontal axis through the pivot point.
6674 *
6675 * @see #getPivotX()
6676 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006677 * @see #setRotationX(float)
6678 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006679 * @return The degrees of X rotation.
6680 */
6681 public float getRotationX() {
6682 return mRotationX;
6683 }
6684
6685 /**
Chet Haase897247b2010-09-09 14:54:47 -07006686 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6687 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6688 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006689 *
6690 * When rotating large views, it is recommended to adjust the camera distance
6691 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006692 *
6693 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006694 *
6695 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006696 * @see #getPivotX()
6697 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006698 * @see #setRotation(float)
6699 * @see #setRotationY(float)
6700 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006701 *
6702 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006703 */
6704 public void setRotationX(float rotationX) {
6705 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006706 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006707 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006708 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006709 mRotationX = rotationX;
6710 mMatrixDirty = true;
6711 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006712 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006713 }
6714 }
6715
6716 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006717 * The amount that the view is scaled in x around the pivot point, as a proportion of
6718 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6719 *
Joe Onorato93162322010-09-16 15:42:01 -04006720 * <p>By default, this is 1.0f.
6721 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006722 * @see #getPivotX()
6723 * @see #getPivotY()
6724 * @return The scaling factor.
6725 */
6726 public float getScaleX() {
6727 return mScaleX;
6728 }
6729
6730 /**
6731 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6732 * the view's unscaled width. A value of 1 means that no scaling is applied.
6733 *
6734 * @param scaleX The scaling factor.
6735 * @see #getPivotX()
6736 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006737 *
6738 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006739 */
6740 public void setScaleX(float scaleX) {
6741 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006742 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006743 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006744 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006745 mScaleX = scaleX;
6746 mMatrixDirty = true;
6747 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006748 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006749 }
6750 }
6751
6752 /**
6753 * The amount that the view is scaled in y around the pivot point, as a proportion of
6754 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6755 *
Joe Onorato93162322010-09-16 15:42:01 -04006756 * <p>By default, this is 1.0f.
6757 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006758 * @see #getPivotX()
6759 * @see #getPivotY()
6760 * @return The scaling factor.
6761 */
6762 public float getScaleY() {
6763 return mScaleY;
6764 }
6765
6766 /**
6767 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
6768 * the view's unscaled width. A value of 1 means that no scaling is applied.
6769 *
6770 * @param scaleY The scaling factor.
6771 * @see #getPivotX()
6772 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006773 *
6774 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07006775 */
6776 public void setScaleY(float scaleY) {
6777 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006778 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006779 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006780 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006781 mScaleY = scaleY;
6782 mMatrixDirty = true;
6783 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006784 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006785 }
6786 }
6787
6788 /**
6789 * The x location of the point around which the view is {@link #setRotation(float) rotated}
6790 * and {@link #setScaleX(float) scaled}.
6791 *
6792 * @see #getRotation()
6793 * @see #getScaleX()
6794 * @see #getScaleY()
6795 * @see #getPivotY()
6796 * @return The x location of the pivot point.
6797 */
6798 public float getPivotX() {
6799 return mPivotX;
6800 }
6801
6802 /**
6803 * Sets the x location of the point around which the view is
6804 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07006805 * By default, the pivot point is centered on the object.
6806 * Setting this property disables this behavior and causes the view to use only the
6807 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006808 *
6809 * @param pivotX The x location of the pivot point.
6810 * @see #getRotation()
6811 * @see #getScaleX()
6812 * @see #getScaleY()
6813 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006814 *
6815 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07006816 */
6817 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006818 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006819 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006820 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006821 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006822 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006823 mPivotX = pivotX;
6824 mMatrixDirty = true;
6825 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006826 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006827 }
6828 }
6829
6830 /**
6831 * The y location of the point around which the view is {@link #setRotation(float) rotated}
6832 * and {@link #setScaleY(float) scaled}.
6833 *
6834 * @see #getRotation()
6835 * @see #getScaleX()
6836 * @see #getScaleY()
6837 * @see #getPivotY()
6838 * @return The y location of the pivot point.
6839 */
6840 public float getPivotY() {
6841 return mPivotY;
6842 }
6843
6844 /**
6845 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07006846 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
6847 * Setting this property disables this behavior and causes the view to use only the
6848 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006849 *
6850 * @param pivotY The y location of the pivot point.
6851 * @see #getRotation()
6852 * @see #getScaleX()
6853 * @see #getScaleY()
6854 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006855 *
6856 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07006857 */
6858 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006859 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006860 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006861 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006862 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006863 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006864 mPivotY = pivotY;
6865 mMatrixDirty = true;
6866 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006867 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006868 }
6869 }
6870
6871 /**
6872 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
6873 * completely transparent and 1 means the view is completely opaque.
6874 *
Joe Onorato93162322010-09-16 15:42:01 -04006875 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07006876 * @return The opacity of the view.
6877 */
6878 public float getAlpha() {
6879 return mAlpha;
6880 }
6881
6882 /**
Romain Guy171c5922011-01-06 10:04:23 -08006883 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
6884 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08006885 *
Romain Guy171c5922011-01-06 10:04:23 -08006886 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
6887 * responsible for applying the opacity itself. Otherwise, calling this method is
6888 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08006889 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07006890 *
6891 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08006892 *
Joe Malin32736f02011-01-19 16:14:20 -08006893 * @see #setLayerType(int, android.graphics.Paint)
6894 *
Chet Haase73066682010-11-29 15:55:32 -08006895 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07006896 */
6897 public void setAlpha(float alpha) {
6898 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006899 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07006900 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07006901 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006902 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006903 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07006904 } else {
Romain Guya3496a92010-10-12 11:53:24 -07006905 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006906 invalidate(false);
6907 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006908 }
6909
6910 /**
Chet Haasea00f3862011-02-22 06:34:40 -08006911 * Faster version of setAlpha() which performs the same steps except there are
6912 * no calls to invalidate(). The caller of this function should perform proper invalidation
6913 * on the parent and this object. The return value indicates whether the subclass handles
6914 * alpha (the return value for onSetAlpha()).
6915 *
6916 * @param alpha The new value for the alpha property
6917 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
6918 */
6919 boolean setAlphaNoInvalidation(float alpha) {
6920 mAlpha = alpha;
6921 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
6922 if (subclassHandlesAlpha) {
6923 mPrivateFlags |= ALPHA_SET;
6924 } else {
6925 mPrivateFlags &= ~ALPHA_SET;
6926 }
6927 return subclassHandlesAlpha;
6928 }
6929
6930 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006931 * Top position of this view relative to its parent.
6932 *
6933 * @return The top of this view, in pixels.
6934 */
6935 @ViewDebug.CapturedViewProperty
6936 public final int getTop() {
6937 return mTop;
6938 }
6939
6940 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006941 * Sets the top position of this view relative to its parent. This method is meant to be called
6942 * by the layout system and should not generally be called otherwise, because the property
6943 * may be changed at any time by the layout.
6944 *
6945 * @param top The top of this view, in pixels.
6946 */
6947 public final void setTop(int top) {
6948 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07006949 updateMatrix();
6950 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006951 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006952 int minTop;
6953 int yLoc;
6954 if (top < mTop) {
6955 minTop = top;
6956 yLoc = top - mTop;
6957 } else {
6958 minTop = mTop;
6959 yLoc = 0;
6960 }
Chet Haasee9140a72011-02-16 16:23:29 -08006961 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006962 }
6963 } else {
6964 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006965 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006966 }
6967
Chet Haaseed032702010-10-01 14:05:54 -07006968 int width = mRight - mLeft;
6969 int oldHeight = mBottom - mTop;
6970
Chet Haase21cd1382010-09-01 17:42:29 -07006971 mTop = top;
6972
Chet Haaseed032702010-10-01 14:05:54 -07006973 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6974
Chet Haase21cd1382010-09-01 17:42:29 -07006975 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006976 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6977 // A change in dimension means an auto-centered pivot point changes, too
6978 mMatrixDirty = true;
6979 }
Chet Haase21cd1382010-09-01 17:42:29 -07006980 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006981 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006982 }
Chet Haase55dbb652010-12-21 20:15:08 -08006983 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006984 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006985 }
6986 }
6987
6988 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006989 * Bottom position of this view relative to its parent.
6990 *
6991 * @return The bottom of this view, in pixels.
6992 */
6993 @ViewDebug.CapturedViewProperty
6994 public final int getBottom() {
6995 return mBottom;
6996 }
6997
6998 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08006999 * True if this view has changed since the last time being drawn.
7000 *
7001 * @return The dirty state of this view.
7002 */
7003 public boolean isDirty() {
7004 return (mPrivateFlags & DIRTY_MASK) != 0;
7005 }
7006
7007 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007008 * Sets the bottom position of this view relative to its parent. This method is meant to be
7009 * called by the layout system and should not generally be called otherwise, because the
7010 * property may be changed at any time by the layout.
7011 *
7012 * @param bottom The bottom of this view, in pixels.
7013 */
7014 public final void setBottom(int bottom) {
7015 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007016 updateMatrix();
7017 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007018 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007019 int maxBottom;
7020 if (bottom < mBottom) {
7021 maxBottom = mBottom;
7022 } else {
7023 maxBottom = bottom;
7024 }
Chet Haasee9140a72011-02-16 16:23:29 -08007025 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007026 }
7027 } else {
7028 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007029 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007030 }
7031
Chet Haaseed032702010-10-01 14:05:54 -07007032 int width = mRight - mLeft;
7033 int oldHeight = mBottom - mTop;
7034
Chet Haase21cd1382010-09-01 17:42:29 -07007035 mBottom = bottom;
7036
Chet Haaseed032702010-10-01 14:05:54 -07007037 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7038
Chet Haase21cd1382010-09-01 17:42:29 -07007039 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007040 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7041 // A change in dimension means an auto-centered pivot point changes, too
7042 mMatrixDirty = true;
7043 }
Chet Haase21cd1382010-09-01 17:42:29 -07007044 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007045 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007046 }
Chet Haase55dbb652010-12-21 20:15:08 -08007047 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007048 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007049 }
7050 }
7051
7052 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007053 * Left position of this view relative to its parent.
7054 *
7055 * @return The left edge of this view, in pixels.
7056 */
7057 @ViewDebug.CapturedViewProperty
7058 public final int getLeft() {
7059 return mLeft;
7060 }
7061
7062 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007063 * Sets the left position of this view relative to its parent. This method is meant to be called
7064 * by the layout system and should not generally be called otherwise, because the property
7065 * may be changed at any time by the layout.
7066 *
7067 * @param left The bottom of this view, in pixels.
7068 */
7069 public final void setLeft(int left) {
7070 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007071 updateMatrix();
7072 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007073 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007074 int minLeft;
7075 int xLoc;
7076 if (left < mLeft) {
7077 minLeft = left;
7078 xLoc = left - mLeft;
7079 } else {
7080 minLeft = mLeft;
7081 xLoc = 0;
7082 }
Chet Haasee9140a72011-02-16 16:23:29 -08007083 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007084 }
7085 } else {
7086 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007087 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007088 }
7089
Chet Haaseed032702010-10-01 14:05:54 -07007090 int oldWidth = mRight - mLeft;
7091 int height = mBottom - mTop;
7092
Chet Haase21cd1382010-09-01 17:42:29 -07007093 mLeft = left;
7094
Chet Haaseed032702010-10-01 14:05:54 -07007095 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7096
Chet Haase21cd1382010-09-01 17:42:29 -07007097 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007098 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7099 // A change in dimension means an auto-centered pivot point changes, too
7100 mMatrixDirty = true;
7101 }
Chet Haase21cd1382010-09-01 17:42:29 -07007102 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007103 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007104 }
Chet Haase55dbb652010-12-21 20:15:08 -08007105 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007106 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007107 }
7108 }
7109
7110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007111 * Right position of this view relative to its parent.
7112 *
7113 * @return The right edge of this view, in pixels.
7114 */
7115 @ViewDebug.CapturedViewProperty
7116 public final int getRight() {
7117 return mRight;
7118 }
7119
7120 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007121 * Sets the right position of this view relative to its parent. This method is meant to be called
7122 * by the layout system and should not generally be called otherwise, because the property
7123 * may be changed at any time by the layout.
7124 *
7125 * @param right The bottom of this view, in pixels.
7126 */
7127 public final void setRight(int right) {
7128 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007129 updateMatrix();
7130 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007131 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007132 int maxRight;
7133 if (right < mRight) {
7134 maxRight = mRight;
7135 } else {
7136 maxRight = right;
7137 }
Chet Haasee9140a72011-02-16 16:23:29 -08007138 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007139 }
7140 } else {
7141 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007142 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007143 }
7144
Chet Haaseed032702010-10-01 14:05:54 -07007145 int oldWidth = mRight - mLeft;
7146 int height = mBottom - mTop;
7147
Chet Haase21cd1382010-09-01 17:42:29 -07007148 mRight = right;
7149
Chet Haaseed032702010-10-01 14:05:54 -07007150 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7151
Chet Haase21cd1382010-09-01 17:42:29 -07007152 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007153 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7154 // A change in dimension means an auto-centered pivot point changes, too
7155 mMatrixDirty = true;
7156 }
Chet Haase21cd1382010-09-01 17:42:29 -07007157 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007158 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007159 }
Chet Haase55dbb652010-12-21 20:15:08 -08007160 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007161 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007162 }
7163 }
7164
7165 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007166 * The visual x position of this view, in pixels. This is equivalent to the
7167 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007168 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007169 *
Chet Haasedf030d22010-07-30 17:22:38 -07007170 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007171 */
Chet Haasedf030d22010-07-30 17:22:38 -07007172 public float getX() {
7173 return mLeft + mTranslationX;
7174 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007175
Chet Haasedf030d22010-07-30 17:22:38 -07007176 /**
7177 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7178 * {@link #setTranslationX(float) translationX} property to be the difference between
7179 * the x value passed in and the current {@link #getLeft() left} property.
7180 *
7181 * @param x The visual x position of this view, in pixels.
7182 */
7183 public void setX(float x) {
7184 setTranslationX(x - mLeft);
7185 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007186
Chet Haasedf030d22010-07-30 17:22:38 -07007187 /**
7188 * The visual y position of this view, in pixels. This is equivalent to the
7189 * {@link #setTranslationY(float) translationY} property plus the current
7190 * {@link #getTop() top} property.
7191 *
7192 * @return The visual y position of this view, in pixels.
7193 */
7194 public float getY() {
7195 return mTop + mTranslationY;
7196 }
7197
7198 /**
7199 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7200 * {@link #setTranslationY(float) translationY} property to be the difference between
7201 * the y value passed in and the current {@link #getTop() top} property.
7202 *
7203 * @param y The visual y position of this view, in pixels.
7204 */
7205 public void setY(float y) {
7206 setTranslationY(y - mTop);
7207 }
7208
7209
7210 /**
7211 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7212 * This position is post-layout, in addition to wherever the object's
7213 * layout placed it.
7214 *
7215 * @return The horizontal position of this view relative to its left position, in pixels.
7216 */
7217 public float getTranslationX() {
7218 return mTranslationX;
7219 }
7220
7221 /**
7222 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7223 * This effectively positions the object post-layout, in addition to wherever the object's
7224 * layout placed it.
7225 *
7226 * @param translationX The horizontal position of this view relative to its left position,
7227 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007228 *
7229 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007230 */
7231 public void setTranslationX(float translationX) {
7232 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007233 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007234 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007235 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007236 mTranslationX = translationX;
7237 mMatrixDirty = true;
7238 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007239 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007240 }
7241 }
7242
7243 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007244 * The horizontal location of this view relative to its {@link #getTop() top} position.
7245 * This position is post-layout, in addition to wherever the object's
7246 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007247 *
Chet Haasedf030d22010-07-30 17:22:38 -07007248 * @return The vertical position of this view relative to its top position,
7249 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007250 */
Chet Haasedf030d22010-07-30 17:22:38 -07007251 public float getTranslationY() {
7252 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07007253 }
7254
7255 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007256 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7257 * This effectively positions the object post-layout, in addition to wherever the object's
7258 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007259 *
Chet Haasedf030d22010-07-30 17:22:38 -07007260 * @param translationY The vertical position of this view relative to its top position,
7261 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007262 *
7263 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007264 */
Chet Haasedf030d22010-07-30 17:22:38 -07007265 public void setTranslationY(float translationY) {
7266 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007267 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007268 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007269 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007270 mTranslationY = translationY;
7271 mMatrixDirty = true;
7272 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007273 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007274 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007275 }
7276
7277 /**
Romain Guyda489792011-02-03 01:05:15 -08007278 * @hide
7279 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007280 public void setFastTranslationX(float x) {
7281 mTranslationX = x;
7282 mMatrixDirty = true;
7283 }
7284
7285 /**
7286 * @hide
7287 */
7288 public void setFastTranslationY(float y) {
7289 mTranslationY = y;
7290 mMatrixDirty = true;
7291 }
7292
7293 /**
7294 * @hide
7295 */
Romain Guyda489792011-02-03 01:05:15 -08007296 public void setFastX(float x) {
7297 mTranslationX = x - mLeft;
7298 mMatrixDirty = true;
7299 }
7300
7301 /**
7302 * @hide
7303 */
7304 public void setFastY(float y) {
7305 mTranslationY = y - mTop;
7306 mMatrixDirty = true;
7307 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007308
Romain Guyda489792011-02-03 01:05:15 -08007309 /**
7310 * @hide
7311 */
7312 public void setFastScaleX(float x) {
7313 mScaleX = x;
7314 mMatrixDirty = true;
7315 }
7316
7317 /**
7318 * @hide
7319 */
7320 public void setFastScaleY(float y) {
7321 mScaleY = y;
7322 mMatrixDirty = true;
7323 }
7324
7325 /**
7326 * @hide
7327 */
7328 public void setFastAlpha(float alpha) {
7329 mAlpha = alpha;
7330 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007331
Romain Guyda489792011-02-03 01:05:15 -08007332 /**
7333 * @hide
7334 */
7335 public void setFastRotationY(float y) {
7336 mRotationY = y;
7337 mMatrixDirty = true;
7338 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007339
Romain Guyda489792011-02-03 01:05:15 -08007340 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007341 * Hit rectangle in parent's coordinates
7342 *
7343 * @param outRect The hit rectangle of the view.
7344 */
7345 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007346 updateMatrix();
7347 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007348 outRect.set(mLeft, mTop, mRight, mBottom);
7349 } else {
7350 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07007351 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07007352 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007353 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7354 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007355 }
7356 }
7357
7358 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007359 * Determines whether the given point, in local coordinates is inside the view.
7360 */
7361 /*package*/ final boolean pointInView(float localX, float localY) {
7362 return localX >= 0 && localX < (mRight - mLeft)
7363 && localY >= 0 && localY < (mBottom - mTop);
7364 }
7365
7366 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007367 * Utility method to determine whether the given point, in local coordinates,
7368 * is inside the view, where the area of the view is expanded by the slop factor.
7369 * This method is called while processing touch-move events to determine if the event
7370 * is still within the view.
7371 */
7372 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007373 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007374 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007375 }
7376
7377 /**
7378 * When a view has focus and the user navigates away from it, the next view is searched for
7379 * starting from the rectangle filled in by this method.
7380 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007381 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7382 * of the view. However, if your view maintains some idea of internal selection,
7383 * such as a cursor, or a selected row or column, you should override this method and
7384 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007385 *
7386 * @param r The rectangle to fill in, in this view's coordinates.
7387 */
7388 public void getFocusedRect(Rect r) {
7389 getDrawingRect(r);
7390 }
7391
7392 /**
7393 * If some part of this view is not clipped by any of its parents, then
7394 * return that area in r in global (root) coordinates. To convert r to local
7395 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7396 * -globalOffset.y)) If the view is completely clipped or translated out,
7397 * return false.
7398 *
7399 * @param r If true is returned, r holds the global coordinates of the
7400 * visible portion of this view.
7401 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7402 * between this view and its root. globalOffet may be null.
7403 * @return true if r is non-empty (i.e. part of the view is visible at the
7404 * root level.
7405 */
7406 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7407 int width = mRight - mLeft;
7408 int height = mBottom - mTop;
7409 if (width > 0 && height > 0) {
7410 r.set(0, 0, width, height);
7411 if (globalOffset != null) {
7412 globalOffset.set(-mScrollX, -mScrollY);
7413 }
7414 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7415 }
7416 return false;
7417 }
7418
7419 public final boolean getGlobalVisibleRect(Rect r) {
7420 return getGlobalVisibleRect(r, null);
7421 }
7422
7423 public final boolean getLocalVisibleRect(Rect r) {
7424 Point offset = new Point();
7425 if (getGlobalVisibleRect(r, offset)) {
7426 r.offset(-offset.x, -offset.y); // make r local
7427 return true;
7428 }
7429 return false;
7430 }
7431
7432 /**
7433 * Offset this view's vertical location by the specified number of pixels.
7434 *
7435 * @param offset the number of pixels to offset the view by
7436 */
7437 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007438 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007439 updateMatrix();
7440 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007441 final ViewParent p = mParent;
7442 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007443 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007444 int minTop;
7445 int maxBottom;
7446 int yLoc;
7447 if (offset < 0) {
7448 minTop = mTop + offset;
7449 maxBottom = mBottom;
7450 yLoc = offset;
7451 } else {
7452 minTop = mTop;
7453 maxBottom = mBottom + offset;
7454 yLoc = 0;
7455 }
7456 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
7457 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007458 }
7459 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007460 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007461 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007462
Chet Haasec3aa3612010-06-17 08:50:37 -07007463 mTop += offset;
7464 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007465
Chet Haasec3aa3612010-06-17 08:50:37 -07007466 if (!mMatrixIsIdentity) {
7467 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007468 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007469 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007470 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007472 }
7473
7474 /**
7475 * Offset this view's horizontal location by the specified amount of pixels.
7476 *
7477 * @param offset the numer of pixels to offset the view by
7478 */
7479 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007480 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007481 updateMatrix();
7482 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007483 final ViewParent p = mParent;
7484 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007485 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007486 int minLeft;
7487 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007488 if (offset < 0) {
7489 minLeft = mLeft + offset;
7490 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007491 } else {
7492 minLeft = mLeft;
7493 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007494 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007495 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07007496 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007497 }
7498 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007499 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007500 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007501
Chet Haasec3aa3612010-06-17 08:50:37 -07007502 mLeft += offset;
7503 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007504
Chet Haasec3aa3612010-06-17 08:50:37 -07007505 if (!mMatrixIsIdentity) {
7506 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007507 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007508 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007509 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007511 }
7512
7513 /**
7514 * Get the LayoutParams associated with this view. All views should have
7515 * layout parameters. These supply parameters to the <i>parent</i> of this
7516 * view specifying how it should be arranged. There are many subclasses of
7517 * ViewGroup.LayoutParams, and these correspond to the different subclasses
7518 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08007519 *
7520 * This method may return null if this View is not attached to a parent
7521 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
7522 * was not invoked successfully. When a View is attached to a parent
7523 * ViewGroup, this method must not return null.
7524 *
7525 * @return The LayoutParams associated with this view, or null if no
7526 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007527 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07007528 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007529 public ViewGroup.LayoutParams getLayoutParams() {
7530 return mLayoutParams;
7531 }
7532
7533 /**
7534 * Set the layout parameters associated with this view. These supply
7535 * parameters to the <i>parent</i> of this view specifying how it should be
7536 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
7537 * correspond to the different subclasses of ViewGroup that are responsible
7538 * for arranging their children.
7539 *
Romain Guy01c174b2011-02-22 11:51:06 -08007540 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007541 */
7542 public void setLayoutParams(ViewGroup.LayoutParams params) {
7543 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007544 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007545 }
7546 mLayoutParams = params;
7547 requestLayout();
7548 }
7549
7550 /**
7551 * Set the scrolled position of your view. This will cause a call to
7552 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7553 * invalidated.
7554 * @param x the x position to scroll to
7555 * @param y the y position to scroll to
7556 */
7557 public void scrollTo(int x, int y) {
7558 if (mScrollX != x || mScrollY != y) {
7559 int oldX = mScrollX;
7560 int oldY = mScrollY;
7561 mScrollX = x;
7562 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007563 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007564 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007565 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007566 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007568 }
7569 }
7570
7571 /**
7572 * Move the scrolled position of your view. This will cause a call to
7573 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7574 * invalidated.
7575 * @param x the amount of pixels to scroll by horizontally
7576 * @param y the amount of pixels to scroll by vertically
7577 */
7578 public void scrollBy(int x, int y) {
7579 scrollTo(mScrollX + x, mScrollY + y);
7580 }
7581
7582 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007583 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7584 * animation to fade the scrollbars out after a default delay. If a subclass
7585 * provides animated scrolling, the start delay should equal the duration
7586 * of the scrolling animation.</p>
7587 *
7588 * <p>The animation starts only if at least one of the scrollbars is
7589 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7590 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7591 * this method returns true, and false otherwise. If the animation is
7592 * started, this method calls {@link #invalidate()}; in that case the
7593 * caller should not call {@link #invalidate()}.</p>
7594 *
7595 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007596 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007597 *
7598 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7599 * and {@link #scrollTo(int, int)}.</p>
7600 *
7601 * @return true if the animation is played, false otherwise
7602 *
7603 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007604 * @see #scrollBy(int, int)
7605 * @see #scrollTo(int, int)
7606 * @see #isHorizontalScrollBarEnabled()
7607 * @see #isVerticalScrollBarEnabled()
7608 * @see #setHorizontalScrollBarEnabled(boolean)
7609 * @see #setVerticalScrollBarEnabled(boolean)
7610 */
7611 protected boolean awakenScrollBars() {
7612 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007613 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007614 }
7615
7616 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007617 * Trigger the scrollbars to draw.
7618 * This method differs from awakenScrollBars() only in its default duration.
7619 * initialAwakenScrollBars() will show the scroll bars for longer than
7620 * usual to give the user more of a chance to notice them.
7621 *
7622 * @return true if the animation is played, false otherwise.
7623 */
7624 private boolean initialAwakenScrollBars() {
7625 return mScrollCache != null &&
7626 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7627 }
7628
7629 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007630 * <p>
7631 * Trigger the scrollbars to draw. When invoked this method starts an
7632 * animation to fade the scrollbars out after a fixed delay. If a subclass
7633 * provides animated scrolling, the start delay should equal the duration of
7634 * the scrolling animation.
7635 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007636 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007637 * <p>
7638 * The animation starts only if at least one of the scrollbars is enabled,
7639 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7640 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7641 * this method returns true, and false otherwise. If the animation is
7642 * started, this method calls {@link #invalidate()}; in that case the caller
7643 * should not call {@link #invalidate()}.
7644 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007645 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007646 * <p>
7647 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007648 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007649 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007650 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007651 * @param startDelay the delay, in milliseconds, after which the animation
7652 * should start; when the delay is 0, the animation starts
7653 * immediately
7654 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007655 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007656 * @see #scrollBy(int, int)
7657 * @see #scrollTo(int, int)
7658 * @see #isHorizontalScrollBarEnabled()
7659 * @see #isVerticalScrollBarEnabled()
7660 * @see #setHorizontalScrollBarEnabled(boolean)
7661 * @see #setVerticalScrollBarEnabled(boolean)
7662 */
7663 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007664 return awakenScrollBars(startDelay, true);
7665 }
Joe Malin32736f02011-01-19 16:14:20 -08007666
Mike Cleron290947b2009-09-29 18:34:32 -07007667 /**
7668 * <p>
7669 * Trigger the scrollbars to draw. When invoked this method starts an
7670 * animation to fade the scrollbars out after a fixed delay. If a subclass
7671 * provides animated scrolling, the start delay should equal the duration of
7672 * the scrolling animation.
7673 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007674 *
Mike Cleron290947b2009-09-29 18:34:32 -07007675 * <p>
7676 * The animation starts only if at least one of the scrollbars is enabled,
7677 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7678 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7679 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08007680 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07007681 * is set to true; in that case the caller
7682 * should not call {@link #invalidate()}.
7683 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007684 *
Mike Cleron290947b2009-09-29 18:34:32 -07007685 * <p>
7686 * This method should be invoked everytime a subclass directly updates the
7687 * scroll parameters.
7688 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007689 *
Mike Cleron290947b2009-09-29 18:34:32 -07007690 * @param startDelay the delay, in milliseconds, after which the animation
7691 * should start; when the delay is 0, the animation starts
7692 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007693 *
Mike Cleron290947b2009-09-29 18:34:32 -07007694 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007695 *
Mike Cleron290947b2009-09-29 18:34:32 -07007696 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007697 *
Mike Cleron290947b2009-09-29 18:34:32 -07007698 * @see #scrollBy(int, int)
7699 * @see #scrollTo(int, int)
7700 * @see #isHorizontalScrollBarEnabled()
7701 * @see #isVerticalScrollBarEnabled()
7702 * @see #setHorizontalScrollBarEnabled(boolean)
7703 * @see #setVerticalScrollBarEnabled(boolean)
7704 */
7705 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007706 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007707
Mike Cleronf116bf82009-09-27 19:14:12 -07007708 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7709 return false;
7710 }
7711
7712 if (scrollCache.scrollBar == null) {
7713 scrollCache.scrollBar = new ScrollBarDrawable();
7714 }
7715
7716 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7717
Mike Cleron290947b2009-09-29 18:34:32 -07007718 if (invalidate) {
7719 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007720 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007721 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007722
7723 if (scrollCache.state == ScrollabilityCache.OFF) {
7724 // FIXME: this is copied from WindowManagerService.
7725 // We should get this value from the system when it
7726 // is possible to do so.
7727 final int KEY_REPEAT_FIRST_DELAY = 750;
7728 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7729 }
7730
7731 // Tell mScrollCache when we should start fading. This may
7732 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007733 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007734 scrollCache.fadeStartTime = fadeStartTime;
7735 scrollCache.state = ScrollabilityCache.ON;
7736
7737 // Schedule our fader to run, unscheduling any old ones first
7738 if (mAttachInfo != null) {
7739 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7740 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7741 }
7742
7743 return true;
7744 }
7745
7746 return false;
7747 }
7748
7749 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007750 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07007751 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
7752 * in the future. This must be called from a UI thread. To call from a non-UI
7753 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007754 *
7755 * WARNING: This method is destructive to dirty.
7756 * @param dirty the rectangle representing the bounds of the dirty region
7757 */
7758 public void invalidate(Rect dirty) {
7759 if (ViewDebug.TRACE_HIERARCHY) {
7760 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7761 }
7762
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007763 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007764 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7765 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007766 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007767 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007768 final ViewParent p = mParent;
7769 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007770 //noinspection PointlessBooleanExpression,ConstantConditions
7771 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7772 if (p != null && ai != null && ai.mHardwareAccelerated) {
7773 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007774 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007775 p.invalidateChild(this, null);
7776 return;
7777 }
Romain Guyaf636eb2010-12-09 17:47:21 -08007778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007779 if (p != null && ai != null) {
7780 final int scrollX = mScrollX;
7781 final int scrollY = mScrollY;
7782 final Rect r = ai.mTmpInvalRect;
7783 r.set(dirty.left - scrollX, dirty.top - scrollY,
7784 dirty.right - scrollX, dirty.bottom - scrollY);
7785 mParent.invalidateChild(this, r);
7786 }
7787 }
7788 }
7789
7790 /**
7791 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
7792 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007793 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
7794 * will be called at some point in the future. This must be called from
7795 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007796 * @param l the left position of the dirty region
7797 * @param t the top position of the dirty region
7798 * @param r the right position of the dirty region
7799 * @param b the bottom position of the dirty region
7800 */
7801 public void invalidate(int l, int t, int r, int b) {
7802 if (ViewDebug.TRACE_HIERARCHY) {
7803 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7804 }
7805
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007806 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007807 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7808 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007809 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007810 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007811 final ViewParent p = mParent;
7812 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007813 //noinspection PointlessBooleanExpression,ConstantConditions
7814 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7815 if (p != null && ai != null && ai.mHardwareAccelerated) {
7816 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007817 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007818 p.invalidateChild(this, null);
7819 return;
7820 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007822 if (p != null && ai != null && l < r && t < b) {
7823 final int scrollX = mScrollX;
7824 final int scrollY = mScrollY;
7825 final Rect tmpr = ai.mTmpInvalRect;
7826 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
7827 p.invalidateChild(this, tmpr);
7828 }
7829 }
7830 }
7831
7832 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07007833 * Invalidate the whole view. If the view is visible,
7834 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
7835 * the future. This must be called from a UI thread. To call from a non-UI thread,
7836 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007837 */
7838 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07007839 invalidate(true);
7840 }
Joe Malin32736f02011-01-19 16:14:20 -08007841
Chet Haaseed032702010-10-01 14:05:54 -07007842 /**
7843 * This is where the invalidate() work actually happens. A full invalidate()
7844 * causes the drawing cache to be invalidated, but this function can be called with
7845 * invalidateCache set to false to skip that invalidation step for cases that do not
7846 * need it (for example, a component that remains at the same dimensions with the same
7847 * content).
7848 *
7849 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
7850 * well. This is usually true for a full invalidate, but may be set to false if the
7851 * View's contents or dimensions have not changed.
7852 */
Romain Guy849d0a32011-02-01 17:20:48 -08007853 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007854 if (ViewDebug.TRACE_HIERARCHY) {
7855 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7856 }
7857
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007858 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08007859 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08007860 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
7861 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07007862 mPrivateFlags &= ~DRAWN;
7863 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08007864 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07007865 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007867 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07007868 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08007869 //noinspection PointlessBooleanExpression,ConstantConditions
7870 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7871 if (p != null && ai != null && ai.mHardwareAccelerated) {
7872 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007873 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007874 p.invalidateChild(this, null);
7875 return;
7876 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007877 }
Michael Jurkaebefea42010-11-15 16:04:01 -08007878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007879 if (p != null && ai != null) {
7880 final Rect r = ai.mTmpInvalRect;
7881 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7882 // Don't call invalidate -- we don't want to internally scroll
7883 // our own bounds
7884 p.invalidateChild(this, r);
7885 }
7886 }
7887 }
7888
7889 /**
Romain Guyda489792011-02-03 01:05:15 -08007890 * @hide
7891 */
7892 public void fastInvalidate() {
7893 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
7894 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7895 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
7896 if (mParent instanceof View) {
7897 ((View) mParent).mPrivateFlags |= INVALIDATED;
7898 }
7899 mPrivateFlags &= ~DRAWN;
7900 mPrivateFlags |= INVALIDATED;
7901 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07007902 if (mParent != null && mAttachInfo != null) {
7903 if (mAttachInfo.mHardwareAccelerated) {
7904 mParent.invalidateChild(this, null);
7905 } else {
7906 final Rect r = mAttachInfo.mTmpInvalRect;
7907 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7908 // Don't call invalidate -- we don't want to internally scroll
7909 // our own bounds
7910 mParent.invalidateChild(this, r);
7911 }
Romain Guyda489792011-02-03 01:05:15 -08007912 }
7913 }
7914 }
7915
7916 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08007917 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08007918 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7919 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08007920 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
7921 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08007922 *
7923 * @hide
7924 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08007925 protected void invalidateParentCaches() {
7926 if (mParent instanceof View) {
7927 ((View) mParent).mPrivateFlags |= INVALIDATED;
7928 }
7929 }
Joe Malin32736f02011-01-19 16:14:20 -08007930
Romain Guy0fd89bf2011-01-26 15:41:30 -08007931 /**
7932 * Used to indicate that the parent of this view should be invalidated. This functionality
7933 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7934 * which is necessary when various parent-managed properties of the view change, such as
7935 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
7936 * an invalidation event to the parent.
7937 *
7938 * @hide
7939 */
7940 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08007941 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007942 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08007943 }
7944 }
7945
7946 /**
Romain Guy24443ea2009-05-11 11:56:30 -07007947 * Indicates whether this View is opaque. An opaque View guarantees that it will
7948 * draw all the pixels overlapping its bounds using a fully opaque color.
7949 *
7950 * Subclasses of View should override this method whenever possible to indicate
7951 * whether an instance is opaque. Opaque Views are treated in a special way by
7952 * the View hierarchy, possibly allowing it to perform optimizations during
7953 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07007954 *
Romain Guy24443ea2009-05-11 11:56:30 -07007955 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07007956 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007957 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07007958 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07007959 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
7960 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07007961 }
7962
Adam Powell20232d02010-12-08 21:08:53 -08007963 /**
7964 * @hide
7965 */
7966 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07007967 // Opaque if:
7968 // - Has a background
7969 // - Background is opaque
7970 // - Doesn't have scrollbars or scrollbars are inside overlay
7971
7972 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
7973 mPrivateFlags |= OPAQUE_BACKGROUND;
7974 } else {
7975 mPrivateFlags &= ~OPAQUE_BACKGROUND;
7976 }
7977
7978 final int flags = mViewFlags;
7979 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
7980 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
7981 mPrivateFlags |= OPAQUE_SCROLLBARS;
7982 } else {
7983 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
7984 }
7985 }
7986
7987 /**
7988 * @hide
7989 */
7990 protected boolean hasOpaqueScrollbars() {
7991 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07007992 }
7993
7994 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007995 * @return A handler associated with the thread running the View. This
7996 * handler can be used to pump events in the UI events queue.
7997 */
7998 public Handler getHandler() {
7999 if (mAttachInfo != null) {
8000 return mAttachInfo.mHandler;
8001 }
8002 return null;
8003 }
8004
8005 /**
8006 * Causes the Runnable to be added to the message queue.
8007 * The runnable will be run on the user interface thread.
8008 *
8009 * @param action The Runnable that will be executed.
8010 *
8011 * @return Returns true if the Runnable was successfully placed in to the
8012 * message queue. Returns false on failure, usually because the
8013 * looper processing the message queue is exiting.
8014 */
8015 public boolean post(Runnable action) {
8016 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008017 AttachInfo attachInfo = mAttachInfo;
8018 if (attachInfo != null) {
8019 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008020 } else {
8021 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008022 ViewAncestor.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008023 return true;
8024 }
8025
8026 return handler.post(action);
8027 }
8028
8029 /**
8030 * Causes the Runnable to be added to the message queue, to be run
8031 * after the specified amount of time elapses.
8032 * The runnable will be run on the user interface thread.
8033 *
8034 * @param action The Runnable that will be executed.
8035 * @param delayMillis The delay (in milliseconds) until the Runnable
8036 * will be executed.
8037 *
8038 * @return true if the Runnable was successfully placed in to the
8039 * message queue. Returns false on failure, usually because the
8040 * looper processing the message queue is exiting. Note that a
8041 * result of true does not mean the Runnable will be processed --
8042 * if the looper is quit before the delivery time of the message
8043 * occurs then the message will be dropped.
8044 */
8045 public boolean postDelayed(Runnable action, long delayMillis) {
8046 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008047 AttachInfo attachInfo = mAttachInfo;
8048 if (attachInfo != null) {
8049 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008050 } else {
8051 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008052 ViewAncestor.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008053 return true;
8054 }
8055
8056 return handler.postDelayed(action, delayMillis);
8057 }
8058
8059 /**
8060 * Removes the specified Runnable from the message queue.
8061 *
8062 * @param action The Runnable to remove from the message handling queue
8063 *
8064 * @return true if this view could ask the Handler to remove the Runnable,
8065 * false otherwise. When the returned value is true, the Runnable
8066 * may or may not have been actually removed from the message queue
8067 * (for instance, if the Runnable was not in the queue already.)
8068 */
8069 public boolean removeCallbacks(Runnable action) {
8070 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008071 AttachInfo attachInfo = mAttachInfo;
8072 if (attachInfo != null) {
8073 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008074 } else {
8075 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008076 ViewAncestor.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008077 return true;
8078 }
8079
8080 handler.removeCallbacks(action);
8081 return true;
8082 }
8083
8084 /**
8085 * Cause an invalidate to happen on a subsequent cycle through the event loop.
8086 * Use this to invalidate the View from a non-UI thread.
8087 *
8088 * @see #invalidate()
8089 */
8090 public void postInvalidate() {
8091 postInvalidateDelayed(0);
8092 }
8093
8094 /**
8095 * Cause an invalidate of the specified area to happen on a subsequent cycle
8096 * through the event loop. Use this to invalidate the View from a non-UI thread.
8097 *
8098 * @param left The left coordinate of the rectangle to invalidate.
8099 * @param top The top coordinate of the rectangle to invalidate.
8100 * @param right The right coordinate of the rectangle to invalidate.
8101 * @param bottom The bottom coordinate of the rectangle to invalidate.
8102 *
8103 * @see #invalidate(int, int, int, int)
8104 * @see #invalidate(Rect)
8105 */
8106 public void postInvalidate(int left, int top, int right, int bottom) {
8107 postInvalidateDelayed(0, left, top, right, bottom);
8108 }
8109
8110 /**
8111 * Cause an invalidate to happen on a subsequent cycle through the event
8112 * loop. Waits for the specified amount of time.
8113 *
8114 * @param delayMilliseconds the duration in milliseconds to delay the
8115 * invalidation by
8116 */
8117 public void postInvalidateDelayed(long delayMilliseconds) {
8118 // We try only with the AttachInfo because there's no point in invalidating
8119 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008120 AttachInfo attachInfo = mAttachInfo;
8121 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008122 Message msg = Message.obtain();
8123 msg.what = AttachInfo.INVALIDATE_MSG;
8124 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008125 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008126 }
8127 }
8128
8129 /**
8130 * Cause an invalidate of the specified area to happen on a subsequent cycle
8131 * through the event loop. Waits for the specified amount of time.
8132 *
8133 * @param delayMilliseconds the duration in milliseconds to delay the
8134 * invalidation by
8135 * @param left The left coordinate of the rectangle to invalidate.
8136 * @param top The top coordinate of the rectangle to invalidate.
8137 * @param right The right coordinate of the rectangle to invalidate.
8138 * @param bottom The bottom coordinate of the rectangle to invalidate.
8139 */
8140 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8141 int right, int bottom) {
8142
8143 // We try only with the AttachInfo because there's no point in invalidating
8144 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008145 AttachInfo attachInfo = mAttachInfo;
8146 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008147 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8148 info.target = this;
8149 info.left = left;
8150 info.top = top;
8151 info.right = right;
8152 info.bottom = bottom;
8153
8154 final Message msg = Message.obtain();
8155 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8156 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008157 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008158 }
8159 }
8160
8161 /**
8162 * Called by a parent to request that a child update its values for mScrollX
8163 * and mScrollY if necessary. This will typically be done if the child is
8164 * animating a scroll using a {@link android.widget.Scroller Scroller}
8165 * object.
8166 */
8167 public void computeScroll() {
8168 }
8169
8170 /**
8171 * <p>Indicate whether the horizontal edges are faded when the view is
8172 * scrolled horizontally.</p>
8173 *
8174 * @return true if the horizontal edges should are faded on scroll, false
8175 * otherwise
8176 *
8177 * @see #setHorizontalFadingEdgeEnabled(boolean)
8178 * @attr ref android.R.styleable#View_fadingEdge
8179 */
8180 public boolean isHorizontalFadingEdgeEnabled() {
8181 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8182 }
8183
8184 /**
8185 * <p>Define whether the horizontal edges should be faded when this view
8186 * is scrolled horizontally.</p>
8187 *
8188 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8189 * be faded when the view is scrolled
8190 * horizontally
8191 *
8192 * @see #isHorizontalFadingEdgeEnabled()
8193 * @attr ref android.R.styleable#View_fadingEdge
8194 */
8195 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8196 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8197 if (horizontalFadingEdgeEnabled) {
8198 initScrollCache();
8199 }
8200
8201 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8202 }
8203 }
8204
8205 /**
8206 * <p>Indicate whether the vertical edges are faded when the view is
8207 * scrolled horizontally.</p>
8208 *
8209 * @return true if the vertical edges should are faded on scroll, false
8210 * otherwise
8211 *
8212 * @see #setVerticalFadingEdgeEnabled(boolean)
8213 * @attr ref android.R.styleable#View_fadingEdge
8214 */
8215 public boolean isVerticalFadingEdgeEnabled() {
8216 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8217 }
8218
8219 /**
8220 * <p>Define whether the vertical edges should be faded when this view
8221 * is scrolled vertically.</p>
8222 *
8223 * @param verticalFadingEdgeEnabled true if the vertical edges should
8224 * be faded when the view is scrolled
8225 * vertically
8226 *
8227 * @see #isVerticalFadingEdgeEnabled()
8228 * @attr ref android.R.styleable#View_fadingEdge
8229 */
8230 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8231 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8232 if (verticalFadingEdgeEnabled) {
8233 initScrollCache();
8234 }
8235
8236 mViewFlags ^= FADING_EDGE_VERTICAL;
8237 }
8238 }
8239
8240 /**
8241 * Returns the strength, or intensity, of the top faded edge. The strength is
8242 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8243 * returns 0.0 or 1.0 but no value in between.
8244 *
8245 * Subclasses should override this method to provide a smoother fade transition
8246 * when scrolling occurs.
8247 *
8248 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8249 */
8250 protected float getTopFadingEdgeStrength() {
8251 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8252 }
8253
8254 /**
8255 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8256 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8257 * returns 0.0 or 1.0 but no value in between.
8258 *
8259 * Subclasses should override this method to provide a smoother fade transition
8260 * when scrolling occurs.
8261 *
8262 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8263 */
8264 protected float getBottomFadingEdgeStrength() {
8265 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8266 computeVerticalScrollRange() ? 1.0f : 0.0f;
8267 }
8268
8269 /**
8270 * Returns the strength, or intensity, of the left faded edge. The strength is
8271 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8272 * returns 0.0 or 1.0 but no value in between.
8273 *
8274 * Subclasses should override this method to provide a smoother fade transition
8275 * when scrolling occurs.
8276 *
8277 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8278 */
8279 protected float getLeftFadingEdgeStrength() {
8280 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8281 }
8282
8283 /**
8284 * Returns the strength, or intensity, of the right faded edge. The strength is
8285 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8286 * returns 0.0 or 1.0 but no value in between.
8287 *
8288 * Subclasses should override this method to provide a smoother fade transition
8289 * when scrolling occurs.
8290 *
8291 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8292 */
8293 protected float getRightFadingEdgeStrength() {
8294 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8295 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8296 }
8297
8298 /**
8299 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8300 * scrollbar is not drawn by default.</p>
8301 *
8302 * @return true if the horizontal scrollbar should be painted, false
8303 * otherwise
8304 *
8305 * @see #setHorizontalScrollBarEnabled(boolean)
8306 */
8307 public boolean isHorizontalScrollBarEnabled() {
8308 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8309 }
8310
8311 /**
8312 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8313 * scrollbar is not drawn by default.</p>
8314 *
8315 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8316 * be painted
8317 *
8318 * @see #isHorizontalScrollBarEnabled()
8319 */
8320 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8321 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8322 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008323 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008324 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008325 }
8326 }
8327
8328 /**
8329 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
8330 * scrollbar is not drawn by default.</p>
8331 *
8332 * @return true if the vertical scrollbar should be painted, false
8333 * otherwise
8334 *
8335 * @see #setVerticalScrollBarEnabled(boolean)
8336 */
8337 public boolean isVerticalScrollBarEnabled() {
8338 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
8339 }
8340
8341 /**
8342 * <p>Define whether the vertical scrollbar should be drawn or not. The
8343 * scrollbar is not drawn by default.</p>
8344 *
8345 * @param verticalScrollBarEnabled true if the vertical scrollbar should
8346 * be painted
8347 *
8348 * @see #isVerticalScrollBarEnabled()
8349 */
8350 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
8351 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
8352 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008353 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008354 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008355 }
8356 }
8357
Adam Powell20232d02010-12-08 21:08:53 -08008358 /**
8359 * @hide
8360 */
8361 protected void recomputePadding() {
8362 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008363 }
Joe Malin32736f02011-01-19 16:14:20 -08008364
Mike Cleronfe81d382009-09-28 14:22:16 -07008365 /**
8366 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08008367 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008368 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08008369 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008370 */
8371 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
8372 initScrollCache();
8373 final ScrollabilityCache scrollabilityCache = mScrollCache;
8374 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008375 if (fadeScrollbars) {
8376 scrollabilityCache.state = ScrollabilityCache.OFF;
8377 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07008378 scrollabilityCache.state = ScrollabilityCache.ON;
8379 }
8380 }
Joe Malin32736f02011-01-19 16:14:20 -08008381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008382 /**
Joe Malin32736f02011-01-19 16:14:20 -08008383 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008384 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08008385 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008386 * @return true if scrollbar fading is enabled
8387 */
8388 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08008389 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008390 }
Joe Malin32736f02011-01-19 16:14:20 -08008391
Mike Cleron52f0a642009-09-28 18:21:37 -07008392 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008393 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
8394 * inset. When inset, they add to the padding of the view. And the scrollbars
8395 * can be drawn inside the padding area or on the edge of the view. For example,
8396 * if a view has a background drawable and you want to draw the scrollbars
8397 * inside the padding specified by the drawable, you can use
8398 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
8399 * appear at the edge of the view, ignoring the padding, then you can use
8400 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
8401 * @param style the style of the scrollbars. Should be one of
8402 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
8403 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
8404 * @see #SCROLLBARS_INSIDE_OVERLAY
8405 * @see #SCROLLBARS_INSIDE_INSET
8406 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8407 * @see #SCROLLBARS_OUTSIDE_INSET
8408 */
8409 public void setScrollBarStyle(int style) {
8410 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
8411 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07008412 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008413 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008414 }
8415 }
8416
8417 /**
8418 * <p>Returns the current scrollbar style.</p>
8419 * @return the current scrollbar style
8420 * @see #SCROLLBARS_INSIDE_OVERLAY
8421 * @see #SCROLLBARS_INSIDE_INSET
8422 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8423 * @see #SCROLLBARS_OUTSIDE_INSET
8424 */
8425 public int getScrollBarStyle() {
8426 return mViewFlags & SCROLLBARS_STYLE_MASK;
8427 }
8428
8429 /**
8430 * <p>Compute the horizontal range that the horizontal scrollbar
8431 * represents.</p>
8432 *
8433 * <p>The range is expressed in arbitrary units that must be the same as the
8434 * units used by {@link #computeHorizontalScrollExtent()} and
8435 * {@link #computeHorizontalScrollOffset()}.</p>
8436 *
8437 * <p>The default range is the drawing width of this view.</p>
8438 *
8439 * @return the total horizontal range represented by the horizontal
8440 * scrollbar
8441 *
8442 * @see #computeHorizontalScrollExtent()
8443 * @see #computeHorizontalScrollOffset()
8444 * @see android.widget.ScrollBarDrawable
8445 */
8446 protected int computeHorizontalScrollRange() {
8447 return getWidth();
8448 }
8449
8450 /**
8451 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
8452 * within the horizontal range. This value is used to compute the position
8453 * of the thumb within the scrollbar's track.</p>
8454 *
8455 * <p>The range is expressed in arbitrary units that must be the same as the
8456 * units used by {@link #computeHorizontalScrollRange()} and
8457 * {@link #computeHorizontalScrollExtent()}.</p>
8458 *
8459 * <p>The default offset is the scroll offset of this view.</p>
8460 *
8461 * @return the horizontal offset of the scrollbar's thumb
8462 *
8463 * @see #computeHorizontalScrollRange()
8464 * @see #computeHorizontalScrollExtent()
8465 * @see android.widget.ScrollBarDrawable
8466 */
8467 protected int computeHorizontalScrollOffset() {
8468 return mScrollX;
8469 }
8470
8471 /**
8472 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
8473 * within the horizontal range. This value is used to compute the length
8474 * of the thumb within the scrollbar's track.</p>
8475 *
8476 * <p>The range is expressed in arbitrary units that must be the same as the
8477 * units used by {@link #computeHorizontalScrollRange()} and
8478 * {@link #computeHorizontalScrollOffset()}.</p>
8479 *
8480 * <p>The default extent is the drawing width of this view.</p>
8481 *
8482 * @return the horizontal extent of the scrollbar's thumb
8483 *
8484 * @see #computeHorizontalScrollRange()
8485 * @see #computeHorizontalScrollOffset()
8486 * @see android.widget.ScrollBarDrawable
8487 */
8488 protected int computeHorizontalScrollExtent() {
8489 return getWidth();
8490 }
8491
8492 /**
8493 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
8494 *
8495 * <p>The range is expressed in arbitrary units that must be the same as the
8496 * units used by {@link #computeVerticalScrollExtent()} and
8497 * {@link #computeVerticalScrollOffset()}.</p>
8498 *
8499 * @return the total vertical range represented by the vertical scrollbar
8500 *
8501 * <p>The default range is the drawing height of this view.</p>
8502 *
8503 * @see #computeVerticalScrollExtent()
8504 * @see #computeVerticalScrollOffset()
8505 * @see android.widget.ScrollBarDrawable
8506 */
8507 protected int computeVerticalScrollRange() {
8508 return getHeight();
8509 }
8510
8511 /**
8512 * <p>Compute the vertical offset of the vertical scrollbar's thumb
8513 * within the horizontal range. This value is used to compute the position
8514 * of the thumb within the scrollbar's track.</p>
8515 *
8516 * <p>The range is expressed in arbitrary units that must be the same as the
8517 * units used by {@link #computeVerticalScrollRange()} and
8518 * {@link #computeVerticalScrollExtent()}.</p>
8519 *
8520 * <p>The default offset is the scroll offset of this view.</p>
8521 *
8522 * @return the vertical offset of the scrollbar's thumb
8523 *
8524 * @see #computeVerticalScrollRange()
8525 * @see #computeVerticalScrollExtent()
8526 * @see android.widget.ScrollBarDrawable
8527 */
8528 protected int computeVerticalScrollOffset() {
8529 return mScrollY;
8530 }
8531
8532 /**
8533 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
8534 * within the vertical range. This value is used to compute the length
8535 * of the thumb within the scrollbar's track.</p>
8536 *
8537 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08008538 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008539 * {@link #computeVerticalScrollOffset()}.</p>
8540 *
8541 * <p>The default extent is the drawing height of this view.</p>
8542 *
8543 * @return the vertical extent of the scrollbar's thumb
8544 *
8545 * @see #computeVerticalScrollRange()
8546 * @see #computeVerticalScrollOffset()
8547 * @see android.widget.ScrollBarDrawable
8548 */
8549 protected int computeVerticalScrollExtent() {
8550 return getHeight();
8551 }
8552
8553 /**
Adam Powell69159442011-06-13 17:53:06 -07008554 * Check if this view can be scrolled horizontally in a certain direction.
8555 *
8556 * @param direction Negative to check scrolling left, positive to check scrolling right.
8557 * @return true if this view can be scrolled in the specified direction, false otherwise.
8558 */
8559 public boolean canScrollHorizontally(int direction) {
8560 final int offset = computeHorizontalScrollOffset();
8561 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
8562 if (range == 0) return false;
8563 if (direction < 0) {
8564 return offset > 0;
8565 } else {
8566 return offset < range - 1;
8567 }
8568 }
8569
8570 /**
8571 * Check if this view can be scrolled vertically in a certain direction.
8572 *
8573 * @param direction Negative to check scrolling up, positive to check scrolling down.
8574 * @return true if this view can be scrolled in the specified direction, false otherwise.
8575 */
8576 public boolean canScrollVertically(int direction) {
8577 final int offset = computeVerticalScrollOffset();
8578 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
8579 if (range == 0) return false;
8580 if (direction < 0) {
8581 return offset > 0;
8582 } else {
8583 return offset < range - 1;
8584 }
8585 }
8586
8587 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008588 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8589 * scrollbars are painted only if they have been awakened first.</p>
8590 *
8591 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008592 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008593 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008594 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008595 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008596 // scrollbars are drawn only when the animation is running
8597 final ScrollabilityCache cache = mScrollCache;
8598 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008599
Mike Cleronf116bf82009-09-27 19:14:12 -07008600 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008601
Mike Cleronf116bf82009-09-27 19:14:12 -07008602 if (state == ScrollabilityCache.OFF) {
8603 return;
8604 }
Joe Malin32736f02011-01-19 16:14:20 -08008605
Mike Cleronf116bf82009-09-27 19:14:12 -07008606 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008607
Mike Cleronf116bf82009-09-27 19:14:12 -07008608 if (state == ScrollabilityCache.FADING) {
8609 // We're fading -- get our fade interpolation
8610 if (cache.interpolatorValues == null) {
8611 cache.interpolatorValues = new float[1];
8612 }
Joe Malin32736f02011-01-19 16:14:20 -08008613
Mike Cleronf116bf82009-09-27 19:14:12 -07008614 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008615
Mike Cleronf116bf82009-09-27 19:14:12 -07008616 // Stops the animation if we're done
8617 if (cache.scrollBarInterpolator.timeToValues(values) ==
8618 Interpolator.Result.FREEZE_END) {
8619 cache.state = ScrollabilityCache.OFF;
8620 } else {
8621 cache.scrollBar.setAlpha(Math.round(values[0]));
8622 }
Joe Malin32736f02011-01-19 16:14:20 -08008623
8624 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008625 // drawing. We only want this when we're fading so that
8626 // we prevent excessive redraws
8627 invalidate = true;
8628 } else {
8629 // We're just on -- but we may have been fading before so
8630 // reset alpha
8631 cache.scrollBar.setAlpha(255);
8632 }
8633
Joe Malin32736f02011-01-19 16:14:20 -08008634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008635 final int viewFlags = mViewFlags;
8636
8637 final boolean drawHorizontalScrollBar =
8638 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8639 final boolean drawVerticalScrollBar =
8640 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8641 && !isVerticalScrollBarHidden();
8642
8643 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8644 final int width = mRight - mLeft;
8645 final int height = mBottom - mTop;
8646
8647 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008648
Mike Reede8853fc2009-09-04 14:01:48 -04008649 final int scrollX = mScrollX;
8650 final int scrollY = mScrollY;
8651 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8652
Mike Cleronf116bf82009-09-27 19:14:12 -07008653 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008655 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008656 int size = scrollBar.getSize(false);
8657 if (size <= 0) {
8658 size = cache.scrollBarSize;
8659 }
8660
Mike Cleronf116bf82009-09-27 19:14:12 -07008661 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008662 computeHorizontalScrollOffset(),
8663 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008664 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008665 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008666 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008667 left = scrollX + (mPaddingLeft & inside);
8668 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8669 bottom = top + size;
8670 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8671 if (invalidate) {
8672 invalidate(left, top, right, bottom);
8673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008674 }
8675
8676 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008677 int size = scrollBar.getSize(true);
8678 if (size <= 0) {
8679 size = cache.scrollBarSize;
8680 }
8681
Mike Reede8853fc2009-09-04 14:01:48 -04008682 scrollBar.setParameters(computeVerticalScrollRange(),
8683 computeVerticalScrollOffset(),
8684 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008685 switch (mVerticalScrollbarPosition) {
8686 default:
8687 case SCROLLBAR_POSITION_DEFAULT:
8688 case SCROLLBAR_POSITION_RIGHT:
8689 left = scrollX + width - size - (mUserPaddingRight & inside);
8690 break;
8691 case SCROLLBAR_POSITION_LEFT:
8692 left = scrollX + (mUserPaddingLeft & inside);
8693 break;
8694 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008695 top = scrollY + (mPaddingTop & inside);
8696 right = left + size;
8697 bottom = scrollY + height - (mUserPaddingBottom & inside);
8698 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8699 if (invalidate) {
8700 invalidate(left, top, right, bottom);
8701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008702 }
8703 }
8704 }
8705 }
Romain Guy8506ab42009-06-11 17:35:47 -07008706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008707 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008708 * 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 -08008709 * FastScroller is visible.
8710 * @return whether to temporarily hide the vertical scrollbar
8711 * @hide
8712 */
8713 protected boolean isVerticalScrollBarHidden() {
8714 return false;
8715 }
8716
8717 /**
8718 * <p>Draw the horizontal scrollbar if
8719 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
8720 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008721 * @param canvas the canvas on which to draw the scrollbar
8722 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008723 *
8724 * @see #isHorizontalScrollBarEnabled()
8725 * @see #computeHorizontalScrollRange()
8726 * @see #computeHorizontalScrollExtent()
8727 * @see #computeHorizontalScrollOffset()
8728 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008729 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008730 */
Romain Guy8fb95422010-08-17 18:38:51 -07008731 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8732 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008733 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008734 scrollBar.draw(canvas);
8735 }
Mike Reede8853fc2009-09-04 14:01:48 -04008736
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008737 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008738 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8739 * returns true.</p>
8740 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008741 * @param canvas the canvas on which to draw the scrollbar
8742 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008743 *
8744 * @see #isVerticalScrollBarEnabled()
8745 * @see #computeVerticalScrollRange()
8746 * @see #computeVerticalScrollExtent()
8747 * @see #computeVerticalScrollOffset()
8748 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008749 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008750 */
Romain Guy8fb95422010-08-17 18:38:51 -07008751 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
8752 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04008753 scrollBar.setBounds(l, t, r, b);
8754 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008755 }
8756
8757 /**
8758 * Implement this to do your drawing.
8759 *
8760 * @param canvas the canvas on which the background will be drawn
8761 */
8762 protected void onDraw(Canvas canvas) {
8763 }
8764
8765 /*
8766 * Caller is responsible for calling requestLayout if necessary.
8767 * (This allows addViewInLayout to not request a new layout.)
8768 */
8769 void assignParent(ViewParent parent) {
8770 if (mParent == null) {
8771 mParent = parent;
8772 } else if (parent == null) {
8773 mParent = null;
8774 } else {
8775 throw new RuntimeException("view " + this + " being added, but"
8776 + " it already has a parent");
8777 }
8778 }
8779
8780 /**
8781 * This is called when the view is attached to a window. At this point it
8782 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008783 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
8784 * however it may be called any time before the first onDraw -- including
8785 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008786 *
8787 * @see #onDetachedFromWindow()
8788 */
8789 protected void onAttachedToWindow() {
8790 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
8791 mParent.requestTransparentRegion(this);
8792 }
Adam Powell8568c3a2010-04-19 14:26:11 -07008793 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
8794 initialAwakenScrollBars();
8795 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
8796 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08008797 jumpDrawablesToCurrentState();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008798 resetLayoutDirectionResolution();
8799 resolveLayoutDirectionIfNeeded();
8800 resolvePadding();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07008801 if (isFocused()) {
8802 InputMethodManager imm = InputMethodManager.peekInstance();
8803 imm.focusIn(this);
8804 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008805 }
Cibu Johny86666632010-02-22 13:01:02 -08008806
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008807 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008808 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
8809 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008810 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008811 private void resolveLayoutDirectionIfNeeded() {
8812 // Do not resolve if it is not needed
8813 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
8814
8815 // Clear any previous layout direction resolution
8816 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
8817
8818 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008819 switch (getLayoutDirection()) {
8820 case LAYOUT_DIRECTION_INHERIT:
Cibu Johny86666632010-02-22 13:01:02 -08008821 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07008822 if (mParent != null &&
8823 mParent instanceof ViewGroup &&
8824 ((ViewGroup) mParent).getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008825 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08008826 }
8827 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008828 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008829 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08008830 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07008831 case LAYOUT_DIRECTION_LOCALE:
8832 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008833 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07008834 }
8835 break;
8836 default:
8837 // Nothing to do, LTR by default
8838 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008839
8840 // Set to resolved
8841 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
8842 }
8843
8844 private void resolvePadding() {
8845 // If the user specified the absolute padding (either with android:padding or
8846 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
8847 // use the default padding or the padding from the background drawable
8848 // (stored at this point in mPadding*)
8849 switch (getResolvedLayoutDirection()) {
8850 case LAYOUT_DIRECTION_RTL:
8851 // Start user padding override Right user padding. Otherwise, if Right user
8852 // padding is not defined, use the default Right padding. If Right user padding
8853 // is defined, just use it.
8854 if (mUserPaddingStart >= 0) {
8855 mUserPaddingRight = mUserPaddingStart;
8856 } else if (mUserPaddingRight < 0) {
8857 mUserPaddingRight = mPaddingRight;
8858 }
8859 if (mUserPaddingEnd >= 0) {
8860 mUserPaddingLeft = mUserPaddingEnd;
8861 } else if (mUserPaddingLeft < 0) {
8862 mUserPaddingLeft = mPaddingLeft;
8863 }
8864 break;
8865 case LAYOUT_DIRECTION_LTR:
8866 default:
8867 // Start user padding override Left user padding. Otherwise, if Left user
8868 // padding is not defined, use the default left padding. If Left user padding
8869 // is defined, just use it.
Fabrice Di Megliof8ed4442011-06-21 17:53:03 -07008870 if (mUserPaddingStart > 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008871 mUserPaddingLeft = mUserPaddingStart;
8872 } else if (mUserPaddingLeft < 0) {
8873 mUserPaddingLeft = mPaddingLeft;
8874 }
Fabrice Di Megliof8ed4442011-06-21 17:53:03 -07008875 if (mUserPaddingEnd > 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008876 mUserPaddingRight = mUserPaddingEnd;
8877 } else if (mUserPaddingRight < 0) {
8878 mUserPaddingRight = mPaddingRight;
8879 }
8880 }
8881
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008882 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
8883
8884 recomputePadding();
8885 }
8886
8887 /**
8888 * Reset the resolved layout direction by clearing the corresponding flag
8889 */
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07008890 void resetLayoutDirectionResolution() {
8891 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008892 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07008893 }
8894
8895 /**
8896 * Check if a Locale is corresponding to a RTL script.
8897 *
8898 * @param locale Locale to check
8899 * @return true if a Locale is corresponding to a RTL script.
8900 */
8901 private static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07008902 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
8903 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008904 }
8905
8906 /**
8907 * This is called when the view is detached from a window. At this point it
8908 * no longer has a surface for drawing.
8909 *
8910 * @see #onAttachedToWindow()
8911 */
8912 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08008913 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08008914
Romain Guya440b002010-02-24 15:57:54 -08008915 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05008916 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08008917 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08008918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008919 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08008920
8921 if (mHardwareLayer != null) {
8922 mHardwareLayer.destroy();
8923 mHardwareLayer = null;
8924 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008925
Chet Haasedaf98e92011-01-10 14:10:36 -08008926 if (mDisplayList != null) {
8927 mDisplayList.invalidate();
8928 }
8929
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008930 if (mAttachInfo != null) {
8931 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
8932 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
8933 }
8934
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08008935 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008936 }
8937
8938 /**
8939 * @return The number of times this view has been attached to a window
8940 */
8941 protected int getWindowAttachCount() {
8942 return mWindowAttachCount;
8943 }
8944
8945 /**
8946 * Retrieve a unique token identifying the window this view is attached to.
8947 * @return Return the window's token for use in
8948 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
8949 */
8950 public IBinder getWindowToken() {
8951 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
8952 }
8953
8954 /**
8955 * Retrieve a unique token identifying the top-level "real" window of
8956 * the window that this view is attached to. That is, this is like
8957 * {@link #getWindowToken}, except if the window this view in is a panel
8958 * window (attached to another containing window), then the token of
8959 * the containing window is returned instead.
8960 *
8961 * @return Returns the associated window token, either
8962 * {@link #getWindowToken()} or the containing window's token.
8963 */
8964 public IBinder getApplicationWindowToken() {
8965 AttachInfo ai = mAttachInfo;
8966 if (ai != null) {
8967 IBinder appWindowToken = ai.mPanelParentWindowToken;
8968 if (appWindowToken == null) {
8969 appWindowToken = ai.mWindowToken;
8970 }
8971 return appWindowToken;
8972 }
8973 return null;
8974 }
8975
8976 /**
8977 * Retrieve private session object this view hierarchy is using to
8978 * communicate with the window manager.
8979 * @return the session object to communicate with the window manager
8980 */
8981 /*package*/ IWindowSession getWindowSession() {
8982 return mAttachInfo != null ? mAttachInfo.mSession : null;
8983 }
8984
8985 /**
8986 * @param info the {@link android.view.View.AttachInfo} to associated with
8987 * this view
8988 */
8989 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
8990 //System.out.println("Attached! " + this);
8991 mAttachInfo = info;
8992 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008993 // We will need to evaluate the drawable state at least once.
8994 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008995 if (mFloatingTreeObserver != null) {
8996 info.mTreeObserver.merge(mFloatingTreeObserver);
8997 mFloatingTreeObserver = null;
8998 }
8999 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9000 mAttachInfo.mScrollContainers.add(this);
9001 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9002 }
9003 performCollectViewAttributes(visibility);
9004 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009005
9006 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9007 mOnAttachStateChangeListeners;
9008 if (listeners != null && listeners.size() > 0) {
9009 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9010 // perform the dispatching. The iterator is a safe guard against listeners that
9011 // could mutate the list by calling the various add/remove methods. This prevents
9012 // the array from being modified while we iterate it.
9013 for (OnAttachStateChangeListener listener : listeners) {
9014 listener.onViewAttachedToWindow(this);
9015 }
9016 }
9017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009018 int vis = info.mWindowVisibility;
9019 if (vis != GONE) {
9020 onWindowVisibilityChanged(vis);
9021 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009022 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9023 // If nobody has evaluated the drawable state yet, then do it now.
9024 refreshDrawableState();
9025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009026 }
9027
9028 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009029 AttachInfo info = mAttachInfo;
9030 if (info != null) {
9031 int vis = info.mWindowVisibility;
9032 if (vis != GONE) {
9033 onWindowVisibilityChanged(GONE);
9034 }
9035 }
9036
9037 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009038
Adam Powell4afd62b2011-02-18 15:02:18 -08009039 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9040 mOnAttachStateChangeListeners;
9041 if (listeners != null && listeners.size() > 0) {
9042 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9043 // perform the dispatching. The iterator is a safe guard against listeners that
9044 // could mutate the list by calling the various add/remove methods. This prevents
9045 // the array from being modified while we iterate it.
9046 for (OnAttachStateChangeListener listener : listeners) {
9047 listener.onViewDetachedFromWindow(this);
9048 }
9049 }
9050
Romain Guy01d5edc2011-01-28 11:28:53 -08009051 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009052 mAttachInfo.mScrollContainers.remove(this);
9053 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9054 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009056 mAttachInfo = null;
9057 }
9058
9059 /**
9060 * Store this view hierarchy's frozen state into the given container.
9061 *
9062 * @param container The SparseArray in which to save the view's state.
9063 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009064 * @see #restoreHierarchyState(android.util.SparseArray)
9065 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9066 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009067 */
9068 public void saveHierarchyState(SparseArray<Parcelable> container) {
9069 dispatchSaveInstanceState(container);
9070 }
9071
9072 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009073 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9074 * this view and its children. May be overridden to modify how freezing happens to a
9075 * 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 -08009076 *
9077 * @param container The SparseArray in which to save the view's state.
9078 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009079 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9080 * @see #saveHierarchyState(android.util.SparseArray)
9081 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009082 */
9083 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9084 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9085 mPrivateFlags &= ~SAVE_STATE_CALLED;
9086 Parcelable state = onSaveInstanceState();
9087 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9088 throw new IllegalStateException(
9089 "Derived class did not call super.onSaveInstanceState()");
9090 }
9091 if (state != null) {
9092 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9093 // + ": " + state);
9094 container.put(mID, state);
9095 }
9096 }
9097 }
9098
9099 /**
9100 * Hook allowing a view to generate a representation of its internal state
9101 * that can later be used to create a new instance with that same state.
9102 * This state should only contain information that is not persistent or can
9103 * not be reconstructed later. For example, you will never store your
9104 * current position on screen because that will be computed again when a
9105 * new instance of the view is placed in its view hierarchy.
9106 * <p>
9107 * Some examples of things you may store here: the current cursor position
9108 * in a text view (but usually not the text itself since that is stored in a
9109 * content provider or other persistent storage), the currently selected
9110 * item in a list view.
9111 *
9112 * @return Returns a Parcelable object containing the view's current dynamic
9113 * state, or null if there is nothing interesting to save. The
9114 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009115 * @see #onRestoreInstanceState(android.os.Parcelable)
9116 * @see #saveHierarchyState(android.util.SparseArray)
9117 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009118 * @see #setSaveEnabled(boolean)
9119 */
9120 protected Parcelable onSaveInstanceState() {
9121 mPrivateFlags |= SAVE_STATE_CALLED;
9122 return BaseSavedState.EMPTY_STATE;
9123 }
9124
9125 /**
9126 * Restore this view hierarchy's frozen state from the given container.
9127 *
9128 * @param container The SparseArray which holds previously frozen states.
9129 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009130 * @see #saveHierarchyState(android.util.SparseArray)
9131 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9132 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009133 */
9134 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9135 dispatchRestoreInstanceState(container);
9136 }
9137
9138 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009139 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9140 * state for this view and its children. May be overridden to modify how restoring
9141 * happens to a view's children; for example, some views may want to not store state
9142 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009143 *
9144 * @param container The SparseArray which holds previously saved state.
9145 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009146 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9147 * @see #restoreHierarchyState(android.util.SparseArray)
9148 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009149 */
9150 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9151 if (mID != NO_ID) {
9152 Parcelable state = container.get(mID);
9153 if (state != null) {
9154 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9155 // + ": " + state);
9156 mPrivateFlags &= ~SAVE_STATE_CALLED;
9157 onRestoreInstanceState(state);
9158 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9159 throw new IllegalStateException(
9160 "Derived class did not call super.onRestoreInstanceState()");
9161 }
9162 }
9163 }
9164 }
9165
9166 /**
9167 * Hook allowing a view to re-apply a representation of its internal state that had previously
9168 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9169 * null state.
9170 *
9171 * @param state The frozen state that had previously been returned by
9172 * {@link #onSaveInstanceState}.
9173 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009174 * @see #onSaveInstanceState()
9175 * @see #restoreHierarchyState(android.util.SparseArray)
9176 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009177 */
9178 protected void onRestoreInstanceState(Parcelable state) {
9179 mPrivateFlags |= SAVE_STATE_CALLED;
9180 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009181 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9182 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009183 + "when two views of different type have the same id in the same hierarchy. "
9184 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009185 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009186 }
9187 }
9188
9189 /**
9190 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9191 *
9192 * @return the drawing start time in milliseconds
9193 */
9194 public long getDrawingTime() {
9195 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9196 }
9197
9198 /**
9199 * <p>Enables or disables the duplication of the parent's state into this view. When
9200 * duplication is enabled, this view gets its drawable state from its parent rather
9201 * than from its own internal properties.</p>
9202 *
9203 * <p>Note: in the current implementation, setting this property to true after the
9204 * view was added to a ViewGroup might have no effect at all. This property should
9205 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9206 *
9207 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9208 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009209 *
Gilles Debunnefb817032011-01-13 13:52:49 -08009210 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
9211 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009212 *
9213 * @param enabled True to enable duplication of the parent's drawable state, false
9214 * to disable it.
9215 *
9216 * @see #getDrawableState()
9217 * @see #isDuplicateParentStateEnabled()
9218 */
9219 public void setDuplicateParentStateEnabled(boolean enabled) {
9220 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
9221 }
9222
9223 /**
9224 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
9225 *
9226 * @return True if this view's drawable state is duplicated from the parent,
9227 * false otherwise
9228 *
9229 * @see #getDrawableState()
9230 * @see #setDuplicateParentStateEnabled(boolean)
9231 */
9232 public boolean isDuplicateParentStateEnabled() {
9233 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
9234 }
9235
9236 /**
Romain Guy171c5922011-01-06 10:04:23 -08009237 * <p>Specifies the type of layer backing this view. The layer can be
9238 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
9239 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009240 *
Romain Guy171c5922011-01-06 10:04:23 -08009241 * <p>A layer is associated with an optional {@link android.graphics.Paint}
9242 * instance that controls how the layer is composed on screen. The following
9243 * properties of the paint are taken into account when composing the layer:</p>
9244 * <ul>
9245 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
9246 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
9247 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
9248 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08009249 *
Romain Guy171c5922011-01-06 10:04:23 -08009250 * <p>If this view has an alpha value set to < 1.0 by calling
9251 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
9252 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
9253 * equivalent to setting a hardware layer on this view and providing a paint with
9254 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08009255 *
Romain Guy171c5922011-01-06 10:04:23 -08009256 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
9257 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
9258 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009259 *
Romain Guy171c5922011-01-06 10:04:23 -08009260 * @param layerType The ype of layer to use with this view, must be one of
9261 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9262 * {@link #LAYER_TYPE_HARDWARE}
9263 * @param paint The paint used to compose the layer. This argument is optional
9264 * and can be null. It is ignored when the layer type is
9265 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08009266 *
9267 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08009268 * @see #LAYER_TYPE_NONE
9269 * @see #LAYER_TYPE_SOFTWARE
9270 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08009271 * @see #setAlpha(float)
9272 *
Romain Guy171c5922011-01-06 10:04:23 -08009273 * @attr ref android.R.styleable#View_layerType
9274 */
9275 public void setLayerType(int layerType, Paint paint) {
9276 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08009277 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08009278 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
9279 }
Chet Haasedaf98e92011-01-10 14:10:36 -08009280
Romain Guyd6cd5722011-01-17 14:42:41 -08009281 if (layerType == mLayerType) {
9282 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
9283 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009284 invalidateParentCaches();
9285 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08009286 }
9287 return;
9288 }
Romain Guy171c5922011-01-06 10:04:23 -08009289
9290 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08009291 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -07009292 case LAYER_TYPE_HARDWARE:
9293 if (mHardwareLayer != null) {
9294 mHardwareLayer.destroy();
9295 mHardwareLayer = null;
9296 }
9297 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -08009298 case LAYER_TYPE_SOFTWARE:
9299 if (mDrawingCache != null) {
9300 mDrawingCache.recycle();
9301 mDrawingCache = null;
9302 }
Joe Malin32736f02011-01-19 16:14:20 -08009303
Romain Guy6c319ca2011-01-11 14:29:25 -08009304 if (mUnscaledDrawingCache != null) {
9305 mUnscaledDrawingCache.recycle();
9306 mUnscaledDrawingCache = null;
9307 }
9308 break;
Romain Guy6c319ca2011-01-11 14:29:25 -08009309 default:
9310 break;
Romain Guy171c5922011-01-06 10:04:23 -08009311 }
9312
9313 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08009314 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
9315 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
9316 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08009317
Romain Guy0fd89bf2011-01-26 15:41:30 -08009318 invalidateParentCaches();
9319 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08009320 }
9321
9322 /**
9323 * Indicates what type of layer is currently associated with this view. By default
9324 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
9325 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
9326 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08009327 *
Romain Guy171c5922011-01-06 10:04:23 -08009328 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9329 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08009330 *
9331 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08009332 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08009333 * @see #LAYER_TYPE_NONE
9334 * @see #LAYER_TYPE_SOFTWARE
9335 * @see #LAYER_TYPE_HARDWARE
9336 */
9337 public int getLayerType() {
9338 return mLayerType;
9339 }
Joe Malin32736f02011-01-19 16:14:20 -08009340
Romain Guy6c319ca2011-01-11 14:29:25 -08009341 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08009342 * Forces this view's layer to be created and this view to be rendered
9343 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
9344 * invoking this method will have no effect.
9345 *
9346 * This method can for instance be used to render a view into its layer before
9347 * starting an animation. If this view is complex, rendering into the layer
9348 * before starting the animation will avoid skipping frames.
9349 *
9350 * @throws IllegalStateException If this view is not attached to a window
9351 *
9352 * @see #setLayerType(int, android.graphics.Paint)
9353 */
9354 public void buildLayer() {
9355 if (mLayerType == LAYER_TYPE_NONE) return;
9356
9357 if (mAttachInfo == null) {
9358 throw new IllegalStateException("This view must be attached to a window first");
9359 }
9360
9361 switch (mLayerType) {
9362 case LAYER_TYPE_HARDWARE:
9363 getHardwareLayer();
9364 break;
9365 case LAYER_TYPE_SOFTWARE:
9366 buildDrawingCache(true);
9367 break;
9368 }
9369 }
9370
9371 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08009372 * <p>Returns a hardware layer that can be used to draw this view again
9373 * without executing its draw method.</p>
9374 *
9375 * @return A HardwareLayer ready to render, or null if an error occurred.
9376 */
Romain Guy5e7f7662011-01-24 22:35:56 -08009377 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08009378 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
9379 return null;
9380 }
9381
9382 final int width = mRight - mLeft;
9383 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08009384
Romain Guy6c319ca2011-01-11 14:29:25 -08009385 if (width == 0 || height == 0) {
9386 return null;
9387 }
9388
9389 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
9390 if (mHardwareLayer == null) {
9391 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
9392 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08009393 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009394 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
9395 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08009396 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009397 }
9398
Romain Guy59a12ca2011-06-09 17:48:21 -07009399 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -08009400 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
9401 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009402 try {
9403 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08009404 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08009405 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009406
Chet Haase88172fe2011-03-07 17:36:33 -08009407 final int restoreCount = canvas.save();
9408
Romain Guy6c319ca2011-01-11 14:29:25 -08009409 computeScroll();
9410 canvas.translate(-mScrollX, -mScrollY);
9411
Romain Guy6c319ca2011-01-11 14:29:25 -08009412 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009413
Romain Guy6c319ca2011-01-11 14:29:25 -08009414 // Fast path for layouts with no backgrounds
9415 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9416 mPrivateFlags &= ~DIRTY_MASK;
9417 dispatchDraw(canvas);
9418 } else {
9419 draw(canvas);
9420 }
Joe Malin32736f02011-01-19 16:14:20 -08009421
Chet Haase88172fe2011-03-07 17:36:33 -08009422 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08009423 } finally {
9424 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08009425 mHardwareLayer.end(currentCanvas);
9426 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009427 }
9428 }
9429
9430 return mHardwareLayer;
9431 }
Romain Guy171c5922011-01-06 10:04:23 -08009432
9433 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009434 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
9435 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
9436 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
9437 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
9438 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
9439 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009440 *
Romain Guy171c5922011-01-06 10:04:23 -08009441 * <p>Enabling the drawing cache is similar to
9442 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08009443 * acceleration is turned off. When hardware acceleration is turned on, enabling the
9444 * drawing cache has no effect on rendering because the system uses a different mechanism
9445 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
9446 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
9447 * for information on how to enable software and hardware layers.</p>
9448 *
9449 * <p>This API can be used to manually generate
9450 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
9451 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009452 *
9453 * @param enabled true to enable the drawing cache, false otherwise
9454 *
9455 * @see #isDrawingCacheEnabled()
9456 * @see #getDrawingCache()
9457 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08009458 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009459 */
9460 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009461 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009462 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
9463 }
9464
9465 /**
9466 * <p>Indicates whether the drawing cache is enabled for this view.</p>
9467 *
9468 * @return true if the drawing cache is enabled
9469 *
9470 * @see #setDrawingCacheEnabled(boolean)
9471 * @see #getDrawingCache()
9472 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009473 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009474 public boolean isDrawingCacheEnabled() {
9475 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
9476 }
9477
9478 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08009479 * Debugging utility which recursively outputs the dirty state of a view and its
9480 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08009481 *
Chet Haasedaf98e92011-01-10 14:10:36 -08009482 * @hide
9483 */
Romain Guy676b1732011-02-14 14:45:33 -08009484 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08009485 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
9486 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
9487 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
9488 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
9489 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
9490 if (clear) {
9491 mPrivateFlags &= clearMask;
9492 }
9493 if (this instanceof ViewGroup) {
9494 ViewGroup parent = (ViewGroup) this;
9495 final int count = parent.getChildCount();
9496 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08009497 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08009498 child.outputDirtyFlags(indent + " ", clear, clearMask);
9499 }
9500 }
9501 }
9502
9503 /**
9504 * This method is used by ViewGroup to cause its children to restore or recreate their
9505 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
9506 * to recreate its own display list, which would happen if it went through the normal
9507 * draw/dispatchDraw mechanisms.
9508 *
9509 * @hide
9510 */
9511 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08009512
9513 /**
9514 * A view that is not attached or hardware accelerated cannot create a display list.
9515 * This method checks these conditions and returns the appropriate result.
9516 *
9517 * @return true if view has the ability to create a display list, false otherwise.
9518 *
9519 * @hide
9520 */
9521 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08009522 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08009523 }
Joe Malin32736f02011-01-19 16:14:20 -08009524
Chet Haasedaf98e92011-01-10 14:10:36 -08009525 /**
Romain Guyb051e892010-09-28 19:09:36 -07009526 * <p>Returns a display list that can be used to draw this view again
9527 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009528 *
Romain Guyb051e892010-09-28 19:09:36 -07009529 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08009530 *
9531 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07009532 */
Chet Haasedaf98e92011-01-10 14:10:36 -08009533 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08009534 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07009535 return null;
9536 }
9537
Chet Haasedaf98e92011-01-10 14:10:36 -08009538 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
9539 mDisplayList == null || !mDisplayList.isValid() ||
9540 mRecreateDisplayList)) {
9541 // Don't need to recreate the display list, just need to tell our
9542 // children to restore/recreate theirs
9543 if (mDisplayList != null && mDisplayList.isValid() &&
9544 !mRecreateDisplayList) {
9545 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9546 mPrivateFlags &= ~DIRTY_MASK;
9547 dispatchGetDisplayList();
9548
9549 return mDisplayList;
9550 }
9551
9552 // If we got here, we're recreating it. Mark it as such to ensure that
9553 // we copy in child display lists into ours in drawChild()
9554 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08009555 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009556 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
9557 // If we're creating a new display list, make sure our parent gets invalidated
9558 // since they will need to recreate their display list to account for this
9559 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08009560 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08009561 }
Romain Guyb051e892010-09-28 19:09:36 -07009562
9563 final HardwareCanvas canvas = mDisplayList.start();
9564 try {
9565 int width = mRight - mLeft;
9566 int height = mBottom - mTop;
9567
9568 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08009569 // The dirty rect should always be null for a display list
9570 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07009571
Chet Haase88172fe2011-03-07 17:36:33 -08009572 final int restoreCount = canvas.save();
9573
Chet Haasedaf98e92011-01-10 14:10:36 -08009574 computeScroll();
9575 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009576 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009577
Romain Guyb051e892010-09-28 19:09:36 -07009578 // Fast path for layouts with no backgrounds
9579 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9580 mPrivateFlags &= ~DIRTY_MASK;
9581 dispatchDraw(canvas);
9582 } else {
9583 draw(canvas);
9584 }
Joe Malin32736f02011-01-19 16:14:20 -08009585
Chet Haase88172fe2011-03-07 17:36:33 -08009586 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07009587 } finally {
9588 canvas.onPostDraw();
9589
9590 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07009591 }
Chet Haase77785f92011-01-25 23:22:09 -08009592 } else {
9593 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9594 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07009595 }
9596
9597 return mDisplayList;
9598 }
9599
9600 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009601 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009602 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009603 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009604 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009605 * @see #getDrawingCache(boolean)
9606 */
9607 public Bitmap getDrawingCache() {
9608 return getDrawingCache(false);
9609 }
9610
9611 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009612 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
9613 * is null when caching is disabled. If caching is enabled and the cache is not ready,
9614 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
9615 * draw from the cache when the cache is enabled. To benefit from the cache, you must
9616 * request the drawing cache by calling this method and draw it on screen if the
9617 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009618 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009619 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9620 * this method will create a bitmap of the same size as this view. Because this bitmap
9621 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9622 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9623 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9624 * size than the view. This implies that your application must be able to handle this
9625 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009626 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009627 * @param autoScale Indicates whether the generated bitmap should be scaled based on
9628 * the current density of the screen when the application is in compatibility
9629 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009630 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009631 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009632 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009633 * @see #setDrawingCacheEnabled(boolean)
9634 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07009635 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009636 * @see #destroyDrawingCache()
9637 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009638 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009639 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
9640 return null;
9641 }
9642 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009643 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009644 }
Romain Guy02890fd2010-08-06 17:58:44 -07009645 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009646 }
9647
9648 /**
9649 * <p>Frees the resources used by the drawing cache. If you call
9650 * {@link #buildDrawingCache()} manually without calling
9651 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9652 * should cleanup the cache with this method afterwards.</p>
9653 *
9654 * @see #setDrawingCacheEnabled(boolean)
9655 * @see #buildDrawingCache()
9656 * @see #getDrawingCache()
9657 */
9658 public void destroyDrawingCache() {
9659 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009660 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009661 mDrawingCache = null;
9662 }
Romain Guyfbd8f692009-06-26 14:51:58 -07009663 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009664 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07009665 mUnscaledDrawingCache = null;
9666 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009667 }
9668
9669 /**
9670 * Setting a solid background color for the drawing cache's bitmaps will improve
9671 * perfromance and memory usage. Note, though that this should only be used if this
9672 * view will always be drawn on top of a solid color.
9673 *
9674 * @param color The background color to use for the drawing cache's bitmap
9675 *
9676 * @see #setDrawingCacheEnabled(boolean)
9677 * @see #buildDrawingCache()
9678 * @see #getDrawingCache()
9679 */
9680 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08009681 if (color != mDrawingCacheBackgroundColor) {
9682 mDrawingCacheBackgroundColor = color;
9683 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009685 }
9686
9687 /**
9688 * @see #setDrawingCacheBackgroundColor(int)
9689 *
9690 * @return The background color to used for the drawing cache's bitmap
9691 */
9692 public int getDrawingCacheBackgroundColor() {
9693 return mDrawingCacheBackgroundColor;
9694 }
9695
9696 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009697 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009698 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009699 * @see #buildDrawingCache(boolean)
9700 */
9701 public void buildDrawingCache() {
9702 buildDrawingCache(false);
9703 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08009704
Romain Guyfbd8f692009-06-26 14:51:58 -07009705 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009706 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
9707 *
9708 * <p>If you call {@link #buildDrawingCache()} manually without calling
9709 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9710 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009711 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009712 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9713 * this method will create a bitmap of the same size as this view. Because this bitmap
9714 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9715 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9716 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9717 * size than the view. This implies that your application must be able to handle this
9718 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009719 *
Romain Guy0d9275e2010-10-26 14:22:30 -07009720 * <p>You should avoid calling this method when hardware acceleration is enabled. If
9721 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08009722 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07009723 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009724 *
9725 * @see #getDrawingCache()
9726 * @see #destroyDrawingCache()
9727 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009728 public void buildDrawingCache(boolean autoScale) {
9729 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07009730 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009731 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009732
9733 if (ViewDebug.TRACE_HIERARCHY) {
9734 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
9735 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009736
Romain Guy8506ab42009-06-11 17:35:47 -07009737 int width = mRight - mLeft;
9738 int height = mBottom - mTop;
9739
9740 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07009741 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07009742
Romain Guye1123222009-06-29 14:24:56 -07009743 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009744 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
9745 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07009746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009747
9748 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07009749 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08009750 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009751
9752 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07009753 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08009754 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009755 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
9756 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08009757 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009758 return;
9759 }
9760
9761 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07009762 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009763
9764 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009765 Bitmap.Config quality;
9766 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08009767 // Never pick ARGB_4444 because it looks awful
9768 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009769 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
9770 case DRAWING_CACHE_QUALITY_AUTO:
9771 quality = Bitmap.Config.ARGB_8888;
9772 break;
9773 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08009774 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009775 break;
9776 case DRAWING_CACHE_QUALITY_HIGH:
9777 quality = Bitmap.Config.ARGB_8888;
9778 break;
9779 default:
9780 quality = Bitmap.Config.ARGB_8888;
9781 break;
9782 }
9783 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07009784 // Optimization for translucent windows
9785 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08009786 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009787 }
9788
9789 // Try to cleanup memory
9790 if (bitmap != null) bitmap.recycle();
9791
9792 try {
9793 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07009794 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07009795 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07009796 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009797 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07009798 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009799 }
Adam Powell26153a32010-11-08 15:22:27 -08009800 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009801 } catch (OutOfMemoryError e) {
9802 // If there is not enough memory to create the bitmap cache, just
9803 // ignore the issue as bitmap caches are not required to draw the
9804 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07009805 if (autoScale) {
9806 mDrawingCache = null;
9807 } else {
9808 mUnscaledDrawingCache = null;
9809 }
Romain Guy0211a0a2011-02-14 16:34:59 -08009810 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009811 return;
9812 }
9813
9814 clear = drawingCacheBackgroundColor != 0;
9815 }
9816
9817 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009818 if (attachInfo != null) {
9819 canvas = attachInfo.mCanvas;
9820 if (canvas == null) {
9821 canvas = new Canvas();
9822 }
9823 canvas.setBitmap(bitmap);
9824 // Temporarily clobber the cached Canvas in case one of our children
9825 // is also using a drawing cache. Without this, the children would
9826 // steal the canvas by attaching their own bitmap to it and bad, bad
9827 // thing would happen (invisible views, corrupted drawings, etc.)
9828 attachInfo.mCanvas = null;
9829 } else {
9830 // This case should hopefully never or seldom happen
9831 canvas = new Canvas(bitmap);
9832 }
9833
9834 if (clear) {
9835 bitmap.eraseColor(drawingCacheBackgroundColor);
9836 }
9837
9838 computeScroll();
9839 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08009840
Romain Guye1123222009-06-29 14:24:56 -07009841 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009842 final float scale = attachInfo.mApplicationScale;
9843 canvas.scale(scale, scale);
9844 }
Joe Malin32736f02011-01-19 16:14:20 -08009845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009846 canvas.translate(-mScrollX, -mScrollY);
9847
Romain Guy5bcdff42009-05-14 21:27:18 -07009848 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08009849 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
9850 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07009851 mPrivateFlags |= DRAWING_CACHE_VALID;
9852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009853
9854 // Fast path for layouts with no backgrounds
9855 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9856 if (ViewDebug.TRACE_HIERARCHY) {
9857 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9858 }
Romain Guy5bcdff42009-05-14 21:27:18 -07009859 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009860 dispatchDraw(canvas);
9861 } else {
9862 draw(canvas);
9863 }
9864
9865 canvas.restoreToCount(restoreCount);
9866
9867 if (attachInfo != null) {
9868 // Restore the cached Canvas for our siblings
9869 attachInfo.mCanvas = canvas;
9870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009871 }
9872 }
9873
9874 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009875 * Create a snapshot of the view into a bitmap. We should probably make
9876 * some form of this public, but should think about the API.
9877 */
Romain Guy223ff5c2010-03-02 17:07:47 -08009878 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009879 int width = mRight - mLeft;
9880 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009881
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009882 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07009883 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009884 width = (int) ((width * scale) + 0.5f);
9885 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08009886
Romain Guy8c11e312009-09-14 15:15:30 -07009887 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009888 if (bitmap == null) {
9889 throw new OutOfMemoryError();
9890 }
9891
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009892 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08009893
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009894 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009895 if (attachInfo != null) {
9896 canvas = attachInfo.mCanvas;
9897 if (canvas == null) {
9898 canvas = new Canvas();
9899 }
9900 canvas.setBitmap(bitmap);
9901 // Temporarily clobber the cached Canvas in case one of our children
9902 // is also using a drawing cache. Without this, the children would
9903 // steal the canvas by attaching their own bitmap to it and bad, bad
9904 // things would happen (invisible views, corrupted drawings, etc.)
9905 attachInfo.mCanvas = null;
9906 } else {
9907 // This case should hopefully never or seldom happen
9908 canvas = new Canvas(bitmap);
9909 }
9910
Romain Guy5bcdff42009-05-14 21:27:18 -07009911 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009912 bitmap.eraseColor(backgroundColor);
9913 }
9914
9915 computeScroll();
9916 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009917 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009918 canvas.translate(-mScrollX, -mScrollY);
9919
Romain Guy5bcdff42009-05-14 21:27:18 -07009920 // Temporarily remove the dirty mask
9921 int flags = mPrivateFlags;
9922 mPrivateFlags &= ~DIRTY_MASK;
9923
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009924 // Fast path for layouts with no backgrounds
9925 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9926 dispatchDraw(canvas);
9927 } else {
9928 draw(canvas);
9929 }
9930
Romain Guy5bcdff42009-05-14 21:27:18 -07009931 mPrivateFlags = flags;
9932
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009933 canvas.restoreToCount(restoreCount);
9934
9935 if (attachInfo != null) {
9936 // Restore the cached Canvas for our siblings
9937 attachInfo.mCanvas = canvas;
9938 }
Romain Guy8506ab42009-06-11 17:35:47 -07009939
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009940 return bitmap;
9941 }
9942
9943 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009944 * Indicates whether this View is currently in edit mode. A View is usually
9945 * in edit mode when displayed within a developer tool. For instance, if
9946 * this View is being drawn by a visual user interface builder, this method
9947 * should return true.
9948 *
9949 * Subclasses should check the return value of this method to provide
9950 * different behaviors if their normal behavior might interfere with the
9951 * host environment. For instance: the class spawns a thread in its
9952 * constructor, the drawing code relies on device-specific features, etc.
9953 *
9954 * This method is usually checked in the drawing code of custom widgets.
9955 *
9956 * @return True if this View is in edit mode, false otherwise.
9957 */
9958 public boolean isInEditMode() {
9959 return false;
9960 }
9961
9962 /**
9963 * If the View draws content inside its padding and enables fading edges,
9964 * it needs to support padding offsets. Padding offsets are added to the
9965 * fading edges to extend the length of the fade so that it covers pixels
9966 * drawn inside the padding.
9967 *
9968 * Subclasses of this class should override this method if they need
9969 * to draw content inside the padding.
9970 *
9971 * @return True if padding offset must be applied, false otherwise.
9972 *
9973 * @see #getLeftPaddingOffset()
9974 * @see #getRightPaddingOffset()
9975 * @see #getTopPaddingOffset()
9976 * @see #getBottomPaddingOffset()
9977 *
9978 * @since CURRENT
9979 */
9980 protected boolean isPaddingOffsetRequired() {
9981 return false;
9982 }
9983
9984 /**
9985 * Amount by which to extend the left fading region. Called only when
9986 * {@link #isPaddingOffsetRequired()} returns true.
9987 *
9988 * @return The left padding offset in pixels.
9989 *
9990 * @see #isPaddingOffsetRequired()
9991 *
9992 * @since CURRENT
9993 */
9994 protected int getLeftPaddingOffset() {
9995 return 0;
9996 }
9997
9998 /**
9999 * Amount by which to extend the right fading region. Called only when
10000 * {@link #isPaddingOffsetRequired()} returns true.
10001 *
10002 * @return The right padding offset in pixels.
10003 *
10004 * @see #isPaddingOffsetRequired()
10005 *
10006 * @since CURRENT
10007 */
10008 protected int getRightPaddingOffset() {
10009 return 0;
10010 }
10011
10012 /**
10013 * Amount by which to extend the top fading region. Called only when
10014 * {@link #isPaddingOffsetRequired()} returns true.
10015 *
10016 * @return The top padding offset in pixels.
10017 *
10018 * @see #isPaddingOffsetRequired()
10019 *
10020 * @since CURRENT
10021 */
10022 protected int getTopPaddingOffset() {
10023 return 0;
10024 }
10025
10026 /**
10027 * Amount by which to extend the bottom fading region. Called only when
10028 * {@link #isPaddingOffsetRequired()} returns true.
10029 *
10030 * @return The bottom padding offset in pixels.
10031 *
10032 * @see #isPaddingOffsetRequired()
10033 *
10034 * @since CURRENT
10035 */
10036 protected int getBottomPaddingOffset() {
10037 return 0;
10038 }
10039
10040 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010041 * <p>Indicates whether this view is attached to an hardware accelerated
10042 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010043 *
Romain Guy2bffd262010-09-12 17:40:02 -070010044 * <p>Even if this method returns true, it does not mean that every call
10045 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10046 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10047 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10048 * window is hardware accelerated,
10049 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10050 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010051 *
Romain Guy2bffd262010-09-12 17:40:02 -070010052 * @return True if the view is attached to a window and the window is
10053 * hardware accelerated; false in any other case.
10054 */
10055 public boolean isHardwareAccelerated() {
10056 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10057 }
Joe Malin32736f02011-01-19 16:14:20 -080010058
Romain Guy2bffd262010-09-12 17:40:02 -070010059 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010060 * Manually render this view (and all of its children) to the given Canvas.
10061 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010062 * called. When implementing a view, implement
10063 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10064 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010065 *
10066 * @param canvas The Canvas to which the View is rendered.
10067 */
10068 public void draw(Canvas canvas) {
10069 if (ViewDebug.TRACE_HIERARCHY) {
10070 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10071 }
10072
Romain Guy5bcdff42009-05-14 21:27:18 -070010073 final int privateFlags = mPrivateFlags;
10074 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
10075 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
10076 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070010077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010078 /*
10079 * Draw traversal performs several drawing steps which must be executed
10080 * in the appropriate order:
10081 *
10082 * 1. Draw the background
10083 * 2. If necessary, save the canvas' layers to prepare for fading
10084 * 3. Draw view's content
10085 * 4. Draw children
10086 * 5. If necessary, draw the fading edges and restore layers
10087 * 6. Draw decorations (scrollbars for instance)
10088 */
10089
10090 // Step 1, draw the background, if needed
10091 int saveCount;
10092
Romain Guy24443ea2009-05-11 11:56:30 -070010093 if (!dirtyOpaque) {
10094 final Drawable background = mBGDrawable;
10095 if (background != null) {
10096 final int scrollX = mScrollX;
10097 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010098
Romain Guy24443ea2009-05-11 11:56:30 -070010099 if (mBackgroundSizeChanged) {
10100 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
10101 mBackgroundSizeChanged = false;
10102 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010103
Romain Guy24443ea2009-05-11 11:56:30 -070010104 if ((scrollX | scrollY) == 0) {
10105 background.draw(canvas);
10106 } else {
10107 canvas.translate(scrollX, scrollY);
10108 background.draw(canvas);
10109 canvas.translate(-scrollX, -scrollY);
10110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010111 }
10112 }
10113
10114 // skip step 2 & 5 if possible (common case)
10115 final int viewFlags = mViewFlags;
10116 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
10117 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
10118 if (!verticalEdges && !horizontalEdges) {
10119 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010120 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010121
10122 // Step 4, draw the children
10123 dispatchDraw(canvas);
10124
10125 // Step 6, draw decorations (scrollbars)
10126 onDrawScrollBars(canvas);
10127
10128 // we're done...
10129 return;
10130 }
10131
10132 /*
10133 * Here we do the full fledged routine...
10134 * (this is an uncommon case where speed matters less,
10135 * this is why we repeat some of the tests that have been
10136 * done above)
10137 */
10138
10139 boolean drawTop = false;
10140 boolean drawBottom = false;
10141 boolean drawLeft = false;
10142 boolean drawRight = false;
10143
10144 float topFadeStrength = 0.0f;
10145 float bottomFadeStrength = 0.0f;
10146 float leftFadeStrength = 0.0f;
10147 float rightFadeStrength = 0.0f;
10148
10149 // Step 2, save the canvas' layers
10150 int paddingLeft = mPaddingLeft;
10151 int paddingTop = mPaddingTop;
10152
10153 final boolean offsetRequired = isPaddingOffsetRequired();
10154 if (offsetRequired) {
10155 paddingLeft += getLeftPaddingOffset();
10156 paddingTop += getTopPaddingOffset();
10157 }
10158
10159 int left = mScrollX + paddingLeft;
10160 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
10161 int top = mScrollY + paddingTop;
10162 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
10163
10164 if (offsetRequired) {
10165 right += getRightPaddingOffset();
10166 bottom += getBottomPaddingOffset();
10167 }
10168
10169 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010170 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
10171 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010172
10173 // clip the fade length if top and bottom fades overlap
10174 // overlapping fades produce odd-looking artifacts
10175 if (verticalEdges && (top + length > bottom - length)) {
10176 length = (bottom - top) / 2;
10177 }
10178
10179 // also clip horizontal fades if necessary
10180 if (horizontalEdges && (left + length > right - length)) {
10181 length = (right - left) / 2;
10182 }
10183
10184 if (verticalEdges) {
10185 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010186 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010187 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010188 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010189 }
10190
10191 if (horizontalEdges) {
10192 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010193 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010194 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010195 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010196 }
10197
10198 saveCount = canvas.getSaveCount();
10199
10200 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070010201 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010202 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
10203
10204 if (drawTop) {
10205 canvas.saveLayer(left, top, right, top + length, null, flags);
10206 }
10207
10208 if (drawBottom) {
10209 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
10210 }
10211
10212 if (drawLeft) {
10213 canvas.saveLayer(left, top, left + length, bottom, null, flags);
10214 }
10215
10216 if (drawRight) {
10217 canvas.saveLayer(right - length, top, right, bottom, null, flags);
10218 }
10219 } else {
10220 scrollabilityCache.setFadeColor(solidColor);
10221 }
10222
10223 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010224 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010225
10226 // Step 4, draw the children
10227 dispatchDraw(canvas);
10228
10229 // Step 5, draw the fade effect and restore layers
10230 final Paint p = scrollabilityCache.paint;
10231 final Matrix matrix = scrollabilityCache.matrix;
10232 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010233
10234 if (drawTop) {
10235 matrix.setScale(1, fadeHeight * topFadeStrength);
10236 matrix.postTranslate(left, top);
10237 fade.setLocalMatrix(matrix);
10238 canvas.drawRect(left, top, right, top + length, p);
10239 }
10240
10241 if (drawBottom) {
10242 matrix.setScale(1, fadeHeight * bottomFadeStrength);
10243 matrix.postRotate(180);
10244 matrix.postTranslate(left, bottom);
10245 fade.setLocalMatrix(matrix);
10246 canvas.drawRect(left, bottom - length, right, bottom, p);
10247 }
10248
10249 if (drawLeft) {
10250 matrix.setScale(1, fadeHeight * leftFadeStrength);
10251 matrix.postRotate(-90);
10252 matrix.postTranslate(left, top);
10253 fade.setLocalMatrix(matrix);
10254 canvas.drawRect(left, top, left + length, bottom, p);
10255 }
10256
10257 if (drawRight) {
10258 matrix.setScale(1, fadeHeight * rightFadeStrength);
10259 matrix.postRotate(90);
10260 matrix.postTranslate(right, top);
10261 fade.setLocalMatrix(matrix);
10262 canvas.drawRect(right - length, top, right, bottom, p);
10263 }
10264
10265 canvas.restoreToCount(saveCount);
10266
10267 // Step 6, draw decorations (scrollbars)
10268 onDrawScrollBars(canvas);
10269 }
10270
10271 /**
10272 * Override this if your view is known to always be drawn on top of a solid color background,
10273 * and needs to draw fading edges. Returning a non-zero color enables the view system to
10274 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
10275 * should be set to 0xFF.
10276 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010277 * @see #setVerticalFadingEdgeEnabled(boolean)
10278 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010279 *
10280 * @return The known solid color background for this view, or 0 if the color may vary
10281 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010282 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010283 public int getSolidColor() {
10284 return 0;
10285 }
10286
10287 /**
10288 * Build a human readable string representation of the specified view flags.
10289 *
10290 * @param flags the view flags to convert to a string
10291 * @return a String representing the supplied flags
10292 */
10293 private static String printFlags(int flags) {
10294 String output = "";
10295 int numFlags = 0;
10296 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
10297 output += "TAKES_FOCUS";
10298 numFlags++;
10299 }
10300
10301 switch (flags & VISIBILITY_MASK) {
10302 case INVISIBLE:
10303 if (numFlags > 0) {
10304 output += " ";
10305 }
10306 output += "INVISIBLE";
10307 // USELESS HERE numFlags++;
10308 break;
10309 case GONE:
10310 if (numFlags > 0) {
10311 output += " ";
10312 }
10313 output += "GONE";
10314 // USELESS HERE numFlags++;
10315 break;
10316 default:
10317 break;
10318 }
10319 return output;
10320 }
10321
10322 /**
10323 * Build a human readable string representation of the specified private
10324 * view flags.
10325 *
10326 * @param privateFlags the private view flags to convert to a string
10327 * @return a String representing the supplied flags
10328 */
10329 private static String printPrivateFlags(int privateFlags) {
10330 String output = "";
10331 int numFlags = 0;
10332
10333 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
10334 output += "WANTS_FOCUS";
10335 numFlags++;
10336 }
10337
10338 if ((privateFlags & FOCUSED) == FOCUSED) {
10339 if (numFlags > 0) {
10340 output += " ";
10341 }
10342 output += "FOCUSED";
10343 numFlags++;
10344 }
10345
10346 if ((privateFlags & SELECTED) == SELECTED) {
10347 if (numFlags > 0) {
10348 output += " ";
10349 }
10350 output += "SELECTED";
10351 numFlags++;
10352 }
10353
10354 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
10355 if (numFlags > 0) {
10356 output += " ";
10357 }
10358 output += "IS_ROOT_NAMESPACE";
10359 numFlags++;
10360 }
10361
10362 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
10363 if (numFlags > 0) {
10364 output += " ";
10365 }
10366 output += "HAS_BOUNDS";
10367 numFlags++;
10368 }
10369
10370 if ((privateFlags & DRAWN) == DRAWN) {
10371 if (numFlags > 0) {
10372 output += " ";
10373 }
10374 output += "DRAWN";
10375 // USELESS HERE numFlags++;
10376 }
10377 return output;
10378 }
10379
10380 /**
10381 * <p>Indicates whether or not this view's layout will be requested during
10382 * the next hierarchy layout pass.</p>
10383 *
10384 * @return true if the layout will be forced during next layout pass
10385 */
10386 public boolean isLayoutRequested() {
10387 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
10388 }
10389
10390 /**
10391 * Assign a size and position to a view and all of its
10392 * descendants
10393 *
10394 * <p>This is the second phase of the layout mechanism.
10395 * (The first is measuring). In this phase, each parent calls
10396 * layout on all of its children to position them.
10397 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080010398 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010399 *
Chet Haase9c087442011-01-12 16:20:16 -080010400 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010401 * Derived classes with children should override
10402 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080010403 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010404 *
10405 * @param l Left position, relative to parent
10406 * @param t Top position, relative to parent
10407 * @param r Right position, relative to parent
10408 * @param b Bottom position, relative to parent
10409 */
Romain Guy5429e1d2010-09-07 12:38:00 -070010410 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080010411 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070010412 int oldL = mLeft;
10413 int oldT = mTop;
10414 int oldB = mBottom;
10415 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010416 boolean changed = setFrame(l, t, r, b);
10417 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
10418 if (ViewDebug.TRACE_HIERARCHY) {
10419 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
10420 }
10421
10422 onLayout(changed, l, t, r, b);
10423 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070010424
10425 if (mOnLayoutChangeListeners != null) {
10426 ArrayList<OnLayoutChangeListener> listenersCopy =
10427 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
10428 int numListeners = listenersCopy.size();
10429 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070010430 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070010431 }
10432 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010433 }
10434 mPrivateFlags &= ~FORCE_LAYOUT;
10435 }
10436
10437 /**
10438 * Called from layout when this view should
10439 * assign a size and position to each of its children.
10440 *
10441 * Derived classes with children should override
10442 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070010443 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010444 * @param changed This is a new size or position for this view
10445 * @param left Left position, relative to parent
10446 * @param top Top position, relative to parent
10447 * @param right Right position, relative to parent
10448 * @param bottom Bottom position, relative to parent
10449 */
10450 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
10451 }
10452
10453 /**
10454 * Assign a size and position to this view.
10455 *
10456 * This is called from layout.
10457 *
10458 * @param left Left position, relative to parent
10459 * @param top Top position, relative to parent
10460 * @param right Right position, relative to parent
10461 * @param bottom Bottom position, relative to parent
10462 * @return true if the new size and position are different than the
10463 * previous ones
10464 * {@hide}
10465 */
10466 protected boolean setFrame(int left, int top, int right, int bottom) {
10467 boolean changed = false;
10468
10469 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070010470 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010471 + right + "," + bottom + ")");
10472 }
10473
10474 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
10475 changed = true;
10476
10477 // Remember our drawn bit
10478 int drawn = mPrivateFlags & DRAWN;
10479
10480 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -080010481 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010482
10483
10484 int oldWidth = mRight - mLeft;
10485 int oldHeight = mBottom - mTop;
10486
10487 mLeft = left;
10488 mTop = top;
10489 mRight = right;
10490 mBottom = bottom;
10491
10492 mPrivateFlags |= HAS_BOUNDS;
10493
10494 int newWidth = right - left;
10495 int newHeight = bottom - top;
10496
10497 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010498 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
10499 // A change in dimension means an auto-centered pivot point changes, too
10500 mMatrixDirty = true;
10501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010502 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
10503 }
10504
10505 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
10506 // If we are visible, force the DRAWN bit to on so that
10507 // this invalidate will go through (at least to our parent).
10508 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010509 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010510 // the DRAWN bit.
10511 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010512 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -080010513 // parent display list may need to be recreated based on a change in the bounds
10514 // of any child
10515 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010516 }
10517
10518 // Reset drawn bit to original value (invalidate turns it off)
10519 mPrivateFlags |= drawn;
10520
10521 mBackgroundSizeChanged = true;
10522 }
10523 return changed;
10524 }
10525
10526 /**
10527 * Finalize inflating a view from XML. This is called as the last phase
10528 * of inflation, after all child views have been added.
10529 *
10530 * <p>Even if the subclass overrides onFinishInflate, they should always be
10531 * sure to call the super method, so that we get called.
10532 */
10533 protected void onFinishInflate() {
10534 }
10535
10536 /**
10537 * Returns the resources associated with this view.
10538 *
10539 * @return Resources object.
10540 */
10541 public Resources getResources() {
10542 return mResources;
10543 }
10544
10545 /**
10546 * Invalidates the specified Drawable.
10547 *
10548 * @param drawable the drawable to invalidate
10549 */
10550 public void invalidateDrawable(Drawable drawable) {
10551 if (verifyDrawable(drawable)) {
10552 final Rect dirty = drawable.getBounds();
10553 final int scrollX = mScrollX;
10554 final int scrollY = mScrollY;
10555
10556 invalidate(dirty.left + scrollX, dirty.top + scrollY,
10557 dirty.right + scrollX, dirty.bottom + scrollY);
10558 }
10559 }
10560
10561 /**
10562 * Schedules an action on a drawable to occur at a specified time.
10563 *
10564 * @param who the recipient of the action
10565 * @param what the action to run on the drawable
10566 * @param when the time at which the action must occur. Uses the
10567 * {@link SystemClock#uptimeMillis} timebase.
10568 */
10569 public void scheduleDrawable(Drawable who, Runnable what, long when) {
10570 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10571 mAttachInfo.mHandler.postAtTime(what, who, when);
10572 }
10573 }
10574
10575 /**
10576 * Cancels a scheduled action on a drawable.
10577 *
10578 * @param who the recipient of the action
10579 * @param what the action to cancel
10580 */
10581 public void unscheduleDrawable(Drawable who, Runnable what) {
10582 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10583 mAttachInfo.mHandler.removeCallbacks(what, who);
10584 }
10585 }
10586
10587 /**
10588 * Unschedule any events associated with the given Drawable. This can be
10589 * used when selecting a new Drawable into a view, so that the previous
10590 * one is completely unscheduled.
10591 *
10592 * @param who The Drawable to unschedule.
10593 *
10594 * @see #drawableStateChanged
10595 */
10596 public void unscheduleDrawable(Drawable who) {
10597 if (mAttachInfo != null) {
10598 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
10599 }
10600 }
10601
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070010602 /**
10603 * Return the layout direction of a given Drawable.
10604 *
10605 * @param who the Drawable to query
10606 *
10607 * @hide
10608 */
10609 public int getResolvedLayoutDirection(Drawable who) {
10610 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070010611 }
10612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010613 /**
10614 * If your view subclass is displaying its own Drawable objects, it should
10615 * override this function and return true for any Drawable it is
10616 * displaying. This allows animations for those drawables to be
10617 * scheduled.
10618 *
10619 * <p>Be sure to call through to the super class when overriding this
10620 * function.
10621 *
10622 * @param who The Drawable to verify. Return true if it is one you are
10623 * displaying, else return the result of calling through to the
10624 * super class.
10625 *
10626 * @return boolean If true than the Drawable is being displayed in the
10627 * view; else false and it is not allowed to animate.
10628 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010629 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
10630 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010631 */
10632 protected boolean verifyDrawable(Drawable who) {
10633 return who == mBGDrawable;
10634 }
10635
10636 /**
10637 * This function is called whenever the state of the view changes in such
10638 * a way that it impacts the state of drawables being shown.
10639 *
10640 * <p>Be sure to call through to the superclass when overriding this
10641 * function.
10642 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010643 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010644 */
10645 protected void drawableStateChanged() {
10646 Drawable d = mBGDrawable;
10647 if (d != null && d.isStateful()) {
10648 d.setState(getDrawableState());
10649 }
10650 }
10651
10652 /**
10653 * Call this to force a view to update its drawable state. This will cause
10654 * drawableStateChanged to be called on this view. Views that are interested
10655 * in the new state should call getDrawableState.
10656 *
10657 * @see #drawableStateChanged
10658 * @see #getDrawableState
10659 */
10660 public void refreshDrawableState() {
10661 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
10662 drawableStateChanged();
10663
10664 ViewParent parent = mParent;
10665 if (parent != null) {
10666 parent.childDrawableStateChanged(this);
10667 }
10668 }
10669
10670 /**
10671 * Return an array of resource IDs of the drawable states representing the
10672 * current state of the view.
10673 *
10674 * @return The current drawable state
10675 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010676 * @see Drawable#setState(int[])
10677 * @see #drawableStateChanged()
10678 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010679 */
10680 public final int[] getDrawableState() {
10681 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
10682 return mDrawableState;
10683 } else {
10684 mDrawableState = onCreateDrawableState(0);
10685 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
10686 return mDrawableState;
10687 }
10688 }
10689
10690 /**
10691 * Generate the new {@link android.graphics.drawable.Drawable} state for
10692 * this view. This is called by the view
10693 * system when the cached Drawable state is determined to be invalid. To
10694 * retrieve the current state, you should use {@link #getDrawableState}.
10695 *
10696 * @param extraSpace if non-zero, this is the number of extra entries you
10697 * would like in the returned array in which you can place your own
10698 * states.
10699 *
10700 * @return Returns an array holding the current {@link Drawable} state of
10701 * the view.
10702 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010703 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010704 */
10705 protected int[] onCreateDrawableState(int extraSpace) {
10706 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
10707 mParent instanceof View) {
10708 return ((View) mParent).onCreateDrawableState(extraSpace);
10709 }
10710
10711 int[] drawableState;
10712
10713 int privateFlags = mPrivateFlags;
10714
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010715 int viewStateIndex = 0;
10716 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
10717 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
10718 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070010719 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010720 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
10721 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070010722 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
10723 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010724 // This is set if HW acceleration is requested, even if the current
10725 // process doesn't allow it. This is just to allow app preview
10726 // windows to better match their app.
10727 viewStateIndex |= VIEW_STATE_ACCELERATED;
10728 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070010729 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010730
Christopher Tate3d4bf172011-03-28 16:16:46 -070010731 final int privateFlags2 = mPrivateFlags2;
10732 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
10733 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
10734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010735 drawableState = VIEW_STATE_SETS[viewStateIndex];
10736
10737 //noinspection ConstantIfStatement
10738 if (false) {
10739 Log.i("View", "drawableStateIndex=" + viewStateIndex);
10740 Log.i("View", toString()
10741 + " pressed=" + ((privateFlags & PRESSED) != 0)
10742 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
10743 + " fo=" + hasFocus()
10744 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010745 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010746 + ": " + Arrays.toString(drawableState));
10747 }
10748
10749 if (extraSpace == 0) {
10750 return drawableState;
10751 }
10752
10753 final int[] fullState;
10754 if (drawableState != null) {
10755 fullState = new int[drawableState.length + extraSpace];
10756 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
10757 } else {
10758 fullState = new int[extraSpace];
10759 }
10760
10761 return fullState;
10762 }
10763
10764 /**
10765 * Merge your own state values in <var>additionalState</var> into the base
10766 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070010767 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010768 *
10769 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070010770 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010771 * own additional state values.
10772 *
10773 * @param additionalState The additional state values you would like
10774 * added to <var>baseState</var>; this array is not modified.
10775 *
10776 * @return As a convenience, the <var>baseState</var> array you originally
10777 * passed into the function is returned.
10778 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010779 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010780 */
10781 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
10782 final int N = baseState.length;
10783 int i = N - 1;
10784 while (i >= 0 && baseState[i] == 0) {
10785 i--;
10786 }
10787 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
10788 return baseState;
10789 }
10790
10791 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070010792 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
10793 * on all Drawable objects associated with this view.
10794 */
10795 public void jumpDrawablesToCurrentState() {
10796 if (mBGDrawable != null) {
10797 mBGDrawable.jumpToCurrentState();
10798 }
10799 }
10800
10801 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010802 * Sets the background color for this view.
10803 * @param color the color of the background
10804 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010805 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010806 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070010807 if (mBGDrawable instanceof ColorDrawable) {
10808 ((ColorDrawable) mBGDrawable).setColor(color);
10809 } else {
10810 setBackgroundDrawable(new ColorDrawable(color));
10811 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010812 }
10813
10814 /**
10815 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070010816 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010817 * @param resid The identifier of the resource.
10818 * @attr ref android.R.styleable#View_background
10819 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010820 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010821 public void setBackgroundResource(int resid) {
10822 if (resid != 0 && resid == mBackgroundResource) {
10823 return;
10824 }
10825
10826 Drawable d= null;
10827 if (resid != 0) {
10828 d = mResources.getDrawable(resid);
10829 }
10830 setBackgroundDrawable(d);
10831
10832 mBackgroundResource = resid;
10833 }
10834
10835 /**
10836 * Set the background to a given Drawable, or remove the background. If the
10837 * background has padding, this View's padding is set to the background's
10838 * padding. However, when a background is removed, this View's padding isn't
10839 * touched. If setting the padding is desired, please use
10840 * {@link #setPadding(int, int, int, int)}.
10841 *
10842 * @param d The Drawable to use as the background, or null to remove the
10843 * background
10844 */
10845 public void setBackgroundDrawable(Drawable d) {
10846 boolean requestLayout = false;
10847
10848 mBackgroundResource = 0;
10849
10850 /*
10851 * Regardless of whether we're setting a new background or not, we want
10852 * to clear the previous drawable.
10853 */
10854 if (mBGDrawable != null) {
10855 mBGDrawable.setCallback(null);
10856 unscheduleDrawable(mBGDrawable);
10857 }
10858
10859 if (d != null) {
10860 Rect padding = sThreadLocal.get();
10861 if (padding == null) {
10862 padding = new Rect();
10863 sThreadLocal.set(padding);
10864 }
10865 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010866 switch (d.getResolvedLayoutDirectionSelf()) {
10867 case LAYOUT_DIRECTION_RTL:
10868 setPadding(padding.right, padding.top, padding.left, padding.bottom);
10869 break;
10870 case LAYOUT_DIRECTION_LTR:
10871 default:
10872 setPadding(padding.left, padding.top, padding.right, padding.bottom);
10873 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010874 }
10875
10876 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
10877 // if it has a different minimum size, we should layout again
10878 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
10879 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
10880 requestLayout = true;
10881 }
10882
10883 d.setCallback(this);
10884 if (d.isStateful()) {
10885 d.setState(getDrawableState());
10886 }
10887 d.setVisible(getVisibility() == VISIBLE, false);
10888 mBGDrawable = d;
10889
10890 if ((mPrivateFlags & SKIP_DRAW) != 0) {
10891 mPrivateFlags &= ~SKIP_DRAW;
10892 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
10893 requestLayout = true;
10894 }
10895 } else {
10896 /* Remove the background */
10897 mBGDrawable = null;
10898
10899 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
10900 /*
10901 * This view ONLY drew the background before and we're removing
10902 * the background, so now it won't draw anything
10903 * (hence we SKIP_DRAW)
10904 */
10905 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
10906 mPrivateFlags |= SKIP_DRAW;
10907 }
10908
10909 /*
10910 * When the background is set, we try to apply its padding to this
10911 * View. When the background is removed, we don't touch this View's
10912 * padding. This is noted in the Javadocs. Hence, we don't need to
10913 * requestLayout(), the invalidate() below is sufficient.
10914 */
10915
10916 // The old background's minimum size could have affected this
10917 // View's layout, so let's requestLayout
10918 requestLayout = true;
10919 }
10920
Romain Guy8f1344f52009-05-15 16:03:59 -070010921 computeOpaqueFlags();
10922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010923 if (requestLayout) {
10924 requestLayout();
10925 }
10926
10927 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010928 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010929 }
10930
10931 /**
10932 * Gets the background drawable
10933 * @return The drawable used as the background for this view, if any.
10934 */
10935 public Drawable getBackground() {
10936 return mBGDrawable;
10937 }
10938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010939 /**
10940 * Sets the padding. The view may add on the space required to display
10941 * the scrollbars, depending on the style and visibility of the scrollbars.
10942 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
10943 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
10944 * from the values set in this call.
10945 *
10946 * @attr ref android.R.styleable#View_padding
10947 * @attr ref android.R.styleable#View_paddingBottom
10948 * @attr ref android.R.styleable#View_paddingLeft
10949 * @attr ref android.R.styleable#View_paddingRight
10950 * @attr ref android.R.styleable#View_paddingTop
10951 * @param left the left padding in pixels
10952 * @param top the top padding in pixels
10953 * @param right the right padding in pixels
10954 * @param bottom the bottom padding in pixels
10955 */
10956 public void setPadding(int left, int top, int right, int bottom) {
10957 boolean changed = false;
10958
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010959 mUserPaddingRelative = false;
10960
Adam Powell20232d02010-12-08 21:08:53 -080010961 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010962 mUserPaddingRight = right;
10963 mUserPaddingBottom = bottom;
10964
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010965 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070010966
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010967 // Common case is there are no scroll bars.
10968 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010969 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080010970 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010971 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080010972 switch (mVerticalScrollbarPosition) {
10973 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010974 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
10975 left += offset;
10976 } else {
10977 right += offset;
10978 }
10979 break;
Adam Powell20232d02010-12-08 21:08:53 -080010980 case SCROLLBAR_POSITION_RIGHT:
10981 right += offset;
10982 break;
10983 case SCROLLBAR_POSITION_LEFT:
10984 left += offset;
10985 break;
10986 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010987 }
Adam Powell20232d02010-12-08 21:08:53 -080010988 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010989 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
10990 ? 0 : getHorizontalScrollbarHeight();
10991 }
10992 }
Romain Guy8506ab42009-06-11 17:35:47 -070010993
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010994 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010995 changed = true;
10996 mPaddingLeft = left;
10997 }
10998 if (mPaddingTop != top) {
10999 changed = true;
11000 mPaddingTop = top;
11001 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011002 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011003 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011004 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011005 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011006 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011007 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011008 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011009 }
11010
11011 if (changed) {
11012 requestLayout();
11013 }
11014 }
11015
11016 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011017 * Sets the relative padding. The view may add on the space required to display
11018 * the scrollbars, depending on the style and visibility of the scrollbars.
11019 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11020 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11021 * from the values set in this call.
11022 *
11023 * @attr ref android.R.styleable#View_padding
11024 * @attr ref android.R.styleable#View_paddingBottom
11025 * @attr ref android.R.styleable#View_paddingStart
11026 * @attr ref android.R.styleable#View_paddingEnd
11027 * @attr ref android.R.styleable#View_paddingTop
11028 * @param start the start padding in pixels
11029 * @param top the top padding in pixels
11030 * @param end the end padding in pixels
11031 * @param bottom the bottom padding in pixels
11032 *
11033 * @hide
11034 */
11035 public void setPaddingRelative(int start, int top, int end, int bottom) {
11036 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011037
11038 mUserPaddingStart = start;
11039 mUserPaddingEnd = end;
11040
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011041 switch(getResolvedLayoutDirection()) {
11042 case LAYOUT_DIRECTION_RTL:
11043 setPadding(end, top, start, bottom);
11044 break;
11045 case LAYOUT_DIRECTION_LTR:
11046 default:
11047 setPadding(start, top, end, bottom);
11048 }
11049 }
11050
11051 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011052 * Returns the top padding of this view.
11053 *
11054 * @return the top padding in pixels
11055 */
11056 public int getPaddingTop() {
11057 return mPaddingTop;
11058 }
11059
11060 /**
11061 * Returns the bottom padding of this view. If there are inset and enabled
11062 * scrollbars, this value may include the space required to display the
11063 * scrollbars as well.
11064 *
11065 * @return the bottom padding in pixels
11066 */
11067 public int getPaddingBottom() {
11068 return mPaddingBottom;
11069 }
11070
11071 /**
11072 * Returns the left padding of this view. If there are inset and enabled
11073 * scrollbars, this value may include the space required to display the
11074 * scrollbars as well.
11075 *
11076 * @return the left padding in pixels
11077 */
11078 public int getPaddingLeft() {
11079 return mPaddingLeft;
11080 }
11081
11082 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011083 * Returns the start padding of this view. If there are inset and enabled
11084 * scrollbars, this value may include the space required to display the
11085 * scrollbars as well.
11086 *
11087 * @return the start padding in pixels
11088 *
11089 * @hide
11090 */
11091 public int getPaddingStart() {
11092 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11093 mPaddingRight : mPaddingLeft;
11094 }
11095
11096 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011097 * Returns the right padding of this view. If there are inset and enabled
11098 * scrollbars, this value may include the space required to display the
11099 * scrollbars as well.
11100 *
11101 * @return the right padding in pixels
11102 */
11103 public int getPaddingRight() {
11104 return mPaddingRight;
11105 }
11106
11107 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011108 * Returns the end padding of this view. If there are inset and enabled
11109 * scrollbars, this value may include the space required to display the
11110 * scrollbars as well.
11111 *
11112 * @return the end padding in pixels
11113 *
11114 * @hide
11115 */
11116 public int getPaddingEnd() {
11117 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11118 mPaddingLeft : mPaddingRight;
11119 }
11120
11121 /**
11122 * Return if the padding as been set thru relative values
11123 * {@link #setPaddingRelative(int, int, int, int)} or thru
11124 * @attr ref android.R.styleable#View_paddingStart or
11125 * @attr ref android.R.styleable#View_paddingEnd
11126 *
11127 * @return true if the padding is relative or false if it is not.
11128 *
11129 * @hide
11130 */
11131 public boolean isPaddingRelative() {
11132 return mUserPaddingRelative;
11133 }
11134
11135 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011136 * Changes the selection state of this view. A view can be selected or not.
11137 * Note that selection is not the same as focus. Views are typically
11138 * selected in the context of an AdapterView like ListView or GridView;
11139 * the selected view is the view that is highlighted.
11140 *
11141 * @param selected true if the view must be selected, false otherwise
11142 */
11143 public void setSelected(boolean selected) {
11144 if (((mPrivateFlags & SELECTED) != 0) != selected) {
11145 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070011146 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080011147 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011148 refreshDrawableState();
11149 dispatchSetSelected(selected);
11150 }
11151 }
11152
11153 /**
11154 * Dispatch setSelected to all of this View's children.
11155 *
11156 * @see #setSelected(boolean)
11157 *
11158 * @param selected The new selected state
11159 */
11160 protected void dispatchSetSelected(boolean selected) {
11161 }
11162
11163 /**
11164 * Indicates the selection state of this view.
11165 *
11166 * @return true if the view is selected, false otherwise
11167 */
11168 @ViewDebug.ExportedProperty
11169 public boolean isSelected() {
11170 return (mPrivateFlags & SELECTED) != 0;
11171 }
11172
11173 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011174 * Changes the activated state of this view. A view can be activated or not.
11175 * Note that activation is not the same as selection. Selection is
11176 * a transient property, representing the view (hierarchy) the user is
11177 * currently interacting with. Activation is a longer-term state that the
11178 * user can move views in and out of. For example, in a list view with
11179 * single or multiple selection enabled, the views in the current selection
11180 * set are activated. (Um, yeah, we are deeply sorry about the terminology
11181 * here.) The activated state is propagated down to children of the view it
11182 * is set on.
11183 *
11184 * @param activated true if the view must be activated, false otherwise
11185 */
11186 public void setActivated(boolean activated) {
11187 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
11188 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011189 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011190 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070011191 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011192 }
11193 }
11194
11195 /**
11196 * Dispatch setActivated to all of this View's children.
11197 *
11198 * @see #setActivated(boolean)
11199 *
11200 * @param activated The new activated state
11201 */
11202 protected void dispatchSetActivated(boolean activated) {
11203 }
11204
11205 /**
11206 * Indicates the activation state of this view.
11207 *
11208 * @return true if the view is activated, false otherwise
11209 */
11210 @ViewDebug.ExportedProperty
11211 public boolean isActivated() {
11212 return (mPrivateFlags & ACTIVATED) != 0;
11213 }
11214
11215 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011216 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
11217 * observer can be used to get notifications when global events, like
11218 * layout, happen.
11219 *
11220 * The returned ViewTreeObserver observer is not guaranteed to remain
11221 * valid for the lifetime of this View. If the caller of this method keeps
11222 * a long-lived reference to ViewTreeObserver, it should always check for
11223 * the return value of {@link ViewTreeObserver#isAlive()}.
11224 *
11225 * @return The ViewTreeObserver for this view's hierarchy.
11226 */
11227 public ViewTreeObserver getViewTreeObserver() {
11228 if (mAttachInfo != null) {
11229 return mAttachInfo.mTreeObserver;
11230 }
11231 if (mFloatingTreeObserver == null) {
11232 mFloatingTreeObserver = new ViewTreeObserver();
11233 }
11234 return mFloatingTreeObserver;
11235 }
11236
11237 /**
11238 * <p>Finds the topmost view in the current view hierarchy.</p>
11239 *
11240 * @return the topmost view containing this view
11241 */
11242 public View getRootView() {
11243 if (mAttachInfo != null) {
11244 final View v = mAttachInfo.mRootView;
11245 if (v != null) {
11246 return v;
11247 }
11248 }
Romain Guy8506ab42009-06-11 17:35:47 -070011249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011250 View parent = this;
11251
11252 while (parent.mParent != null && parent.mParent instanceof View) {
11253 parent = (View) parent.mParent;
11254 }
11255
11256 return parent;
11257 }
11258
11259 /**
11260 * <p>Computes the coordinates of this view on the screen. The argument
11261 * must be an array of two integers. After the method returns, the array
11262 * contains the x and y location in that order.</p>
11263 *
11264 * @param location an array of two integers in which to hold the coordinates
11265 */
11266 public void getLocationOnScreen(int[] location) {
11267 getLocationInWindow(location);
11268
11269 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070011270 if (info != null) {
11271 location[0] += info.mWindowLeft;
11272 location[1] += info.mWindowTop;
11273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011274 }
11275
11276 /**
11277 * <p>Computes the coordinates of this view in its window. The argument
11278 * must be an array of two integers. After the method returns, the array
11279 * contains the x and y location in that order.</p>
11280 *
11281 * @param location an array of two integers in which to hold the coordinates
11282 */
11283 public void getLocationInWindow(int[] location) {
11284 if (location == null || location.length < 2) {
11285 throw new IllegalArgumentException("location must be an array of "
11286 + "two integers");
11287 }
11288
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011289 location[0] = mLeft + (int) (mTranslationX + 0.5f);
11290 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011291
11292 ViewParent viewParent = mParent;
11293 while (viewParent instanceof View) {
11294 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011295 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
11296 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011297 viewParent = view.mParent;
11298 }
Romain Guy8506ab42009-06-11 17:35:47 -070011299
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011300 if (viewParent instanceof ViewAncestor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011301 // *cough*
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011302 final ViewAncestor vr = (ViewAncestor)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011303 location[1] -= vr.mCurScrollY;
11304 }
11305 }
11306
11307 /**
11308 * {@hide}
11309 * @param id the id of the view to be found
11310 * @return the view of the specified id, null if cannot be found
11311 */
11312 protected View findViewTraversal(int id) {
11313 if (id == mID) {
11314 return this;
11315 }
11316 return null;
11317 }
11318
11319 /**
11320 * {@hide}
11321 * @param tag the tag of the view to be found
11322 * @return the view of specified tag, null if cannot be found
11323 */
11324 protected View findViewWithTagTraversal(Object tag) {
11325 if (tag != null && tag.equals(mTag)) {
11326 return this;
11327 }
11328 return null;
11329 }
11330
11331 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011332 * {@hide}
11333 * @param predicate The predicate to evaluate.
11334 * @return The first view that matches the predicate or null.
11335 */
11336 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
11337 if (predicate.apply(this)) {
11338 return this;
11339 }
11340 return null;
11341 }
11342
11343 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011344 * Look for a child view with the given id. If this view has the given
11345 * id, return this view.
11346 *
11347 * @param id The id to search for.
11348 * @return The view that has the given id in the hierarchy or null
11349 */
11350 public final View findViewById(int id) {
11351 if (id < 0) {
11352 return null;
11353 }
11354 return findViewTraversal(id);
11355 }
11356
11357 /**
11358 * Look for a child view with the given tag. If this view has the given
11359 * tag, return this view.
11360 *
11361 * @param tag The tag to search for, using "tag.equals(getTag())".
11362 * @return The View that has the given tag in the hierarchy or null
11363 */
11364 public final View findViewWithTag(Object tag) {
11365 if (tag == null) {
11366 return null;
11367 }
11368 return findViewWithTagTraversal(tag);
11369 }
11370
11371 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011372 * {@hide}
11373 * Look for a child view that matches the specified predicate.
11374 * If this view matches the predicate, return this view.
11375 *
11376 * @param predicate The predicate to evaluate.
11377 * @return The first view that matches the predicate or null.
11378 */
11379 public final View findViewByPredicate(Predicate<View> predicate) {
11380 return findViewByPredicateTraversal(predicate);
11381 }
11382
11383 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011384 * Sets the identifier for this view. The identifier does not have to be
11385 * unique in this view's hierarchy. The identifier should be a positive
11386 * number.
11387 *
11388 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070011389 * @see #getId()
11390 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011391 *
11392 * @param id a number used to identify the view
11393 *
11394 * @attr ref android.R.styleable#View_id
11395 */
11396 public void setId(int id) {
11397 mID = id;
11398 }
11399
11400 /**
11401 * {@hide}
11402 *
11403 * @param isRoot true if the view belongs to the root namespace, false
11404 * otherwise
11405 */
11406 public void setIsRootNamespace(boolean isRoot) {
11407 if (isRoot) {
11408 mPrivateFlags |= IS_ROOT_NAMESPACE;
11409 } else {
11410 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
11411 }
11412 }
11413
11414 /**
11415 * {@hide}
11416 *
11417 * @return true if the view belongs to the root namespace, false otherwise
11418 */
11419 public boolean isRootNamespace() {
11420 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
11421 }
11422
11423 /**
11424 * Returns this view's identifier.
11425 *
11426 * @return a positive integer used to identify the view or {@link #NO_ID}
11427 * if the view has no ID
11428 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011429 * @see #setId(int)
11430 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011431 * @attr ref android.R.styleable#View_id
11432 */
11433 @ViewDebug.CapturedViewProperty
11434 public int getId() {
11435 return mID;
11436 }
11437
11438 /**
11439 * Returns this view's tag.
11440 *
11441 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070011442 *
11443 * @see #setTag(Object)
11444 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011445 */
11446 @ViewDebug.ExportedProperty
11447 public Object getTag() {
11448 return mTag;
11449 }
11450
11451 /**
11452 * Sets the tag associated with this view. A tag can be used to mark
11453 * a view in its hierarchy and does not have to be unique within the
11454 * hierarchy. Tags can also be used to store data within a view without
11455 * resorting to another data structure.
11456 *
11457 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070011458 *
11459 * @see #getTag()
11460 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011461 */
11462 public void setTag(final Object tag) {
11463 mTag = tag;
11464 }
11465
11466 /**
Romain Guyd90a3312009-05-06 14:54:28 -070011467 * Returns the tag associated with this view and the specified key.
11468 *
11469 * @param key The key identifying the tag
11470 *
11471 * @return the Object stored in this view as a tag
11472 *
11473 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070011474 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070011475 */
11476 public Object getTag(int key) {
11477 SparseArray<Object> tags = null;
11478 synchronized (sTagsLock) {
11479 if (sTags != null) {
11480 tags = sTags.get(this);
11481 }
11482 }
11483
11484 if (tags != null) return tags.get(key);
11485 return null;
11486 }
11487
11488 /**
11489 * Sets a tag associated with this view and a key. A tag can be used
11490 * to mark a view in its hierarchy and does not have to be unique within
11491 * the hierarchy. Tags can also be used to store data within a view
11492 * without resorting to another data structure.
11493 *
11494 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070011495 * application to ensure it is unique (see the <a
11496 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
11497 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070011498 * the Android framework or not associated with any package will cause
11499 * an {@link IllegalArgumentException} to be thrown.
11500 *
11501 * @param key The key identifying the tag
11502 * @param tag An Object to tag the view with
11503 *
11504 * @throws IllegalArgumentException If they specified key is not valid
11505 *
11506 * @see #setTag(Object)
11507 * @see #getTag(int)
11508 */
11509 public void setTag(int key, final Object tag) {
11510 // If the package id is 0x00 or 0x01, it's either an undefined package
11511 // or a framework id
11512 if ((key >>> 24) < 2) {
11513 throw new IllegalArgumentException("The key must be an application-specific "
11514 + "resource id.");
11515 }
11516
11517 setTagInternal(this, key, tag);
11518 }
11519
11520 /**
11521 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
11522 * framework id.
11523 *
11524 * @hide
11525 */
11526 public void setTagInternal(int key, Object tag) {
11527 if ((key >>> 24) != 0x1) {
11528 throw new IllegalArgumentException("The key must be a framework-specific "
11529 + "resource id.");
11530 }
11531
Romain Guy8506ab42009-06-11 17:35:47 -070011532 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070011533 }
11534
11535 private static void setTagInternal(View view, int key, Object tag) {
11536 SparseArray<Object> tags = null;
11537 synchronized (sTagsLock) {
11538 if (sTags == null) {
11539 sTags = new WeakHashMap<View, SparseArray<Object>>();
11540 } else {
11541 tags = sTags.get(view);
11542 }
11543 }
11544
11545 if (tags == null) {
11546 tags = new SparseArray<Object>(2);
11547 synchronized (sTagsLock) {
11548 sTags.put(view, tags);
11549 }
11550 }
11551
11552 tags.put(key, tag);
11553 }
11554
11555 /**
Romain Guy13922e02009-05-12 17:56:14 -070011556 * @param consistency The type of consistency. See ViewDebug for more information.
11557 *
11558 * @hide
11559 */
11560 protected boolean dispatchConsistencyCheck(int consistency) {
11561 return onConsistencyCheck(consistency);
11562 }
11563
11564 /**
11565 * Method that subclasses should implement to check their consistency. The type of
11566 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070011567 *
Romain Guy13922e02009-05-12 17:56:14 -070011568 * @param consistency The type of consistency. See ViewDebug for more information.
11569 *
11570 * @throws IllegalStateException if the view is in an inconsistent state.
11571 *
11572 * @hide
11573 */
11574 protected boolean onConsistencyCheck(int consistency) {
11575 boolean result = true;
11576
11577 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
11578 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
11579
11580 if (checkLayout) {
11581 if (getParent() == null) {
11582 result = false;
11583 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11584 "View " + this + " does not have a parent.");
11585 }
11586
11587 if (mAttachInfo == null) {
11588 result = false;
11589 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11590 "View " + this + " is not attached to a window.");
11591 }
11592 }
11593
11594 if (checkDrawing) {
11595 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
11596 // from their draw() method
11597
11598 if ((mPrivateFlags & DRAWN) != DRAWN &&
11599 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
11600 result = false;
11601 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11602 "View " + this + " was invalidated but its drawing cache is valid.");
11603 }
11604 }
11605
11606 return result;
11607 }
11608
11609 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011610 * Prints information about this view in the log output, with the tag
11611 * {@link #VIEW_LOG_TAG}.
11612 *
11613 * @hide
11614 */
11615 public void debug() {
11616 debug(0);
11617 }
11618
11619 /**
11620 * Prints information about this view in the log output, with the tag
11621 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
11622 * indentation defined by the <code>depth</code>.
11623 *
11624 * @param depth the indentation level
11625 *
11626 * @hide
11627 */
11628 protected void debug(int depth) {
11629 String output = debugIndent(depth - 1);
11630
11631 output += "+ " + this;
11632 int id = getId();
11633 if (id != -1) {
11634 output += " (id=" + id + ")";
11635 }
11636 Object tag = getTag();
11637 if (tag != null) {
11638 output += " (tag=" + tag + ")";
11639 }
11640 Log.d(VIEW_LOG_TAG, output);
11641
11642 if ((mPrivateFlags & FOCUSED) != 0) {
11643 output = debugIndent(depth) + " FOCUSED";
11644 Log.d(VIEW_LOG_TAG, output);
11645 }
11646
11647 output = debugIndent(depth);
11648 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
11649 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
11650 + "} ";
11651 Log.d(VIEW_LOG_TAG, output);
11652
11653 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
11654 || mPaddingBottom != 0) {
11655 output = debugIndent(depth);
11656 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
11657 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
11658 Log.d(VIEW_LOG_TAG, output);
11659 }
11660
11661 output = debugIndent(depth);
11662 output += "mMeasureWidth=" + mMeasuredWidth +
11663 " mMeasureHeight=" + mMeasuredHeight;
11664 Log.d(VIEW_LOG_TAG, output);
11665
11666 output = debugIndent(depth);
11667 if (mLayoutParams == null) {
11668 output += "BAD! no layout params";
11669 } else {
11670 output = mLayoutParams.debug(output);
11671 }
11672 Log.d(VIEW_LOG_TAG, output);
11673
11674 output = debugIndent(depth);
11675 output += "flags={";
11676 output += View.printFlags(mViewFlags);
11677 output += "}";
11678 Log.d(VIEW_LOG_TAG, output);
11679
11680 output = debugIndent(depth);
11681 output += "privateFlags={";
11682 output += View.printPrivateFlags(mPrivateFlags);
11683 output += "}";
11684 Log.d(VIEW_LOG_TAG, output);
11685 }
11686
11687 /**
11688 * Creates an string of whitespaces used for indentation.
11689 *
11690 * @param depth the indentation level
11691 * @return a String containing (depth * 2 + 3) * 2 white spaces
11692 *
11693 * @hide
11694 */
11695 protected static String debugIndent(int depth) {
11696 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
11697 for (int i = 0; i < (depth * 2) + 3; i++) {
11698 spaces.append(' ').append(' ');
11699 }
11700 return spaces.toString();
11701 }
11702
11703 /**
11704 * <p>Return the offset of the widget's text baseline from the widget's top
11705 * boundary. If this widget does not support baseline alignment, this
11706 * method returns -1. </p>
11707 *
11708 * @return the offset of the baseline within the widget's bounds or -1
11709 * if baseline alignment is not supported
11710 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011711 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011712 public int getBaseline() {
11713 return -1;
11714 }
11715
11716 /**
11717 * Call this when something has changed which has invalidated the
11718 * layout of this view. This will schedule a layout pass of the view
11719 * tree.
11720 */
11721 public void requestLayout() {
11722 if (ViewDebug.TRACE_HIERARCHY) {
11723 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
11724 }
11725
11726 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011727 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011728
11729 if (mParent != null && !mParent.isLayoutRequested()) {
11730 mParent.requestLayout();
11731 }
11732 }
11733
11734 /**
11735 * Forces this view to be laid out during the next layout pass.
11736 * This method does not call requestLayout() or forceLayout()
11737 * on the parent.
11738 */
11739 public void forceLayout() {
11740 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011741 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011742 }
11743
11744 /**
11745 * <p>
11746 * This is called to find out how big a view should be. The parent
11747 * supplies constraint information in the width and height parameters.
11748 * </p>
11749 *
11750 * <p>
11751 * The actual mesurement work of a view is performed in
11752 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
11753 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
11754 * </p>
11755 *
11756 *
11757 * @param widthMeasureSpec Horizontal space requirements as imposed by the
11758 * parent
11759 * @param heightMeasureSpec Vertical space requirements as imposed by the
11760 * parent
11761 *
11762 * @see #onMeasure(int, int)
11763 */
11764 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
11765 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
11766 widthMeasureSpec != mOldWidthMeasureSpec ||
11767 heightMeasureSpec != mOldHeightMeasureSpec) {
11768
11769 // first clears the measured dimension flag
11770 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
11771
11772 if (ViewDebug.TRACE_HIERARCHY) {
11773 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
11774 }
11775
11776 // measure ourselves, this should set the measured dimension flag back
11777 onMeasure(widthMeasureSpec, heightMeasureSpec);
11778
11779 // flag not set, setMeasuredDimension() was not invoked, we raise
11780 // an exception to warn the developer
11781 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
11782 throw new IllegalStateException("onMeasure() did not set the"
11783 + " measured dimension by calling"
11784 + " setMeasuredDimension()");
11785 }
11786
11787 mPrivateFlags |= LAYOUT_REQUIRED;
11788 }
11789
11790 mOldWidthMeasureSpec = widthMeasureSpec;
11791 mOldHeightMeasureSpec = heightMeasureSpec;
11792 }
11793
11794 /**
11795 * <p>
11796 * Measure the view and its content to determine the measured width and the
11797 * measured height. This method is invoked by {@link #measure(int, int)} and
11798 * should be overriden by subclasses to provide accurate and efficient
11799 * measurement of their contents.
11800 * </p>
11801 *
11802 * <p>
11803 * <strong>CONTRACT:</strong> When overriding this method, you
11804 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
11805 * measured width and height of this view. Failure to do so will trigger an
11806 * <code>IllegalStateException</code>, thrown by
11807 * {@link #measure(int, int)}. Calling the superclass'
11808 * {@link #onMeasure(int, int)} is a valid use.
11809 * </p>
11810 *
11811 * <p>
11812 * The base class implementation of measure defaults to the background size,
11813 * unless a larger size is allowed by the MeasureSpec. Subclasses should
11814 * override {@link #onMeasure(int, int)} to provide better measurements of
11815 * their content.
11816 * </p>
11817 *
11818 * <p>
11819 * If this method is overridden, it is the subclass's responsibility to make
11820 * sure the measured height and width are at least the view's minimum height
11821 * and width ({@link #getSuggestedMinimumHeight()} and
11822 * {@link #getSuggestedMinimumWidth()}).
11823 * </p>
11824 *
11825 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
11826 * The requirements are encoded with
11827 * {@link android.view.View.MeasureSpec}.
11828 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
11829 * The requirements are encoded with
11830 * {@link android.view.View.MeasureSpec}.
11831 *
11832 * @see #getMeasuredWidth()
11833 * @see #getMeasuredHeight()
11834 * @see #setMeasuredDimension(int, int)
11835 * @see #getSuggestedMinimumHeight()
11836 * @see #getSuggestedMinimumWidth()
11837 * @see android.view.View.MeasureSpec#getMode(int)
11838 * @see android.view.View.MeasureSpec#getSize(int)
11839 */
11840 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
11841 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
11842 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
11843 }
11844
11845 /**
11846 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
11847 * measured width and measured height. Failing to do so will trigger an
11848 * exception at measurement time.</p>
11849 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080011850 * @param measuredWidth The measured width of this view. May be a complex
11851 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11852 * {@link #MEASURED_STATE_TOO_SMALL}.
11853 * @param measuredHeight The measured height of this view. May be a complex
11854 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11855 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011856 */
11857 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
11858 mMeasuredWidth = measuredWidth;
11859 mMeasuredHeight = measuredHeight;
11860
11861 mPrivateFlags |= MEASURED_DIMENSION_SET;
11862 }
11863
11864 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080011865 * Merge two states as returned by {@link #getMeasuredState()}.
11866 * @param curState The current state as returned from a view or the result
11867 * of combining multiple views.
11868 * @param newState The new view state to combine.
11869 * @return Returns a new integer reflecting the combination of the two
11870 * states.
11871 */
11872 public static int combineMeasuredStates(int curState, int newState) {
11873 return curState | newState;
11874 }
11875
11876 /**
11877 * Version of {@link #resolveSizeAndState(int, int, int)}
11878 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
11879 */
11880 public static int resolveSize(int size, int measureSpec) {
11881 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
11882 }
11883
11884 /**
11885 * Utility to reconcile a desired size and state, with constraints imposed
11886 * by a MeasureSpec. Will take the desired size, unless a different size
11887 * is imposed by the constraints. The returned value is a compound integer,
11888 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
11889 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
11890 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011891 *
11892 * @param size How big the view wants to be
11893 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080011894 * @return Size information bit mask as defined by
11895 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011896 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080011897 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011898 int result = size;
11899 int specMode = MeasureSpec.getMode(measureSpec);
11900 int specSize = MeasureSpec.getSize(measureSpec);
11901 switch (specMode) {
11902 case MeasureSpec.UNSPECIFIED:
11903 result = size;
11904 break;
11905 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080011906 if (specSize < size) {
11907 result = specSize | MEASURED_STATE_TOO_SMALL;
11908 } else {
11909 result = size;
11910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011911 break;
11912 case MeasureSpec.EXACTLY:
11913 result = specSize;
11914 break;
11915 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080011916 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011917 }
11918
11919 /**
11920 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070011921 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011922 * by the MeasureSpec.
11923 *
11924 * @param size Default size for this view
11925 * @param measureSpec Constraints imposed by the parent
11926 * @return The size this view should be.
11927 */
11928 public static int getDefaultSize(int size, int measureSpec) {
11929 int result = size;
11930 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070011931 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011932
11933 switch (specMode) {
11934 case MeasureSpec.UNSPECIFIED:
11935 result = size;
11936 break;
11937 case MeasureSpec.AT_MOST:
11938 case MeasureSpec.EXACTLY:
11939 result = specSize;
11940 break;
11941 }
11942 return result;
11943 }
11944
11945 /**
11946 * Returns the suggested minimum height that the view should use. This
11947 * returns the maximum of the view's minimum height
11948 * and the background's minimum height
11949 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
11950 * <p>
11951 * When being used in {@link #onMeasure(int, int)}, the caller should still
11952 * ensure the returned height is within the requirements of the parent.
11953 *
11954 * @return The suggested minimum height of the view.
11955 */
11956 protected int getSuggestedMinimumHeight() {
11957 int suggestedMinHeight = mMinHeight;
11958
11959 if (mBGDrawable != null) {
11960 final int bgMinHeight = mBGDrawable.getMinimumHeight();
11961 if (suggestedMinHeight < bgMinHeight) {
11962 suggestedMinHeight = bgMinHeight;
11963 }
11964 }
11965
11966 return suggestedMinHeight;
11967 }
11968
11969 /**
11970 * Returns the suggested minimum width that the view should use. This
11971 * returns the maximum of the view's minimum width)
11972 * and the background's minimum width
11973 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
11974 * <p>
11975 * When being used in {@link #onMeasure(int, int)}, the caller should still
11976 * ensure the returned width is within the requirements of the parent.
11977 *
11978 * @return The suggested minimum width of the view.
11979 */
11980 protected int getSuggestedMinimumWidth() {
11981 int suggestedMinWidth = mMinWidth;
11982
11983 if (mBGDrawable != null) {
11984 final int bgMinWidth = mBGDrawable.getMinimumWidth();
11985 if (suggestedMinWidth < bgMinWidth) {
11986 suggestedMinWidth = bgMinWidth;
11987 }
11988 }
11989
11990 return suggestedMinWidth;
11991 }
11992
11993 /**
11994 * Sets the minimum height of the view. It is not guaranteed the view will
11995 * be able to achieve this minimum height (for example, if its parent layout
11996 * constrains it with less available height).
11997 *
11998 * @param minHeight The minimum height the view will try to be.
11999 */
12000 public void setMinimumHeight(int minHeight) {
12001 mMinHeight = minHeight;
12002 }
12003
12004 /**
12005 * Sets the minimum width of the view. It is not guaranteed the view will
12006 * be able to achieve this minimum width (for example, if its parent layout
12007 * constrains it with less available width).
12008 *
12009 * @param minWidth The minimum width the view will try to be.
12010 */
12011 public void setMinimumWidth(int minWidth) {
12012 mMinWidth = minWidth;
12013 }
12014
12015 /**
12016 * Get the animation currently associated with this view.
12017 *
12018 * @return The animation that is currently playing or
12019 * scheduled to play for this view.
12020 */
12021 public Animation getAnimation() {
12022 return mCurrentAnimation;
12023 }
12024
12025 /**
12026 * Start the specified animation now.
12027 *
12028 * @param animation the animation to start now
12029 */
12030 public void startAnimation(Animation animation) {
12031 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
12032 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012033 invalidateParentCaches();
12034 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012035 }
12036
12037 /**
12038 * Cancels any animations for this view.
12039 */
12040 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080012041 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080012042 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080012043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012044 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012045 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012046 }
12047
12048 /**
12049 * Sets the next animation to play for this view.
12050 * If you want the animation to play immediately, use
12051 * startAnimation. This method provides allows fine-grained
12052 * control over the start time and invalidation, but you
12053 * must make sure that 1) the animation has a start time set, and
12054 * 2) the view will be invalidated when the animation is supposed to
12055 * start.
12056 *
12057 * @param animation The next animation, or null.
12058 */
12059 public void setAnimation(Animation animation) {
12060 mCurrentAnimation = animation;
12061 if (animation != null) {
12062 animation.reset();
12063 }
12064 }
12065
12066 /**
12067 * Invoked by a parent ViewGroup to notify the start of the animation
12068 * currently associated with this view. If you override this method,
12069 * always call super.onAnimationStart();
12070 *
12071 * @see #setAnimation(android.view.animation.Animation)
12072 * @see #getAnimation()
12073 */
12074 protected void onAnimationStart() {
12075 mPrivateFlags |= ANIMATION_STARTED;
12076 }
12077
12078 /**
12079 * Invoked by a parent ViewGroup to notify the end of the animation
12080 * currently associated with this view. If you override this method,
12081 * always call super.onAnimationEnd();
12082 *
12083 * @see #setAnimation(android.view.animation.Animation)
12084 * @see #getAnimation()
12085 */
12086 protected void onAnimationEnd() {
12087 mPrivateFlags &= ~ANIMATION_STARTED;
12088 }
12089
12090 /**
12091 * Invoked if there is a Transform that involves alpha. Subclass that can
12092 * draw themselves with the specified alpha should return true, and then
12093 * respect that alpha when their onDraw() is called. If this returns false
12094 * then the view may be redirected to draw into an offscreen buffer to
12095 * fulfill the request, which will look fine, but may be slower than if the
12096 * subclass handles it internally. The default implementation returns false.
12097 *
12098 * @param alpha The alpha (0..255) to apply to the view's drawing
12099 * @return true if the view can draw with the specified alpha.
12100 */
12101 protected boolean onSetAlpha(int alpha) {
12102 return false;
12103 }
12104
12105 /**
12106 * This is used by the RootView to perform an optimization when
12107 * the view hierarchy contains one or several SurfaceView.
12108 * SurfaceView is always considered transparent, but its children are not,
12109 * therefore all View objects remove themselves from the global transparent
12110 * region (passed as a parameter to this function).
12111 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012112 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012113 *
12114 * @return Returns true if the effective visibility of the view at this
12115 * point is opaque, regardless of the transparent region; returns false
12116 * if it is possible for underlying windows to be seen behind the view.
12117 *
12118 * {@hide}
12119 */
12120 public boolean gatherTransparentRegion(Region region) {
12121 final AttachInfo attachInfo = mAttachInfo;
12122 if (region != null && attachInfo != null) {
12123 final int pflags = mPrivateFlags;
12124 if ((pflags & SKIP_DRAW) == 0) {
12125 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
12126 // remove it from the transparent region.
12127 final int[] location = attachInfo.mTransparentLocation;
12128 getLocationInWindow(location);
12129 region.op(location[0], location[1], location[0] + mRight - mLeft,
12130 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
12131 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
12132 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
12133 // exists, so we remove the background drawable's non-transparent
12134 // parts from this transparent region.
12135 applyDrawableToTransparentRegion(mBGDrawable, region);
12136 }
12137 }
12138 return true;
12139 }
12140
12141 /**
12142 * Play a sound effect for this view.
12143 *
12144 * <p>The framework will play sound effects for some built in actions, such as
12145 * clicking, but you may wish to play these effects in your widget,
12146 * for instance, for internal navigation.
12147 *
12148 * <p>The sound effect will only be played if sound effects are enabled by the user, and
12149 * {@link #isSoundEffectsEnabled()} is true.
12150 *
12151 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
12152 */
12153 public void playSoundEffect(int soundConstant) {
12154 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
12155 return;
12156 }
12157 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
12158 }
12159
12160 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012161 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012162 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012163 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012164 *
12165 * <p>The framework will provide haptic feedback for some built in actions,
12166 * such as long presses, but you may wish to provide feedback for your
12167 * own widget.
12168 *
12169 * <p>The feedback will only be performed if
12170 * {@link #isHapticFeedbackEnabled()} is true.
12171 *
12172 * @param feedbackConstant One of the constants defined in
12173 * {@link HapticFeedbackConstants}
12174 */
12175 public boolean performHapticFeedback(int feedbackConstant) {
12176 return performHapticFeedback(feedbackConstant, 0);
12177 }
12178
12179 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012180 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012181 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012182 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012183 *
12184 * @param feedbackConstant One of the constants defined in
12185 * {@link HapticFeedbackConstants}
12186 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
12187 */
12188 public boolean performHapticFeedback(int feedbackConstant, int flags) {
12189 if (mAttachInfo == null) {
12190 return false;
12191 }
Romain Guyf607bdc2010-09-10 19:20:06 -070012192 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070012193 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012194 && !isHapticFeedbackEnabled()) {
12195 return false;
12196 }
Romain Guy812ccbe2010-06-01 14:07:24 -070012197 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
12198 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012199 }
12200
12201 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012202 * Request that the visibility of the status bar be changed.
Scott Mainec6331b2011-05-24 16:55:56 -070012203 * @param visibility Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080012204 */
12205 public void setSystemUiVisibility(int visibility) {
12206 if (visibility != mSystemUiVisibility) {
12207 mSystemUiVisibility = visibility;
12208 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12209 mParent.recomputeViewAttributes(this);
12210 }
12211 }
12212 }
12213
12214 /**
12215 * Returns the status bar visibility that this view has requested.
Scott Mainec6331b2011-05-24 16:55:56 -070012216 * @return Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080012217 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080012218 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080012219 return mSystemUiVisibility;
12220 }
12221
Scott Mainec6331b2011-05-24 16:55:56 -070012222 /**
12223 * Set a listener to receive callbacks when the visibility of the system bar changes.
12224 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
12225 */
Joe Onorato664644d2011-01-23 17:53:23 -080012226 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
12227 mOnSystemUiVisibilityChangeListener = l;
12228 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12229 mParent.recomputeViewAttributes(this);
12230 }
12231 }
12232
12233 /**
12234 */
12235 public void dispatchSystemUiVisibilityChanged(int visibility) {
12236 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080012237 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080012238 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080012239 }
12240 }
12241
12242 /**
Joe Malin32736f02011-01-19 16:14:20 -080012243 * Creates an image that the system displays during the drag and drop
12244 * operation. This is called a &quot;drag shadow&quot;. The default implementation
12245 * for a DragShadowBuilder based on a View returns an image that has exactly the same
12246 * appearance as the given View. The default also positions the center of the drag shadow
12247 * directly under the touch point. If no View is provided (the constructor with no parameters
12248 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
12249 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
12250 * default is an invisible drag shadow.
12251 * <p>
12252 * You are not required to use the View you provide to the constructor as the basis of the
12253 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
12254 * anything you want as the drag shadow.
12255 * </p>
12256 * <p>
12257 * You pass a DragShadowBuilder object to the system when you start the drag. The system
12258 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
12259 * size and position of the drag shadow. It uses this data to construct a
12260 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
12261 * so that your application can draw the shadow image in the Canvas.
12262 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012263 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012264 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070012265 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070012266
12267 /**
Joe Malin32736f02011-01-19 16:14:20 -080012268 * Constructs a shadow image builder based on a View. By default, the resulting drag
12269 * shadow will have the same appearance and dimensions as the View, with the touch point
12270 * over the center of the View.
12271 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070012272 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012273 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070012274 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070012275 }
12276
Christopher Tate17ed60c2011-01-18 12:50:26 -080012277 /**
12278 * Construct a shadow builder object with no associated View. This
12279 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
12280 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
12281 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080012282 * reference to any View object. If they are not overridden, then the result is an
12283 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080012284 */
12285 public DragShadowBuilder() {
12286 mView = new WeakReference<View>(null);
12287 }
12288
12289 /**
12290 * Returns the View object that had been passed to the
12291 * {@link #View.DragShadowBuilder(View)}
12292 * constructor. If that View parameter was {@code null} or if the
12293 * {@link #View.DragShadowBuilder()}
12294 * constructor was used to instantiate the builder object, this method will return
12295 * null.
12296 *
12297 * @return The View object associate with this builder object.
12298 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070012299 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070012300 final public View getView() {
12301 return mView.get();
12302 }
12303
Christopher Tate2c095f32010-10-04 14:13:40 -070012304 /**
Joe Malin32736f02011-01-19 16:14:20 -080012305 * Provides the metrics for the shadow image. These include the dimensions of
12306 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070012307 * be centered under the touch location while dragging.
12308 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012309 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080012310 * same as the dimensions of the View itself and centers the shadow under
12311 * the touch point.
12312 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012313 *
Joe Malin32736f02011-01-19 16:14:20 -080012314 * @param shadowSize A {@link android.graphics.Point} containing the width and height
12315 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
12316 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
12317 * image.
12318 *
12319 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
12320 * shadow image that should be underneath the touch point during the drag and drop
12321 * operation. Your application must set {@link android.graphics.Point#x} to the
12322 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070012323 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012324 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070012325 final View view = mView.get();
12326 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012327 shadowSize.set(view.getWidth(), view.getHeight());
12328 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070012329 } else {
12330 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
12331 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012332 }
12333
12334 /**
Joe Malin32736f02011-01-19 16:14:20 -080012335 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
12336 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012337 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070012338 *
Joe Malin32736f02011-01-19 16:14:20 -080012339 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070012340 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012341 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070012342 final View view = mView.get();
12343 if (view != null) {
12344 view.draw(canvas);
12345 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012346 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070012347 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012348 }
12349 }
12350
12351 /**
Joe Malin32736f02011-01-19 16:14:20 -080012352 * Starts a drag and drop operation. When your application calls this method, it passes a
12353 * {@link android.view.View.DragShadowBuilder} object to the system. The
12354 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
12355 * to get metrics for the drag shadow, and then calls the object's
12356 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
12357 * <p>
12358 * Once the system has the drag shadow, it begins the drag and drop operation by sending
12359 * drag events to all the View objects in your application that are currently visible. It does
12360 * this either by calling the View object's drag listener (an implementation of
12361 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
12362 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
12363 * Both are passed a {@link android.view.DragEvent} object that has a
12364 * {@link android.view.DragEvent#getAction()} value of
12365 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
12366 * </p>
12367 * <p>
12368 * Your application can invoke startDrag() on any attached View object. The View object does not
12369 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
12370 * be related to the View the user selected for dragging.
12371 * </p>
12372 * @param data A {@link android.content.ClipData} object pointing to the data to be
12373 * transferred by the drag and drop operation.
12374 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
12375 * drag shadow.
12376 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
12377 * drop operation. This Object is put into every DragEvent object sent by the system during the
12378 * current drag.
12379 * <p>
12380 * myLocalState is a lightweight mechanism for the sending information from the dragged View
12381 * to the target Views. For example, it can contain flags that differentiate between a
12382 * a copy operation and a move operation.
12383 * </p>
12384 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
12385 * so the parameter should be set to 0.
12386 * @return {@code true} if the method completes successfully, or
12387 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
12388 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070012389 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012390 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012391 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070012392 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012393 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070012394 }
12395 boolean okay = false;
12396
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012397 Point shadowSize = new Point();
12398 Point shadowTouchPoint = new Point();
12399 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070012400
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012401 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
12402 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
12403 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070012404 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012405
Chris Tatea32dcf72010-10-14 12:13:50 -070012406 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012407 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
12408 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070012409 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012410 Surface surface = new Surface();
12411 try {
12412 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012413 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070012414 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070012415 + " surface=" + surface);
12416 if (token != null) {
12417 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070012418 try {
Chris Tate6b391282010-10-14 15:48:59 -070012419 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012420 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070012421 } finally {
12422 surface.unlockCanvasAndPost(canvas);
12423 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012424
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012425 final ViewAncestor root = getViewAncestor();
Christopher Tate407b4e92010-11-30 17:14:08 -080012426
12427 // Cache the local state object for delivery with DragEvents
12428 root.setLocalDragState(myLocalState);
12429
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012430 // repurpose 'shadowSize' for the last touch point
12431 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070012432
Christopher Tatea53146c2010-09-07 11:57:52 -070012433 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012434 shadowSize.x, shadowSize.y,
12435 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070012436 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070012437 }
12438 } catch (Exception e) {
12439 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
12440 surface.destroy();
12441 }
12442
12443 return okay;
12444 }
12445
Christopher Tatea53146c2010-09-07 11:57:52 -070012446 /**
Joe Malin32736f02011-01-19 16:14:20 -080012447 * Handles drag events sent by the system following a call to
12448 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
12449 *<p>
12450 * When the system calls this method, it passes a
12451 * {@link android.view.DragEvent} object. A call to
12452 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
12453 * in DragEvent. The method uses these to determine what is happening in the drag and drop
12454 * operation.
12455 * @param event The {@link android.view.DragEvent} sent by the system.
12456 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
12457 * in DragEvent, indicating the type of drag event represented by this object.
12458 * @return {@code true} if the method was successful, otherwise {@code false}.
12459 * <p>
12460 * The method should return {@code true} in response to an action type of
12461 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
12462 * operation.
12463 * </p>
12464 * <p>
12465 * The method should also return {@code true} in response to an action type of
12466 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
12467 * {@code false} if it didn't.
12468 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012469 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070012470 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070012471 return false;
12472 }
12473
12474 /**
Joe Malin32736f02011-01-19 16:14:20 -080012475 * Detects if this View is enabled and has a drag event listener.
12476 * If both are true, then it calls the drag event listener with the
12477 * {@link android.view.DragEvent} it received. If the drag event listener returns
12478 * {@code true}, then dispatchDragEvent() returns {@code true}.
12479 * <p>
12480 * For all other cases, the method calls the
12481 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
12482 * method and returns its result.
12483 * </p>
12484 * <p>
12485 * This ensures that a drag event is always consumed, even if the View does not have a drag
12486 * event listener. However, if the View has a listener and the listener returns true, then
12487 * onDragEvent() is not called.
12488 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012489 */
12490 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080012491 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070012492 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
12493 && mOnDragListener.onDrag(this, event)) {
12494 return true;
12495 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012496 return onDragEvent(event);
12497 }
12498
Christopher Tate3d4bf172011-03-28 16:16:46 -070012499 boolean canAcceptDrag() {
12500 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
12501 }
12502
Christopher Tatea53146c2010-09-07 11:57:52 -070012503 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070012504 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
12505 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070012506 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070012507 */
12508 public void onCloseSystemDialogs(String reason) {
12509 }
Joe Malin32736f02011-01-19 16:14:20 -080012510
Dianne Hackbornffa42482009-09-23 22:20:11 -070012511 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012512 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070012513 * update a Region being computed for
12514 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012515 * that any non-transparent parts of the Drawable are removed from the
12516 * given transparent region.
12517 *
12518 * @param dr The Drawable whose transparency is to be applied to the region.
12519 * @param region A Region holding the current transparency information,
12520 * where any parts of the region that are set are considered to be
12521 * transparent. On return, this region will be modified to have the
12522 * transparency information reduced by the corresponding parts of the
12523 * Drawable that are not transparent.
12524 * {@hide}
12525 */
12526 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
12527 if (DBG) {
12528 Log.i("View", "Getting transparent region for: " + this);
12529 }
12530 final Region r = dr.getTransparentRegion();
12531 final Rect db = dr.getBounds();
12532 final AttachInfo attachInfo = mAttachInfo;
12533 if (r != null && attachInfo != null) {
12534 final int w = getRight()-getLeft();
12535 final int h = getBottom()-getTop();
12536 if (db.left > 0) {
12537 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
12538 r.op(0, 0, db.left, h, Region.Op.UNION);
12539 }
12540 if (db.right < w) {
12541 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
12542 r.op(db.right, 0, w, h, Region.Op.UNION);
12543 }
12544 if (db.top > 0) {
12545 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
12546 r.op(0, 0, w, db.top, Region.Op.UNION);
12547 }
12548 if (db.bottom < h) {
12549 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
12550 r.op(0, db.bottom, w, h, Region.Op.UNION);
12551 }
12552 final int[] location = attachInfo.mTransparentLocation;
12553 getLocationInWindow(location);
12554 r.translate(location[0], location[1]);
12555 region.op(r, Region.Op.INTERSECT);
12556 } else {
12557 region.op(db, Region.Op.DIFFERENCE);
12558 }
12559 }
12560
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012561 private void checkForLongClick(int delayOffset) {
12562 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
12563 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012564
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012565 if (mPendingCheckForLongPress == null) {
12566 mPendingCheckForLongPress = new CheckForLongPress();
12567 }
12568 mPendingCheckForLongPress.rememberWindowAttachCount();
12569 postDelayed(mPendingCheckForLongPress,
12570 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012571 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012572 }
12573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012574 /**
12575 * Inflate a view from an XML resource. This convenience method wraps the {@link
12576 * LayoutInflater} class, which provides a full range of options for view inflation.
12577 *
12578 * @param context The Context object for your activity or application.
12579 * @param resource The resource ID to inflate
12580 * @param root A view group that will be the parent. Used to properly inflate the
12581 * layout_* parameters.
12582 * @see LayoutInflater
12583 */
12584 public static View inflate(Context context, int resource, ViewGroup root) {
12585 LayoutInflater factory = LayoutInflater.from(context);
12586 return factory.inflate(resource, root);
12587 }
Romain Guy33e72ae2010-07-17 12:40:29 -070012588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012589 /**
Adam Powell637d3372010-08-25 14:37:03 -070012590 * Scroll the view with standard behavior for scrolling beyond the normal
12591 * content boundaries. Views that call this method should override
12592 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
12593 * results of an over-scroll operation.
12594 *
12595 * Views can use this method to handle any touch or fling-based scrolling.
12596 *
12597 * @param deltaX Change in X in pixels
12598 * @param deltaY Change in Y in pixels
12599 * @param scrollX Current X scroll value in pixels before applying deltaX
12600 * @param scrollY Current Y scroll value in pixels before applying deltaY
12601 * @param scrollRangeX Maximum content scroll range along the X axis
12602 * @param scrollRangeY Maximum content scroll range along the Y axis
12603 * @param maxOverScrollX Number of pixels to overscroll by in either direction
12604 * along the X axis.
12605 * @param maxOverScrollY Number of pixels to overscroll by in either direction
12606 * along the Y axis.
12607 * @param isTouchEvent true if this scroll operation is the result of a touch event.
12608 * @return true if scrolling was clamped to an over-scroll boundary along either
12609 * axis, false otherwise.
12610 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012611 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070012612 protected boolean overScrollBy(int deltaX, int deltaY,
12613 int scrollX, int scrollY,
12614 int scrollRangeX, int scrollRangeY,
12615 int maxOverScrollX, int maxOverScrollY,
12616 boolean isTouchEvent) {
12617 final int overScrollMode = mOverScrollMode;
12618 final boolean canScrollHorizontal =
12619 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
12620 final boolean canScrollVertical =
12621 computeVerticalScrollRange() > computeVerticalScrollExtent();
12622 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
12623 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
12624 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
12625 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
12626
12627 int newScrollX = scrollX + deltaX;
12628 if (!overScrollHorizontal) {
12629 maxOverScrollX = 0;
12630 }
12631
12632 int newScrollY = scrollY + deltaY;
12633 if (!overScrollVertical) {
12634 maxOverScrollY = 0;
12635 }
12636
12637 // Clamp values if at the limits and record
12638 final int left = -maxOverScrollX;
12639 final int right = maxOverScrollX + scrollRangeX;
12640 final int top = -maxOverScrollY;
12641 final int bottom = maxOverScrollY + scrollRangeY;
12642
12643 boolean clampedX = false;
12644 if (newScrollX > right) {
12645 newScrollX = right;
12646 clampedX = true;
12647 } else if (newScrollX < left) {
12648 newScrollX = left;
12649 clampedX = true;
12650 }
12651
12652 boolean clampedY = false;
12653 if (newScrollY > bottom) {
12654 newScrollY = bottom;
12655 clampedY = true;
12656 } else if (newScrollY < top) {
12657 newScrollY = top;
12658 clampedY = true;
12659 }
12660
12661 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
12662
12663 return clampedX || clampedY;
12664 }
12665
12666 /**
12667 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
12668 * respond to the results of an over-scroll operation.
12669 *
12670 * @param scrollX New X scroll value in pixels
12671 * @param scrollY New Y scroll value in pixels
12672 * @param clampedX True if scrollX was clamped to an over-scroll boundary
12673 * @param clampedY True if scrollY was clamped to an over-scroll boundary
12674 */
12675 protected void onOverScrolled(int scrollX, int scrollY,
12676 boolean clampedX, boolean clampedY) {
12677 // Intentionally empty.
12678 }
12679
12680 /**
12681 * Returns the over-scroll mode for this view. The result will be
12682 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12683 * (allow over-scrolling only if the view content is larger than the container),
12684 * or {@link #OVER_SCROLL_NEVER}.
12685 *
12686 * @return This view's over-scroll mode.
12687 */
12688 public int getOverScrollMode() {
12689 return mOverScrollMode;
12690 }
12691
12692 /**
12693 * Set the over-scroll mode for this view. Valid over-scroll modes are
12694 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12695 * (allow over-scrolling only if the view content is larger than the container),
12696 * or {@link #OVER_SCROLL_NEVER}.
12697 *
12698 * Setting the over-scroll mode of a view will have an effect only if the
12699 * view is capable of scrolling.
12700 *
12701 * @param overScrollMode The new over-scroll mode for this view.
12702 */
12703 public void setOverScrollMode(int overScrollMode) {
12704 if (overScrollMode != OVER_SCROLL_ALWAYS &&
12705 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
12706 overScrollMode != OVER_SCROLL_NEVER) {
12707 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
12708 }
12709 mOverScrollMode = overScrollMode;
12710 }
12711
12712 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012713 * Gets a scale factor that determines the distance the view should scroll
12714 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
12715 * @return The vertical scroll scale factor.
12716 * @hide
12717 */
12718 protected float getVerticalScrollFactor() {
12719 if (mVerticalScrollFactor == 0) {
12720 TypedValue outValue = new TypedValue();
12721 if (!mContext.getTheme().resolveAttribute(
12722 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
12723 throw new IllegalStateException(
12724 "Expected theme to define listPreferredItemHeight.");
12725 }
12726 mVerticalScrollFactor = outValue.getDimension(
12727 mContext.getResources().getDisplayMetrics());
12728 }
12729 return mVerticalScrollFactor;
12730 }
12731
12732 /**
12733 * Gets a scale factor that determines the distance the view should scroll
12734 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
12735 * @return The horizontal scroll scale factor.
12736 * @hide
12737 */
12738 protected float getHorizontalScrollFactor() {
12739 // TODO: Should use something else.
12740 return getVerticalScrollFactor();
12741 }
12742
Chet Haaseb39f0512011-05-24 14:36:40 -070012743 //
12744 // Properties
12745 //
12746 /**
12747 * A Property wrapper around the <code>alpha</code> functionality handled by the
12748 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
12749 */
12750 static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
12751 @Override
12752 public void setValue(View object, float value) {
12753 object.setAlpha(value);
12754 }
12755
12756 @Override
12757 public Float get(View object) {
12758 return object.getAlpha();
12759 }
12760 };
12761
12762 /**
12763 * A Property wrapper around the <code>translationX</code> functionality handled by the
12764 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
12765 */
12766 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
12767 @Override
12768 public void setValue(View object, float value) {
12769 object.setTranslationX(value);
12770 }
12771
12772 @Override
12773 public Float get(View object) {
12774 return object.getTranslationX();
12775 }
12776 };
12777
12778 /**
12779 * A Property wrapper around the <code>translationY</code> functionality handled by the
12780 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
12781 */
12782 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
12783 @Override
12784 public void setValue(View object, float value) {
12785 object.setTranslationY(value);
12786 }
12787
12788 @Override
12789 public Float get(View object) {
12790 return object.getTranslationY();
12791 }
12792 };
12793
12794 /**
12795 * A Property wrapper around the <code>x</code> functionality handled by the
12796 * {@link View#setX(float)} and {@link View#getX()} methods.
12797 */
12798 public static Property<View, Float> X = new FloatProperty<View>("x") {
12799 @Override
12800 public void setValue(View object, float value) {
12801 object.setX(value);
12802 }
12803
12804 @Override
12805 public Float get(View object) {
12806 return object.getX();
12807 }
12808 };
12809
12810 /**
12811 * A Property wrapper around the <code>y</code> functionality handled by the
12812 * {@link View#setY(float)} and {@link View#getY()} methods.
12813 */
12814 public static Property<View, Float> Y = new FloatProperty<View>("y") {
12815 @Override
12816 public void setValue(View object, float value) {
12817 object.setY(value);
12818 }
12819
12820 @Override
12821 public Float get(View object) {
12822 return object.getY();
12823 }
12824 };
12825
12826 /**
12827 * A Property wrapper around the <code>rotation</code> functionality handled by the
12828 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
12829 */
12830 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
12831 @Override
12832 public void setValue(View object, float value) {
12833 object.setRotation(value);
12834 }
12835
12836 @Override
12837 public Float get(View object) {
12838 return object.getRotation();
12839 }
12840 };
12841
12842 /**
12843 * A Property wrapper around the <code>rotationX</code> functionality handled by the
12844 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
12845 */
12846 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
12847 @Override
12848 public void setValue(View object, float value) {
12849 object.setRotationX(value);
12850 }
12851
12852 @Override
12853 public Float get(View object) {
12854 return object.getRotationX();
12855 }
12856 };
12857
12858 /**
12859 * A Property wrapper around the <code>rotationY</code> functionality handled by the
12860 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
12861 */
12862 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
12863 @Override
12864 public void setValue(View object, float value) {
12865 object.setRotationY(value);
12866 }
12867
12868 @Override
12869 public Float get(View object) {
12870 return object.getRotationY();
12871 }
12872 };
12873
12874 /**
12875 * A Property wrapper around the <code>scaleX</code> functionality handled by the
12876 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
12877 */
12878 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
12879 @Override
12880 public void setValue(View object, float value) {
12881 object.setScaleX(value);
12882 }
12883
12884 @Override
12885 public Float get(View object) {
12886 return object.getScaleX();
12887 }
12888 };
12889
12890 /**
12891 * A Property wrapper around the <code>scaleY</code> functionality handled by the
12892 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
12893 */
12894 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
12895 @Override
12896 public void setValue(View object, float value) {
12897 object.setScaleY(value);
12898 }
12899
12900 @Override
12901 public Float get(View object) {
12902 return object.getScaleY();
12903 }
12904 };
12905
Jeff Brown33bbfd22011-02-24 20:55:35 -080012906 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012907 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
12908 * Each MeasureSpec represents a requirement for either the width or the height.
12909 * A MeasureSpec is comprised of a size and a mode. There are three possible
12910 * modes:
12911 * <dl>
12912 * <dt>UNSPECIFIED</dt>
12913 * <dd>
12914 * The parent has not imposed any constraint on the child. It can be whatever size
12915 * it wants.
12916 * </dd>
12917 *
12918 * <dt>EXACTLY</dt>
12919 * <dd>
12920 * The parent has determined an exact size for the child. The child is going to be
12921 * given those bounds regardless of how big it wants to be.
12922 * </dd>
12923 *
12924 * <dt>AT_MOST</dt>
12925 * <dd>
12926 * The child can be as large as it wants up to the specified size.
12927 * </dd>
12928 * </dl>
12929 *
12930 * MeasureSpecs are implemented as ints to reduce object allocation. This class
12931 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
12932 */
12933 public static class MeasureSpec {
12934 private static final int MODE_SHIFT = 30;
12935 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
12936
12937 /**
12938 * Measure specification mode: The parent has not imposed any constraint
12939 * on the child. It can be whatever size it wants.
12940 */
12941 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
12942
12943 /**
12944 * Measure specification mode: The parent has determined an exact size
12945 * for the child. The child is going to be given those bounds regardless
12946 * of how big it wants to be.
12947 */
12948 public static final int EXACTLY = 1 << MODE_SHIFT;
12949
12950 /**
12951 * Measure specification mode: The child can be as large as it wants up
12952 * to the specified size.
12953 */
12954 public static final int AT_MOST = 2 << MODE_SHIFT;
12955
12956 /**
12957 * Creates a measure specification based on the supplied size and mode.
12958 *
12959 * The mode must always be one of the following:
12960 * <ul>
12961 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
12962 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
12963 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
12964 * </ul>
12965 *
12966 * @param size the size of the measure specification
12967 * @param mode the mode of the measure specification
12968 * @return the measure specification based on size and mode
12969 */
12970 public static int makeMeasureSpec(int size, int mode) {
12971 return size + mode;
12972 }
12973
12974 /**
12975 * Extracts the mode from the supplied measure specification.
12976 *
12977 * @param measureSpec the measure specification to extract the mode from
12978 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
12979 * {@link android.view.View.MeasureSpec#AT_MOST} or
12980 * {@link android.view.View.MeasureSpec#EXACTLY}
12981 */
12982 public static int getMode(int measureSpec) {
12983 return (measureSpec & MODE_MASK);
12984 }
12985
12986 /**
12987 * Extracts the size from the supplied measure specification.
12988 *
12989 * @param measureSpec the measure specification to extract the size from
12990 * @return the size in pixels defined in the supplied measure specification
12991 */
12992 public static int getSize(int measureSpec) {
12993 return (measureSpec & ~MODE_MASK);
12994 }
12995
12996 /**
12997 * Returns a String representation of the specified measure
12998 * specification.
12999 *
13000 * @param measureSpec the measure specification to convert to a String
13001 * @return a String with the following format: "MeasureSpec: MODE SIZE"
13002 */
13003 public static String toString(int measureSpec) {
13004 int mode = getMode(measureSpec);
13005 int size = getSize(measureSpec);
13006
13007 StringBuilder sb = new StringBuilder("MeasureSpec: ");
13008
13009 if (mode == UNSPECIFIED)
13010 sb.append("UNSPECIFIED ");
13011 else if (mode == EXACTLY)
13012 sb.append("EXACTLY ");
13013 else if (mode == AT_MOST)
13014 sb.append("AT_MOST ");
13015 else
13016 sb.append(mode).append(" ");
13017
13018 sb.append(size);
13019 return sb.toString();
13020 }
13021 }
13022
13023 class CheckForLongPress implements Runnable {
13024
13025 private int mOriginalWindowAttachCount;
13026
13027 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070013028 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013029 && mOriginalWindowAttachCount == mWindowAttachCount) {
13030 if (performLongClick()) {
13031 mHasPerformedLongPress = true;
13032 }
13033 }
13034 }
13035
13036 public void rememberWindowAttachCount() {
13037 mOriginalWindowAttachCount = mWindowAttachCount;
13038 }
13039 }
Joe Malin32736f02011-01-19 16:14:20 -080013040
Adam Powelle14579b2009-12-16 18:39:52 -080013041 private final class CheckForTap implements Runnable {
13042 public void run() {
13043 mPrivateFlags &= ~PREPRESSED;
13044 mPrivateFlags |= PRESSED;
13045 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013046 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080013047 }
13048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013049
Adam Powella35d7682010-03-12 14:48:13 -080013050 private final class PerformClick implements Runnable {
13051 public void run() {
13052 performClick();
13053 }
13054 }
13055
Dianne Hackborn63042d62011-01-26 18:56:29 -080013056 /** @hide */
13057 public void hackTurnOffWindowResizeAnim(boolean off) {
13058 mAttachInfo.mTurnOffWindowResizeAnim = off;
13059 }
Joe Malin32736f02011-01-19 16:14:20 -080013060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013061 /**
Chet Haasea00f3862011-02-22 06:34:40 -080013062 * This method returns a ViewPropertyAnimator object, which can be used to animate
13063 * specific properties on this View.
13064 *
13065 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
13066 */
13067 public ViewPropertyAnimator animate() {
13068 if (mAnimator == null) {
13069 mAnimator = new ViewPropertyAnimator(this);
13070 }
13071 return mAnimator;
13072 }
13073
13074 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013075 * Interface definition for a callback to be invoked when a key event is
13076 * dispatched to this view. The callback will be invoked before the key
13077 * event is given to the view.
13078 */
13079 public interface OnKeyListener {
13080 /**
13081 * Called when a key is dispatched to a view. This allows listeners to
13082 * get a chance to respond before the target view.
13083 *
13084 * @param v The view the key has been dispatched to.
13085 * @param keyCode The code for the physical key that was pressed
13086 * @param event The KeyEvent object containing full information about
13087 * the event.
13088 * @return True if the listener has consumed the event, false otherwise.
13089 */
13090 boolean onKey(View v, int keyCode, KeyEvent event);
13091 }
13092
13093 /**
13094 * Interface definition for a callback to be invoked when a touch event is
13095 * dispatched to this view. The callback will be invoked before the touch
13096 * event is given to the view.
13097 */
13098 public interface OnTouchListener {
13099 /**
13100 * Called when a touch event is dispatched to a view. This allows listeners to
13101 * get a chance to respond before the target view.
13102 *
13103 * @param v The view the touch event has been dispatched to.
13104 * @param event The MotionEvent object containing full information about
13105 * the event.
13106 * @return True if the listener has consumed the event, false otherwise.
13107 */
13108 boolean onTouch(View v, MotionEvent event);
13109 }
13110
13111 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013112 * Interface definition for a callback to be invoked when a generic motion event is
13113 * dispatched to this view. The callback will be invoked before the generic motion
13114 * event is given to the view.
13115 */
13116 public interface OnGenericMotionListener {
13117 /**
13118 * Called when a generic motion event is dispatched to a view. This allows listeners to
13119 * get a chance to respond before the target view.
13120 *
13121 * @param v The view the generic motion event has been dispatched to.
13122 * @param event The MotionEvent object containing full information about
13123 * the event.
13124 * @return True if the listener has consumed the event, false otherwise.
13125 */
13126 boolean onGenericMotion(View v, MotionEvent event);
13127 }
13128
13129 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013130 * Interface definition for a callback to be invoked when a view has been clicked and held.
13131 */
13132 public interface OnLongClickListener {
13133 /**
13134 * Called when a view has been clicked and held.
13135 *
13136 * @param v The view that was clicked and held.
13137 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080013138 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013139 */
13140 boolean onLongClick(View v);
13141 }
13142
13143 /**
Chris Tate32affef2010-10-18 15:29:21 -070013144 * Interface definition for a callback to be invoked when a drag is being dispatched
13145 * to this view. The callback will be invoked before the hosting view's own
13146 * onDrag(event) method. If the listener wants to fall back to the hosting view's
13147 * onDrag(event) behavior, it should return 'false' from this callback.
13148 */
13149 public interface OnDragListener {
13150 /**
13151 * Called when a drag event is dispatched to a view. This allows listeners
13152 * to get a chance to override base View behavior.
13153 *
Joe Malin32736f02011-01-19 16:14:20 -080013154 * @param v The View that received the drag event.
13155 * @param event The {@link android.view.DragEvent} object for the drag event.
13156 * @return {@code true} if the drag event was handled successfully, or {@code false}
13157 * if the drag event was not handled. Note that {@code false} will trigger the View
13158 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070013159 */
13160 boolean onDrag(View v, DragEvent event);
13161 }
13162
13163 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013164 * Interface definition for a callback to be invoked when the focus state of
13165 * a view changed.
13166 */
13167 public interface OnFocusChangeListener {
13168 /**
13169 * Called when the focus state of a view has changed.
13170 *
13171 * @param v The view whose state has changed.
13172 * @param hasFocus The new focus state of v.
13173 */
13174 void onFocusChange(View v, boolean hasFocus);
13175 }
13176
13177 /**
13178 * Interface definition for a callback to be invoked when a view is clicked.
13179 */
13180 public interface OnClickListener {
13181 /**
13182 * Called when a view has been clicked.
13183 *
13184 * @param v The view that was clicked.
13185 */
13186 void onClick(View v);
13187 }
13188
13189 /**
13190 * Interface definition for a callback to be invoked when the context menu
13191 * for this view is being built.
13192 */
13193 public interface OnCreateContextMenuListener {
13194 /**
13195 * Called when the context menu for this view is being built. It is not
13196 * safe to hold onto the menu after this method returns.
13197 *
13198 * @param menu The context menu that is being built
13199 * @param v The view for which the context menu is being built
13200 * @param menuInfo Extra information about the item for which the
13201 * context menu should be shown. This information will vary
13202 * depending on the class of v.
13203 */
13204 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
13205 }
13206
Joe Onorato664644d2011-01-23 17:53:23 -080013207 /**
13208 * Interface definition for a callback to be invoked when the status bar changes
13209 * visibility.
13210 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070013211 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080013212 */
13213 public interface OnSystemUiVisibilityChangeListener {
13214 /**
13215 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070013216 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080013217 *
13218 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
13219 */
13220 public void onSystemUiVisibilityChange(int visibility);
13221 }
13222
Adam Powell4afd62b2011-02-18 15:02:18 -080013223 /**
13224 * Interface definition for a callback to be invoked when this view is attached
13225 * or detached from its window.
13226 */
13227 public interface OnAttachStateChangeListener {
13228 /**
13229 * Called when the view is attached to a window.
13230 * @param v The view that was attached
13231 */
13232 public void onViewAttachedToWindow(View v);
13233 /**
13234 * Called when the view is detached from a window.
13235 * @param v The view that was detached
13236 */
13237 public void onViewDetachedFromWindow(View v);
13238 }
13239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013240 private final class UnsetPressedState implements Runnable {
13241 public void run() {
13242 setPressed(false);
13243 }
13244 }
13245
13246 /**
13247 * Base class for derived classes that want to save and restore their own
13248 * state in {@link android.view.View#onSaveInstanceState()}.
13249 */
13250 public static class BaseSavedState extends AbsSavedState {
13251 /**
13252 * Constructor used when reading from a parcel. Reads the state of the superclass.
13253 *
13254 * @param source
13255 */
13256 public BaseSavedState(Parcel source) {
13257 super(source);
13258 }
13259
13260 /**
13261 * Constructor called by derived classes when creating their SavedState objects
13262 *
13263 * @param superState The state of the superclass of this view
13264 */
13265 public BaseSavedState(Parcelable superState) {
13266 super(superState);
13267 }
13268
13269 public static final Parcelable.Creator<BaseSavedState> CREATOR =
13270 new Parcelable.Creator<BaseSavedState>() {
13271 public BaseSavedState createFromParcel(Parcel in) {
13272 return new BaseSavedState(in);
13273 }
13274
13275 public BaseSavedState[] newArray(int size) {
13276 return new BaseSavedState[size];
13277 }
13278 };
13279 }
13280
13281 /**
13282 * A set of information given to a view when it is attached to its parent
13283 * window.
13284 */
13285 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013286 interface Callbacks {
13287 void playSoundEffect(int effectId);
13288 boolean performHapticFeedback(int effectId, boolean always);
13289 }
13290
13291 /**
13292 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
13293 * to a Handler. This class contains the target (View) to invalidate and
13294 * the coordinates of the dirty rectangle.
13295 *
13296 * For performance purposes, this class also implements a pool of up to
13297 * POOL_LIMIT objects that get reused. This reduces memory allocations
13298 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013299 */
Romain Guyd928d682009-03-31 17:52:16 -070013300 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013301 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070013302 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
13303 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070013304 public InvalidateInfo newInstance() {
13305 return new InvalidateInfo();
13306 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013307
Romain Guyd928d682009-03-31 17:52:16 -070013308 public void onAcquired(InvalidateInfo element) {
13309 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013310
Romain Guyd928d682009-03-31 17:52:16 -070013311 public void onReleased(InvalidateInfo element) {
13312 }
13313 }, POOL_LIMIT)
13314 );
13315
13316 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013317 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013318
13319 View target;
13320
13321 int left;
13322 int top;
13323 int right;
13324 int bottom;
13325
Romain Guyd928d682009-03-31 17:52:16 -070013326 public void setNextPoolable(InvalidateInfo element) {
13327 mNext = element;
13328 }
13329
13330 public InvalidateInfo getNextPoolable() {
13331 return mNext;
13332 }
13333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013334 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070013335 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013336 }
13337
13338 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070013339 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013340 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013341
13342 public boolean isPooled() {
13343 return mIsPooled;
13344 }
13345
13346 public void setPooled(boolean isPooled) {
13347 mIsPooled = isPooled;
13348 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013349 }
13350
13351 final IWindowSession mSession;
13352
13353 final IWindow mWindow;
13354
13355 final IBinder mWindowToken;
13356
13357 final Callbacks mRootCallbacks;
13358
Romain Guy59a12ca2011-06-09 17:48:21 -070013359 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080013360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013361 /**
13362 * The top view of the hierarchy.
13363 */
13364 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070013365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013366 IBinder mPanelParentWindowToken;
13367 Surface mSurface;
13368
Romain Guyb051e892010-09-28 19:09:36 -070013369 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013370 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070013371 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080013372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013373 /**
Romain Guy8506ab42009-06-11 17:35:47 -070013374 * Scale factor used by the compatibility mode
13375 */
13376 float mApplicationScale;
13377
13378 /**
13379 * Indicates whether the application is in compatibility mode
13380 */
13381 boolean mScalingRequired;
13382
13383 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013384 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080013385 */
13386 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080013387
Dianne Hackborn63042d62011-01-26 18:56:29 -080013388 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013389 * Left position of this view's window
13390 */
13391 int mWindowLeft;
13392
13393 /**
13394 * Top position of this view's window
13395 */
13396 int mWindowTop;
13397
13398 /**
Adam Powell26153a32010-11-08 15:22:27 -080013399 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070013400 */
Adam Powell26153a32010-11-08 15:22:27 -080013401 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070013402
13403 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013404 * For windows that are full-screen but using insets to layout inside
13405 * of the screen decorations, these are the current insets for the
13406 * content of the window.
13407 */
13408 final Rect mContentInsets = new Rect();
13409
13410 /**
13411 * For windows that are full-screen but using insets to layout inside
13412 * of the screen decorations, these are the current insets for the
13413 * actual visible parts of the window.
13414 */
13415 final Rect mVisibleInsets = new Rect();
13416
13417 /**
13418 * The internal insets given by this window. This value is
13419 * supplied by the client (through
13420 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
13421 * be given to the window manager when changed to be used in laying
13422 * out windows behind it.
13423 */
13424 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
13425 = new ViewTreeObserver.InternalInsetsInfo();
13426
13427 /**
13428 * All views in the window's hierarchy that serve as scroll containers,
13429 * used to determine if the window can be resized or must be panned
13430 * to adjust for a soft input area.
13431 */
13432 final ArrayList<View> mScrollContainers = new ArrayList<View>();
13433
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070013434 final KeyEvent.DispatcherState mKeyDispatchState
13435 = new KeyEvent.DispatcherState();
13436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013437 /**
13438 * Indicates whether the view's window currently has the focus.
13439 */
13440 boolean mHasWindowFocus;
13441
13442 /**
13443 * The current visibility of the window.
13444 */
13445 int mWindowVisibility;
13446
13447 /**
13448 * Indicates the time at which drawing started to occur.
13449 */
13450 long mDrawingTime;
13451
13452 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070013453 * Indicates whether or not ignoring the DIRTY_MASK flags.
13454 */
13455 boolean mIgnoreDirtyState;
13456
13457 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013458 * Indicates whether the view's window is currently in touch mode.
13459 */
13460 boolean mInTouchMode;
13461
13462 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013463 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013464 * the next time it performs a traversal
13465 */
13466 boolean mRecomputeGlobalAttributes;
13467
13468 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013469 * Set during a traveral if any views want to keep the screen on.
13470 */
13471 boolean mKeepScreenOn;
13472
13473 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013474 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
13475 */
13476 int mSystemUiVisibility;
13477
13478 /**
13479 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
13480 * attached.
13481 */
13482 boolean mHasSystemUiListeners;
13483
13484 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013485 * Set if the visibility of any views has changed.
13486 */
13487 boolean mViewVisibilityChanged;
13488
13489 /**
13490 * Set to true if a view has been scrolled.
13491 */
13492 boolean mViewScrollChanged;
13493
13494 /**
13495 * Global to the view hierarchy used as a temporary for dealing with
13496 * x/y points in the transparent region computations.
13497 */
13498 final int[] mTransparentLocation = new int[2];
13499
13500 /**
13501 * Global to the view hierarchy used as a temporary for dealing with
13502 * x/y points in the ViewGroup.invalidateChild implementation.
13503 */
13504 final int[] mInvalidateChildLocation = new int[2];
13505
Chet Haasec3aa3612010-06-17 08:50:37 -070013506
13507 /**
13508 * Global to the view hierarchy used as a temporary for dealing with
13509 * x/y location when view is transformed.
13510 */
13511 final float[] mTmpTransformLocation = new float[2];
13512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013513 /**
13514 * The view tree observer used to dispatch global events like
13515 * layout, pre-draw, touch mode change, etc.
13516 */
13517 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
13518
13519 /**
13520 * A Canvas used by the view hierarchy to perform bitmap caching.
13521 */
13522 Canvas mCanvas;
13523
13524 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013525 * A Handler supplied by a view's {@link android.view.ViewAncestor}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013526 * handler can be used to pump events in the UI events queue.
13527 */
13528 final Handler mHandler;
13529
13530 /**
13531 * Identifier for messages requesting the view to be invalidated.
13532 * Such messages should be sent to {@link #mHandler}.
13533 */
13534 static final int INVALIDATE_MSG = 0x1;
13535
13536 /**
13537 * Identifier for messages requesting the view to invalidate a region.
13538 * Such messages should be sent to {@link #mHandler}.
13539 */
13540 static final int INVALIDATE_RECT_MSG = 0x2;
13541
13542 /**
13543 * Temporary for use in computing invalidate rectangles while
13544 * calling up the hierarchy.
13545 */
13546 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070013547
13548 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070013549 * Temporary for use in computing hit areas with transformed views
13550 */
13551 final RectF mTmpTransformRect = new RectF();
13552
13553 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070013554 * Temporary list for use in collecting focusable descendents of a view.
13555 */
13556 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
13557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013558 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013559 * The id of the window for accessibility purposes.
13560 */
13561 int mAccessibilityWindowId = View.NO_ID;
13562
13563 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013564 * Creates a new set of attachment information with the specified
13565 * events handler and thread.
13566 *
13567 * @param handler the events handler the view must use
13568 */
13569 AttachInfo(IWindowSession session, IWindow window,
13570 Handler handler, Callbacks effectPlayer) {
13571 mSession = session;
13572 mWindow = window;
13573 mWindowToken = window.asBinder();
13574 mHandler = handler;
13575 mRootCallbacks = effectPlayer;
13576 }
13577 }
13578
13579 /**
13580 * <p>ScrollabilityCache holds various fields used by a View when scrolling
13581 * is supported. This avoids keeping too many unused fields in most
13582 * instances of View.</p>
13583 */
Mike Cleronf116bf82009-09-27 19:14:12 -070013584 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080013585
Mike Cleronf116bf82009-09-27 19:14:12 -070013586 /**
13587 * Scrollbars are not visible
13588 */
13589 public static final int OFF = 0;
13590
13591 /**
13592 * Scrollbars are visible
13593 */
13594 public static final int ON = 1;
13595
13596 /**
13597 * Scrollbars are fading away
13598 */
13599 public static final int FADING = 2;
13600
13601 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080013602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013603 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070013604 public int scrollBarDefaultDelayBeforeFade;
13605 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013606
13607 public int scrollBarSize;
13608 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070013609 public float[] interpolatorValues;
13610 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013611
13612 public final Paint paint;
13613 public final Matrix matrix;
13614 public Shader shader;
13615
Mike Cleronf116bf82009-09-27 19:14:12 -070013616 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
13617
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013618 private static final float[] OPAQUE = { 255 };
13619 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080013620
Mike Cleronf116bf82009-09-27 19:14:12 -070013621 /**
13622 * When fading should start. This time moves into the future every time
13623 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
13624 */
13625 public long fadeStartTime;
13626
13627
13628 /**
13629 * The current state of the scrollbars: ON, OFF, or FADING
13630 */
13631 public int state = OFF;
13632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013633 private int mLastColor;
13634
Mike Cleronf116bf82009-09-27 19:14:12 -070013635 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013636 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
13637 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070013638 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
13639 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013640
13641 paint = new Paint();
13642 matrix = new Matrix();
13643 // use use a height of 1, and then wack the matrix each time we
13644 // actually use it.
13645 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070013646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013647 paint.setShader(shader);
13648 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070013649 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013650 }
Romain Guy8506ab42009-06-11 17:35:47 -070013651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013652 public void setFadeColor(int color) {
13653 if (color != 0 && color != mLastColor) {
13654 mLastColor = color;
13655 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070013656
Romain Guye55e1a72009-08-27 10:42:26 -070013657 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
13658 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070013659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013660 paint.setShader(shader);
13661 // Restore the default transfer mode (src_over)
13662 paint.setXfermode(null);
13663 }
13664 }
Joe Malin32736f02011-01-19 16:14:20 -080013665
Mike Cleronf116bf82009-09-27 19:14:12 -070013666 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070013667 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070013668 if (now >= fadeStartTime) {
13669
13670 // the animation fades the scrollbars out by changing
13671 // the opacity (alpha) from fully opaque to fully
13672 // transparent
13673 int nextFrame = (int) now;
13674 int framesCount = 0;
13675
13676 Interpolator interpolator = scrollBarInterpolator;
13677
13678 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013679 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070013680
13681 // End transparent
13682 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013683 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070013684
13685 state = FADING;
13686
13687 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080013688 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070013689 }
13690 }
13691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013692 }
13693}