blob: 683b47e87332c4acd9415efd48e68b7fbbe3944b [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 Meglioc46f7ff2011-06-06 18:23:10 -07004340 * Set the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004341 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004342 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4343 * {@link #LAYOUT_DIRECTION_RTL},
4344 * {@link #LAYOUT_DIRECTION_INHERIT} or
4345 * {@link #LAYOUT_DIRECTION_LOCALE}.
4346 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004347 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004348 * @hide
4349 */
4350 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004351 public void setLayoutDirection(int layoutDirection) {
4352 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
Cibu Johny7632cb92010-02-22 13:01:02 -08004353 }
4354
4355 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004356 * Returns the resolved layout direction for this view.
4357 *
4358 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4359 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4360 *
4361 * @hide
4362 */
4363 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4364 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4365 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4366 })
4367 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004368 resolveLayoutDirectionIfNeeded();
4369 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004370 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4371 }
4372
4373 /**
4374 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4375 * layout attribute and/or the inherited value from the parent.</p>
4376 *
4377 * @return true if the layout is right-to-left.
4378 *
4379 * @hide
4380 */
4381 @ViewDebug.ExportedProperty(category = "layout")
4382 public boolean isLayoutRtl() {
4383 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4384 }
4385
4386 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004387 * If this view doesn't do any drawing on its own, set this flag to
4388 * allow further optimizations. By default, this flag is not set on
4389 * View, but could be set on some View subclasses such as ViewGroup.
4390 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004391 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4392 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004393 *
4394 * @param willNotDraw whether or not this View draw on its own
4395 */
4396 public void setWillNotDraw(boolean willNotDraw) {
4397 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4398 }
4399
4400 /**
4401 * Returns whether or not this View draws on its own.
4402 *
4403 * @return true if this view has nothing to draw, false otherwise
4404 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004405 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004406 public boolean willNotDraw() {
4407 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4408 }
4409
4410 /**
4411 * When a View's drawing cache is enabled, drawing is redirected to an
4412 * offscreen bitmap. Some views, like an ImageView, must be able to
4413 * bypass this mechanism if they already draw a single bitmap, to avoid
4414 * unnecessary usage of the memory.
4415 *
4416 * @param willNotCacheDrawing true if this view does not cache its
4417 * drawing, false otherwise
4418 */
4419 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4420 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4421 }
4422
4423 /**
4424 * Returns whether or not this View can cache its drawing or not.
4425 *
4426 * @return true if this view does not cache its drawing, false otherwise
4427 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004428 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004429 public boolean willNotCacheDrawing() {
4430 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4431 }
4432
4433 /**
4434 * Indicates whether this view reacts to click events or not.
4435 *
4436 * @return true if the view is clickable, false otherwise
4437 *
4438 * @see #setClickable(boolean)
4439 * @attr ref android.R.styleable#View_clickable
4440 */
4441 @ViewDebug.ExportedProperty
4442 public boolean isClickable() {
4443 return (mViewFlags & CLICKABLE) == CLICKABLE;
4444 }
4445
4446 /**
4447 * Enables or disables click events for this view. When a view
4448 * is clickable it will change its state to "pressed" on every click.
4449 * Subclasses should set the view clickable to visually react to
4450 * user's clicks.
4451 *
4452 * @param clickable true to make the view clickable, false otherwise
4453 *
4454 * @see #isClickable()
4455 * @attr ref android.R.styleable#View_clickable
4456 */
4457 public void setClickable(boolean clickable) {
4458 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4459 }
4460
4461 /**
4462 * Indicates whether this view reacts to long click events or not.
4463 *
4464 * @return true if the view is long clickable, false otherwise
4465 *
4466 * @see #setLongClickable(boolean)
4467 * @attr ref android.R.styleable#View_longClickable
4468 */
4469 public boolean isLongClickable() {
4470 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4471 }
4472
4473 /**
4474 * Enables or disables long click events for this view. When a view is long
4475 * clickable it reacts to the user holding down the button for a longer
4476 * duration than a tap. This event can either launch the listener or a
4477 * context menu.
4478 *
4479 * @param longClickable true to make the view long clickable, false otherwise
4480 * @see #isLongClickable()
4481 * @attr ref android.R.styleable#View_longClickable
4482 */
4483 public void setLongClickable(boolean longClickable) {
4484 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4485 }
4486
4487 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004488 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004489 *
4490 * @see #isClickable()
4491 * @see #setClickable(boolean)
4492 *
4493 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4494 * the View's internal state from a previously set "pressed" state.
4495 */
4496 public void setPressed(boolean pressed) {
4497 if (pressed) {
4498 mPrivateFlags |= PRESSED;
4499 } else {
4500 mPrivateFlags &= ~PRESSED;
4501 }
4502 refreshDrawableState();
4503 dispatchSetPressed(pressed);
4504 }
4505
4506 /**
4507 * Dispatch setPressed to all of this View's children.
4508 *
4509 * @see #setPressed(boolean)
4510 *
4511 * @param pressed The new pressed state
4512 */
4513 protected void dispatchSetPressed(boolean pressed) {
4514 }
4515
4516 /**
4517 * Indicates whether the view is currently in pressed state. Unless
4518 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4519 * the pressed state.
4520 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004521 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004522 * @see #isClickable()
4523 * @see #setClickable(boolean)
4524 *
4525 * @return true if the view is currently pressed, false otherwise
4526 */
4527 public boolean isPressed() {
4528 return (mPrivateFlags & PRESSED) == PRESSED;
4529 }
4530
4531 /**
4532 * Indicates whether this view will save its state (that is,
4533 * whether its {@link #onSaveInstanceState} method will be called).
4534 *
4535 * @return Returns true if the view state saving is enabled, else false.
4536 *
4537 * @see #setSaveEnabled(boolean)
4538 * @attr ref android.R.styleable#View_saveEnabled
4539 */
4540 public boolean isSaveEnabled() {
4541 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4542 }
4543
4544 /**
4545 * Controls whether the saving of this view's state is
4546 * enabled (that is, whether its {@link #onSaveInstanceState} method
4547 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004548 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004549 * for its state to be saved. This flag can only disable the
4550 * saving of this view; any child views may still have their state saved.
4551 *
4552 * @param enabled Set to false to <em>disable</em> state saving, or true
4553 * (the default) to allow it.
4554 *
4555 * @see #isSaveEnabled()
4556 * @see #setId(int)
4557 * @see #onSaveInstanceState()
4558 * @attr ref android.R.styleable#View_saveEnabled
4559 */
4560 public void setSaveEnabled(boolean enabled) {
4561 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4562 }
4563
Jeff Brown85a31762010-09-01 17:01:00 -07004564 /**
4565 * Gets whether the framework should discard touches when the view's
4566 * window is obscured by another visible window.
4567 * Refer to the {@link View} security documentation for more details.
4568 *
4569 * @return True if touch filtering is enabled.
4570 *
4571 * @see #setFilterTouchesWhenObscured(boolean)
4572 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4573 */
4574 @ViewDebug.ExportedProperty
4575 public boolean getFilterTouchesWhenObscured() {
4576 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4577 }
4578
4579 /**
4580 * Sets whether the framework should discard touches when the view's
4581 * window is obscured by another visible window.
4582 * Refer to the {@link View} security documentation for more details.
4583 *
4584 * @param enabled True if touch filtering should be enabled.
4585 *
4586 * @see #getFilterTouchesWhenObscured
4587 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4588 */
4589 public void setFilterTouchesWhenObscured(boolean enabled) {
4590 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4591 FILTER_TOUCHES_WHEN_OBSCURED);
4592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004593
4594 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004595 * Indicates whether the entire hierarchy under this view will save its
4596 * state when a state saving traversal occurs from its parent. The default
4597 * is true; if false, these views will not be saved unless
4598 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4599 *
4600 * @return Returns true if the view state saving from parent is enabled, else false.
4601 *
4602 * @see #setSaveFromParentEnabled(boolean)
4603 */
4604 public boolean isSaveFromParentEnabled() {
4605 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4606 }
4607
4608 /**
4609 * Controls whether the entire hierarchy under this view will save its
4610 * state when a state saving traversal occurs from its parent. The default
4611 * is true; if false, these views will not be saved unless
4612 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4613 *
4614 * @param enabled Set to false to <em>disable</em> state saving, or true
4615 * (the default) to allow it.
4616 *
4617 * @see #isSaveFromParentEnabled()
4618 * @see #setId(int)
4619 * @see #onSaveInstanceState()
4620 */
4621 public void setSaveFromParentEnabled(boolean enabled) {
4622 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4623 }
4624
4625
4626 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004627 * Returns whether this View is able to take focus.
4628 *
4629 * @return True if this view can take focus, or false otherwise.
4630 * @attr ref android.R.styleable#View_focusable
4631 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004632 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004633 public final boolean isFocusable() {
4634 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4635 }
4636
4637 /**
4638 * When a view is focusable, it may not want to take focus when in touch mode.
4639 * For example, a button would like focus when the user is navigating via a D-pad
4640 * so that the user can click on it, but once the user starts touching the screen,
4641 * the button shouldn't take focus
4642 * @return Whether the view is focusable in touch mode.
4643 * @attr ref android.R.styleable#View_focusableInTouchMode
4644 */
4645 @ViewDebug.ExportedProperty
4646 public final boolean isFocusableInTouchMode() {
4647 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4648 }
4649
4650 /**
4651 * Find the nearest view in the specified direction that can take focus.
4652 * This does not actually give focus to that view.
4653 *
4654 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4655 *
4656 * @return The nearest focusable in the specified direction, or null if none
4657 * can be found.
4658 */
4659 public View focusSearch(int direction) {
4660 if (mParent != null) {
4661 return mParent.focusSearch(this, direction);
4662 } else {
4663 return null;
4664 }
4665 }
4666
4667 /**
4668 * This method is the last chance for the focused view and its ancestors to
4669 * respond to an arrow key. This is called when the focused view did not
4670 * consume the key internally, nor could the view system find a new view in
4671 * the requested direction to give focus to.
4672 *
4673 * @param focused The currently focused view.
4674 * @param direction The direction focus wants to move. One of FOCUS_UP,
4675 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4676 * @return True if the this view consumed this unhandled move.
4677 */
4678 public boolean dispatchUnhandledMove(View focused, int direction) {
4679 return false;
4680 }
4681
4682 /**
4683 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004684 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004685 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004686 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4687 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004688 * @return The user specified next view, or null if there is none.
4689 */
4690 View findUserSetNextFocus(View root, int direction) {
4691 switch (direction) {
4692 case FOCUS_LEFT:
4693 if (mNextFocusLeftId == View.NO_ID) return null;
4694 return findViewShouldExist(root, mNextFocusLeftId);
4695 case FOCUS_RIGHT:
4696 if (mNextFocusRightId == View.NO_ID) return null;
4697 return findViewShouldExist(root, mNextFocusRightId);
4698 case FOCUS_UP:
4699 if (mNextFocusUpId == View.NO_ID) return null;
4700 return findViewShouldExist(root, mNextFocusUpId);
4701 case FOCUS_DOWN:
4702 if (mNextFocusDownId == View.NO_ID) return null;
4703 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004704 case FOCUS_FORWARD:
4705 if (mNextFocusForwardId == View.NO_ID) return null;
4706 return findViewShouldExist(root, mNextFocusForwardId);
4707 case FOCUS_BACKWARD: {
4708 final int id = mID;
4709 return root.findViewByPredicate(new Predicate<View>() {
4710 @Override
4711 public boolean apply(View t) {
4712 return t.mNextFocusForwardId == id;
4713 }
4714 });
4715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004716 }
4717 return null;
4718 }
4719
4720 private static View findViewShouldExist(View root, int childViewId) {
4721 View result = root.findViewById(childViewId);
4722 if (result == null) {
4723 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4724 + "by user for id " + childViewId);
4725 }
4726 return result;
4727 }
4728
4729 /**
4730 * Find and return all focusable views that are descendants of this view,
4731 * possibly including this view if it is focusable itself.
4732 *
4733 * @param direction The direction of the focus
4734 * @return A list of focusable views
4735 */
4736 public ArrayList<View> getFocusables(int direction) {
4737 ArrayList<View> result = new ArrayList<View>(24);
4738 addFocusables(result, direction);
4739 return result;
4740 }
4741
4742 /**
4743 * Add any focusable views that are descendants of this view (possibly
4744 * including this view if it is focusable itself) to views. If we are in touch mode,
4745 * only add views that are also focusable in touch mode.
4746 *
4747 * @param views Focusable views found so far
4748 * @param direction The direction of the focus
4749 */
4750 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004751 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004753
svetoslavganov75986cf2009-05-14 22:28:01 -07004754 /**
4755 * Adds any focusable views that are descendants of this view (possibly
4756 * including this view if it is focusable itself) to views. This method
4757 * adds all focusable views regardless if we are in touch mode or
4758 * only views focusable in touch mode if we are in touch mode depending on
4759 * the focusable mode paramater.
4760 *
4761 * @param views Focusable views found so far or null if all we are interested is
4762 * the number of focusables.
4763 * @param direction The direction of the focus.
4764 * @param focusableMode The type of focusables to be added.
4765 *
4766 * @see #FOCUSABLES_ALL
4767 * @see #FOCUSABLES_TOUCH_MODE
4768 */
4769 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4770 if (!isFocusable()) {
4771 return;
4772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004773
svetoslavganov75986cf2009-05-14 22:28:01 -07004774 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4775 isInTouchMode() && !isFocusableInTouchMode()) {
4776 return;
4777 }
4778
4779 if (views != null) {
4780 views.add(this);
4781 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004782 }
4783
4784 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004785 * Finds the Views that contain given text. The containment is case insensitive.
4786 * As View's text is considered any text content that View renders.
4787 *
4788 * @param outViews The output list of matching Views.
4789 * @param text The text to match against.
4790 */
4791 public void findViewsWithText(ArrayList<View> outViews, CharSequence text) {
4792 }
4793
4794 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004795 * Find and return all touchable views that are descendants of this view,
4796 * possibly including this view if it is touchable itself.
4797 *
4798 * @return A list of touchable views
4799 */
4800 public ArrayList<View> getTouchables() {
4801 ArrayList<View> result = new ArrayList<View>();
4802 addTouchables(result);
4803 return result;
4804 }
4805
4806 /**
4807 * Add any touchable views that are descendants of this view (possibly
4808 * including this view if it is touchable itself) to views.
4809 *
4810 * @param views Touchable views found so far
4811 */
4812 public void addTouchables(ArrayList<View> views) {
4813 final int viewFlags = mViewFlags;
4814
4815 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4816 && (viewFlags & ENABLED_MASK) == ENABLED) {
4817 views.add(this);
4818 }
4819 }
4820
4821 /**
4822 * Call this to try to give focus to a specific view or to one of its
4823 * descendants.
4824 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004825 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4826 * false), or if it is focusable and it is not focusable in touch mode
4827 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004828 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004829 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004830 * have focus, and you want your parent to look for the next one.
4831 *
4832 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4833 * {@link #FOCUS_DOWN} and <code>null</code>.
4834 *
4835 * @return Whether this view or one of its descendants actually took focus.
4836 */
4837 public final boolean requestFocus() {
4838 return requestFocus(View.FOCUS_DOWN);
4839 }
4840
4841
4842 /**
4843 * Call this to try to give focus to a specific view or to one of its
4844 * descendants and give it a hint about what direction focus is heading.
4845 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004846 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4847 * false), or if it is focusable and it is not focusable in touch mode
4848 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004849 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004850 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004851 * have focus, and you want your parent to look for the next one.
4852 *
4853 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4854 * <code>null</code> set for the previously focused rectangle.
4855 *
4856 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4857 * @return Whether this view or one of its descendants actually took focus.
4858 */
4859 public final boolean requestFocus(int direction) {
4860 return requestFocus(direction, null);
4861 }
4862
4863 /**
4864 * Call this to try to give focus to a specific view or to one of its descendants
4865 * and give it hints about the direction and a specific rectangle that the focus
4866 * is coming from. The rectangle can help give larger views a finer grained hint
4867 * about where focus is coming from, and therefore, where to show selection, or
4868 * forward focus change internally.
4869 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004870 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4871 * false), or if it is focusable and it is not focusable in touch mode
4872 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004873 *
4874 * A View will not take focus if it is not visible.
4875 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004876 * A View will not take focus if one of its parents has
4877 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
4878 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004879 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004880 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004881 * have focus, and you want your parent to look for the next one.
4882 *
4883 * You may wish to override this method if your custom {@link View} has an internal
4884 * {@link View} that it wishes to forward the request to.
4885 *
4886 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4887 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
4888 * to give a finer grained hint about where focus is coming from. May be null
4889 * if there is no hint.
4890 * @return Whether this view or one of its descendants actually took focus.
4891 */
4892 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
4893 // need to be focusable
4894 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
4895 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4896 return false;
4897 }
4898
4899 // need to be focusable in touch mode if in touch mode
4900 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004901 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
4902 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004903 }
4904
4905 // need to not have any parents blocking us
4906 if (hasAncestorThatBlocksDescendantFocus()) {
4907 return false;
4908 }
4909
4910 handleFocusGainInternal(direction, previouslyFocusedRect);
4911 return true;
4912 }
4913
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004914 /** Gets the ViewAncestor, or null if not attached. */
4915 /*package*/ ViewAncestor getViewAncestor() {
Christopher Tate2c095f32010-10-04 14:13:40 -07004916 View root = getRootView();
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004917 return root != null ? (ViewAncestor)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07004918 }
4919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004920 /**
4921 * Call this to try to give focus to a specific view or to one of its descendants. This is a
4922 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
4923 * touch mode to request focus when they are touched.
4924 *
4925 * @return Whether this view or one of its descendants actually took focus.
4926 *
4927 * @see #isInTouchMode()
4928 *
4929 */
4930 public final boolean requestFocusFromTouch() {
4931 // Leave touch mode if we need to
4932 if (isInTouchMode()) {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004933 ViewAncestor viewRoot = getViewAncestor();
Christopher Tate2c095f32010-10-04 14:13:40 -07004934 if (viewRoot != null) {
4935 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004936 }
4937 }
4938 return requestFocus(View.FOCUS_DOWN);
4939 }
4940
4941 /**
4942 * @return Whether any ancestor of this view blocks descendant focus.
4943 */
4944 private boolean hasAncestorThatBlocksDescendantFocus() {
4945 ViewParent ancestor = mParent;
4946 while (ancestor instanceof ViewGroup) {
4947 final ViewGroup vgAncestor = (ViewGroup) ancestor;
4948 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
4949 return true;
4950 } else {
4951 ancestor = vgAncestor.getParent();
4952 }
4953 }
4954 return false;
4955 }
4956
4957 /**
Romain Guya440b002010-02-24 15:57:54 -08004958 * @hide
4959 */
4960 public void dispatchStartTemporaryDetach() {
4961 onStartTemporaryDetach();
4962 }
4963
4964 /**
4965 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004966 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
4967 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08004968 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004969 */
4970 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08004971 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08004972 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08004973 }
4974
4975 /**
4976 * @hide
4977 */
4978 public void dispatchFinishTemporaryDetach() {
4979 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004980 }
Romain Guy8506ab42009-06-11 17:35:47 -07004981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004982 /**
4983 * Called after {@link #onStartTemporaryDetach} when the container is done
4984 * changing the view.
4985 */
4986 public void onFinishTemporaryDetach() {
4987 }
Romain Guy8506ab42009-06-11 17:35:47 -07004988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004989 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004990 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
4991 * for this view's window. Returns null if the view is not currently attached
4992 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07004993 * just use the standard high-level event callbacks like
4994 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004995 */
4996 public KeyEvent.DispatcherState getKeyDispatcherState() {
4997 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
4998 }
Joe Malin32736f02011-01-19 16:14:20 -08004999
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005000 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005001 * Dispatch a key event before it is processed by any input method
5002 * associated with the view hierarchy. This can be used to intercept
5003 * key events in special situations before the IME consumes them; a
5004 * typical example would be handling the BACK key to update the application's
5005 * UI instead of allowing the IME to see it and close itself.
5006 *
5007 * @param event The key event to be dispatched.
5008 * @return True if the event was handled, false otherwise.
5009 */
5010 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5011 return onKeyPreIme(event.getKeyCode(), event);
5012 }
5013
5014 /**
5015 * Dispatch a key event to the next view on the focus path. This path runs
5016 * from the top of the view tree down to the currently focused view. If this
5017 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5018 * the next node down the focus path. This method also fires any key
5019 * listeners.
5020 *
5021 * @param event The key event to be dispatched.
5022 * @return True if the event was handled, false otherwise.
5023 */
5024 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005025 if (mInputEventConsistencyVerifier != null) {
5026 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5027 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005028
Jeff Brown21bc5c92011-02-28 18:27:14 -08005029 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005030 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005031 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5032 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
5033 return true;
5034 }
5035
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005036 if (event.dispatch(this, mAttachInfo != null
5037 ? mAttachInfo.mKeyDispatchState : null, this)) {
5038 return true;
5039 }
5040
5041 if (mInputEventConsistencyVerifier != null) {
5042 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5043 }
5044 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005045 }
5046
5047 /**
5048 * Dispatches a key shortcut event.
5049 *
5050 * @param event The key event to be dispatched.
5051 * @return True if the event was handled by the view, false otherwise.
5052 */
5053 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5054 return onKeyShortcut(event.getKeyCode(), event);
5055 }
5056
5057 /**
5058 * Pass the touch screen motion event down to the target view, or this
5059 * view if it is the target.
5060 *
5061 * @param event The motion event to be dispatched.
5062 * @return True if the event was handled by the view, false otherwise.
5063 */
5064 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005065 if (mInputEventConsistencyVerifier != null) {
5066 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5067 }
5068
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005069 if (onFilterTouchEventForSecurity(event)) {
5070 //noinspection SimplifiableIfStatement
5071 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
5072 mOnTouchListener.onTouch(this, event)) {
5073 return true;
5074 }
5075
5076 if (onTouchEvent(event)) {
5077 return true;
5078 }
Jeff Brown85a31762010-09-01 17:01:00 -07005079 }
5080
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005081 if (mInputEventConsistencyVerifier != null) {
5082 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005083 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005084 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005085 }
5086
5087 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005088 * Filter the touch event to apply security policies.
5089 *
5090 * @param event The motion event to be filtered.
5091 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005092 *
Jeff Brown85a31762010-09-01 17:01:00 -07005093 * @see #getFilterTouchesWhenObscured
5094 */
5095 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005096 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005097 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5098 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5099 // Window is obscured, drop this touch.
5100 return false;
5101 }
5102 return true;
5103 }
5104
5105 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005106 * Pass a trackball motion event down to the focused view.
5107 *
5108 * @param event The motion event to be dispatched.
5109 * @return True if the event was handled by the view, false otherwise.
5110 */
5111 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005112 if (mInputEventConsistencyVerifier != null) {
5113 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5114 }
5115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005116 //Log.i("view", "view=" + this + ", " + event.toString());
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005117 if (onTrackballEvent(event)) {
5118 return true;
5119 }
5120
5121 if (mInputEventConsistencyVerifier != null) {
5122 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5123 }
5124 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005125 }
5126
5127 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005128 * Dispatch a generic motion event.
5129 * <p>
5130 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5131 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005132 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005133 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005134 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005135 *
5136 * @param event The motion event to be dispatched.
5137 * @return True if the event was handled by the view, false otherwise.
5138 */
5139 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005140 if (mInputEventConsistencyVerifier != null) {
5141 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5142 }
5143
Jeff Browna032cc02011-03-07 16:56:21 -08005144 final int source = event.getSource();
5145 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5146 final int action = event.getAction();
5147 if (action == MotionEvent.ACTION_HOVER_ENTER
5148 || action == MotionEvent.ACTION_HOVER_MOVE
5149 || action == MotionEvent.ACTION_HOVER_EXIT) {
5150 if (dispatchHoverEvent(event)) {
5151 return true;
5152 }
5153 } else if (dispatchGenericPointerEvent(event)) {
5154 return true;
5155 }
5156 } else if (dispatchGenericFocusedEvent(event)) {
5157 return true;
5158 }
5159
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005160 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08005161 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5162 && mOnGenericMotionListener.onGenericMotion(this, event)) {
5163 return true;
5164 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005165
5166 if (onGenericMotionEvent(event)) {
5167 return true;
5168 }
5169
5170 if (mInputEventConsistencyVerifier != null) {
5171 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5172 }
5173 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005174 }
5175
5176 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005177 * Dispatch a hover event.
5178 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005179 * Do not call this method directly.
5180 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005181 * </p>
5182 *
5183 * @param event The motion event to be dispatched.
5184 * @return True if the event was handled by the view, false otherwise.
5185 * @hide
5186 */
5187 protected boolean dispatchHoverEvent(MotionEvent event) {
5188 return onHoverEvent(event);
5189 }
5190
5191 /**
5192 * Dispatch a generic motion event to the view under the first pointer.
5193 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005194 * Do not call this method directly.
5195 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005196 * </p>
5197 *
5198 * @param event The motion event to be dispatched.
5199 * @return True if the event was handled by the view, false otherwise.
5200 * @hide
5201 */
5202 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5203 return false;
5204 }
5205
5206 /**
5207 * Dispatch a generic motion event to the currently focused view.
5208 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005209 * Do not call this method directly.
5210 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005211 * </p>
5212 *
5213 * @param event The motion event to be dispatched.
5214 * @return True if the event was handled by the view, false otherwise.
5215 * @hide
5216 */
5217 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5218 return false;
5219 }
5220
5221 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005222 * Dispatch a pointer event.
5223 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005224 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5225 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5226 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005227 * and should not be expected to handle other pointing device features.
5228 * </p>
5229 *
5230 * @param event The motion event to be dispatched.
5231 * @return True if the event was handled by the view, false otherwise.
5232 * @hide
5233 */
5234 public final boolean dispatchPointerEvent(MotionEvent event) {
5235 if (event.isTouchEvent()) {
5236 return dispatchTouchEvent(event);
5237 } else {
5238 return dispatchGenericMotionEvent(event);
5239 }
5240 }
5241
5242 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005243 * Called when the window containing this view gains or loses window focus.
5244 * ViewGroups should override to route to their children.
5245 *
5246 * @param hasFocus True if the window containing this view now has focus,
5247 * false otherwise.
5248 */
5249 public void dispatchWindowFocusChanged(boolean hasFocus) {
5250 onWindowFocusChanged(hasFocus);
5251 }
5252
5253 /**
5254 * Called when the window containing this view gains or loses focus. Note
5255 * that this is separate from view focus: to receive key events, both
5256 * your view and its window must have focus. If a window is displayed
5257 * on top of yours that takes input focus, then your own window will lose
5258 * focus but the view focus will remain unchanged.
5259 *
5260 * @param hasWindowFocus True if the window containing this view now has
5261 * focus, false otherwise.
5262 */
5263 public void onWindowFocusChanged(boolean hasWindowFocus) {
5264 InputMethodManager imm = InputMethodManager.peekInstance();
5265 if (!hasWindowFocus) {
5266 if (isPressed()) {
5267 setPressed(false);
5268 }
5269 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5270 imm.focusOut(this);
5271 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005272 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005273 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005274 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005275 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5276 imm.focusIn(this);
5277 }
5278 refreshDrawableState();
5279 }
5280
5281 /**
5282 * Returns true if this view is in a window that currently has window focus.
5283 * Note that this is not the same as the view itself having focus.
5284 *
5285 * @return True if this view is in a window that currently has window focus.
5286 */
5287 public boolean hasWindowFocus() {
5288 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5289 }
5290
5291 /**
Adam Powell326d8082009-12-09 15:10:07 -08005292 * Dispatch a view visibility change down the view hierarchy.
5293 * ViewGroups should override to route to their children.
5294 * @param changedView The view whose visibility changed. Could be 'this' or
5295 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005296 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5297 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005298 */
5299 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5300 onVisibilityChanged(changedView, visibility);
5301 }
5302
5303 /**
5304 * Called when the visibility of the view or an ancestor of the view is changed.
5305 * @param changedView The view whose visibility changed. Could be 'this' or
5306 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005307 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5308 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005309 */
5310 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005311 if (visibility == VISIBLE) {
5312 if (mAttachInfo != null) {
5313 initialAwakenScrollBars();
5314 } else {
5315 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5316 }
5317 }
Adam Powell326d8082009-12-09 15:10:07 -08005318 }
5319
5320 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005321 * Dispatch a hint about whether this view is displayed. For instance, when
5322 * a View moves out of the screen, it might receives a display hint indicating
5323 * the view is not displayed. Applications should not <em>rely</em> on this hint
5324 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005325 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005326 * @param hint A hint about whether or not this view is displayed:
5327 * {@link #VISIBLE} or {@link #INVISIBLE}.
5328 */
5329 public void dispatchDisplayHint(int hint) {
5330 onDisplayHint(hint);
5331 }
5332
5333 /**
5334 * Gives this view a hint about whether is displayed or not. For instance, when
5335 * a View moves out of the screen, it might receives a display hint indicating
5336 * the view is not displayed. Applications should not <em>rely</em> on this hint
5337 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005338 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005339 * @param hint A hint about whether or not this view is displayed:
5340 * {@link #VISIBLE} or {@link #INVISIBLE}.
5341 */
5342 protected void onDisplayHint(int hint) {
5343 }
5344
5345 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005346 * Dispatch a window visibility change down the view hierarchy.
5347 * ViewGroups should override to route to their children.
5348 *
5349 * @param visibility The new visibility of the window.
5350 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005351 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005352 */
5353 public void dispatchWindowVisibilityChanged(int visibility) {
5354 onWindowVisibilityChanged(visibility);
5355 }
5356
5357 /**
5358 * Called when the window containing has change its visibility
5359 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5360 * that this tells you whether or not your window is being made visible
5361 * to the window manager; this does <em>not</em> tell you whether or not
5362 * your window is obscured by other windows on the screen, even if it
5363 * is itself visible.
5364 *
5365 * @param visibility The new visibility of the window.
5366 */
5367 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005368 if (visibility == VISIBLE) {
5369 initialAwakenScrollBars();
5370 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005371 }
5372
5373 /**
5374 * Returns the current visibility of the window this view is attached to
5375 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5376 *
5377 * @return Returns the current visibility of the view's window.
5378 */
5379 public int getWindowVisibility() {
5380 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5381 }
5382
5383 /**
5384 * Retrieve the overall visible display size in which the window this view is
5385 * attached to has been positioned in. This takes into account screen
5386 * decorations above the window, for both cases where the window itself
5387 * is being position inside of them or the window is being placed under
5388 * then and covered insets are used for the window to position its content
5389 * inside. In effect, this tells you the available area where content can
5390 * be placed and remain visible to users.
5391 *
5392 * <p>This function requires an IPC back to the window manager to retrieve
5393 * the requested information, so should not be used in performance critical
5394 * code like drawing.
5395 *
5396 * @param outRect Filled in with the visible display frame. If the view
5397 * is not attached to a window, this is simply the raw display size.
5398 */
5399 public void getWindowVisibleDisplayFrame(Rect outRect) {
5400 if (mAttachInfo != null) {
5401 try {
5402 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5403 } catch (RemoteException e) {
5404 return;
5405 }
5406 // XXX This is really broken, and probably all needs to be done
5407 // in the window manager, and we need to know more about whether
5408 // we want the area behind or in front of the IME.
5409 final Rect insets = mAttachInfo.mVisibleInsets;
5410 outRect.left += insets.left;
5411 outRect.top += insets.top;
5412 outRect.right -= insets.right;
5413 outRect.bottom -= insets.bottom;
5414 return;
5415 }
5416 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005417 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005418 }
5419
5420 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005421 * Dispatch a notification about a resource configuration change down
5422 * the view hierarchy.
5423 * ViewGroups should override to route to their children.
5424 *
5425 * @param newConfig The new resource configuration.
5426 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005427 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005428 */
5429 public void dispatchConfigurationChanged(Configuration newConfig) {
5430 onConfigurationChanged(newConfig);
5431 }
5432
5433 /**
5434 * Called when the current configuration of the resources being used
5435 * by the application have changed. You can use this to decide when
5436 * to reload resources that can changed based on orientation and other
5437 * configuration characterstics. You only need to use this if you are
5438 * not relying on the normal {@link android.app.Activity} mechanism of
5439 * recreating the activity instance upon a configuration change.
5440 *
5441 * @param newConfig The new resource configuration.
5442 */
5443 protected void onConfigurationChanged(Configuration newConfig) {
5444 }
5445
5446 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005447 * Private function to aggregate all per-view attributes in to the view
5448 * root.
5449 */
5450 void dispatchCollectViewAttributes(int visibility) {
5451 performCollectViewAttributes(visibility);
5452 }
5453
5454 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005455 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005456 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5457 mAttachInfo.mKeepScreenOn = true;
5458 }
5459 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5460 if (mOnSystemUiVisibilityChangeListener != null) {
5461 mAttachInfo.mHasSystemUiListeners = true;
5462 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005463 }
5464 }
5465
5466 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005467 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005468 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005469 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5470 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005471 ai.mRecomputeGlobalAttributes = true;
5472 }
5473 }
5474 }
5475
5476 /**
5477 * Returns whether the device is currently in touch mode. Touch mode is entered
5478 * once the user begins interacting with the device by touch, and affects various
5479 * things like whether focus is always visible to the user.
5480 *
5481 * @return Whether the device is in touch mode.
5482 */
5483 @ViewDebug.ExportedProperty
5484 public boolean isInTouchMode() {
5485 if (mAttachInfo != null) {
5486 return mAttachInfo.mInTouchMode;
5487 } else {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005488 return ViewAncestor.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005489 }
5490 }
5491
5492 /**
5493 * Returns the context the view is running in, through which it can
5494 * access the current theme, resources, etc.
5495 *
5496 * @return The view's Context.
5497 */
5498 @ViewDebug.CapturedViewProperty
5499 public final Context getContext() {
5500 return mContext;
5501 }
5502
5503 /**
5504 * Handle a key event before it is processed by any input method
5505 * associated with the view hierarchy. This can be used to intercept
5506 * key events in special situations before the IME consumes them; a
5507 * typical example would be handling the BACK key to update the application's
5508 * UI instead of allowing the IME to see it and close itself.
5509 *
5510 * @param keyCode The value in event.getKeyCode().
5511 * @param event Description of the key event.
5512 * @return If you handled the event, return true. If you want to allow the
5513 * event to be handled by the next receiver, return false.
5514 */
5515 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5516 return false;
5517 }
5518
5519 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005520 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5521 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005522 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5523 * is released, if the view is enabled and clickable.
5524 *
5525 * @param keyCode A key code that represents the button pressed, from
5526 * {@link android.view.KeyEvent}.
5527 * @param event The KeyEvent object that defines the button action.
5528 */
5529 public boolean onKeyDown(int keyCode, KeyEvent event) {
5530 boolean result = false;
5531
5532 switch (keyCode) {
5533 case KeyEvent.KEYCODE_DPAD_CENTER:
5534 case KeyEvent.KEYCODE_ENTER: {
5535 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5536 return true;
5537 }
5538 // Long clickable items don't necessarily have to be clickable
5539 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5540 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5541 (event.getRepeatCount() == 0)) {
5542 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005543 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005544 return true;
5545 }
5546 break;
5547 }
5548 }
5549 return result;
5550 }
5551
5552 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005553 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5554 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5555 * the event).
5556 */
5557 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5558 return false;
5559 }
5560
5561 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005562 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5563 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005564 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5565 * {@link KeyEvent#KEYCODE_ENTER} is released.
5566 *
5567 * @param keyCode A key code that represents the button pressed, from
5568 * {@link android.view.KeyEvent}.
5569 * @param event The KeyEvent object that defines the button action.
5570 */
5571 public boolean onKeyUp(int keyCode, KeyEvent event) {
5572 boolean result = false;
5573
5574 switch (keyCode) {
5575 case KeyEvent.KEYCODE_DPAD_CENTER:
5576 case KeyEvent.KEYCODE_ENTER: {
5577 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5578 return true;
5579 }
5580 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5581 setPressed(false);
5582
5583 if (!mHasPerformedLongPress) {
5584 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005585 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005586
5587 result = performClick();
5588 }
5589 }
5590 break;
5591 }
5592 }
5593 return result;
5594 }
5595
5596 /**
5597 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5598 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5599 * the event).
5600 *
5601 * @param keyCode A key code that represents the button pressed, from
5602 * {@link android.view.KeyEvent}.
5603 * @param repeatCount The number of times the action was made.
5604 * @param event The KeyEvent object that defines the button action.
5605 */
5606 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5607 return false;
5608 }
5609
5610 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005611 * Called on the focused view when a key shortcut event is not handled.
5612 * Override this method to implement local key shortcuts for the View.
5613 * Key shortcuts can also be implemented by setting the
5614 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005615 *
5616 * @param keyCode The value in event.getKeyCode().
5617 * @param event Description of the key event.
5618 * @return If you handled the event, return true. If you want to allow the
5619 * event to be handled by the next receiver, return false.
5620 */
5621 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5622 return false;
5623 }
5624
5625 /**
5626 * Check whether the called view is a text editor, in which case it
5627 * would make sense to automatically display a soft input window for
5628 * it. Subclasses should override this if they implement
5629 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005630 * a call on that method would return a non-null InputConnection, and
5631 * they are really a first-class editor that the user would normally
5632 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005633 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005634 * <p>The default implementation always returns false. This does
5635 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5636 * will not be called or the user can not otherwise perform edits on your
5637 * view; it is just a hint to the system that this is not the primary
5638 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005639 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005640 * @return Returns true if this view is a text editor, else false.
5641 */
5642 public boolean onCheckIsTextEditor() {
5643 return false;
5644 }
Romain Guy8506ab42009-06-11 17:35:47 -07005645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005646 /**
5647 * Create a new InputConnection for an InputMethod to interact
5648 * with the view. The default implementation returns null, since it doesn't
5649 * support input methods. You can override this to implement such support.
5650 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005651 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005652 * <p>When implementing this, you probably also want to implement
5653 * {@link #onCheckIsTextEditor()} to indicate you will return a
5654 * non-null InputConnection.
5655 *
5656 * @param outAttrs Fill in with attribute information about the connection.
5657 */
5658 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5659 return null;
5660 }
5661
5662 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005663 * Called by the {@link android.view.inputmethod.InputMethodManager}
5664 * when a view who is not the current
5665 * input connection target is trying to make a call on the manager. The
5666 * default implementation returns false; you can override this to return
5667 * true for certain views if you are performing InputConnection proxying
5668 * to them.
5669 * @param view The View that is making the InputMethodManager call.
5670 * @return Return true to allow the call, false to reject.
5671 */
5672 public boolean checkInputConnectionProxy(View view) {
5673 return false;
5674 }
Romain Guy8506ab42009-06-11 17:35:47 -07005675
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005676 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005677 * Show the context menu for this view. It is not safe to hold on to the
5678 * menu after returning from this method.
5679 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005680 * You should normally not overload this method. Overload
5681 * {@link #onCreateContextMenu(ContextMenu)} or define an
5682 * {@link OnCreateContextMenuListener} to add items to the context menu.
5683 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005684 * @param menu The context menu to populate
5685 */
5686 public void createContextMenu(ContextMenu menu) {
5687 ContextMenuInfo menuInfo = getContextMenuInfo();
5688
5689 // Sets the current menu info so all items added to menu will have
5690 // my extra info set.
5691 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5692
5693 onCreateContextMenu(menu);
5694 if (mOnCreateContextMenuListener != null) {
5695 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5696 }
5697
5698 // Clear the extra information so subsequent items that aren't mine don't
5699 // have my extra info.
5700 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5701
5702 if (mParent != null) {
5703 mParent.createContextMenu(menu);
5704 }
5705 }
5706
5707 /**
5708 * Views should implement this if they have extra information to associate
5709 * with the context menu. The return result is supplied as a parameter to
5710 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5711 * callback.
5712 *
5713 * @return Extra information about the item for which the context menu
5714 * should be shown. This information will vary across different
5715 * subclasses of View.
5716 */
5717 protected ContextMenuInfo getContextMenuInfo() {
5718 return null;
5719 }
5720
5721 /**
5722 * Views should implement this if the view itself is going to add items to
5723 * the context menu.
5724 *
5725 * @param menu the context menu to populate
5726 */
5727 protected void onCreateContextMenu(ContextMenu menu) {
5728 }
5729
5730 /**
5731 * Implement this method to handle trackball motion events. The
5732 * <em>relative</em> movement of the trackball since the last event
5733 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5734 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5735 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5736 * they will often be fractional values, representing the more fine-grained
5737 * movement information available from a trackball).
5738 *
5739 * @param event The motion event.
5740 * @return True if the event was handled, false otherwise.
5741 */
5742 public boolean onTrackballEvent(MotionEvent event) {
5743 return false;
5744 }
5745
5746 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005747 * Implement this method to handle generic motion events.
5748 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005749 * Generic motion events describe joystick movements, mouse hovers, track pad
5750 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005751 * {@link MotionEvent#getSource() source} of the motion event specifies
5752 * the class of input that was received. Implementations of this method
5753 * must examine the bits in the source before processing the event.
5754 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005755 * </p><p>
5756 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5757 * are delivered to the view under the pointer. All other generic motion events are
5758 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005759 * </p>
5760 * <code>
5761 * public boolean onGenericMotionEvent(MotionEvent event) {
5762 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005763 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5764 * // process the joystick movement...
5765 * return true;
5766 * }
5767 * }
5768 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5769 * switch (event.getAction()) {
5770 * case MotionEvent.ACTION_HOVER_MOVE:
5771 * // process the mouse hover movement...
5772 * return true;
5773 * case MotionEvent.ACTION_SCROLL:
5774 * // process the scroll wheel movement...
5775 * return true;
5776 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005777 * }
5778 * return super.onGenericMotionEvent(event);
5779 * }
5780 * </code>
5781 *
5782 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08005783 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08005784 */
5785 public boolean onGenericMotionEvent(MotionEvent event) {
5786 return false;
5787 }
5788
5789 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005790 * Implement this method to handle hover events.
5791 * <p>
5792 * Hover events are pointer events with action {@link MotionEvent#ACTION_HOVER_ENTER},
5793 * {@link MotionEvent#ACTION_HOVER_MOVE}, or {@link MotionEvent#ACTION_HOVER_EXIT}.
5794 * </p><p>
5795 * The view receives hover enter as the pointer enters the bounds of the view and hover
5796 * exit as the pointer exits the bound of the view or just before the pointer goes down
Romain Guy5c22a8c2011-05-13 11:48:45 -07005797 * (which implies that {@link #onTouchEvent(MotionEvent)} will be called soon).
Jeff Browna032cc02011-03-07 16:56:21 -08005798 * </p><p>
5799 * If the view would like to handle the hover event itself and prevent its children
5800 * from receiving hover, it should return true from this method. If this method returns
5801 * true and a child has already received a hover enter event, the child will
5802 * automatically receive a hover exit event.
5803 * </p><p>
5804 * The default implementation sets the hovered state of the view if the view is
5805 * clickable.
5806 * </p>
5807 *
5808 * @param event The motion event that describes the hover.
5809 * @return True if this view handled the hover event and does not want its children
5810 * to receive the hover event.
5811 */
5812 public boolean onHoverEvent(MotionEvent event) {
Jeff Browna032cc02011-03-07 16:56:21 -08005813 switch (event.getAction()) {
5814 case MotionEvent.ACTION_HOVER_ENTER:
5815 setHovered(true);
5816 break;
5817
5818 case MotionEvent.ACTION_HOVER_EXIT:
5819 setHovered(false);
5820 break;
5821 }
5822
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005823 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08005824 }
5825
5826 /**
5827 * Returns true if the view is currently hovered.
5828 *
5829 * @return True if the view is currently hovered.
5830 */
5831 public boolean isHovered() {
5832 return (mPrivateFlags & HOVERED) != 0;
5833 }
5834
5835 /**
5836 * Sets whether the view is currently hovered.
5837 *
5838 * @param hovered True if the view is hovered.
5839 */
5840 public void setHovered(boolean hovered) {
5841 if (hovered) {
5842 if ((mPrivateFlags & HOVERED) == 0) {
5843 mPrivateFlags |= HOVERED;
5844 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005845 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Jeff Browna032cc02011-03-07 16:56:21 -08005846 }
5847 } else {
5848 if ((mPrivateFlags & HOVERED) != 0) {
5849 mPrivateFlags &= ~HOVERED;
5850 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005851 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Jeff Browna032cc02011-03-07 16:56:21 -08005852 }
5853 }
5854 }
5855
5856 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005857 * Implement this method to handle touch screen motion events.
5858 *
5859 * @param event The motion event.
5860 * @return True if the event was handled, false otherwise.
5861 */
5862 public boolean onTouchEvent(MotionEvent event) {
5863 final int viewFlags = mViewFlags;
5864
5865 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07005866 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
5867 mPrivateFlags &= ~PRESSED;
5868 refreshDrawableState();
5869 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005870 // A disabled view that is clickable still consumes the touch
5871 // events, it just doesn't respond to them.
5872 return (((viewFlags & CLICKABLE) == CLICKABLE ||
5873 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
5874 }
5875
5876 if (mTouchDelegate != null) {
5877 if (mTouchDelegate.onTouchEvent(event)) {
5878 return true;
5879 }
5880 }
5881
5882 if (((viewFlags & CLICKABLE) == CLICKABLE ||
5883 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
5884 switch (event.getAction()) {
5885 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08005886 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
5887 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005888 // take focus if we don't have it already and we should in
5889 // touch mode.
5890 boolean focusTaken = false;
5891 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
5892 focusTaken = requestFocus();
5893 }
5894
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08005895 if (prepressed) {
5896 // The button is being released before we actually
5897 // showed it as pressed. Make it show the pressed
5898 // state now (before scheduling the click) to ensure
5899 // the user sees it.
5900 mPrivateFlags |= PRESSED;
5901 refreshDrawableState();
5902 }
Joe Malin32736f02011-01-19 16:14:20 -08005903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005904 if (!mHasPerformedLongPress) {
5905 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005906 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005907
5908 // Only perform take click actions if we were in the pressed state
5909 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08005910 // Use a Runnable and post this rather than calling
5911 // performClick directly. This lets other visual state
5912 // of the view update before click actions start.
5913 if (mPerformClick == null) {
5914 mPerformClick = new PerformClick();
5915 }
5916 if (!post(mPerformClick)) {
5917 performClick();
5918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005919 }
5920 }
5921
5922 if (mUnsetPressedState == null) {
5923 mUnsetPressedState = new UnsetPressedState();
5924 }
5925
Adam Powelle14579b2009-12-16 18:39:52 -08005926 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08005927 postDelayed(mUnsetPressedState,
5928 ViewConfiguration.getPressedStateDuration());
5929 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005930 // If the post failed, unpress right now
5931 mUnsetPressedState.run();
5932 }
Adam Powelle14579b2009-12-16 18:39:52 -08005933 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005934 }
5935 break;
5936
5937 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08005938 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005939
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005940 if (performButtonActionOnTouchDown(event)) {
5941 break;
5942 }
5943
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005944 // Walk up the hierarchy to determine if we're inside a scrolling container.
5945 boolean isInScrollingContainer = false;
5946 ViewParent p = getParent();
5947 while (p != null && p instanceof ViewGroup) {
5948 if (((ViewGroup) p).shouldDelayChildPressedState()) {
5949 isInScrollingContainer = true;
5950 break;
5951 }
5952 p = p.getParent();
5953 }
5954
5955 // For views inside a scrolling container, delay the pressed feedback for
5956 // a short period in case this is a scroll.
5957 if (isInScrollingContainer) {
5958 mPrivateFlags |= PREPRESSED;
5959 if (mPendingCheckForTap == null) {
5960 mPendingCheckForTap = new CheckForTap();
5961 }
5962 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
5963 } else {
5964 // Not inside a scrolling container, so show the feedback right away
5965 mPrivateFlags |= PRESSED;
5966 refreshDrawableState();
5967 checkForLongClick(0);
5968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005969 break;
5970
5971 case MotionEvent.ACTION_CANCEL:
5972 mPrivateFlags &= ~PRESSED;
5973 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08005974 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005975 break;
5976
5977 case MotionEvent.ACTION_MOVE:
5978 final int x = (int) event.getX();
5979 final int y = (int) event.getY();
5980
5981 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07005982 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005983 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08005984 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005985 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08005986 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05005987 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005988
5989 // Need to switch from pressed to not pressed
5990 mPrivateFlags &= ~PRESSED;
5991 refreshDrawableState();
5992 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005993 }
5994 break;
5995 }
5996 return true;
5997 }
5998
5999 return false;
6000 }
6001
6002 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006003 * Remove the longpress detection timer.
6004 */
6005 private void removeLongPressCallback() {
6006 if (mPendingCheckForLongPress != null) {
6007 removeCallbacks(mPendingCheckForLongPress);
6008 }
6009 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006010
6011 /**
6012 * Remove the pending click action
6013 */
6014 private void removePerformClickCallback() {
6015 if (mPerformClick != null) {
6016 removeCallbacks(mPerformClick);
6017 }
6018 }
6019
Adam Powelle14579b2009-12-16 18:39:52 -08006020 /**
Romain Guya440b002010-02-24 15:57:54 -08006021 * Remove the prepress detection timer.
6022 */
6023 private void removeUnsetPressCallback() {
6024 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6025 setPressed(false);
6026 removeCallbacks(mUnsetPressedState);
6027 }
6028 }
6029
6030 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006031 * Remove the tap detection timer.
6032 */
6033 private void removeTapCallback() {
6034 if (mPendingCheckForTap != null) {
6035 mPrivateFlags &= ~PREPRESSED;
6036 removeCallbacks(mPendingCheckForTap);
6037 }
6038 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006039
6040 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006041 * Cancels a pending long press. Your subclass can use this if you
6042 * want the context menu to come up if the user presses and holds
6043 * at the same place, but you don't want it to come up if they press
6044 * and then move around enough to cause scrolling.
6045 */
6046 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006047 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006048
6049 /*
6050 * The prepressed state handled by the tap callback is a display
6051 * construct, but the tap callback will post a long press callback
6052 * less its own timeout. Remove it here.
6053 */
6054 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006055 }
6056
6057 /**
6058 * Sets the TouchDelegate for this View.
6059 */
6060 public void setTouchDelegate(TouchDelegate delegate) {
6061 mTouchDelegate = delegate;
6062 }
6063
6064 /**
6065 * Gets the TouchDelegate for this View.
6066 */
6067 public TouchDelegate getTouchDelegate() {
6068 return mTouchDelegate;
6069 }
6070
6071 /**
6072 * Set flags controlling behavior of this view.
6073 *
6074 * @param flags Constant indicating the value which should be set
6075 * @param mask Constant indicating the bit range that should be changed
6076 */
6077 void setFlags(int flags, int mask) {
6078 int old = mViewFlags;
6079 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6080
6081 int changed = mViewFlags ^ old;
6082 if (changed == 0) {
6083 return;
6084 }
6085 int privateFlags = mPrivateFlags;
6086
6087 /* Check if the FOCUSABLE bit has changed */
6088 if (((changed & FOCUSABLE_MASK) != 0) &&
6089 ((privateFlags & HAS_BOUNDS) !=0)) {
6090 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6091 && ((privateFlags & FOCUSED) != 0)) {
6092 /* Give up focus if we are no longer focusable */
6093 clearFocus();
6094 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6095 && ((privateFlags & FOCUSED) == 0)) {
6096 /*
6097 * Tell the view system that we are now available to take focus
6098 * if no one else already has it.
6099 */
6100 if (mParent != null) mParent.focusableViewAvailable(this);
6101 }
6102 }
6103
6104 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6105 if ((changed & VISIBILITY_MASK) != 0) {
6106 /*
6107 * If this view is becoming visible, set the DRAWN flag so that
6108 * the next invalidate() will not be skipped.
6109 */
6110 mPrivateFlags |= DRAWN;
6111
6112 needGlobalAttributesUpdate(true);
6113
6114 // a view becoming visible is worth notifying the parent
6115 // about in case nothing has focus. even if this specific view
6116 // isn't focusable, it may contain something that is, so let
6117 // the root view try to give this focus if nothing else does.
6118 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6119 mParent.focusableViewAvailable(this);
6120 }
6121 }
6122 }
6123
6124 /* Check if the GONE bit has changed */
6125 if ((changed & GONE) != 0) {
6126 needGlobalAttributesUpdate(false);
6127 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006128 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006129
Romain Guyecd80ee2009-12-03 17:13:02 -08006130 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6131 if (hasFocus()) clearFocus();
6132 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006133 }
6134 if (mAttachInfo != null) {
6135 mAttachInfo.mViewVisibilityChanged = true;
6136 }
6137 }
6138
6139 /* Check if the VISIBLE bit has changed */
6140 if ((changed & INVISIBLE) != 0) {
6141 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006142 /*
6143 * If this view is becoming invisible, set the DRAWN flag so that
6144 * the next invalidate() will not be skipped.
6145 */
6146 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006147
6148 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6149 // root view becoming invisible shouldn't clear focus
6150 if (getRootView() != this) {
6151 clearFocus();
6152 }
6153 }
6154 if (mAttachInfo != null) {
6155 mAttachInfo.mViewVisibilityChanged = true;
6156 }
6157 }
6158
Adam Powell326d8082009-12-09 15:10:07 -08006159 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006160 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006161 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6162 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006163 }
Adam Powell326d8082009-12-09 15:10:07 -08006164 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6165 }
6166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006167 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6168 destroyDrawingCache();
6169 }
6170
6171 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6172 destroyDrawingCache();
6173 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006174 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006175 }
6176
6177 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6178 destroyDrawingCache();
6179 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6180 }
6181
6182 if ((changed & DRAW_MASK) != 0) {
6183 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6184 if (mBGDrawable != null) {
6185 mPrivateFlags &= ~SKIP_DRAW;
6186 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6187 } else {
6188 mPrivateFlags |= SKIP_DRAW;
6189 }
6190 } else {
6191 mPrivateFlags &= ~SKIP_DRAW;
6192 }
6193 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006194 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006195 }
6196
6197 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006198 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006199 mParent.recomputeViewAttributes(this);
6200 }
6201 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006202
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006203 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006204 requestLayout();
6205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006206 }
6207
6208 /**
6209 * Change the view's z order in the tree, so it's on top of other sibling
6210 * views
6211 */
6212 public void bringToFront() {
6213 if (mParent != null) {
6214 mParent.bringChildToFront(this);
6215 }
6216 }
6217
6218 /**
6219 * This is called in response to an internal scroll in this view (i.e., the
6220 * view scrolled its own contents). This is typically as a result of
6221 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6222 * called.
6223 *
6224 * @param l Current horizontal scroll origin.
6225 * @param t Current vertical scroll origin.
6226 * @param oldl Previous horizontal scroll origin.
6227 * @param oldt Previous vertical scroll origin.
6228 */
6229 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
6230 mBackgroundSizeChanged = true;
6231
6232 final AttachInfo ai = mAttachInfo;
6233 if (ai != null) {
6234 ai.mViewScrollChanged = true;
6235 }
6236 }
6237
6238 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006239 * Interface definition for a callback to be invoked when the layout bounds of a view
6240 * changes due to layout processing.
6241 */
6242 public interface OnLayoutChangeListener {
6243 /**
6244 * Called when the focus state of a view has changed.
6245 *
6246 * @param v The view whose state has changed.
6247 * @param left The new value of the view's left property.
6248 * @param top The new value of the view's top property.
6249 * @param right The new value of the view's right property.
6250 * @param bottom The new value of the view's bottom property.
6251 * @param oldLeft The previous value of the view's left property.
6252 * @param oldTop The previous value of the view's top property.
6253 * @param oldRight The previous value of the view's right property.
6254 * @param oldBottom The previous value of the view's bottom property.
6255 */
6256 void onLayoutChange(View v, int left, int top, int right, int bottom,
6257 int oldLeft, int oldTop, int oldRight, int oldBottom);
6258 }
6259
6260 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006261 * This is called during layout when the size of this view has changed. If
6262 * you were just added to the view hierarchy, you're called with the old
6263 * values of 0.
6264 *
6265 * @param w Current width of this view.
6266 * @param h Current height of this view.
6267 * @param oldw Old width of this view.
6268 * @param oldh Old height of this view.
6269 */
6270 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6271 }
6272
6273 /**
6274 * Called by draw to draw the child views. This may be overridden
6275 * by derived classes to gain control just before its children are drawn
6276 * (but after its own view has been drawn).
6277 * @param canvas the canvas on which to draw the view
6278 */
6279 protected void dispatchDraw(Canvas canvas) {
6280 }
6281
6282 /**
6283 * Gets the parent of this view. Note that the parent is a
6284 * ViewParent and not necessarily a View.
6285 *
6286 * @return Parent of this view.
6287 */
6288 public final ViewParent getParent() {
6289 return mParent;
6290 }
6291
6292 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006293 * Set the horizontal scrolled position of your view. This will cause a call to
6294 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6295 * invalidated.
6296 * @param value the x position to scroll to
6297 */
6298 public void setScrollX(int value) {
6299 scrollTo(value, mScrollY);
6300 }
6301
6302 /**
6303 * Set the vertical scrolled position of your view. This will cause a call to
6304 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6305 * invalidated.
6306 * @param value the y position to scroll to
6307 */
6308 public void setScrollY(int value) {
6309 scrollTo(mScrollX, value);
6310 }
6311
6312 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006313 * Return the scrolled left position of this view. This is the left edge of
6314 * the displayed part of your view. You do not need to draw any pixels
6315 * farther left, since those are outside of the frame of your view on
6316 * screen.
6317 *
6318 * @return The left edge of the displayed part of your view, in pixels.
6319 */
6320 public final int getScrollX() {
6321 return mScrollX;
6322 }
6323
6324 /**
6325 * Return the scrolled top position of this view. This is the top edge of
6326 * the displayed part of your view. You do not need to draw any pixels above
6327 * it, since those are outside of the frame of your view on screen.
6328 *
6329 * @return The top edge of the displayed part of your view, in pixels.
6330 */
6331 public final int getScrollY() {
6332 return mScrollY;
6333 }
6334
6335 /**
6336 * Return the width of the your view.
6337 *
6338 * @return The width of your view, in pixels.
6339 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006340 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006341 public final int getWidth() {
6342 return mRight - mLeft;
6343 }
6344
6345 /**
6346 * Return the height of your view.
6347 *
6348 * @return The height of your view, in pixels.
6349 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006350 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006351 public final int getHeight() {
6352 return mBottom - mTop;
6353 }
6354
6355 /**
6356 * Return the visible drawing bounds of your view. Fills in the output
6357 * rectangle with the values from getScrollX(), getScrollY(),
6358 * getWidth(), and getHeight().
6359 *
6360 * @param outRect The (scrolled) drawing bounds of the view.
6361 */
6362 public void getDrawingRect(Rect outRect) {
6363 outRect.left = mScrollX;
6364 outRect.top = mScrollY;
6365 outRect.right = mScrollX + (mRight - mLeft);
6366 outRect.bottom = mScrollY + (mBottom - mTop);
6367 }
6368
6369 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006370 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6371 * raw width component (that is the result is masked by
6372 * {@link #MEASURED_SIZE_MASK}).
6373 *
6374 * @return The raw measured width of this view.
6375 */
6376 public final int getMeasuredWidth() {
6377 return mMeasuredWidth & MEASURED_SIZE_MASK;
6378 }
6379
6380 /**
6381 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006382 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006383 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006384 * This should be used during measurement and layout calculations only. Use
6385 * {@link #getWidth()} to see how wide a view is after layout.
6386 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006387 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006388 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006389 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006390 return mMeasuredWidth;
6391 }
6392
6393 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006394 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6395 * raw width component (that is the result is masked by
6396 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006397 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006398 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006399 */
6400 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006401 return mMeasuredHeight & MEASURED_SIZE_MASK;
6402 }
6403
6404 /**
6405 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006406 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006407 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6408 * This should be used during measurement and layout calculations only. Use
6409 * {@link #getHeight()} to see how wide a view is after layout.
6410 *
6411 * @return The measured width of this view as a bit mask.
6412 */
6413 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006414 return mMeasuredHeight;
6415 }
6416
6417 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006418 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6419 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6420 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6421 * and the height component is at the shifted bits
6422 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6423 */
6424 public final int getMeasuredState() {
6425 return (mMeasuredWidth&MEASURED_STATE_MASK)
6426 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6427 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6428 }
6429
6430 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006431 * The transform matrix of this view, which is calculated based on the current
6432 * roation, scale, and pivot properties.
6433 *
6434 * @see #getRotation()
6435 * @see #getScaleX()
6436 * @see #getScaleY()
6437 * @see #getPivotX()
6438 * @see #getPivotY()
6439 * @return The current transform matrix for the view
6440 */
6441 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07006442 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07006443 return mMatrix;
6444 }
6445
6446 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006447 * Utility function to determine if the value is far enough away from zero to be
6448 * considered non-zero.
6449 * @param value A floating point value to check for zero-ness
6450 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6451 */
6452 private static boolean nonzero(float value) {
6453 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6454 }
6455
6456 /**
Jeff Brown86671742010-09-30 20:00:15 -07006457 * Returns true if the transform matrix is the identity matrix.
6458 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006459 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006460 * @return True if the transform matrix is the identity matrix, false otherwise.
6461 */
Jeff Brown86671742010-09-30 20:00:15 -07006462 final boolean hasIdentityMatrix() {
6463 updateMatrix();
6464 return mMatrixIsIdentity;
6465 }
6466
6467 /**
6468 * Recomputes the transform matrix if necessary.
6469 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006470 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07006471 if (mMatrixDirty) {
6472 // transform-related properties have changed since the last time someone
6473 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006474
6475 // Figure out if we need to update the pivot point
6476 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006477 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006478 mPrevWidth = mRight - mLeft;
6479 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08006480 mPivotX = mPrevWidth / 2f;
6481 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07006482 }
6483 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006484 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07006485 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
6486 mMatrix.setTranslate(mTranslationX, mTranslationY);
6487 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
6488 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
6489 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07006490 if (mCamera == null) {
6491 mCamera = new Camera();
6492 matrix3D = new Matrix();
6493 }
6494 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07006495 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08006496 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07006497 mCamera.getMatrix(matrix3D);
6498 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006499 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07006500 mMatrix.postConcat(matrix3D);
6501 mCamera.restore();
6502 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006503 mMatrixDirty = false;
6504 mMatrixIsIdentity = mMatrix.isIdentity();
6505 mInverseMatrixDirty = true;
6506 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006507 }
6508
6509 /**
6510 * Utility method to retrieve the inverse of the current mMatrix property.
6511 * We cache the matrix to avoid recalculating it when transform properties
6512 * have not changed.
6513 *
6514 * @return The inverse of the current matrix of this view.
6515 */
Jeff Brown86671742010-09-30 20:00:15 -07006516 final Matrix getInverseMatrix() {
6517 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07006518 if (mInverseMatrixDirty) {
6519 if (mInverseMatrix == null) {
6520 mInverseMatrix = new Matrix();
6521 }
6522 mMatrix.invert(mInverseMatrix);
6523 mInverseMatrixDirty = false;
6524 }
6525 return mInverseMatrix;
6526 }
6527
6528 /**
Romain Guya5364ee2011-02-24 14:46:04 -08006529 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
6530 * views are drawn) from the camera to this view. The camera's distance
6531 * affects 3D transformations, for instance rotations around the X and Y
6532 * axis. If the rotationX or rotationY properties are changed and this view is
6533 * large (more than half the size of the screen), it is recommended to always
6534 * use a camera distance that's greater than the height (X axis rotation) or
6535 * the width (Y axis rotation) of this view.</p>
6536 *
6537 * <p>The distance of the camera from the view plane can have an affect on the
6538 * perspective distortion of the view when it is rotated around the x or y axis.
6539 * For example, a large distance will result in a large viewing angle, and there
6540 * will not be much perspective distortion of the view as it rotates. A short
6541 * distance may cause much more perspective distortion upon rotation, and can
6542 * also result in some drawing artifacts if the rotated view ends up partially
6543 * behind the camera (which is why the recommendation is to use a distance at
6544 * least as far as the size of the view, if the view is to be rotated.)</p>
6545 *
6546 * <p>The distance is expressed in "depth pixels." The default distance depends
6547 * on the screen density. For instance, on a medium density display, the
6548 * default distance is 1280. On a high density display, the default distance
6549 * is 1920.</p>
6550 *
6551 * <p>If you want to specify a distance that leads to visually consistent
6552 * results across various densities, use the following formula:</p>
6553 * <pre>
6554 * float scale = context.getResources().getDisplayMetrics().density;
6555 * view.setCameraDistance(distance * scale);
6556 * </pre>
6557 *
6558 * <p>The density scale factor of a high density display is 1.5,
6559 * and 1920 = 1280 * 1.5.</p>
6560 *
6561 * @param distance The distance in "depth pixels", if negative the opposite
6562 * value is used
6563 *
6564 * @see #setRotationX(float)
6565 * @see #setRotationY(float)
6566 */
6567 public void setCameraDistance(float distance) {
6568 invalidateParentCaches();
6569 invalidate(false);
6570
6571 final float dpi = mResources.getDisplayMetrics().densityDpi;
6572 if (mCamera == null) {
6573 mCamera = new Camera();
6574 matrix3D = new Matrix();
6575 }
6576
6577 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6578 mMatrixDirty = true;
6579
6580 invalidate(false);
6581 }
6582
6583 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006584 * The degrees that the view is rotated around the pivot point.
6585 *
Romain Guya5364ee2011-02-24 14:46:04 -08006586 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006587 * @see #getPivotX()
6588 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006589 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006590 * @return The degrees of rotation.
6591 */
6592 public float getRotation() {
6593 return mRotation;
6594 }
6595
6596 /**
Chet Haase897247b2010-09-09 14:54:47 -07006597 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6598 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006599 *
6600 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006601 *
6602 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006603 * @see #getPivotX()
6604 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006605 * @see #setRotationX(float)
6606 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006607 *
6608 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006609 */
6610 public void setRotation(float rotation) {
6611 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006612 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006613 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006614 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006615 mRotation = rotation;
6616 mMatrixDirty = true;
6617 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006618 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006619 }
6620 }
6621
6622 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006623 * The degrees that the view is rotated around the vertical axis through the pivot point.
6624 *
6625 * @see #getPivotX()
6626 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006627 * @see #setRotationY(float)
6628 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006629 * @return The degrees of Y rotation.
6630 */
6631 public float getRotationY() {
6632 return mRotationY;
6633 }
6634
6635 /**
Chet Haase897247b2010-09-09 14:54:47 -07006636 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6637 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6638 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006639 *
6640 * When rotating large views, it is recommended to adjust the camera distance
6641 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006642 *
6643 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006644 *
6645 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006646 * @see #getPivotX()
6647 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006648 * @see #setRotation(float)
6649 * @see #setRotationX(float)
6650 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006651 *
6652 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006653 */
6654 public void setRotationY(float rotationY) {
6655 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006656 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006657 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006658 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006659 mRotationY = rotationY;
6660 mMatrixDirty = true;
6661 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006662 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006663 }
6664 }
6665
6666 /**
6667 * The degrees that the view is rotated around the horizontal axis through the pivot point.
6668 *
6669 * @see #getPivotX()
6670 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006671 * @see #setRotationX(float)
6672 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006673 * @return The degrees of X rotation.
6674 */
6675 public float getRotationX() {
6676 return mRotationX;
6677 }
6678
6679 /**
Chet Haase897247b2010-09-09 14:54:47 -07006680 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6681 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6682 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006683 *
6684 * When rotating large views, it is recommended to adjust the camera distance
6685 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006686 *
6687 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006688 *
6689 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006690 * @see #getPivotX()
6691 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006692 * @see #setRotation(float)
6693 * @see #setRotationY(float)
6694 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006695 *
6696 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006697 */
6698 public void setRotationX(float rotationX) {
6699 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006700 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006701 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006702 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006703 mRotationX = rotationX;
6704 mMatrixDirty = true;
6705 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006706 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006707 }
6708 }
6709
6710 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006711 * The amount that the view is scaled in x around the pivot point, as a proportion of
6712 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6713 *
Joe Onorato93162322010-09-16 15:42:01 -04006714 * <p>By default, this is 1.0f.
6715 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006716 * @see #getPivotX()
6717 * @see #getPivotY()
6718 * @return The scaling factor.
6719 */
6720 public float getScaleX() {
6721 return mScaleX;
6722 }
6723
6724 /**
6725 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6726 * the view's unscaled width. A value of 1 means that no scaling is applied.
6727 *
6728 * @param scaleX The scaling factor.
6729 * @see #getPivotX()
6730 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006731 *
6732 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006733 */
6734 public void setScaleX(float scaleX) {
6735 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006736 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006737 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006738 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006739 mScaleX = scaleX;
6740 mMatrixDirty = true;
6741 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006742 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006743 }
6744 }
6745
6746 /**
6747 * The amount that the view is scaled in y around the pivot point, as a proportion of
6748 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6749 *
Joe Onorato93162322010-09-16 15:42:01 -04006750 * <p>By default, this is 1.0f.
6751 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006752 * @see #getPivotX()
6753 * @see #getPivotY()
6754 * @return The scaling factor.
6755 */
6756 public float getScaleY() {
6757 return mScaleY;
6758 }
6759
6760 /**
6761 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
6762 * the view's unscaled width. A value of 1 means that no scaling is applied.
6763 *
6764 * @param scaleY The scaling factor.
6765 * @see #getPivotX()
6766 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006767 *
6768 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07006769 */
6770 public void setScaleY(float scaleY) {
6771 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006772 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006773 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006774 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006775 mScaleY = scaleY;
6776 mMatrixDirty = true;
6777 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006778 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006779 }
6780 }
6781
6782 /**
6783 * The x location of the point around which the view is {@link #setRotation(float) rotated}
6784 * and {@link #setScaleX(float) scaled}.
6785 *
6786 * @see #getRotation()
6787 * @see #getScaleX()
6788 * @see #getScaleY()
6789 * @see #getPivotY()
6790 * @return The x location of the pivot point.
6791 */
6792 public float getPivotX() {
6793 return mPivotX;
6794 }
6795
6796 /**
6797 * Sets the x location of the point around which the view is
6798 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07006799 * By default, the pivot point is centered on the object.
6800 * Setting this property disables this behavior and causes the view to use only the
6801 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006802 *
6803 * @param pivotX The x location of the pivot point.
6804 * @see #getRotation()
6805 * @see #getScaleX()
6806 * @see #getScaleY()
6807 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006808 *
6809 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07006810 */
6811 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006812 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006813 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006814 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006815 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006816 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006817 mPivotX = pivotX;
6818 mMatrixDirty = true;
6819 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006820 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006821 }
6822 }
6823
6824 /**
6825 * The y location of the point around which the view is {@link #setRotation(float) rotated}
6826 * and {@link #setScaleY(float) scaled}.
6827 *
6828 * @see #getRotation()
6829 * @see #getScaleX()
6830 * @see #getScaleY()
6831 * @see #getPivotY()
6832 * @return The y location of the pivot point.
6833 */
6834 public float getPivotY() {
6835 return mPivotY;
6836 }
6837
6838 /**
6839 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07006840 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
6841 * Setting this property disables this behavior and causes the view to use only the
6842 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006843 *
6844 * @param pivotY The y location of the pivot point.
6845 * @see #getRotation()
6846 * @see #getScaleX()
6847 * @see #getScaleY()
6848 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006849 *
6850 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07006851 */
6852 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006853 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006854 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006855 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006856 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006857 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006858 mPivotY = pivotY;
6859 mMatrixDirty = true;
6860 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006861 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006862 }
6863 }
6864
6865 /**
6866 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
6867 * completely transparent and 1 means the view is completely opaque.
6868 *
Joe Onorato93162322010-09-16 15:42:01 -04006869 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07006870 * @return The opacity of the view.
6871 */
6872 public float getAlpha() {
6873 return mAlpha;
6874 }
6875
6876 /**
Romain Guy171c5922011-01-06 10:04:23 -08006877 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
6878 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08006879 *
Romain Guy171c5922011-01-06 10:04:23 -08006880 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
6881 * responsible for applying the opacity itself. Otherwise, calling this method is
6882 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08006883 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07006884 *
6885 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08006886 *
Joe Malin32736f02011-01-19 16:14:20 -08006887 * @see #setLayerType(int, android.graphics.Paint)
6888 *
Chet Haase73066682010-11-29 15:55:32 -08006889 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07006890 */
6891 public void setAlpha(float alpha) {
6892 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006893 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07006894 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07006895 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006896 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006897 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07006898 } else {
Romain Guya3496a92010-10-12 11:53:24 -07006899 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006900 invalidate(false);
6901 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006902 }
6903
6904 /**
Chet Haasea00f3862011-02-22 06:34:40 -08006905 * Faster version of setAlpha() which performs the same steps except there are
6906 * no calls to invalidate(). The caller of this function should perform proper invalidation
6907 * on the parent and this object. The return value indicates whether the subclass handles
6908 * alpha (the return value for onSetAlpha()).
6909 *
6910 * @param alpha The new value for the alpha property
6911 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
6912 */
6913 boolean setAlphaNoInvalidation(float alpha) {
6914 mAlpha = alpha;
6915 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
6916 if (subclassHandlesAlpha) {
6917 mPrivateFlags |= ALPHA_SET;
6918 } else {
6919 mPrivateFlags &= ~ALPHA_SET;
6920 }
6921 return subclassHandlesAlpha;
6922 }
6923
6924 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006925 * Top position of this view relative to its parent.
6926 *
6927 * @return The top of this view, in pixels.
6928 */
6929 @ViewDebug.CapturedViewProperty
6930 public final int getTop() {
6931 return mTop;
6932 }
6933
6934 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006935 * Sets the top position of this view relative to its parent. This method is meant to be called
6936 * by the layout system and should not generally be called otherwise, because the property
6937 * may be changed at any time by the layout.
6938 *
6939 * @param top The top of this view, in pixels.
6940 */
6941 public final void setTop(int top) {
6942 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07006943 updateMatrix();
6944 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006945 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006946 int minTop;
6947 int yLoc;
6948 if (top < mTop) {
6949 minTop = top;
6950 yLoc = top - mTop;
6951 } else {
6952 minTop = mTop;
6953 yLoc = 0;
6954 }
Chet Haasee9140a72011-02-16 16:23:29 -08006955 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006956 }
6957 } else {
6958 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006959 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006960 }
6961
Chet Haaseed032702010-10-01 14:05:54 -07006962 int width = mRight - mLeft;
6963 int oldHeight = mBottom - mTop;
6964
Chet Haase21cd1382010-09-01 17:42:29 -07006965 mTop = top;
6966
Chet Haaseed032702010-10-01 14:05:54 -07006967 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6968
Chet Haase21cd1382010-09-01 17:42:29 -07006969 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006970 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6971 // A change in dimension means an auto-centered pivot point changes, too
6972 mMatrixDirty = true;
6973 }
Chet Haase21cd1382010-09-01 17:42:29 -07006974 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006975 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006976 }
Chet Haase55dbb652010-12-21 20:15:08 -08006977 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006978 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006979 }
6980 }
6981
6982 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006983 * Bottom position of this view relative to its parent.
6984 *
6985 * @return The bottom of this view, in pixels.
6986 */
6987 @ViewDebug.CapturedViewProperty
6988 public final int getBottom() {
6989 return mBottom;
6990 }
6991
6992 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08006993 * True if this view has changed since the last time being drawn.
6994 *
6995 * @return The dirty state of this view.
6996 */
6997 public boolean isDirty() {
6998 return (mPrivateFlags & DIRTY_MASK) != 0;
6999 }
7000
7001 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007002 * Sets the bottom position of this view relative to its parent. This method is meant to be
7003 * called by the layout system and should not generally be called otherwise, because the
7004 * property may be changed at any time by the layout.
7005 *
7006 * @param bottom The bottom of this view, in pixels.
7007 */
7008 public final void setBottom(int bottom) {
7009 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007010 updateMatrix();
7011 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007012 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007013 int maxBottom;
7014 if (bottom < mBottom) {
7015 maxBottom = mBottom;
7016 } else {
7017 maxBottom = bottom;
7018 }
Chet Haasee9140a72011-02-16 16:23:29 -08007019 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007020 }
7021 } else {
7022 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007023 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007024 }
7025
Chet Haaseed032702010-10-01 14:05:54 -07007026 int width = mRight - mLeft;
7027 int oldHeight = mBottom - mTop;
7028
Chet Haase21cd1382010-09-01 17:42:29 -07007029 mBottom = bottom;
7030
Chet Haaseed032702010-10-01 14:05:54 -07007031 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7032
Chet Haase21cd1382010-09-01 17:42:29 -07007033 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007034 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7035 // A change in dimension means an auto-centered pivot point changes, too
7036 mMatrixDirty = true;
7037 }
Chet Haase21cd1382010-09-01 17:42:29 -07007038 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007039 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007040 }
Chet Haase55dbb652010-12-21 20:15:08 -08007041 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007042 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007043 }
7044 }
7045
7046 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007047 * Left position of this view relative to its parent.
7048 *
7049 * @return The left edge of this view, in pixels.
7050 */
7051 @ViewDebug.CapturedViewProperty
7052 public final int getLeft() {
7053 return mLeft;
7054 }
7055
7056 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007057 * Sets the left position of this view relative to its parent. This method is meant to be called
7058 * by the layout system and should not generally be called otherwise, because the property
7059 * may be changed at any time by the layout.
7060 *
7061 * @param left The bottom of this view, in pixels.
7062 */
7063 public final void setLeft(int left) {
7064 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007065 updateMatrix();
7066 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007067 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007068 int minLeft;
7069 int xLoc;
7070 if (left < mLeft) {
7071 minLeft = left;
7072 xLoc = left - mLeft;
7073 } else {
7074 minLeft = mLeft;
7075 xLoc = 0;
7076 }
Chet Haasee9140a72011-02-16 16:23:29 -08007077 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007078 }
7079 } else {
7080 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007081 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007082 }
7083
Chet Haaseed032702010-10-01 14:05:54 -07007084 int oldWidth = mRight - mLeft;
7085 int height = mBottom - mTop;
7086
Chet Haase21cd1382010-09-01 17:42:29 -07007087 mLeft = left;
7088
Chet Haaseed032702010-10-01 14:05:54 -07007089 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7090
Chet Haase21cd1382010-09-01 17:42:29 -07007091 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007092 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7093 // A change in dimension means an auto-centered pivot point changes, too
7094 mMatrixDirty = true;
7095 }
Chet Haase21cd1382010-09-01 17:42:29 -07007096 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007097 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007098 }
Chet Haase55dbb652010-12-21 20:15:08 -08007099 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007100 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007101 }
7102 }
7103
7104 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007105 * Right position of this view relative to its parent.
7106 *
7107 * @return The right edge of this view, in pixels.
7108 */
7109 @ViewDebug.CapturedViewProperty
7110 public final int getRight() {
7111 return mRight;
7112 }
7113
7114 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007115 * Sets the right position of this view relative to its parent. This method is meant to be called
7116 * by the layout system and should not generally be called otherwise, because the property
7117 * may be changed at any time by the layout.
7118 *
7119 * @param right The bottom of this view, in pixels.
7120 */
7121 public final void setRight(int right) {
7122 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007123 updateMatrix();
7124 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007125 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007126 int maxRight;
7127 if (right < mRight) {
7128 maxRight = mRight;
7129 } else {
7130 maxRight = right;
7131 }
Chet Haasee9140a72011-02-16 16:23:29 -08007132 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007133 }
7134 } else {
7135 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007136 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007137 }
7138
Chet Haaseed032702010-10-01 14:05:54 -07007139 int oldWidth = mRight - mLeft;
7140 int height = mBottom - mTop;
7141
Chet Haase21cd1382010-09-01 17:42:29 -07007142 mRight = right;
7143
Chet Haaseed032702010-10-01 14:05:54 -07007144 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7145
Chet Haase21cd1382010-09-01 17:42:29 -07007146 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007147 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7148 // A change in dimension means an auto-centered pivot point changes, too
7149 mMatrixDirty = true;
7150 }
Chet Haase21cd1382010-09-01 17:42:29 -07007151 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007152 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007153 }
Chet Haase55dbb652010-12-21 20:15:08 -08007154 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007155 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007156 }
7157 }
7158
7159 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007160 * The visual x position of this view, in pixels. This is equivalent to the
7161 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007162 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007163 *
Chet Haasedf030d22010-07-30 17:22:38 -07007164 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007165 */
Chet Haasedf030d22010-07-30 17:22:38 -07007166 public float getX() {
7167 return mLeft + mTranslationX;
7168 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007169
Chet Haasedf030d22010-07-30 17:22:38 -07007170 /**
7171 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7172 * {@link #setTranslationX(float) translationX} property to be the difference between
7173 * the x value passed in and the current {@link #getLeft() left} property.
7174 *
7175 * @param x The visual x position of this view, in pixels.
7176 */
7177 public void setX(float x) {
7178 setTranslationX(x - mLeft);
7179 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007180
Chet Haasedf030d22010-07-30 17:22:38 -07007181 /**
7182 * The visual y position of this view, in pixels. This is equivalent to the
7183 * {@link #setTranslationY(float) translationY} property plus the current
7184 * {@link #getTop() top} property.
7185 *
7186 * @return The visual y position of this view, in pixels.
7187 */
7188 public float getY() {
7189 return mTop + mTranslationY;
7190 }
7191
7192 /**
7193 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7194 * {@link #setTranslationY(float) translationY} property to be the difference between
7195 * the y value passed in and the current {@link #getTop() top} property.
7196 *
7197 * @param y The visual y position of this view, in pixels.
7198 */
7199 public void setY(float y) {
7200 setTranslationY(y - mTop);
7201 }
7202
7203
7204 /**
7205 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7206 * This position is post-layout, in addition to wherever the object's
7207 * layout placed it.
7208 *
7209 * @return The horizontal position of this view relative to its left position, in pixels.
7210 */
7211 public float getTranslationX() {
7212 return mTranslationX;
7213 }
7214
7215 /**
7216 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7217 * This effectively positions the object post-layout, in addition to wherever the object's
7218 * layout placed it.
7219 *
7220 * @param translationX The horizontal position of this view relative to its left position,
7221 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007222 *
7223 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007224 */
7225 public void setTranslationX(float translationX) {
7226 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007227 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007228 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007229 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007230 mTranslationX = translationX;
7231 mMatrixDirty = true;
7232 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007233 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007234 }
7235 }
7236
7237 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007238 * The horizontal location of this view relative to its {@link #getTop() top} position.
7239 * This position is post-layout, in addition to wherever the object's
7240 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007241 *
Chet Haasedf030d22010-07-30 17:22:38 -07007242 * @return The vertical position of this view relative to its top position,
7243 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007244 */
Chet Haasedf030d22010-07-30 17:22:38 -07007245 public float getTranslationY() {
7246 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07007247 }
7248
7249 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007250 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7251 * This effectively positions the object post-layout, in addition to wherever the object's
7252 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007253 *
Chet Haasedf030d22010-07-30 17:22:38 -07007254 * @param translationY The vertical position of this view relative to its top position,
7255 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007256 *
7257 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007258 */
Chet Haasedf030d22010-07-30 17:22:38 -07007259 public void setTranslationY(float translationY) {
7260 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007261 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007262 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007263 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007264 mTranslationY = translationY;
7265 mMatrixDirty = true;
7266 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007267 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007268 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007269 }
7270
7271 /**
Romain Guyda489792011-02-03 01:05:15 -08007272 * @hide
7273 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007274 public void setFastTranslationX(float x) {
7275 mTranslationX = x;
7276 mMatrixDirty = true;
7277 }
7278
7279 /**
7280 * @hide
7281 */
7282 public void setFastTranslationY(float y) {
7283 mTranslationY = y;
7284 mMatrixDirty = true;
7285 }
7286
7287 /**
7288 * @hide
7289 */
Romain Guyda489792011-02-03 01:05:15 -08007290 public void setFastX(float x) {
7291 mTranslationX = x - mLeft;
7292 mMatrixDirty = true;
7293 }
7294
7295 /**
7296 * @hide
7297 */
7298 public void setFastY(float y) {
7299 mTranslationY = y - mTop;
7300 mMatrixDirty = true;
7301 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007302
Romain Guyda489792011-02-03 01:05:15 -08007303 /**
7304 * @hide
7305 */
7306 public void setFastScaleX(float x) {
7307 mScaleX = x;
7308 mMatrixDirty = true;
7309 }
7310
7311 /**
7312 * @hide
7313 */
7314 public void setFastScaleY(float y) {
7315 mScaleY = y;
7316 mMatrixDirty = true;
7317 }
7318
7319 /**
7320 * @hide
7321 */
7322 public void setFastAlpha(float alpha) {
7323 mAlpha = alpha;
7324 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007325
Romain Guyda489792011-02-03 01:05:15 -08007326 /**
7327 * @hide
7328 */
7329 public void setFastRotationY(float y) {
7330 mRotationY = y;
7331 mMatrixDirty = true;
7332 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007333
Romain Guyda489792011-02-03 01:05:15 -08007334 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007335 * Hit rectangle in parent's coordinates
7336 *
7337 * @param outRect The hit rectangle of the view.
7338 */
7339 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007340 updateMatrix();
7341 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007342 outRect.set(mLeft, mTop, mRight, mBottom);
7343 } else {
7344 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07007345 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07007346 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007347 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7348 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007349 }
7350 }
7351
7352 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007353 * Determines whether the given point, in local coordinates is inside the view.
7354 */
7355 /*package*/ final boolean pointInView(float localX, float localY) {
7356 return localX >= 0 && localX < (mRight - mLeft)
7357 && localY >= 0 && localY < (mBottom - mTop);
7358 }
7359
7360 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007361 * Utility method to determine whether the given point, in local coordinates,
7362 * is inside the view, where the area of the view is expanded by the slop factor.
7363 * This method is called while processing touch-move events to determine if the event
7364 * is still within the view.
7365 */
7366 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007367 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007368 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007369 }
7370
7371 /**
7372 * When a view has focus and the user navigates away from it, the next view is searched for
7373 * starting from the rectangle filled in by this method.
7374 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007375 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7376 * of the view. However, if your view maintains some idea of internal selection,
7377 * such as a cursor, or a selected row or column, you should override this method and
7378 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007379 *
7380 * @param r The rectangle to fill in, in this view's coordinates.
7381 */
7382 public void getFocusedRect(Rect r) {
7383 getDrawingRect(r);
7384 }
7385
7386 /**
7387 * If some part of this view is not clipped by any of its parents, then
7388 * return that area in r in global (root) coordinates. To convert r to local
7389 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7390 * -globalOffset.y)) If the view is completely clipped or translated out,
7391 * return false.
7392 *
7393 * @param r If true is returned, r holds the global coordinates of the
7394 * visible portion of this view.
7395 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7396 * between this view and its root. globalOffet may be null.
7397 * @return true if r is non-empty (i.e. part of the view is visible at the
7398 * root level.
7399 */
7400 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7401 int width = mRight - mLeft;
7402 int height = mBottom - mTop;
7403 if (width > 0 && height > 0) {
7404 r.set(0, 0, width, height);
7405 if (globalOffset != null) {
7406 globalOffset.set(-mScrollX, -mScrollY);
7407 }
7408 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7409 }
7410 return false;
7411 }
7412
7413 public final boolean getGlobalVisibleRect(Rect r) {
7414 return getGlobalVisibleRect(r, null);
7415 }
7416
7417 public final boolean getLocalVisibleRect(Rect r) {
7418 Point offset = new Point();
7419 if (getGlobalVisibleRect(r, offset)) {
7420 r.offset(-offset.x, -offset.y); // make r local
7421 return true;
7422 }
7423 return false;
7424 }
7425
7426 /**
7427 * Offset this view's vertical location by the specified number of pixels.
7428 *
7429 * @param offset the number of pixels to offset the view by
7430 */
7431 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007432 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007433 updateMatrix();
7434 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007435 final ViewParent p = mParent;
7436 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007437 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007438 int minTop;
7439 int maxBottom;
7440 int yLoc;
7441 if (offset < 0) {
7442 minTop = mTop + offset;
7443 maxBottom = mBottom;
7444 yLoc = offset;
7445 } else {
7446 minTop = mTop;
7447 maxBottom = mBottom + offset;
7448 yLoc = 0;
7449 }
7450 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
7451 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007452 }
7453 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007454 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007455 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007456
Chet Haasec3aa3612010-06-17 08:50:37 -07007457 mTop += offset;
7458 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007459
Chet Haasec3aa3612010-06-17 08:50:37 -07007460 if (!mMatrixIsIdentity) {
7461 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007462 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007463 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007464 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007466 }
7467
7468 /**
7469 * Offset this view's horizontal location by the specified amount of pixels.
7470 *
7471 * @param offset the numer of pixels to offset the view by
7472 */
7473 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007474 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007475 updateMatrix();
7476 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007477 final ViewParent p = mParent;
7478 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007479 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007480 int minLeft;
7481 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007482 if (offset < 0) {
7483 minLeft = mLeft + offset;
7484 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007485 } else {
7486 minLeft = mLeft;
7487 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007488 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007489 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07007490 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007491 }
7492 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007493 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007494 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007495
Chet Haasec3aa3612010-06-17 08:50:37 -07007496 mLeft += offset;
7497 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007498
Chet Haasec3aa3612010-06-17 08:50:37 -07007499 if (!mMatrixIsIdentity) {
7500 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007501 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007502 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007503 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007505 }
7506
7507 /**
7508 * Get the LayoutParams associated with this view. All views should have
7509 * layout parameters. These supply parameters to the <i>parent</i> of this
7510 * view specifying how it should be arranged. There are many subclasses of
7511 * ViewGroup.LayoutParams, and these correspond to the different subclasses
7512 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08007513 *
7514 * This method may return null if this View is not attached to a parent
7515 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
7516 * was not invoked successfully. When a View is attached to a parent
7517 * ViewGroup, this method must not return null.
7518 *
7519 * @return The LayoutParams associated with this view, or null if no
7520 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007521 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07007522 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007523 public ViewGroup.LayoutParams getLayoutParams() {
7524 return mLayoutParams;
7525 }
7526
7527 /**
7528 * Set the layout parameters associated with this view. These supply
7529 * parameters to the <i>parent</i> of this view specifying how it should be
7530 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
7531 * correspond to the different subclasses of ViewGroup that are responsible
7532 * for arranging their children.
7533 *
Romain Guy01c174b2011-02-22 11:51:06 -08007534 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007535 */
7536 public void setLayoutParams(ViewGroup.LayoutParams params) {
7537 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007538 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007539 }
7540 mLayoutParams = params;
7541 requestLayout();
7542 }
7543
7544 /**
7545 * Set the scrolled position of your view. This will cause a call to
7546 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7547 * invalidated.
7548 * @param x the x position to scroll to
7549 * @param y the y position to scroll to
7550 */
7551 public void scrollTo(int x, int y) {
7552 if (mScrollX != x || mScrollY != y) {
7553 int oldX = mScrollX;
7554 int oldY = mScrollY;
7555 mScrollX = x;
7556 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007557 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007558 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007559 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007560 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007562 }
7563 }
7564
7565 /**
7566 * Move the scrolled position of your view. This will cause a call to
7567 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7568 * invalidated.
7569 * @param x the amount of pixels to scroll by horizontally
7570 * @param y the amount of pixels to scroll by vertically
7571 */
7572 public void scrollBy(int x, int y) {
7573 scrollTo(mScrollX + x, mScrollY + y);
7574 }
7575
7576 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007577 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7578 * animation to fade the scrollbars out after a default delay. If a subclass
7579 * provides animated scrolling, the start delay should equal the duration
7580 * of the scrolling animation.</p>
7581 *
7582 * <p>The animation starts only if at least one of the scrollbars is
7583 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7584 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7585 * this method returns true, and false otherwise. If the animation is
7586 * started, this method calls {@link #invalidate()}; in that case the
7587 * caller should not call {@link #invalidate()}.</p>
7588 *
7589 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007590 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007591 *
7592 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7593 * and {@link #scrollTo(int, int)}.</p>
7594 *
7595 * @return true if the animation is played, false otherwise
7596 *
7597 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007598 * @see #scrollBy(int, int)
7599 * @see #scrollTo(int, int)
7600 * @see #isHorizontalScrollBarEnabled()
7601 * @see #isVerticalScrollBarEnabled()
7602 * @see #setHorizontalScrollBarEnabled(boolean)
7603 * @see #setVerticalScrollBarEnabled(boolean)
7604 */
7605 protected boolean awakenScrollBars() {
7606 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007607 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007608 }
7609
7610 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007611 * Trigger the scrollbars to draw.
7612 * This method differs from awakenScrollBars() only in its default duration.
7613 * initialAwakenScrollBars() will show the scroll bars for longer than
7614 * usual to give the user more of a chance to notice them.
7615 *
7616 * @return true if the animation is played, false otherwise.
7617 */
7618 private boolean initialAwakenScrollBars() {
7619 return mScrollCache != null &&
7620 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7621 }
7622
7623 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007624 * <p>
7625 * Trigger the scrollbars to draw. When invoked this method starts an
7626 * animation to fade the scrollbars out after a fixed delay. If a subclass
7627 * provides animated scrolling, the start delay should equal the duration of
7628 * the scrolling animation.
7629 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007630 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007631 * <p>
7632 * The animation starts only if at least one of the scrollbars is enabled,
7633 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7634 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7635 * this method returns true, and false otherwise. If the animation is
7636 * started, this method calls {@link #invalidate()}; in that case the caller
7637 * should not call {@link #invalidate()}.
7638 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007639 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007640 * <p>
7641 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007642 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007643 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007644 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007645 * @param startDelay the delay, in milliseconds, after which the animation
7646 * should start; when the delay is 0, the animation starts
7647 * immediately
7648 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007649 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007650 * @see #scrollBy(int, int)
7651 * @see #scrollTo(int, int)
7652 * @see #isHorizontalScrollBarEnabled()
7653 * @see #isVerticalScrollBarEnabled()
7654 * @see #setHorizontalScrollBarEnabled(boolean)
7655 * @see #setVerticalScrollBarEnabled(boolean)
7656 */
7657 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007658 return awakenScrollBars(startDelay, true);
7659 }
Joe Malin32736f02011-01-19 16:14:20 -08007660
Mike Cleron290947b2009-09-29 18:34:32 -07007661 /**
7662 * <p>
7663 * Trigger the scrollbars to draw. When invoked this method starts an
7664 * animation to fade the scrollbars out after a fixed delay. If a subclass
7665 * provides animated scrolling, the start delay should equal the duration of
7666 * the scrolling animation.
7667 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007668 *
Mike Cleron290947b2009-09-29 18:34:32 -07007669 * <p>
7670 * The animation starts only if at least one of the scrollbars is enabled,
7671 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7672 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7673 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08007674 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07007675 * is set to true; in that case the caller
7676 * should not call {@link #invalidate()}.
7677 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007678 *
Mike Cleron290947b2009-09-29 18:34:32 -07007679 * <p>
7680 * This method should be invoked everytime a subclass directly updates the
7681 * scroll parameters.
7682 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007683 *
Mike Cleron290947b2009-09-29 18:34:32 -07007684 * @param startDelay the delay, in milliseconds, after which the animation
7685 * should start; when the delay is 0, the animation starts
7686 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007687 *
Mike Cleron290947b2009-09-29 18:34:32 -07007688 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007689 *
Mike Cleron290947b2009-09-29 18:34:32 -07007690 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007691 *
Mike Cleron290947b2009-09-29 18:34:32 -07007692 * @see #scrollBy(int, int)
7693 * @see #scrollTo(int, int)
7694 * @see #isHorizontalScrollBarEnabled()
7695 * @see #isVerticalScrollBarEnabled()
7696 * @see #setHorizontalScrollBarEnabled(boolean)
7697 * @see #setVerticalScrollBarEnabled(boolean)
7698 */
7699 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007700 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007701
Mike Cleronf116bf82009-09-27 19:14:12 -07007702 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7703 return false;
7704 }
7705
7706 if (scrollCache.scrollBar == null) {
7707 scrollCache.scrollBar = new ScrollBarDrawable();
7708 }
7709
7710 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7711
Mike Cleron290947b2009-09-29 18:34:32 -07007712 if (invalidate) {
7713 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007714 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007715 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007716
7717 if (scrollCache.state == ScrollabilityCache.OFF) {
7718 // FIXME: this is copied from WindowManagerService.
7719 // We should get this value from the system when it
7720 // is possible to do so.
7721 final int KEY_REPEAT_FIRST_DELAY = 750;
7722 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7723 }
7724
7725 // Tell mScrollCache when we should start fading. This may
7726 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007727 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007728 scrollCache.fadeStartTime = fadeStartTime;
7729 scrollCache.state = ScrollabilityCache.ON;
7730
7731 // Schedule our fader to run, unscheduling any old ones first
7732 if (mAttachInfo != null) {
7733 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7734 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7735 }
7736
7737 return true;
7738 }
7739
7740 return false;
7741 }
7742
7743 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007744 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07007745 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
7746 * in the future. This must be called from a UI thread. To call from a non-UI
7747 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007748 *
7749 * WARNING: This method is destructive to dirty.
7750 * @param dirty the rectangle representing the bounds of the dirty region
7751 */
7752 public void invalidate(Rect dirty) {
7753 if (ViewDebug.TRACE_HIERARCHY) {
7754 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7755 }
7756
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007757 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007758 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7759 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007760 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007761 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007762 final ViewParent p = mParent;
7763 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007764 //noinspection PointlessBooleanExpression,ConstantConditions
7765 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7766 if (p != null && ai != null && ai.mHardwareAccelerated) {
7767 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007768 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007769 p.invalidateChild(this, null);
7770 return;
7771 }
Romain Guyaf636eb2010-12-09 17:47:21 -08007772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007773 if (p != null && ai != null) {
7774 final int scrollX = mScrollX;
7775 final int scrollY = mScrollY;
7776 final Rect r = ai.mTmpInvalRect;
7777 r.set(dirty.left - scrollX, dirty.top - scrollY,
7778 dirty.right - scrollX, dirty.bottom - scrollY);
7779 mParent.invalidateChild(this, r);
7780 }
7781 }
7782 }
7783
7784 /**
7785 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
7786 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007787 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
7788 * will be called at some point in the future. This must be called from
7789 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007790 * @param l the left position of the dirty region
7791 * @param t the top position of the dirty region
7792 * @param r the right position of the dirty region
7793 * @param b the bottom position of the dirty region
7794 */
7795 public void invalidate(int l, int t, int r, int b) {
7796 if (ViewDebug.TRACE_HIERARCHY) {
7797 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7798 }
7799
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007800 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007801 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7802 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007803 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007804 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007805 final ViewParent p = mParent;
7806 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007807 //noinspection PointlessBooleanExpression,ConstantConditions
7808 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7809 if (p != null && ai != null && ai.mHardwareAccelerated) {
7810 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007811 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007812 p.invalidateChild(this, null);
7813 return;
7814 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007815 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007816 if (p != null && ai != null && l < r && t < b) {
7817 final int scrollX = mScrollX;
7818 final int scrollY = mScrollY;
7819 final Rect tmpr = ai.mTmpInvalRect;
7820 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
7821 p.invalidateChild(this, tmpr);
7822 }
7823 }
7824 }
7825
7826 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07007827 * Invalidate the whole view. If the view is visible,
7828 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
7829 * the future. This must be called from a UI thread. To call from a non-UI thread,
7830 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007831 */
7832 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07007833 invalidate(true);
7834 }
Joe Malin32736f02011-01-19 16:14:20 -08007835
Chet Haaseed032702010-10-01 14:05:54 -07007836 /**
7837 * This is where the invalidate() work actually happens. A full invalidate()
7838 * causes the drawing cache to be invalidated, but this function can be called with
7839 * invalidateCache set to false to skip that invalidation step for cases that do not
7840 * need it (for example, a component that remains at the same dimensions with the same
7841 * content).
7842 *
7843 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
7844 * well. This is usually true for a full invalidate, but may be set to false if the
7845 * View's contents or dimensions have not changed.
7846 */
Romain Guy849d0a32011-02-01 17:20:48 -08007847 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007848 if (ViewDebug.TRACE_HIERARCHY) {
7849 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7850 }
7851
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007852 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08007853 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08007854 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
7855 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07007856 mPrivateFlags &= ~DRAWN;
7857 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08007858 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07007859 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007861 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07007862 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08007863 //noinspection PointlessBooleanExpression,ConstantConditions
7864 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7865 if (p != null && ai != null && ai.mHardwareAccelerated) {
7866 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007867 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007868 p.invalidateChild(this, null);
7869 return;
7870 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007871 }
Michael Jurkaebefea42010-11-15 16:04:01 -08007872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007873 if (p != null && ai != null) {
7874 final Rect r = ai.mTmpInvalRect;
7875 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7876 // Don't call invalidate -- we don't want to internally scroll
7877 // our own bounds
7878 p.invalidateChild(this, r);
7879 }
7880 }
7881 }
7882
7883 /**
Romain Guyda489792011-02-03 01:05:15 -08007884 * @hide
7885 */
7886 public void fastInvalidate() {
7887 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
7888 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7889 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
7890 if (mParent instanceof View) {
7891 ((View) mParent).mPrivateFlags |= INVALIDATED;
7892 }
7893 mPrivateFlags &= ~DRAWN;
7894 mPrivateFlags |= INVALIDATED;
7895 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07007896 if (mParent != null && mAttachInfo != null) {
7897 if (mAttachInfo.mHardwareAccelerated) {
7898 mParent.invalidateChild(this, null);
7899 } else {
7900 final Rect r = mAttachInfo.mTmpInvalRect;
7901 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7902 // Don't call invalidate -- we don't want to internally scroll
7903 // our own bounds
7904 mParent.invalidateChild(this, r);
7905 }
Romain Guyda489792011-02-03 01:05:15 -08007906 }
7907 }
7908 }
7909
7910 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08007911 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08007912 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7913 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08007914 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
7915 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08007916 *
7917 * @hide
7918 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08007919 protected void invalidateParentCaches() {
7920 if (mParent instanceof View) {
7921 ((View) mParent).mPrivateFlags |= INVALIDATED;
7922 }
7923 }
Joe Malin32736f02011-01-19 16:14:20 -08007924
Romain Guy0fd89bf2011-01-26 15:41:30 -08007925 /**
7926 * Used to indicate that the parent of this view should be invalidated. This functionality
7927 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7928 * which is necessary when various parent-managed properties of the view change, such as
7929 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
7930 * an invalidation event to the parent.
7931 *
7932 * @hide
7933 */
7934 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08007935 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007936 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08007937 }
7938 }
7939
7940 /**
Romain Guy24443ea2009-05-11 11:56:30 -07007941 * Indicates whether this View is opaque. An opaque View guarantees that it will
7942 * draw all the pixels overlapping its bounds using a fully opaque color.
7943 *
7944 * Subclasses of View should override this method whenever possible to indicate
7945 * whether an instance is opaque. Opaque Views are treated in a special way by
7946 * the View hierarchy, possibly allowing it to perform optimizations during
7947 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07007948 *
Romain Guy24443ea2009-05-11 11:56:30 -07007949 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07007950 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007951 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07007952 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07007953 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
7954 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07007955 }
7956
Adam Powell20232d02010-12-08 21:08:53 -08007957 /**
7958 * @hide
7959 */
7960 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07007961 // Opaque if:
7962 // - Has a background
7963 // - Background is opaque
7964 // - Doesn't have scrollbars or scrollbars are inside overlay
7965
7966 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
7967 mPrivateFlags |= OPAQUE_BACKGROUND;
7968 } else {
7969 mPrivateFlags &= ~OPAQUE_BACKGROUND;
7970 }
7971
7972 final int flags = mViewFlags;
7973 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
7974 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
7975 mPrivateFlags |= OPAQUE_SCROLLBARS;
7976 } else {
7977 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
7978 }
7979 }
7980
7981 /**
7982 * @hide
7983 */
7984 protected boolean hasOpaqueScrollbars() {
7985 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07007986 }
7987
7988 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007989 * @return A handler associated with the thread running the View. This
7990 * handler can be used to pump events in the UI events queue.
7991 */
7992 public Handler getHandler() {
7993 if (mAttachInfo != null) {
7994 return mAttachInfo.mHandler;
7995 }
7996 return null;
7997 }
7998
7999 /**
8000 * Causes the Runnable to be added to the message queue.
8001 * The runnable will be run on the user interface thread.
8002 *
8003 * @param action The Runnable that will be executed.
8004 *
8005 * @return Returns true if the Runnable was successfully placed in to the
8006 * message queue. Returns false on failure, usually because the
8007 * looper processing the message queue is exiting.
8008 */
8009 public boolean post(Runnable action) {
8010 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008011 AttachInfo attachInfo = mAttachInfo;
8012 if (attachInfo != null) {
8013 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008014 } else {
8015 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008016 ViewAncestor.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008017 return true;
8018 }
8019
8020 return handler.post(action);
8021 }
8022
8023 /**
8024 * Causes the Runnable to be added to the message queue, to be run
8025 * after the specified amount of time elapses.
8026 * The runnable will be run on the user interface thread.
8027 *
8028 * @param action The Runnable that will be executed.
8029 * @param delayMillis The delay (in milliseconds) until the Runnable
8030 * will be executed.
8031 *
8032 * @return true if the Runnable was successfully placed in to the
8033 * message queue. Returns false on failure, usually because the
8034 * looper processing the message queue is exiting. Note that a
8035 * result of true does not mean the Runnable will be processed --
8036 * if the looper is quit before the delivery time of the message
8037 * occurs then the message will be dropped.
8038 */
8039 public boolean postDelayed(Runnable action, long delayMillis) {
8040 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008041 AttachInfo attachInfo = mAttachInfo;
8042 if (attachInfo != null) {
8043 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008044 } else {
8045 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008046 ViewAncestor.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008047 return true;
8048 }
8049
8050 return handler.postDelayed(action, delayMillis);
8051 }
8052
8053 /**
8054 * Removes the specified Runnable from the message queue.
8055 *
8056 * @param action The Runnable to remove from the message handling queue
8057 *
8058 * @return true if this view could ask the Handler to remove the Runnable,
8059 * false otherwise. When the returned value is true, the Runnable
8060 * may or may not have been actually removed from the message queue
8061 * (for instance, if the Runnable was not in the queue already.)
8062 */
8063 public boolean removeCallbacks(Runnable action) {
8064 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008065 AttachInfo attachInfo = mAttachInfo;
8066 if (attachInfo != null) {
8067 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008068 } else {
8069 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008070 ViewAncestor.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008071 return true;
8072 }
8073
8074 handler.removeCallbacks(action);
8075 return true;
8076 }
8077
8078 /**
8079 * Cause an invalidate to happen on a subsequent cycle through the event loop.
8080 * Use this to invalidate the View from a non-UI thread.
8081 *
8082 * @see #invalidate()
8083 */
8084 public void postInvalidate() {
8085 postInvalidateDelayed(0);
8086 }
8087
8088 /**
8089 * Cause an invalidate of the specified area to happen on a subsequent cycle
8090 * through the event loop. Use this to invalidate the View from a non-UI thread.
8091 *
8092 * @param left The left coordinate of the rectangle to invalidate.
8093 * @param top The top coordinate of the rectangle to invalidate.
8094 * @param right The right coordinate of the rectangle to invalidate.
8095 * @param bottom The bottom coordinate of the rectangle to invalidate.
8096 *
8097 * @see #invalidate(int, int, int, int)
8098 * @see #invalidate(Rect)
8099 */
8100 public void postInvalidate(int left, int top, int right, int bottom) {
8101 postInvalidateDelayed(0, left, top, right, bottom);
8102 }
8103
8104 /**
8105 * Cause an invalidate to happen on a subsequent cycle through the event
8106 * loop. Waits for the specified amount of time.
8107 *
8108 * @param delayMilliseconds the duration in milliseconds to delay the
8109 * invalidation by
8110 */
8111 public void postInvalidateDelayed(long delayMilliseconds) {
8112 // We try only with the AttachInfo because there's no point in invalidating
8113 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008114 AttachInfo attachInfo = mAttachInfo;
8115 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008116 Message msg = Message.obtain();
8117 msg.what = AttachInfo.INVALIDATE_MSG;
8118 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008119 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008120 }
8121 }
8122
8123 /**
8124 * Cause an invalidate of the specified area to happen on a subsequent cycle
8125 * through the event loop. Waits for the specified amount of time.
8126 *
8127 * @param delayMilliseconds the duration in milliseconds to delay the
8128 * invalidation by
8129 * @param left The left coordinate of the rectangle to invalidate.
8130 * @param top The top coordinate of the rectangle to invalidate.
8131 * @param right The right coordinate of the rectangle to invalidate.
8132 * @param bottom The bottom coordinate of the rectangle to invalidate.
8133 */
8134 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8135 int right, int bottom) {
8136
8137 // We try only with the AttachInfo because there's no point in invalidating
8138 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008139 AttachInfo attachInfo = mAttachInfo;
8140 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008141 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8142 info.target = this;
8143 info.left = left;
8144 info.top = top;
8145 info.right = right;
8146 info.bottom = bottom;
8147
8148 final Message msg = Message.obtain();
8149 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8150 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008151 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008152 }
8153 }
8154
8155 /**
8156 * Called by a parent to request that a child update its values for mScrollX
8157 * and mScrollY if necessary. This will typically be done if the child is
8158 * animating a scroll using a {@link android.widget.Scroller Scroller}
8159 * object.
8160 */
8161 public void computeScroll() {
8162 }
8163
8164 /**
8165 * <p>Indicate whether the horizontal edges are faded when the view is
8166 * scrolled horizontally.</p>
8167 *
8168 * @return true if the horizontal edges should are faded on scroll, false
8169 * otherwise
8170 *
8171 * @see #setHorizontalFadingEdgeEnabled(boolean)
8172 * @attr ref android.R.styleable#View_fadingEdge
8173 */
8174 public boolean isHorizontalFadingEdgeEnabled() {
8175 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8176 }
8177
8178 /**
8179 * <p>Define whether the horizontal edges should be faded when this view
8180 * is scrolled horizontally.</p>
8181 *
8182 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8183 * be faded when the view is scrolled
8184 * horizontally
8185 *
8186 * @see #isHorizontalFadingEdgeEnabled()
8187 * @attr ref android.R.styleable#View_fadingEdge
8188 */
8189 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8190 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8191 if (horizontalFadingEdgeEnabled) {
8192 initScrollCache();
8193 }
8194
8195 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8196 }
8197 }
8198
8199 /**
8200 * <p>Indicate whether the vertical edges are faded when the view is
8201 * scrolled horizontally.</p>
8202 *
8203 * @return true if the vertical edges should are faded on scroll, false
8204 * otherwise
8205 *
8206 * @see #setVerticalFadingEdgeEnabled(boolean)
8207 * @attr ref android.R.styleable#View_fadingEdge
8208 */
8209 public boolean isVerticalFadingEdgeEnabled() {
8210 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8211 }
8212
8213 /**
8214 * <p>Define whether the vertical edges should be faded when this view
8215 * is scrolled vertically.</p>
8216 *
8217 * @param verticalFadingEdgeEnabled true if the vertical edges should
8218 * be faded when the view is scrolled
8219 * vertically
8220 *
8221 * @see #isVerticalFadingEdgeEnabled()
8222 * @attr ref android.R.styleable#View_fadingEdge
8223 */
8224 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8225 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8226 if (verticalFadingEdgeEnabled) {
8227 initScrollCache();
8228 }
8229
8230 mViewFlags ^= FADING_EDGE_VERTICAL;
8231 }
8232 }
8233
8234 /**
8235 * Returns the strength, or intensity, of the top faded edge. The strength is
8236 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8237 * returns 0.0 or 1.0 but no value in between.
8238 *
8239 * Subclasses should override this method to provide a smoother fade transition
8240 * when scrolling occurs.
8241 *
8242 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8243 */
8244 protected float getTopFadingEdgeStrength() {
8245 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8246 }
8247
8248 /**
8249 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8250 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8251 * returns 0.0 or 1.0 but no value in between.
8252 *
8253 * Subclasses should override this method to provide a smoother fade transition
8254 * when scrolling occurs.
8255 *
8256 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8257 */
8258 protected float getBottomFadingEdgeStrength() {
8259 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8260 computeVerticalScrollRange() ? 1.0f : 0.0f;
8261 }
8262
8263 /**
8264 * Returns the strength, or intensity, of the left faded edge. The strength is
8265 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8266 * returns 0.0 or 1.0 but no value in between.
8267 *
8268 * Subclasses should override this method to provide a smoother fade transition
8269 * when scrolling occurs.
8270 *
8271 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8272 */
8273 protected float getLeftFadingEdgeStrength() {
8274 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8275 }
8276
8277 /**
8278 * Returns the strength, or intensity, of the right faded edge. The strength is
8279 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8280 * returns 0.0 or 1.0 but no value in between.
8281 *
8282 * Subclasses should override this method to provide a smoother fade transition
8283 * when scrolling occurs.
8284 *
8285 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8286 */
8287 protected float getRightFadingEdgeStrength() {
8288 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8289 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8290 }
8291
8292 /**
8293 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8294 * scrollbar is not drawn by default.</p>
8295 *
8296 * @return true if the horizontal scrollbar should be painted, false
8297 * otherwise
8298 *
8299 * @see #setHorizontalScrollBarEnabled(boolean)
8300 */
8301 public boolean isHorizontalScrollBarEnabled() {
8302 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8303 }
8304
8305 /**
8306 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8307 * scrollbar is not drawn by default.</p>
8308 *
8309 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8310 * be painted
8311 *
8312 * @see #isHorizontalScrollBarEnabled()
8313 */
8314 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8315 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8316 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008317 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008318 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008319 }
8320 }
8321
8322 /**
8323 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
8324 * scrollbar is not drawn by default.</p>
8325 *
8326 * @return true if the vertical scrollbar should be painted, false
8327 * otherwise
8328 *
8329 * @see #setVerticalScrollBarEnabled(boolean)
8330 */
8331 public boolean isVerticalScrollBarEnabled() {
8332 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
8333 }
8334
8335 /**
8336 * <p>Define whether the vertical scrollbar should be drawn or not. The
8337 * scrollbar is not drawn by default.</p>
8338 *
8339 * @param verticalScrollBarEnabled true if the vertical scrollbar should
8340 * be painted
8341 *
8342 * @see #isVerticalScrollBarEnabled()
8343 */
8344 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
8345 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
8346 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008347 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008348 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008349 }
8350 }
8351
Adam Powell20232d02010-12-08 21:08:53 -08008352 /**
8353 * @hide
8354 */
8355 protected void recomputePadding() {
8356 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008357 }
Joe Malin32736f02011-01-19 16:14:20 -08008358
Mike Cleronfe81d382009-09-28 14:22:16 -07008359 /**
8360 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08008361 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008362 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08008363 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008364 */
8365 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
8366 initScrollCache();
8367 final ScrollabilityCache scrollabilityCache = mScrollCache;
8368 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008369 if (fadeScrollbars) {
8370 scrollabilityCache.state = ScrollabilityCache.OFF;
8371 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07008372 scrollabilityCache.state = ScrollabilityCache.ON;
8373 }
8374 }
Joe Malin32736f02011-01-19 16:14:20 -08008375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008376 /**
Joe Malin32736f02011-01-19 16:14:20 -08008377 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008378 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08008379 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008380 * @return true if scrollbar fading is enabled
8381 */
8382 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08008383 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008384 }
Joe Malin32736f02011-01-19 16:14:20 -08008385
Mike Cleron52f0a642009-09-28 18:21:37 -07008386 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008387 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
8388 * inset. When inset, they add to the padding of the view. And the scrollbars
8389 * can be drawn inside the padding area or on the edge of the view. For example,
8390 * if a view has a background drawable and you want to draw the scrollbars
8391 * inside the padding specified by the drawable, you can use
8392 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
8393 * appear at the edge of the view, ignoring the padding, then you can use
8394 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
8395 * @param style the style of the scrollbars. Should be one of
8396 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
8397 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
8398 * @see #SCROLLBARS_INSIDE_OVERLAY
8399 * @see #SCROLLBARS_INSIDE_INSET
8400 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8401 * @see #SCROLLBARS_OUTSIDE_INSET
8402 */
8403 public void setScrollBarStyle(int style) {
8404 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
8405 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07008406 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008407 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008408 }
8409 }
8410
8411 /**
8412 * <p>Returns the current scrollbar style.</p>
8413 * @return the current scrollbar style
8414 * @see #SCROLLBARS_INSIDE_OVERLAY
8415 * @see #SCROLLBARS_INSIDE_INSET
8416 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8417 * @see #SCROLLBARS_OUTSIDE_INSET
8418 */
8419 public int getScrollBarStyle() {
8420 return mViewFlags & SCROLLBARS_STYLE_MASK;
8421 }
8422
8423 /**
8424 * <p>Compute the horizontal range that the horizontal scrollbar
8425 * represents.</p>
8426 *
8427 * <p>The range is expressed in arbitrary units that must be the same as the
8428 * units used by {@link #computeHorizontalScrollExtent()} and
8429 * {@link #computeHorizontalScrollOffset()}.</p>
8430 *
8431 * <p>The default range is the drawing width of this view.</p>
8432 *
8433 * @return the total horizontal range represented by the horizontal
8434 * scrollbar
8435 *
8436 * @see #computeHorizontalScrollExtent()
8437 * @see #computeHorizontalScrollOffset()
8438 * @see android.widget.ScrollBarDrawable
8439 */
8440 protected int computeHorizontalScrollRange() {
8441 return getWidth();
8442 }
8443
8444 /**
8445 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
8446 * within the horizontal range. This value is used to compute the position
8447 * of the thumb within the scrollbar's track.</p>
8448 *
8449 * <p>The range is expressed in arbitrary units that must be the same as the
8450 * units used by {@link #computeHorizontalScrollRange()} and
8451 * {@link #computeHorizontalScrollExtent()}.</p>
8452 *
8453 * <p>The default offset is the scroll offset of this view.</p>
8454 *
8455 * @return the horizontal offset of the scrollbar's thumb
8456 *
8457 * @see #computeHorizontalScrollRange()
8458 * @see #computeHorizontalScrollExtent()
8459 * @see android.widget.ScrollBarDrawable
8460 */
8461 protected int computeHorizontalScrollOffset() {
8462 return mScrollX;
8463 }
8464
8465 /**
8466 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
8467 * within the horizontal range. This value is used to compute the length
8468 * of the thumb within the scrollbar's track.</p>
8469 *
8470 * <p>The range is expressed in arbitrary units that must be the same as the
8471 * units used by {@link #computeHorizontalScrollRange()} and
8472 * {@link #computeHorizontalScrollOffset()}.</p>
8473 *
8474 * <p>The default extent is the drawing width of this view.</p>
8475 *
8476 * @return the horizontal extent of the scrollbar's thumb
8477 *
8478 * @see #computeHorizontalScrollRange()
8479 * @see #computeHorizontalScrollOffset()
8480 * @see android.widget.ScrollBarDrawable
8481 */
8482 protected int computeHorizontalScrollExtent() {
8483 return getWidth();
8484 }
8485
8486 /**
8487 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
8488 *
8489 * <p>The range is expressed in arbitrary units that must be the same as the
8490 * units used by {@link #computeVerticalScrollExtent()} and
8491 * {@link #computeVerticalScrollOffset()}.</p>
8492 *
8493 * @return the total vertical range represented by the vertical scrollbar
8494 *
8495 * <p>The default range is the drawing height of this view.</p>
8496 *
8497 * @see #computeVerticalScrollExtent()
8498 * @see #computeVerticalScrollOffset()
8499 * @see android.widget.ScrollBarDrawable
8500 */
8501 protected int computeVerticalScrollRange() {
8502 return getHeight();
8503 }
8504
8505 /**
8506 * <p>Compute the vertical offset of the vertical scrollbar's thumb
8507 * within the horizontal range. This value is used to compute the position
8508 * of the thumb within the scrollbar's track.</p>
8509 *
8510 * <p>The range is expressed in arbitrary units that must be the same as the
8511 * units used by {@link #computeVerticalScrollRange()} and
8512 * {@link #computeVerticalScrollExtent()}.</p>
8513 *
8514 * <p>The default offset is the scroll offset of this view.</p>
8515 *
8516 * @return the vertical offset of the scrollbar's thumb
8517 *
8518 * @see #computeVerticalScrollRange()
8519 * @see #computeVerticalScrollExtent()
8520 * @see android.widget.ScrollBarDrawable
8521 */
8522 protected int computeVerticalScrollOffset() {
8523 return mScrollY;
8524 }
8525
8526 /**
8527 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
8528 * within the vertical range. This value is used to compute the length
8529 * of the thumb within the scrollbar's track.</p>
8530 *
8531 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08008532 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008533 * {@link #computeVerticalScrollOffset()}.</p>
8534 *
8535 * <p>The default extent is the drawing height of this view.</p>
8536 *
8537 * @return the vertical extent of the scrollbar's thumb
8538 *
8539 * @see #computeVerticalScrollRange()
8540 * @see #computeVerticalScrollOffset()
8541 * @see android.widget.ScrollBarDrawable
8542 */
8543 protected int computeVerticalScrollExtent() {
8544 return getHeight();
8545 }
8546
8547 /**
Adam Powell69159442011-06-13 17:53:06 -07008548 * Check if this view can be scrolled horizontally in a certain direction.
8549 *
8550 * @param direction Negative to check scrolling left, positive to check scrolling right.
8551 * @return true if this view can be scrolled in the specified direction, false otherwise.
8552 */
8553 public boolean canScrollHorizontally(int direction) {
8554 final int offset = computeHorizontalScrollOffset();
8555 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
8556 if (range == 0) return false;
8557 if (direction < 0) {
8558 return offset > 0;
8559 } else {
8560 return offset < range - 1;
8561 }
8562 }
8563
8564 /**
8565 * Check if this view can be scrolled vertically in a certain direction.
8566 *
8567 * @param direction Negative to check scrolling up, positive to check scrolling down.
8568 * @return true if this view can be scrolled in the specified direction, false otherwise.
8569 */
8570 public boolean canScrollVertically(int direction) {
8571 final int offset = computeVerticalScrollOffset();
8572 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
8573 if (range == 0) return false;
8574 if (direction < 0) {
8575 return offset > 0;
8576 } else {
8577 return offset < range - 1;
8578 }
8579 }
8580
8581 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008582 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8583 * scrollbars are painted only if they have been awakened first.</p>
8584 *
8585 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008586 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008587 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008588 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008589 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008590 // scrollbars are drawn only when the animation is running
8591 final ScrollabilityCache cache = mScrollCache;
8592 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008593
Mike Cleronf116bf82009-09-27 19:14:12 -07008594 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008595
Mike Cleronf116bf82009-09-27 19:14:12 -07008596 if (state == ScrollabilityCache.OFF) {
8597 return;
8598 }
Joe Malin32736f02011-01-19 16:14:20 -08008599
Mike Cleronf116bf82009-09-27 19:14:12 -07008600 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008601
Mike Cleronf116bf82009-09-27 19:14:12 -07008602 if (state == ScrollabilityCache.FADING) {
8603 // We're fading -- get our fade interpolation
8604 if (cache.interpolatorValues == null) {
8605 cache.interpolatorValues = new float[1];
8606 }
Joe Malin32736f02011-01-19 16:14:20 -08008607
Mike Cleronf116bf82009-09-27 19:14:12 -07008608 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008609
Mike Cleronf116bf82009-09-27 19:14:12 -07008610 // Stops the animation if we're done
8611 if (cache.scrollBarInterpolator.timeToValues(values) ==
8612 Interpolator.Result.FREEZE_END) {
8613 cache.state = ScrollabilityCache.OFF;
8614 } else {
8615 cache.scrollBar.setAlpha(Math.round(values[0]));
8616 }
Joe Malin32736f02011-01-19 16:14:20 -08008617
8618 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008619 // drawing. We only want this when we're fading so that
8620 // we prevent excessive redraws
8621 invalidate = true;
8622 } else {
8623 // We're just on -- but we may have been fading before so
8624 // reset alpha
8625 cache.scrollBar.setAlpha(255);
8626 }
8627
Joe Malin32736f02011-01-19 16:14:20 -08008628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008629 final int viewFlags = mViewFlags;
8630
8631 final boolean drawHorizontalScrollBar =
8632 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8633 final boolean drawVerticalScrollBar =
8634 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8635 && !isVerticalScrollBarHidden();
8636
8637 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8638 final int width = mRight - mLeft;
8639 final int height = mBottom - mTop;
8640
8641 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008642
Mike Reede8853fc2009-09-04 14:01:48 -04008643 final int scrollX = mScrollX;
8644 final int scrollY = mScrollY;
8645 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8646
Mike Cleronf116bf82009-09-27 19:14:12 -07008647 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008649 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008650 int size = scrollBar.getSize(false);
8651 if (size <= 0) {
8652 size = cache.scrollBarSize;
8653 }
8654
Mike Cleronf116bf82009-09-27 19:14:12 -07008655 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008656 computeHorizontalScrollOffset(),
8657 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008658 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008659 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008660 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008661 left = scrollX + (mPaddingLeft & inside);
8662 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8663 bottom = top + size;
8664 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8665 if (invalidate) {
8666 invalidate(left, top, right, bottom);
8667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008668 }
8669
8670 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008671 int size = scrollBar.getSize(true);
8672 if (size <= 0) {
8673 size = cache.scrollBarSize;
8674 }
8675
Mike Reede8853fc2009-09-04 14:01:48 -04008676 scrollBar.setParameters(computeVerticalScrollRange(),
8677 computeVerticalScrollOffset(),
8678 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008679 switch (mVerticalScrollbarPosition) {
8680 default:
8681 case SCROLLBAR_POSITION_DEFAULT:
8682 case SCROLLBAR_POSITION_RIGHT:
8683 left = scrollX + width - size - (mUserPaddingRight & inside);
8684 break;
8685 case SCROLLBAR_POSITION_LEFT:
8686 left = scrollX + (mUserPaddingLeft & inside);
8687 break;
8688 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008689 top = scrollY + (mPaddingTop & inside);
8690 right = left + size;
8691 bottom = scrollY + height - (mUserPaddingBottom & inside);
8692 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8693 if (invalidate) {
8694 invalidate(left, top, right, bottom);
8695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008696 }
8697 }
8698 }
8699 }
Romain Guy8506ab42009-06-11 17:35:47 -07008700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008701 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008702 * 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 -08008703 * FastScroller is visible.
8704 * @return whether to temporarily hide the vertical scrollbar
8705 * @hide
8706 */
8707 protected boolean isVerticalScrollBarHidden() {
8708 return false;
8709 }
8710
8711 /**
8712 * <p>Draw the horizontal scrollbar if
8713 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
8714 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008715 * @param canvas the canvas on which to draw the scrollbar
8716 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008717 *
8718 * @see #isHorizontalScrollBarEnabled()
8719 * @see #computeHorizontalScrollRange()
8720 * @see #computeHorizontalScrollExtent()
8721 * @see #computeHorizontalScrollOffset()
8722 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008723 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008724 */
Romain Guy8fb95422010-08-17 18:38:51 -07008725 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8726 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008727 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008728 scrollBar.draw(canvas);
8729 }
Mike Reede8853fc2009-09-04 14:01:48 -04008730
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008731 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008732 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8733 * returns true.</p>
8734 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008735 * @param canvas the canvas on which to draw the scrollbar
8736 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008737 *
8738 * @see #isVerticalScrollBarEnabled()
8739 * @see #computeVerticalScrollRange()
8740 * @see #computeVerticalScrollExtent()
8741 * @see #computeVerticalScrollOffset()
8742 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008743 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008744 */
Romain Guy8fb95422010-08-17 18:38:51 -07008745 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
8746 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04008747 scrollBar.setBounds(l, t, r, b);
8748 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008749 }
8750
8751 /**
8752 * Implement this to do your drawing.
8753 *
8754 * @param canvas the canvas on which the background will be drawn
8755 */
8756 protected void onDraw(Canvas canvas) {
8757 }
8758
8759 /*
8760 * Caller is responsible for calling requestLayout if necessary.
8761 * (This allows addViewInLayout to not request a new layout.)
8762 */
8763 void assignParent(ViewParent parent) {
8764 if (mParent == null) {
8765 mParent = parent;
8766 } else if (parent == null) {
8767 mParent = null;
8768 } else {
8769 throw new RuntimeException("view " + this + " being added, but"
8770 + " it already has a parent");
8771 }
8772 }
8773
8774 /**
8775 * This is called when the view is attached to a window. At this point it
8776 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008777 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
8778 * however it may be called any time before the first onDraw -- including
8779 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008780 *
8781 * @see #onDetachedFromWindow()
8782 */
8783 protected void onAttachedToWindow() {
8784 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
8785 mParent.requestTransparentRegion(this);
8786 }
Adam Powell8568c3a2010-04-19 14:26:11 -07008787 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
8788 initialAwakenScrollBars();
8789 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
8790 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08008791 jumpDrawablesToCurrentState();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008792 resetLayoutDirectionResolution();
8793 resolveLayoutDirectionIfNeeded();
8794 resolvePadding();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07008795 if (isFocused()) {
8796 InputMethodManager imm = InputMethodManager.peekInstance();
8797 imm.focusIn(this);
8798 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008799 }
Cibu Johny86666632010-02-22 13:01:02 -08008800
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008801 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008802 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
8803 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008804 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008805 private void resolveLayoutDirectionIfNeeded() {
8806 // Do not resolve if it is not needed
8807 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
8808
8809 // Clear any previous layout direction resolution
8810 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
8811
8812 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008813 switch (getLayoutDirection()) {
8814 case LAYOUT_DIRECTION_INHERIT:
Cibu Johny86666632010-02-22 13:01:02 -08008815 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07008816 if (mParent != null &&
8817 mParent instanceof ViewGroup &&
8818 ((ViewGroup) mParent).getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008819 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08008820 }
8821 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008822 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008823 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08008824 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07008825 case LAYOUT_DIRECTION_LOCALE:
8826 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008827 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07008828 }
8829 break;
8830 default:
8831 // Nothing to do, LTR by default
8832 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008833
8834 // Set to resolved
8835 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
8836 }
8837
8838 private void resolvePadding() {
8839 // If the user specified the absolute padding (either with android:padding or
8840 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
8841 // use the default padding or the padding from the background drawable
8842 // (stored at this point in mPadding*)
8843 switch (getResolvedLayoutDirection()) {
8844 case LAYOUT_DIRECTION_RTL:
8845 // Start user padding override Right user padding. Otherwise, if Right user
8846 // padding is not defined, use the default Right padding. If Right user padding
8847 // is defined, just use it.
8848 if (mUserPaddingStart >= 0) {
8849 mUserPaddingRight = mUserPaddingStart;
8850 } else if (mUserPaddingRight < 0) {
8851 mUserPaddingRight = mPaddingRight;
8852 }
8853 if (mUserPaddingEnd >= 0) {
8854 mUserPaddingLeft = mUserPaddingEnd;
8855 } else if (mUserPaddingLeft < 0) {
8856 mUserPaddingLeft = mPaddingLeft;
8857 }
8858 break;
8859 case LAYOUT_DIRECTION_LTR:
8860 default:
8861 // Start user padding override Left user padding. Otherwise, if Left user
8862 // padding is not defined, use the default left padding. If Left user padding
8863 // is defined, just use it.
8864 if (mUserPaddingStart >= 0) {
8865 mUserPaddingLeft = mUserPaddingStart;
8866 } else if (mUserPaddingLeft < 0) {
8867 mUserPaddingLeft = mPaddingLeft;
8868 }
8869 if (mUserPaddingEnd >= 0) {
8870 mUserPaddingRight = mUserPaddingEnd;
8871 } else if (mUserPaddingRight < 0) {
8872 mUserPaddingRight = mPaddingRight;
8873 }
8874 }
8875
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008876 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
8877
8878 recomputePadding();
8879 }
8880
8881 /**
8882 * Reset the resolved layout direction by clearing the corresponding flag
8883 */
8884 private void resetLayoutDirectionResolution() {
8885 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07008886 }
8887
8888 /**
8889 * Check if a Locale is corresponding to a RTL script.
8890 *
8891 * @param locale Locale to check
8892 * @return true if a Locale is corresponding to a RTL script.
8893 */
8894 private static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07008895 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
8896 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008897 }
8898
8899 /**
8900 * This is called when the view is detached from a window. At this point it
8901 * no longer has a surface for drawing.
8902 *
8903 * @see #onAttachedToWindow()
8904 */
8905 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08008906 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08008907
Romain Guya440b002010-02-24 15:57:54 -08008908 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05008909 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08008910 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08008911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008912 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08008913
8914 if (mHardwareLayer != null) {
8915 mHardwareLayer.destroy();
8916 mHardwareLayer = null;
8917 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008918
Chet Haasedaf98e92011-01-10 14:10:36 -08008919 if (mDisplayList != null) {
8920 mDisplayList.invalidate();
8921 }
8922
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008923 if (mAttachInfo != null) {
8924 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
8925 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
8926 }
8927
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08008928 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008929 }
8930
8931 /**
8932 * @return The number of times this view has been attached to a window
8933 */
8934 protected int getWindowAttachCount() {
8935 return mWindowAttachCount;
8936 }
8937
8938 /**
8939 * Retrieve a unique token identifying the window this view is attached to.
8940 * @return Return the window's token for use in
8941 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
8942 */
8943 public IBinder getWindowToken() {
8944 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
8945 }
8946
8947 /**
8948 * Retrieve a unique token identifying the top-level "real" window of
8949 * the window that this view is attached to. That is, this is like
8950 * {@link #getWindowToken}, except if the window this view in is a panel
8951 * window (attached to another containing window), then the token of
8952 * the containing window is returned instead.
8953 *
8954 * @return Returns the associated window token, either
8955 * {@link #getWindowToken()} or the containing window's token.
8956 */
8957 public IBinder getApplicationWindowToken() {
8958 AttachInfo ai = mAttachInfo;
8959 if (ai != null) {
8960 IBinder appWindowToken = ai.mPanelParentWindowToken;
8961 if (appWindowToken == null) {
8962 appWindowToken = ai.mWindowToken;
8963 }
8964 return appWindowToken;
8965 }
8966 return null;
8967 }
8968
8969 /**
8970 * Retrieve private session object this view hierarchy is using to
8971 * communicate with the window manager.
8972 * @return the session object to communicate with the window manager
8973 */
8974 /*package*/ IWindowSession getWindowSession() {
8975 return mAttachInfo != null ? mAttachInfo.mSession : null;
8976 }
8977
8978 /**
8979 * @param info the {@link android.view.View.AttachInfo} to associated with
8980 * this view
8981 */
8982 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
8983 //System.out.println("Attached! " + this);
8984 mAttachInfo = info;
8985 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008986 // We will need to evaluate the drawable state at least once.
8987 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008988 if (mFloatingTreeObserver != null) {
8989 info.mTreeObserver.merge(mFloatingTreeObserver);
8990 mFloatingTreeObserver = null;
8991 }
8992 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
8993 mAttachInfo.mScrollContainers.add(this);
8994 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
8995 }
8996 performCollectViewAttributes(visibility);
8997 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08008998
8999 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9000 mOnAttachStateChangeListeners;
9001 if (listeners != null && listeners.size() > 0) {
9002 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9003 // perform the dispatching. The iterator is a safe guard against listeners that
9004 // could mutate the list by calling the various add/remove methods. This prevents
9005 // the array from being modified while we iterate it.
9006 for (OnAttachStateChangeListener listener : listeners) {
9007 listener.onViewAttachedToWindow(this);
9008 }
9009 }
9010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009011 int vis = info.mWindowVisibility;
9012 if (vis != GONE) {
9013 onWindowVisibilityChanged(vis);
9014 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009015 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9016 // If nobody has evaluated the drawable state yet, then do it now.
9017 refreshDrawableState();
9018 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009019 }
9020
9021 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009022 AttachInfo info = mAttachInfo;
9023 if (info != null) {
9024 int vis = info.mWindowVisibility;
9025 if (vis != GONE) {
9026 onWindowVisibilityChanged(GONE);
9027 }
9028 }
9029
9030 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009031
Adam Powell4afd62b2011-02-18 15:02:18 -08009032 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9033 mOnAttachStateChangeListeners;
9034 if (listeners != null && listeners.size() > 0) {
9035 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9036 // perform the dispatching. The iterator is a safe guard against listeners that
9037 // could mutate the list by calling the various add/remove methods. This prevents
9038 // the array from being modified while we iterate it.
9039 for (OnAttachStateChangeListener listener : listeners) {
9040 listener.onViewDetachedFromWindow(this);
9041 }
9042 }
9043
Romain Guy01d5edc2011-01-28 11:28:53 -08009044 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009045 mAttachInfo.mScrollContainers.remove(this);
9046 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9047 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009049 mAttachInfo = null;
9050 }
9051
9052 /**
9053 * Store this view hierarchy's frozen state into the given container.
9054 *
9055 * @param container The SparseArray in which to save the view's state.
9056 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009057 * @see #restoreHierarchyState(android.util.SparseArray)
9058 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9059 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009060 */
9061 public void saveHierarchyState(SparseArray<Parcelable> container) {
9062 dispatchSaveInstanceState(container);
9063 }
9064
9065 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009066 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9067 * this view and its children. May be overridden to modify how freezing happens to a
9068 * 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 -08009069 *
9070 * @param container The SparseArray in which to save the view's state.
9071 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009072 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9073 * @see #saveHierarchyState(android.util.SparseArray)
9074 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009075 */
9076 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9077 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9078 mPrivateFlags &= ~SAVE_STATE_CALLED;
9079 Parcelable state = onSaveInstanceState();
9080 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9081 throw new IllegalStateException(
9082 "Derived class did not call super.onSaveInstanceState()");
9083 }
9084 if (state != null) {
9085 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9086 // + ": " + state);
9087 container.put(mID, state);
9088 }
9089 }
9090 }
9091
9092 /**
9093 * Hook allowing a view to generate a representation of its internal state
9094 * that can later be used to create a new instance with that same state.
9095 * This state should only contain information that is not persistent or can
9096 * not be reconstructed later. For example, you will never store your
9097 * current position on screen because that will be computed again when a
9098 * new instance of the view is placed in its view hierarchy.
9099 * <p>
9100 * Some examples of things you may store here: the current cursor position
9101 * in a text view (but usually not the text itself since that is stored in a
9102 * content provider or other persistent storage), the currently selected
9103 * item in a list view.
9104 *
9105 * @return Returns a Parcelable object containing the view's current dynamic
9106 * state, or null if there is nothing interesting to save. The
9107 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009108 * @see #onRestoreInstanceState(android.os.Parcelable)
9109 * @see #saveHierarchyState(android.util.SparseArray)
9110 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009111 * @see #setSaveEnabled(boolean)
9112 */
9113 protected Parcelable onSaveInstanceState() {
9114 mPrivateFlags |= SAVE_STATE_CALLED;
9115 return BaseSavedState.EMPTY_STATE;
9116 }
9117
9118 /**
9119 * Restore this view hierarchy's frozen state from the given container.
9120 *
9121 * @param container The SparseArray which holds previously frozen states.
9122 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009123 * @see #saveHierarchyState(android.util.SparseArray)
9124 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9125 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009126 */
9127 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9128 dispatchRestoreInstanceState(container);
9129 }
9130
9131 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009132 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9133 * state for this view and its children. May be overridden to modify how restoring
9134 * happens to a view's children; for example, some views may want to not store state
9135 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009136 *
9137 * @param container The SparseArray which holds previously saved state.
9138 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009139 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9140 * @see #restoreHierarchyState(android.util.SparseArray)
9141 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009142 */
9143 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9144 if (mID != NO_ID) {
9145 Parcelable state = container.get(mID);
9146 if (state != null) {
9147 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9148 // + ": " + state);
9149 mPrivateFlags &= ~SAVE_STATE_CALLED;
9150 onRestoreInstanceState(state);
9151 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9152 throw new IllegalStateException(
9153 "Derived class did not call super.onRestoreInstanceState()");
9154 }
9155 }
9156 }
9157 }
9158
9159 /**
9160 * Hook allowing a view to re-apply a representation of its internal state that had previously
9161 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9162 * null state.
9163 *
9164 * @param state The frozen state that had previously been returned by
9165 * {@link #onSaveInstanceState}.
9166 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009167 * @see #onSaveInstanceState()
9168 * @see #restoreHierarchyState(android.util.SparseArray)
9169 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009170 */
9171 protected void onRestoreInstanceState(Parcelable state) {
9172 mPrivateFlags |= SAVE_STATE_CALLED;
9173 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009174 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9175 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009176 + "when two views of different type have the same id in the same hierarchy. "
9177 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009178 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009179 }
9180 }
9181
9182 /**
9183 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9184 *
9185 * @return the drawing start time in milliseconds
9186 */
9187 public long getDrawingTime() {
9188 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9189 }
9190
9191 /**
9192 * <p>Enables or disables the duplication of the parent's state into this view. When
9193 * duplication is enabled, this view gets its drawable state from its parent rather
9194 * than from its own internal properties.</p>
9195 *
9196 * <p>Note: in the current implementation, setting this property to true after the
9197 * view was added to a ViewGroup might have no effect at all. This property should
9198 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9199 *
9200 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9201 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009202 *
Gilles Debunnefb817032011-01-13 13:52:49 -08009203 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
9204 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009205 *
9206 * @param enabled True to enable duplication of the parent's drawable state, false
9207 * to disable it.
9208 *
9209 * @see #getDrawableState()
9210 * @see #isDuplicateParentStateEnabled()
9211 */
9212 public void setDuplicateParentStateEnabled(boolean enabled) {
9213 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
9214 }
9215
9216 /**
9217 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
9218 *
9219 * @return True if this view's drawable state is duplicated from the parent,
9220 * false otherwise
9221 *
9222 * @see #getDrawableState()
9223 * @see #setDuplicateParentStateEnabled(boolean)
9224 */
9225 public boolean isDuplicateParentStateEnabled() {
9226 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
9227 }
9228
9229 /**
Romain Guy171c5922011-01-06 10:04:23 -08009230 * <p>Specifies the type of layer backing this view. The layer can be
9231 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
9232 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009233 *
Romain Guy171c5922011-01-06 10:04:23 -08009234 * <p>A layer is associated with an optional {@link android.graphics.Paint}
9235 * instance that controls how the layer is composed on screen. The following
9236 * properties of the paint are taken into account when composing the layer:</p>
9237 * <ul>
9238 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
9239 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
9240 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
9241 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08009242 *
Romain Guy171c5922011-01-06 10:04:23 -08009243 * <p>If this view has an alpha value set to < 1.0 by calling
9244 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
9245 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
9246 * equivalent to setting a hardware layer on this view and providing a paint with
9247 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08009248 *
Romain Guy171c5922011-01-06 10:04:23 -08009249 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
9250 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
9251 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009252 *
Romain Guy171c5922011-01-06 10:04:23 -08009253 * @param layerType The ype of layer to use with this view, must be one of
9254 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9255 * {@link #LAYER_TYPE_HARDWARE}
9256 * @param paint The paint used to compose the layer. This argument is optional
9257 * and can be null. It is ignored when the layer type is
9258 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08009259 *
9260 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08009261 * @see #LAYER_TYPE_NONE
9262 * @see #LAYER_TYPE_SOFTWARE
9263 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08009264 * @see #setAlpha(float)
9265 *
Romain Guy171c5922011-01-06 10:04:23 -08009266 * @attr ref android.R.styleable#View_layerType
9267 */
9268 public void setLayerType(int layerType, Paint paint) {
9269 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08009270 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08009271 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
9272 }
Chet Haasedaf98e92011-01-10 14:10:36 -08009273
Romain Guyd6cd5722011-01-17 14:42:41 -08009274 if (layerType == mLayerType) {
9275 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
9276 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009277 invalidateParentCaches();
9278 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08009279 }
9280 return;
9281 }
Romain Guy171c5922011-01-06 10:04:23 -08009282
9283 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08009284 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -07009285 case LAYER_TYPE_HARDWARE:
9286 if (mHardwareLayer != null) {
9287 mHardwareLayer.destroy();
9288 mHardwareLayer = null;
9289 }
9290 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -08009291 case LAYER_TYPE_SOFTWARE:
9292 if (mDrawingCache != null) {
9293 mDrawingCache.recycle();
9294 mDrawingCache = null;
9295 }
Joe Malin32736f02011-01-19 16:14:20 -08009296
Romain Guy6c319ca2011-01-11 14:29:25 -08009297 if (mUnscaledDrawingCache != null) {
9298 mUnscaledDrawingCache.recycle();
9299 mUnscaledDrawingCache = null;
9300 }
9301 break;
Romain Guy6c319ca2011-01-11 14:29:25 -08009302 default:
9303 break;
Romain Guy171c5922011-01-06 10:04:23 -08009304 }
9305
9306 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08009307 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
9308 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
9309 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08009310
Romain Guy0fd89bf2011-01-26 15:41:30 -08009311 invalidateParentCaches();
9312 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08009313 }
9314
9315 /**
9316 * Indicates what type of layer is currently associated with this view. By default
9317 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
9318 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
9319 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08009320 *
Romain Guy171c5922011-01-06 10:04:23 -08009321 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9322 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08009323 *
9324 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08009325 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08009326 * @see #LAYER_TYPE_NONE
9327 * @see #LAYER_TYPE_SOFTWARE
9328 * @see #LAYER_TYPE_HARDWARE
9329 */
9330 public int getLayerType() {
9331 return mLayerType;
9332 }
Joe Malin32736f02011-01-19 16:14:20 -08009333
Romain Guy6c319ca2011-01-11 14:29:25 -08009334 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08009335 * Forces this view's layer to be created and this view to be rendered
9336 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
9337 * invoking this method will have no effect.
9338 *
9339 * This method can for instance be used to render a view into its layer before
9340 * starting an animation. If this view is complex, rendering into the layer
9341 * before starting the animation will avoid skipping frames.
9342 *
9343 * @throws IllegalStateException If this view is not attached to a window
9344 *
9345 * @see #setLayerType(int, android.graphics.Paint)
9346 */
9347 public void buildLayer() {
9348 if (mLayerType == LAYER_TYPE_NONE) return;
9349
9350 if (mAttachInfo == null) {
9351 throw new IllegalStateException("This view must be attached to a window first");
9352 }
9353
9354 switch (mLayerType) {
9355 case LAYER_TYPE_HARDWARE:
9356 getHardwareLayer();
9357 break;
9358 case LAYER_TYPE_SOFTWARE:
9359 buildDrawingCache(true);
9360 break;
9361 }
9362 }
9363
9364 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08009365 * <p>Returns a hardware layer that can be used to draw this view again
9366 * without executing its draw method.</p>
9367 *
9368 * @return A HardwareLayer ready to render, or null if an error occurred.
9369 */
Romain Guy5e7f7662011-01-24 22:35:56 -08009370 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08009371 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
9372 return null;
9373 }
9374
9375 final int width = mRight - mLeft;
9376 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08009377
Romain Guy6c319ca2011-01-11 14:29:25 -08009378 if (width == 0 || height == 0) {
9379 return null;
9380 }
9381
9382 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
9383 if (mHardwareLayer == null) {
9384 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
9385 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08009386 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009387 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
9388 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08009389 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009390 }
9391
Romain Guy59a12ca2011-06-09 17:48:21 -07009392 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -08009393 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
9394 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009395 try {
9396 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08009397 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08009398 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009399
Chet Haase88172fe2011-03-07 17:36:33 -08009400 final int restoreCount = canvas.save();
9401
Romain Guy6c319ca2011-01-11 14:29:25 -08009402 computeScroll();
9403 canvas.translate(-mScrollX, -mScrollY);
9404
Romain Guy6c319ca2011-01-11 14:29:25 -08009405 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009406
Romain Guy6c319ca2011-01-11 14:29:25 -08009407 // Fast path for layouts with no backgrounds
9408 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9409 mPrivateFlags &= ~DIRTY_MASK;
9410 dispatchDraw(canvas);
9411 } else {
9412 draw(canvas);
9413 }
Joe Malin32736f02011-01-19 16:14:20 -08009414
Chet Haase88172fe2011-03-07 17:36:33 -08009415 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08009416 } finally {
9417 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08009418 mHardwareLayer.end(currentCanvas);
9419 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009420 }
9421 }
9422
9423 return mHardwareLayer;
9424 }
Romain Guy171c5922011-01-06 10:04:23 -08009425
9426 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009427 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
9428 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
9429 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
9430 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
9431 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
9432 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009433 *
Romain Guy171c5922011-01-06 10:04:23 -08009434 * <p>Enabling the drawing cache is similar to
9435 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08009436 * acceleration is turned off. When hardware acceleration is turned on, enabling the
9437 * drawing cache has no effect on rendering because the system uses a different mechanism
9438 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
9439 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
9440 * for information on how to enable software and hardware layers.</p>
9441 *
9442 * <p>This API can be used to manually generate
9443 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
9444 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009445 *
9446 * @param enabled true to enable the drawing cache, false otherwise
9447 *
9448 * @see #isDrawingCacheEnabled()
9449 * @see #getDrawingCache()
9450 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08009451 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009452 */
9453 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009454 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009455 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
9456 }
9457
9458 /**
9459 * <p>Indicates whether the drawing cache is enabled for this view.</p>
9460 *
9461 * @return true if the drawing cache is enabled
9462 *
9463 * @see #setDrawingCacheEnabled(boolean)
9464 * @see #getDrawingCache()
9465 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009466 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009467 public boolean isDrawingCacheEnabled() {
9468 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
9469 }
9470
9471 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08009472 * Debugging utility which recursively outputs the dirty state of a view and its
9473 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08009474 *
Chet Haasedaf98e92011-01-10 14:10:36 -08009475 * @hide
9476 */
Romain Guy676b1732011-02-14 14:45:33 -08009477 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08009478 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
9479 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
9480 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
9481 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
9482 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
9483 if (clear) {
9484 mPrivateFlags &= clearMask;
9485 }
9486 if (this instanceof ViewGroup) {
9487 ViewGroup parent = (ViewGroup) this;
9488 final int count = parent.getChildCount();
9489 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08009490 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08009491 child.outputDirtyFlags(indent + " ", clear, clearMask);
9492 }
9493 }
9494 }
9495
9496 /**
9497 * This method is used by ViewGroup to cause its children to restore or recreate their
9498 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
9499 * to recreate its own display list, which would happen if it went through the normal
9500 * draw/dispatchDraw mechanisms.
9501 *
9502 * @hide
9503 */
9504 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08009505
9506 /**
9507 * A view that is not attached or hardware accelerated cannot create a display list.
9508 * This method checks these conditions and returns the appropriate result.
9509 *
9510 * @return true if view has the ability to create a display list, false otherwise.
9511 *
9512 * @hide
9513 */
9514 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08009515 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08009516 }
Joe Malin32736f02011-01-19 16:14:20 -08009517
Chet Haasedaf98e92011-01-10 14:10:36 -08009518 /**
Romain Guyb051e892010-09-28 19:09:36 -07009519 * <p>Returns a display list that can be used to draw this view again
9520 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009521 *
Romain Guyb051e892010-09-28 19:09:36 -07009522 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08009523 *
9524 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07009525 */
Chet Haasedaf98e92011-01-10 14:10:36 -08009526 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08009527 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07009528 return null;
9529 }
9530
Chet Haasedaf98e92011-01-10 14:10:36 -08009531 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
9532 mDisplayList == null || !mDisplayList.isValid() ||
9533 mRecreateDisplayList)) {
9534 // Don't need to recreate the display list, just need to tell our
9535 // children to restore/recreate theirs
9536 if (mDisplayList != null && mDisplayList.isValid() &&
9537 !mRecreateDisplayList) {
9538 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9539 mPrivateFlags &= ~DIRTY_MASK;
9540 dispatchGetDisplayList();
9541
9542 return mDisplayList;
9543 }
9544
9545 // If we got here, we're recreating it. Mark it as such to ensure that
9546 // we copy in child display lists into ours in drawChild()
9547 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08009548 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009549 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
9550 // If we're creating a new display list, make sure our parent gets invalidated
9551 // since they will need to recreate their display list to account for this
9552 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08009553 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08009554 }
Romain Guyb051e892010-09-28 19:09:36 -07009555
9556 final HardwareCanvas canvas = mDisplayList.start();
9557 try {
9558 int width = mRight - mLeft;
9559 int height = mBottom - mTop;
9560
9561 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08009562 // The dirty rect should always be null for a display list
9563 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07009564
Chet Haase88172fe2011-03-07 17:36:33 -08009565 final int restoreCount = canvas.save();
9566
Chet Haasedaf98e92011-01-10 14:10:36 -08009567 computeScroll();
9568 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009569 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009570
Romain Guyb051e892010-09-28 19:09:36 -07009571 // Fast path for layouts with no backgrounds
9572 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9573 mPrivateFlags &= ~DIRTY_MASK;
9574 dispatchDraw(canvas);
9575 } else {
9576 draw(canvas);
9577 }
Joe Malin32736f02011-01-19 16:14:20 -08009578
Chet Haase88172fe2011-03-07 17:36:33 -08009579 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07009580 } finally {
9581 canvas.onPostDraw();
9582
9583 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07009584 }
Chet Haase77785f92011-01-25 23:22:09 -08009585 } else {
9586 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9587 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07009588 }
9589
9590 return mDisplayList;
9591 }
9592
9593 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009594 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009595 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009596 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009597 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009598 * @see #getDrawingCache(boolean)
9599 */
9600 public Bitmap getDrawingCache() {
9601 return getDrawingCache(false);
9602 }
9603
9604 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009605 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
9606 * is null when caching is disabled. If caching is enabled and the cache is not ready,
9607 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
9608 * draw from the cache when the cache is enabled. To benefit from the cache, you must
9609 * request the drawing cache by calling this method and draw it on screen if the
9610 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009611 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009612 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9613 * this method will create a bitmap of the same size as this view. Because this bitmap
9614 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9615 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9616 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9617 * size than the view. This implies that your application must be able to handle this
9618 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009619 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009620 * @param autoScale Indicates whether the generated bitmap should be scaled based on
9621 * the current density of the screen when the application is in compatibility
9622 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009623 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009624 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009625 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009626 * @see #setDrawingCacheEnabled(boolean)
9627 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07009628 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009629 * @see #destroyDrawingCache()
9630 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009631 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009632 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
9633 return null;
9634 }
9635 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009636 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009637 }
Romain Guy02890fd2010-08-06 17:58:44 -07009638 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009639 }
9640
9641 /**
9642 * <p>Frees the resources used by the drawing cache. If you call
9643 * {@link #buildDrawingCache()} manually without calling
9644 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9645 * should cleanup the cache with this method afterwards.</p>
9646 *
9647 * @see #setDrawingCacheEnabled(boolean)
9648 * @see #buildDrawingCache()
9649 * @see #getDrawingCache()
9650 */
9651 public void destroyDrawingCache() {
9652 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009653 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009654 mDrawingCache = null;
9655 }
Romain Guyfbd8f692009-06-26 14:51:58 -07009656 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009657 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07009658 mUnscaledDrawingCache = null;
9659 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009660 }
9661
9662 /**
9663 * Setting a solid background color for the drawing cache's bitmaps will improve
9664 * perfromance and memory usage. Note, though that this should only be used if this
9665 * view will always be drawn on top of a solid color.
9666 *
9667 * @param color The background color to use for the drawing cache's bitmap
9668 *
9669 * @see #setDrawingCacheEnabled(boolean)
9670 * @see #buildDrawingCache()
9671 * @see #getDrawingCache()
9672 */
9673 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08009674 if (color != mDrawingCacheBackgroundColor) {
9675 mDrawingCacheBackgroundColor = color;
9676 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009678 }
9679
9680 /**
9681 * @see #setDrawingCacheBackgroundColor(int)
9682 *
9683 * @return The background color to used for the drawing cache's bitmap
9684 */
9685 public int getDrawingCacheBackgroundColor() {
9686 return mDrawingCacheBackgroundColor;
9687 }
9688
9689 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009690 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009691 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009692 * @see #buildDrawingCache(boolean)
9693 */
9694 public void buildDrawingCache() {
9695 buildDrawingCache(false);
9696 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08009697
Romain Guyfbd8f692009-06-26 14:51:58 -07009698 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009699 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
9700 *
9701 * <p>If you call {@link #buildDrawingCache()} manually without calling
9702 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9703 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009704 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009705 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9706 * this method will create a bitmap of the same size as this view. Because this bitmap
9707 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9708 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9709 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9710 * size than the view. This implies that your application must be able to handle this
9711 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009712 *
Romain Guy0d9275e2010-10-26 14:22:30 -07009713 * <p>You should avoid calling this method when hardware acceleration is enabled. If
9714 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08009715 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07009716 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009717 *
9718 * @see #getDrawingCache()
9719 * @see #destroyDrawingCache()
9720 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009721 public void buildDrawingCache(boolean autoScale) {
9722 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07009723 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009724 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009725
9726 if (ViewDebug.TRACE_HIERARCHY) {
9727 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
9728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009729
Romain Guy8506ab42009-06-11 17:35:47 -07009730 int width = mRight - mLeft;
9731 int height = mBottom - mTop;
9732
9733 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07009734 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07009735
Romain Guye1123222009-06-29 14:24:56 -07009736 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009737 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
9738 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07009739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009740
9741 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07009742 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08009743 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009744
9745 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07009746 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08009747 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009748 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
9749 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08009750 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009751 return;
9752 }
9753
9754 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07009755 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009756
9757 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009758 Bitmap.Config quality;
9759 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08009760 // Never pick ARGB_4444 because it looks awful
9761 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009762 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
9763 case DRAWING_CACHE_QUALITY_AUTO:
9764 quality = Bitmap.Config.ARGB_8888;
9765 break;
9766 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08009767 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009768 break;
9769 case DRAWING_CACHE_QUALITY_HIGH:
9770 quality = Bitmap.Config.ARGB_8888;
9771 break;
9772 default:
9773 quality = Bitmap.Config.ARGB_8888;
9774 break;
9775 }
9776 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07009777 // Optimization for translucent windows
9778 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08009779 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009780 }
9781
9782 // Try to cleanup memory
9783 if (bitmap != null) bitmap.recycle();
9784
9785 try {
9786 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07009787 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07009788 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07009789 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009790 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07009791 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009792 }
Adam Powell26153a32010-11-08 15:22:27 -08009793 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009794 } catch (OutOfMemoryError e) {
9795 // If there is not enough memory to create the bitmap cache, just
9796 // ignore the issue as bitmap caches are not required to draw the
9797 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07009798 if (autoScale) {
9799 mDrawingCache = null;
9800 } else {
9801 mUnscaledDrawingCache = null;
9802 }
Romain Guy0211a0a2011-02-14 16:34:59 -08009803 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009804 return;
9805 }
9806
9807 clear = drawingCacheBackgroundColor != 0;
9808 }
9809
9810 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009811 if (attachInfo != null) {
9812 canvas = attachInfo.mCanvas;
9813 if (canvas == null) {
9814 canvas = new Canvas();
9815 }
9816 canvas.setBitmap(bitmap);
9817 // Temporarily clobber the cached Canvas in case one of our children
9818 // is also using a drawing cache. Without this, the children would
9819 // steal the canvas by attaching their own bitmap to it and bad, bad
9820 // thing would happen (invisible views, corrupted drawings, etc.)
9821 attachInfo.mCanvas = null;
9822 } else {
9823 // This case should hopefully never or seldom happen
9824 canvas = new Canvas(bitmap);
9825 }
9826
9827 if (clear) {
9828 bitmap.eraseColor(drawingCacheBackgroundColor);
9829 }
9830
9831 computeScroll();
9832 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08009833
Romain Guye1123222009-06-29 14:24:56 -07009834 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009835 final float scale = attachInfo.mApplicationScale;
9836 canvas.scale(scale, scale);
9837 }
Joe Malin32736f02011-01-19 16:14:20 -08009838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009839 canvas.translate(-mScrollX, -mScrollY);
9840
Romain Guy5bcdff42009-05-14 21:27:18 -07009841 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08009842 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
9843 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07009844 mPrivateFlags |= DRAWING_CACHE_VALID;
9845 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009846
9847 // Fast path for layouts with no backgrounds
9848 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9849 if (ViewDebug.TRACE_HIERARCHY) {
9850 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9851 }
Romain Guy5bcdff42009-05-14 21:27:18 -07009852 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009853 dispatchDraw(canvas);
9854 } else {
9855 draw(canvas);
9856 }
9857
9858 canvas.restoreToCount(restoreCount);
9859
9860 if (attachInfo != null) {
9861 // Restore the cached Canvas for our siblings
9862 attachInfo.mCanvas = canvas;
9863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009864 }
9865 }
9866
9867 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009868 * Create a snapshot of the view into a bitmap. We should probably make
9869 * some form of this public, but should think about the API.
9870 */
Romain Guy223ff5c2010-03-02 17:07:47 -08009871 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009872 int width = mRight - mLeft;
9873 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009874
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009875 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07009876 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009877 width = (int) ((width * scale) + 0.5f);
9878 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08009879
Romain Guy8c11e312009-09-14 15:15:30 -07009880 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009881 if (bitmap == null) {
9882 throw new OutOfMemoryError();
9883 }
9884
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009885 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08009886
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009887 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009888 if (attachInfo != null) {
9889 canvas = attachInfo.mCanvas;
9890 if (canvas == null) {
9891 canvas = new Canvas();
9892 }
9893 canvas.setBitmap(bitmap);
9894 // Temporarily clobber the cached Canvas in case one of our children
9895 // is also using a drawing cache. Without this, the children would
9896 // steal the canvas by attaching their own bitmap to it and bad, bad
9897 // things would happen (invisible views, corrupted drawings, etc.)
9898 attachInfo.mCanvas = null;
9899 } else {
9900 // This case should hopefully never or seldom happen
9901 canvas = new Canvas(bitmap);
9902 }
9903
Romain Guy5bcdff42009-05-14 21:27:18 -07009904 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009905 bitmap.eraseColor(backgroundColor);
9906 }
9907
9908 computeScroll();
9909 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009910 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009911 canvas.translate(-mScrollX, -mScrollY);
9912
Romain Guy5bcdff42009-05-14 21:27:18 -07009913 // Temporarily remove the dirty mask
9914 int flags = mPrivateFlags;
9915 mPrivateFlags &= ~DIRTY_MASK;
9916
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009917 // Fast path for layouts with no backgrounds
9918 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9919 dispatchDraw(canvas);
9920 } else {
9921 draw(canvas);
9922 }
9923
Romain Guy5bcdff42009-05-14 21:27:18 -07009924 mPrivateFlags = flags;
9925
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009926 canvas.restoreToCount(restoreCount);
9927
9928 if (attachInfo != null) {
9929 // Restore the cached Canvas for our siblings
9930 attachInfo.mCanvas = canvas;
9931 }
Romain Guy8506ab42009-06-11 17:35:47 -07009932
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009933 return bitmap;
9934 }
9935
9936 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009937 * Indicates whether this View is currently in edit mode. A View is usually
9938 * in edit mode when displayed within a developer tool. For instance, if
9939 * this View is being drawn by a visual user interface builder, this method
9940 * should return true.
9941 *
9942 * Subclasses should check the return value of this method to provide
9943 * different behaviors if their normal behavior might interfere with the
9944 * host environment. For instance: the class spawns a thread in its
9945 * constructor, the drawing code relies on device-specific features, etc.
9946 *
9947 * This method is usually checked in the drawing code of custom widgets.
9948 *
9949 * @return True if this View is in edit mode, false otherwise.
9950 */
9951 public boolean isInEditMode() {
9952 return false;
9953 }
9954
9955 /**
9956 * If the View draws content inside its padding and enables fading edges,
9957 * it needs to support padding offsets. Padding offsets are added to the
9958 * fading edges to extend the length of the fade so that it covers pixels
9959 * drawn inside the padding.
9960 *
9961 * Subclasses of this class should override this method if they need
9962 * to draw content inside the padding.
9963 *
9964 * @return True if padding offset must be applied, false otherwise.
9965 *
9966 * @see #getLeftPaddingOffset()
9967 * @see #getRightPaddingOffset()
9968 * @see #getTopPaddingOffset()
9969 * @see #getBottomPaddingOffset()
9970 *
9971 * @since CURRENT
9972 */
9973 protected boolean isPaddingOffsetRequired() {
9974 return false;
9975 }
9976
9977 /**
9978 * Amount by which to extend the left fading region. Called only when
9979 * {@link #isPaddingOffsetRequired()} returns true.
9980 *
9981 * @return The left padding offset in pixels.
9982 *
9983 * @see #isPaddingOffsetRequired()
9984 *
9985 * @since CURRENT
9986 */
9987 protected int getLeftPaddingOffset() {
9988 return 0;
9989 }
9990
9991 /**
9992 * Amount by which to extend the right fading region. Called only when
9993 * {@link #isPaddingOffsetRequired()} returns true.
9994 *
9995 * @return The right padding offset in pixels.
9996 *
9997 * @see #isPaddingOffsetRequired()
9998 *
9999 * @since CURRENT
10000 */
10001 protected int getRightPaddingOffset() {
10002 return 0;
10003 }
10004
10005 /**
10006 * Amount by which to extend the top fading region. Called only when
10007 * {@link #isPaddingOffsetRequired()} returns true.
10008 *
10009 * @return The top padding offset in pixels.
10010 *
10011 * @see #isPaddingOffsetRequired()
10012 *
10013 * @since CURRENT
10014 */
10015 protected int getTopPaddingOffset() {
10016 return 0;
10017 }
10018
10019 /**
10020 * Amount by which to extend the bottom fading region. Called only when
10021 * {@link #isPaddingOffsetRequired()} returns true.
10022 *
10023 * @return The bottom padding offset in pixels.
10024 *
10025 * @see #isPaddingOffsetRequired()
10026 *
10027 * @since CURRENT
10028 */
10029 protected int getBottomPaddingOffset() {
10030 return 0;
10031 }
10032
10033 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010034 * <p>Indicates whether this view is attached to an hardware accelerated
10035 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010036 *
Romain Guy2bffd262010-09-12 17:40:02 -070010037 * <p>Even if this method returns true, it does not mean that every call
10038 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10039 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10040 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10041 * window is hardware accelerated,
10042 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10043 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010044 *
Romain Guy2bffd262010-09-12 17:40:02 -070010045 * @return True if the view is attached to a window and the window is
10046 * hardware accelerated; false in any other case.
10047 */
10048 public boolean isHardwareAccelerated() {
10049 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10050 }
Joe Malin32736f02011-01-19 16:14:20 -080010051
Romain Guy2bffd262010-09-12 17:40:02 -070010052 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010053 * Manually render this view (and all of its children) to the given Canvas.
10054 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010055 * called. When implementing a view, implement
10056 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10057 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010058 *
10059 * @param canvas The Canvas to which the View is rendered.
10060 */
10061 public void draw(Canvas canvas) {
10062 if (ViewDebug.TRACE_HIERARCHY) {
10063 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10064 }
10065
Romain Guy5bcdff42009-05-14 21:27:18 -070010066 final int privateFlags = mPrivateFlags;
10067 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
10068 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
10069 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070010070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010071 /*
10072 * Draw traversal performs several drawing steps which must be executed
10073 * in the appropriate order:
10074 *
10075 * 1. Draw the background
10076 * 2. If necessary, save the canvas' layers to prepare for fading
10077 * 3. Draw view's content
10078 * 4. Draw children
10079 * 5. If necessary, draw the fading edges and restore layers
10080 * 6. Draw decorations (scrollbars for instance)
10081 */
10082
10083 // Step 1, draw the background, if needed
10084 int saveCount;
10085
Romain Guy24443ea2009-05-11 11:56:30 -070010086 if (!dirtyOpaque) {
10087 final Drawable background = mBGDrawable;
10088 if (background != null) {
10089 final int scrollX = mScrollX;
10090 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010091
Romain Guy24443ea2009-05-11 11:56:30 -070010092 if (mBackgroundSizeChanged) {
10093 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
10094 mBackgroundSizeChanged = false;
10095 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010096
Romain Guy24443ea2009-05-11 11:56:30 -070010097 if ((scrollX | scrollY) == 0) {
10098 background.draw(canvas);
10099 } else {
10100 canvas.translate(scrollX, scrollY);
10101 background.draw(canvas);
10102 canvas.translate(-scrollX, -scrollY);
10103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010104 }
10105 }
10106
10107 // skip step 2 & 5 if possible (common case)
10108 final int viewFlags = mViewFlags;
10109 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
10110 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
10111 if (!verticalEdges && !horizontalEdges) {
10112 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010113 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010114
10115 // Step 4, draw the children
10116 dispatchDraw(canvas);
10117
10118 // Step 6, draw decorations (scrollbars)
10119 onDrawScrollBars(canvas);
10120
10121 // we're done...
10122 return;
10123 }
10124
10125 /*
10126 * Here we do the full fledged routine...
10127 * (this is an uncommon case where speed matters less,
10128 * this is why we repeat some of the tests that have been
10129 * done above)
10130 */
10131
10132 boolean drawTop = false;
10133 boolean drawBottom = false;
10134 boolean drawLeft = false;
10135 boolean drawRight = false;
10136
10137 float topFadeStrength = 0.0f;
10138 float bottomFadeStrength = 0.0f;
10139 float leftFadeStrength = 0.0f;
10140 float rightFadeStrength = 0.0f;
10141
10142 // Step 2, save the canvas' layers
10143 int paddingLeft = mPaddingLeft;
10144 int paddingTop = mPaddingTop;
10145
10146 final boolean offsetRequired = isPaddingOffsetRequired();
10147 if (offsetRequired) {
10148 paddingLeft += getLeftPaddingOffset();
10149 paddingTop += getTopPaddingOffset();
10150 }
10151
10152 int left = mScrollX + paddingLeft;
10153 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
10154 int top = mScrollY + paddingTop;
10155 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
10156
10157 if (offsetRequired) {
10158 right += getRightPaddingOffset();
10159 bottom += getBottomPaddingOffset();
10160 }
10161
10162 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010163 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
10164 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010165
10166 // clip the fade length if top and bottom fades overlap
10167 // overlapping fades produce odd-looking artifacts
10168 if (verticalEdges && (top + length > bottom - length)) {
10169 length = (bottom - top) / 2;
10170 }
10171
10172 // also clip horizontal fades if necessary
10173 if (horizontalEdges && (left + length > right - length)) {
10174 length = (right - left) / 2;
10175 }
10176
10177 if (verticalEdges) {
10178 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010179 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010180 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010181 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010182 }
10183
10184 if (horizontalEdges) {
10185 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010186 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010187 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010188 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010189 }
10190
10191 saveCount = canvas.getSaveCount();
10192
10193 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070010194 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010195 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
10196
10197 if (drawTop) {
10198 canvas.saveLayer(left, top, right, top + length, null, flags);
10199 }
10200
10201 if (drawBottom) {
10202 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
10203 }
10204
10205 if (drawLeft) {
10206 canvas.saveLayer(left, top, left + length, bottom, null, flags);
10207 }
10208
10209 if (drawRight) {
10210 canvas.saveLayer(right - length, top, right, bottom, null, flags);
10211 }
10212 } else {
10213 scrollabilityCache.setFadeColor(solidColor);
10214 }
10215
10216 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010217 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010218
10219 // Step 4, draw the children
10220 dispatchDraw(canvas);
10221
10222 // Step 5, draw the fade effect and restore layers
10223 final Paint p = scrollabilityCache.paint;
10224 final Matrix matrix = scrollabilityCache.matrix;
10225 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010226
10227 if (drawTop) {
10228 matrix.setScale(1, fadeHeight * topFadeStrength);
10229 matrix.postTranslate(left, top);
10230 fade.setLocalMatrix(matrix);
10231 canvas.drawRect(left, top, right, top + length, p);
10232 }
10233
10234 if (drawBottom) {
10235 matrix.setScale(1, fadeHeight * bottomFadeStrength);
10236 matrix.postRotate(180);
10237 matrix.postTranslate(left, bottom);
10238 fade.setLocalMatrix(matrix);
10239 canvas.drawRect(left, bottom - length, right, bottom, p);
10240 }
10241
10242 if (drawLeft) {
10243 matrix.setScale(1, fadeHeight * leftFadeStrength);
10244 matrix.postRotate(-90);
10245 matrix.postTranslate(left, top);
10246 fade.setLocalMatrix(matrix);
10247 canvas.drawRect(left, top, left + length, bottom, p);
10248 }
10249
10250 if (drawRight) {
10251 matrix.setScale(1, fadeHeight * rightFadeStrength);
10252 matrix.postRotate(90);
10253 matrix.postTranslate(right, top);
10254 fade.setLocalMatrix(matrix);
10255 canvas.drawRect(right - length, top, right, bottom, p);
10256 }
10257
10258 canvas.restoreToCount(saveCount);
10259
10260 // Step 6, draw decorations (scrollbars)
10261 onDrawScrollBars(canvas);
10262 }
10263
10264 /**
10265 * Override this if your view is known to always be drawn on top of a solid color background,
10266 * and needs to draw fading edges. Returning a non-zero color enables the view system to
10267 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
10268 * should be set to 0xFF.
10269 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010270 * @see #setVerticalFadingEdgeEnabled(boolean)
10271 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010272 *
10273 * @return The known solid color background for this view, or 0 if the color may vary
10274 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010275 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010276 public int getSolidColor() {
10277 return 0;
10278 }
10279
10280 /**
10281 * Build a human readable string representation of the specified view flags.
10282 *
10283 * @param flags the view flags to convert to a string
10284 * @return a String representing the supplied flags
10285 */
10286 private static String printFlags(int flags) {
10287 String output = "";
10288 int numFlags = 0;
10289 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
10290 output += "TAKES_FOCUS";
10291 numFlags++;
10292 }
10293
10294 switch (flags & VISIBILITY_MASK) {
10295 case INVISIBLE:
10296 if (numFlags > 0) {
10297 output += " ";
10298 }
10299 output += "INVISIBLE";
10300 // USELESS HERE numFlags++;
10301 break;
10302 case GONE:
10303 if (numFlags > 0) {
10304 output += " ";
10305 }
10306 output += "GONE";
10307 // USELESS HERE numFlags++;
10308 break;
10309 default:
10310 break;
10311 }
10312 return output;
10313 }
10314
10315 /**
10316 * Build a human readable string representation of the specified private
10317 * view flags.
10318 *
10319 * @param privateFlags the private view flags to convert to a string
10320 * @return a String representing the supplied flags
10321 */
10322 private static String printPrivateFlags(int privateFlags) {
10323 String output = "";
10324 int numFlags = 0;
10325
10326 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
10327 output += "WANTS_FOCUS";
10328 numFlags++;
10329 }
10330
10331 if ((privateFlags & FOCUSED) == FOCUSED) {
10332 if (numFlags > 0) {
10333 output += " ";
10334 }
10335 output += "FOCUSED";
10336 numFlags++;
10337 }
10338
10339 if ((privateFlags & SELECTED) == SELECTED) {
10340 if (numFlags > 0) {
10341 output += " ";
10342 }
10343 output += "SELECTED";
10344 numFlags++;
10345 }
10346
10347 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
10348 if (numFlags > 0) {
10349 output += " ";
10350 }
10351 output += "IS_ROOT_NAMESPACE";
10352 numFlags++;
10353 }
10354
10355 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
10356 if (numFlags > 0) {
10357 output += " ";
10358 }
10359 output += "HAS_BOUNDS";
10360 numFlags++;
10361 }
10362
10363 if ((privateFlags & DRAWN) == DRAWN) {
10364 if (numFlags > 0) {
10365 output += " ";
10366 }
10367 output += "DRAWN";
10368 // USELESS HERE numFlags++;
10369 }
10370 return output;
10371 }
10372
10373 /**
10374 * <p>Indicates whether or not this view's layout will be requested during
10375 * the next hierarchy layout pass.</p>
10376 *
10377 * @return true if the layout will be forced during next layout pass
10378 */
10379 public boolean isLayoutRequested() {
10380 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
10381 }
10382
10383 /**
10384 * Assign a size and position to a view and all of its
10385 * descendants
10386 *
10387 * <p>This is the second phase of the layout mechanism.
10388 * (The first is measuring). In this phase, each parent calls
10389 * layout on all of its children to position them.
10390 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080010391 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010392 *
Chet Haase9c087442011-01-12 16:20:16 -080010393 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010394 * Derived classes with children should override
10395 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080010396 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010397 *
10398 * @param l Left position, relative to parent
10399 * @param t Top position, relative to parent
10400 * @param r Right position, relative to parent
10401 * @param b Bottom position, relative to parent
10402 */
Romain Guy5429e1d2010-09-07 12:38:00 -070010403 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080010404 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070010405 int oldL = mLeft;
10406 int oldT = mTop;
10407 int oldB = mBottom;
10408 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010409 boolean changed = setFrame(l, t, r, b);
10410 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
10411 if (ViewDebug.TRACE_HIERARCHY) {
10412 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
10413 }
10414
10415 onLayout(changed, l, t, r, b);
10416 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070010417
10418 if (mOnLayoutChangeListeners != null) {
10419 ArrayList<OnLayoutChangeListener> listenersCopy =
10420 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
10421 int numListeners = listenersCopy.size();
10422 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070010423 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070010424 }
10425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010426 }
10427 mPrivateFlags &= ~FORCE_LAYOUT;
10428 }
10429
10430 /**
10431 * Called from layout when this view should
10432 * assign a size and position to each of its children.
10433 *
10434 * Derived classes with children should override
10435 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070010436 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010437 * @param changed This is a new size or position for this view
10438 * @param left Left position, relative to parent
10439 * @param top Top position, relative to parent
10440 * @param right Right position, relative to parent
10441 * @param bottom Bottom position, relative to parent
10442 */
10443 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
10444 }
10445
10446 /**
10447 * Assign a size and position to this view.
10448 *
10449 * This is called from layout.
10450 *
10451 * @param left Left position, relative to parent
10452 * @param top Top position, relative to parent
10453 * @param right Right position, relative to parent
10454 * @param bottom Bottom position, relative to parent
10455 * @return true if the new size and position are different than the
10456 * previous ones
10457 * {@hide}
10458 */
10459 protected boolean setFrame(int left, int top, int right, int bottom) {
10460 boolean changed = false;
10461
10462 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070010463 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010464 + right + "," + bottom + ")");
10465 }
10466
10467 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
10468 changed = true;
10469
10470 // Remember our drawn bit
10471 int drawn = mPrivateFlags & DRAWN;
10472
10473 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -080010474 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010475
10476
10477 int oldWidth = mRight - mLeft;
10478 int oldHeight = mBottom - mTop;
10479
10480 mLeft = left;
10481 mTop = top;
10482 mRight = right;
10483 mBottom = bottom;
10484
10485 mPrivateFlags |= HAS_BOUNDS;
10486
10487 int newWidth = right - left;
10488 int newHeight = bottom - top;
10489
10490 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010491 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
10492 // A change in dimension means an auto-centered pivot point changes, too
10493 mMatrixDirty = true;
10494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010495 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
10496 }
10497
10498 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
10499 // If we are visible, force the DRAWN bit to on so that
10500 // this invalidate will go through (at least to our parent).
10501 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010502 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010503 // the DRAWN bit.
10504 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010505 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -080010506 // parent display list may need to be recreated based on a change in the bounds
10507 // of any child
10508 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010509 }
10510
10511 // Reset drawn bit to original value (invalidate turns it off)
10512 mPrivateFlags |= drawn;
10513
10514 mBackgroundSizeChanged = true;
10515 }
10516 return changed;
10517 }
10518
10519 /**
10520 * Finalize inflating a view from XML. This is called as the last phase
10521 * of inflation, after all child views have been added.
10522 *
10523 * <p>Even if the subclass overrides onFinishInflate, they should always be
10524 * sure to call the super method, so that we get called.
10525 */
10526 protected void onFinishInflate() {
10527 }
10528
10529 /**
10530 * Returns the resources associated with this view.
10531 *
10532 * @return Resources object.
10533 */
10534 public Resources getResources() {
10535 return mResources;
10536 }
10537
10538 /**
10539 * Invalidates the specified Drawable.
10540 *
10541 * @param drawable the drawable to invalidate
10542 */
10543 public void invalidateDrawable(Drawable drawable) {
10544 if (verifyDrawable(drawable)) {
10545 final Rect dirty = drawable.getBounds();
10546 final int scrollX = mScrollX;
10547 final int scrollY = mScrollY;
10548
10549 invalidate(dirty.left + scrollX, dirty.top + scrollY,
10550 dirty.right + scrollX, dirty.bottom + scrollY);
10551 }
10552 }
10553
10554 /**
10555 * Schedules an action on a drawable to occur at a specified time.
10556 *
10557 * @param who the recipient of the action
10558 * @param what the action to run on the drawable
10559 * @param when the time at which the action must occur. Uses the
10560 * {@link SystemClock#uptimeMillis} timebase.
10561 */
10562 public void scheduleDrawable(Drawable who, Runnable what, long when) {
10563 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10564 mAttachInfo.mHandler.postAtTime(what, who, when);
10565 }
10566 }
10567
10568 /**
10569 * Cancels a scheduled action on a drawable.
10570 *
10571 * @param who the recipient of the action
10572 * @param what the action to cancel
10573 */
10574 public void unscheduleDrawable(Drawable who, Runnable what) {
10575 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10576 mAttachInfo.mHandler.removeCallbacks(what, who);
10577 }
10578 }
10579
10580 /**
10581 * Unschedule any events associated with the given Drawable. This can be
10582 * used when selecting a new Drawable into a view, so that the previous
10583 * one is completely unscheduled.
10584 *
10585 * @param who The Drawable to unschedule.
10586 *
10587 * @see #drawableStateChanged
10588 */
10589 public void unscheduleDrawable(Drawable who) {
10590 if (mAttachInfo != null) {
10591 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
10592 }
10593 }
10594
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070010595 /**
10596 * Return the layout direction of a given Drawable.
10597 *
10598 * @param who the Drawable to query
10599 *
10600 * @hide
10601 */
10602 public int getResolvedLayoutDirection(Drawable who) {
10603 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070010604 }
10605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010606 /**
10607 * If your view subclass is displaying its own Drawable objects, it should
10608 * override this function and return true for any Drawable it is
10609 * displaying. This allows animations for those drawables to be
10610 * scheduled.
10611 *
10612 * <p>Be sure to call through to the super class when overriding this
10613 * function.
10614 *
10615 * @param who The Drawable to verify. Return true if it is one you are
10616 * displaying, else return the result of calling through to the
10617 * super class.
10618 *
10619 * @return boolean If true than the Drawable is being displayed in the
10620 * view; else false and it is not allowed to animate.
10621 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010622 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
10623 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010624 */
10625 protected boolean verifyDrawable(Drawable who) {
10626 return who == mBGDrawable;
10627 }
10628
10629 /**
10630 * This function is called whenever the state of the view changes in such
10631 * a way that it impacts the state of drawables being shown.
10632 *
10633 * <p>Be sure to call through to the superclass when overriding this
10634 * function.
10635 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010636 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010637 */
10638 protected void drawableStateChanged() {
10639 Drawable d = mBGDrawable;
10640 if (d != null && d.isStateful()) {
10641 d.setState(getDrawableState());
10642 }
10643 }
10644
10645 /**
10646 * Call this to force a view to update its drawable state. This will cause
10647 * drawableStateChanged to be called on this view. Views that are interested
10648 * in the new state should call getDrawableState.
10649 *
10650 * @see #drawableStateChanged
10651 * @see #getDrawableState
10652 */
10653 public void refreshDrawableState() {
10654 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
10655 drawableStateChanged();
10656
10657 ViewParent parent = mParent;
10658 if (parent != null) {
10659 parent.childDrawableStateChanged(this);
10660 }
10661 }
10662
10663 /**
10664 * Return an array of resource IDs of the drawable states representing the
10665 * current state of the view.
10666 *
10667 * @return The current drawable state
10668 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010669 * @see Drawable#setState(int[])
10670 * @see #drawableStateChanged()
10671 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010672 */
10673 public final int[] getDrawableState() {
10674 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
10675 return mDrawableState;
10676 } else {
10677 mDrawableState = onCreateDrawableState(0);
10678 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
10679 return mDrawableState;
10680 }
10681 }
10682
10683 /**
10684 * Generate the new {@link android.graphics.drawable.Drawable} state for
10685 * this view. This is called by the view
10686 * system when the cached Drawable state is determined to be invalid. To
10687 * retrieve the current state, you should use {@link #getDrawableState}.
10688 *
10689 * @param extraSpace if non-zero, this is the number of extra entries you
10690 * would like in the returned array in which you can place your own
10691 * states.
10692 *
10693 * @return Returns an array holding the current {@link Drawable} state of
10694 * the view.
10695 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010696 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010697 */
10698 protected int[] onCreateDrawableState(int extraSpace) {
10699 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
10700 mParent instanceof View) {
10701 return ((View) mParent).onCreateDrawableState(extraSpace);
10702 }
10703
10704 int[] drawableState;
10705
10706 int privateFlags = mPrivateFlags;
10707
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010708 int viewStateIndex = 0;
10709 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
10710 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
10711 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070010712 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010713 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
10714 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070010715 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
10716 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010717 // This is set if HW acceleration is requested, even if the current
10718 // process doesn't allow it. This is just to allow app preview
10719 // windows to better match their app.
10720 viewStateIndex |= VIEW_STATE_ACCELERATED;
10721 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070010722 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010723
Christopher Tate3d4bf172011-03-28 16:16:46 -070010724 final int privateFlags2 = mPrivateFlags2;
10725 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
10726 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
10727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010728 drawableState = VIEW_STATE_SETS[viewStateIndex];
10729
10730 //noinspection ConstantIfStatement
10731 if (false) {
10732 Log.i("View", "drawableStateIndex=" + viewStateIndex);
10733 Log.i("View", toString()
10734 + " pressed=" + ((privateFlags & PRESSED) != 0)
10735 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
10736 + " fo=" + hasFocus()
10737 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010738 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010739 + ": " + Arrays.toString(drawableState));
10740 }
10741
10742 if (extraSpace == 0) {
10743 return drawableState;
10744 }
10745
10746 final int[] fullState;
10747 if (drawableState != null) {
10748 fullState = new int[drawableState.length + extraSpace];
10749 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
10750 } else {
10751 fullState = new int[extraSpace];
10752 }
10753
10754 return fullState;
10755 }
10756
10757 /**
10758 * Merge your own state values in <var>additionalState</var> into the base
10759 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070010760 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010761 *
10762 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070010763 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010764 * own additional state values.
10765 *
10766 * @param additionalState The additional state values you would like
10767 * added to <var>baseState</var>; this array is not modified.
10768 *
10769 * @return As a convenience, the <var>baseState</var> array you originally
10770 * passed into the function is returned.
10771 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010772 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010773 */
10774 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
10775 final int N = baseState.length;
10776 int i = N - 1;
10777 while (i >= 0 && baseState[i] == 0) {
10778 i--;
10779 }
10780 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
10781 return baseState;
10782 }
10783
10784 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070010785 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
10786 * on all Drawable objects associated with this view.
10787 */
10788 public void jumpDrawablesToCurrentState() {
10789 if (mBGDrawable != null) {
10790 mBGDrawable.jumpToCurrentState();
10791 }
10792 }
10793
10794 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010795 * Sets the background color for this view.
10796 * @param color the color of the background
10797 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010798 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010799 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070010800 if (mBGDrawable instanceof ColorDrawable) {
10801 ((ColorDrawable) mBGDrawable).setColor(color);
10802 } else {
10803 setBackgroundDrawable(new ColorDrawable(color));
10804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010805 }
10806
10807 /**
10808 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070010809 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010810 * @param resid The identifier of the resource.
10811 * @attr ref android.R.styleable#View_background
10812 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010813 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010814 public void setBackgroundResource(int resid) {
10815 if (resid != 0 && resid == mBackgroundResource) {
10816 return;
10817 }
10818
10819 Drawable d= null;
10820 if (resid != 0) {
10821 d = mResources.getDrawable(resid);
10822 }
10823 setBackgroundDrawable(d);
10824
10825 mBackgroundResource = resid;
10826 }
10827
10828 /**
10829 * Set the background to a given Drawable, or remove the background. If the
10830 * background has padding, this View's padding is set to the background's
10831 * padding. However, when a background is removed, this View's padding isn't
10832 * touched. If setting the padding is desired, please use
10833 * {@link #setPadding(int, int, int, int)}.
10834 *
10835 * @param d The Drawable to use as the background, or null to remove the
10836 * background
10837 */
10838 public void setBackgroundDrawable(Drawable d) {
10839 boolean requestLayout = false;
10840
10841 mBackgroundResource = 0;
10842
10843 /*
10844 * Regardless of whether we're setting a new background or not, we want
10845 * to clear the previous drawable.
10846 */
10847 if (mBGDrawable != null) {
10848 mBGDrawable.setCallback(null);
10849 unscheduleDrawable(mBGDrawable);
10850 }
10851
10852 if (d != null) {
10853 Rect padding = sThreadLocal.get();
10854 if (padding == null) {
10855 padding = new Rect();
10856 sThreadLocal.set(padding);
10857 }
10858 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010859 switch (d.getResolvedLayoutDirectionSelf()) {
10860 case LAYOUT_DIRECTION_RTL:
10861 setPadding(padding.right, padding.top, padding.left, padding.bottom);
10862 break;
10863 case LAYOUT_DIRECTION_LTR:
10864 default:
10865 setPadding(padding.left, padding.top, padding.right, padding.bottom);
10866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010867 }
10868
10869 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
10870 // if it has a different minimum size, we should layout again
10871 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
10872 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
10873 requestLayout = true;
10874 }
10875
10876 d.setCallback(this);
10877 if (d.isStateful()) {
10878 d.setState(getDrawableState());
10879 }
10880 d.setVisible(getVisibility() == VISIBLE, false);
10881 mBGDrawable = d;
10882
10883 if ((mPrivateFlags & SKIP_DRAW) != 0) {
10884 mPrivateFlags &= ~SKIP_DRAW;
10885 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
10886 requestLayout = true;
10887 }
10888 } else {
10889 /* Remove the background */
10890 mBGDrawable = null;
10891
10892 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
10893 /*
10894 * This view ONLY drew the background before and we're removing
10895 * the background, so now it won't draw anything
10896 * (hence we SKIP_DRAW)
10897 */
10898 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
10899 mPrivateFlags |= SKIP_DRAW;
10900 }
10901
10902 /*
10903 * When the background is set, we try to apply its padding to this
10904 * View. When the background is removed, we don't touch this View's
10905 * padding. This is noted in the Javadocs. Hence, we don't need to
10906 * requestLayout(), the invalidate() below is sufficient.
10907 */
10908
10909 // The old background's minimum size could have affected this
10910 // View's layout, so let's requestLayout
10911 requestLayout = true;
10912 }
10913
Romain Guy8f1344f52009-05-15 16:03:59 -070010914 computeOpaqueFlags();
10915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010916 if (requestLayout) {
10917 requestLayout();
10918 }
10919
10920 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010921 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010922 }
10923
10924 /**
10925 * Gets the background drawable
10926 * @return The drawable used as the background for this view, if any.
10927 */
10928 public Drawable getBackground() {
10929 return mBGDrawable;
10930 }
10931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010932 /**
10933 * Sets the padding. The view may add on the space required to display
10934 * the scrollbars, depending on the style and visibility of the scrollbars.
10935 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
10936 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
10937 * from the values set in this call.
10938 *
10939 * @attr ref android.R.styleable#View_padding
10940 * @attr ref android.R.styleable#View_paddingBottom
10941 * @attr ref android.R.styleable#View_paddingLeft
10942 * @attr ref android.R.styleable#View_paddingRight
10943 * @attr ref android.R.styleable#View_paddingTop
10944 * @param left the left padding in pixels
10945 * @param top the top padding in pixels
10946 * @param right the right padding in pixels
10947 * @param bottom the bottom padding in pixels
10948 */
10949 public void setPadding(int left, int top, int right, int bottom) {
10950 boolean changed = false;
10951
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010952 mUserPaddingRelative = false;
10953
Adam Powell20232d02010-12-08 21:08:53 -080010954 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010955 mUserPaddingRight = right;
10956 mUserPaddingBottom = bottom;
10957
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010958 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070010959
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010960 // Common case is there are no scroll bars.
10961 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010962 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080010963 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010964 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080010965 switch (mVerticalScrollbarPosition) {
10966 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010967 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
10968 left += offset;
10969 } else {
10970 right += offset;
10971 }
10972 break;
Adam Powell20232d02010-12-08 21:08:53 -080010973 case SCROLLBAR_POSITION_RIGHT:
10974 right += offset;
10975 break;
10976 case SCROLLBAR_POSITION_LEFT:
10977 left += offset;
10978 break;
10979 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010980 }
Adam Powell20232d02010-12-08 21:08:53 -080010981 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010982 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
10983 ? 0 : getHorizontalScrollbarHeight();
10984 }
10985 }
Romain Guy8506ab42009-06-11 17:35:47 -070010986
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010987 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010988 changed = true;
10989 mPaddingLeft = left;
10990 }
10991 if (mPaddingTop != top) {
10992 changed = true;
10993 mPaddingTop = top;
10994 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010995 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010996 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010997 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010998 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010999 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011000 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011001 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011002 }
11003
11004 if (changed) {
11005 requestLayout();
11006 }
11007 }
11008
11009 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011010 * Sets the relative padding. The view may add on the space required to display
11011 * the scrollbars, depending on the style and visibility of the scrollbars.
11012 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11013 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11014 * from the values set in this call.
11015 *
11016 * @attr ref android.R.styleable#View_padding
11017 * @attr ref android.R.styleable#View_paddingBottom
11018 * @attr ref android.R.styleable#View_paddingStart
11019 * @attr ref android.R.styleable#View_paddingEnd
11020 * @attr ref android.R.styleable#View_paddingTop
11021 * @param start the start padding in pixels
11022 * @param top the top padding in pixels
11023 * @param end the end padding in pixels
11024 * @param bottom the bottom padding in pixels
11025 *
11026 * @hide
11027 */
11028 public void setPaddingRelative(int start, int top, int end, int bottom) {
11029 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011030
11031 mUserPaddingStart = start;
11032 mUserPaddingEnd = end;
11033
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011034 switch(getResolvedLayoutDirection()) {
11035 case LAYOUT_DIRECTION_RTL:
11036 setPadding(end, top, start, bottom);
11037 break;
11038 case LAYOUT_DIRECTION_LTR:
11039 default:
11040 setPadding(start, top, end, bottom);
11041 }
11042 }
11043
11044 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011045 * Returns the top padding of this view.
11046 *
11047 * @return the top padding in pixels
11048 */
11049 public int getPaddingTop() {
11050 return mPaddingTop;
11051 }
11052
11053 /**
11054 * Returns the bottom padding of this view. If there are inset and enabled
11055 * scrollbars, this value may include the space required to display the
11056 * scrollbars as well.
11057 *
11058 * @return the bottom padding in pixels
11059 */
11060 public int getPaddingBottom() {
11061 return mPaddingBottom;
11062 }
11063
11064 /**
11065 * Returns the left padding of this view. If there are inset and enabled
11066 * scrollbars, this value may include the space required to display the
11067 * scrollbars as well.
11068 *
11069 * @return the left padding in pixels
11070 */
11071 public int getPaddingLeft() {
11072 return mPaddingLeft;
11073 }
11074
11075 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011076 * Returns the start padding of this view. If there are inset and enabled
11077 * scrollbars, this value may include the space required to display the
11078 * scrollbars as well.
11079 *
11080 * @return the start padding in pixels
11081 *
11082 * @hide
11083 */
11084 public int getPaddingStart() {
11085 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11086 mPaddingRight : mPaddingLeft;
11087 }
11088
11089 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011090 * Returns the right padding of this view. If there are inset and enabled
11091 * scrollbars, this value may include the space required to display the
11092 * scrollbars as well.
11093 *
11094 * @return the right padding in pixels
11095 */
11096 public int getPaddingRight() {
11097 return mPaddingRight;
11098 }
11099
11100 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011101 * Returns the end padding of this view. If there are inset and enabled
11102 * scrollbars, this value may include the space required to display the
11103 * scrollbars as well.
11104 *
11105 * @return the end padding in pixels
11106 *
11107 * @hide
11108 */
11109 public int getPaddingEnd() {
11110 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11111 mPaddingLeft : mPaddingRight;
11112 }
11113
11114 /**
11115 * Return if the padding as been set thru relative values
11116 * {@link #setPaddingRelative(int, int, int, int)} or thru
11117 * @attr ref android.R.styleable#View_paddingStart or
11118 * @attr ref android.R.styleable#View_paddingEnd
11119 *
11120 * @return true if the padding is relative or false if it is not.
11121 *
11122 * @hide
11123 */
11124 public boolean isPaddingRelative() {
11125 return mUserPaddingRelative;
11126 }
11127
11128 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011129 * Changes the selection state of this view. A view can be selected or not.
11130 * Note that selection is not the same as focus. Views are typically
11131 * selected in the context of an AdapterView like ListView or GridView;
11132 * the selected view is the view that is highlighted.
11133 *
11134 * @param selected true if the view must be selected, false otherwise
11135 */
11136 public void setSelected(boolean selected) {
11137 if (((mPrivateFlags & SELECTED) != 0) != selected) {
11138 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070011139 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080011140 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011141 refreshDrawableState();
11142 dispatchSetSelected(selected);
11143 }
11144 }
11145
11146 /**
11147 * Dispatch setSelected to all of this View's children.
11148 *
11149 * @see #setSelected(boolean)
11150 *
11151 * @param selected The new selected state
11152 */
11153 protected void dispatchSetSelected(boolean selected) {
11154 }
11155
11156 /**
11157 * Indicates the selection state of this view.
11158 *
11159 * @return true if the view is selected, false otherwise
11160 */
11161 @ViewDebug.ExportedProperty
11162 public boolean isSelected() {
11163 return (mPrivateFlags & SELECTED) != 0;
11164 }
11165
11166 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011167 * Changes the activated state of this view. A view can be activated or not.
11168 * Note that activation is not the same as selection. Selection is
11169 * a transient property, representing the view (hierarchy) the user is
11170 * currently interacting with. Activation is a longer-term state that the
11171 * user can move views in and out of. For example, in a list view with
11172 * single or multiple selection enabled, the views in the current selection
11173 * set are activated. (Um, yeah, we are deeply sorry about the terminology
11174 * here.) The activated state is propagated down to children of the view it
11175 * is set on.
11176 *
11177 * @param activated true if the view must be activated, false otherwise
11178 */
11179 public void setActivated(boolean activated) {
11180 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
11181 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011182 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011183 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070011184 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011185 }
11186 }
11187
11188 /**
11189 * Dispatch setActivated to all of this View's children.
11190 *
11191 * @see #setActivated(boolean)
11192 *
11193 * @param activated The new activated state
11194 */
11195 protected void dispatchSetActivated(boolean activated) {
11196 }
11197
11198 /**
11199 * Indicates the activation state of this view.
11200 *
11201 * @return true if the view is activated, false otherwise
11202 */
11203 @ViewDebug.ExportedProperty
11204 public boolean isActivated() {
11205 return (mPrivateFlags & ACTIVATED) != 0;
11206 }
11207
11208 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011209 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
11210 * observer can be used to get notifications when global events, like
11211 * layout, happen.
11212 *
11213 * The returned ViewTreeObserver observer is not guaranteed to remain
11214 * valid for the lifetime of this View. If the caller of this method keeps
11215 * a long-lived reference to ViewTreeObserver, it should always check for
11216 * the return value of {@link ViewTreeObserver#isAlive()}.
11217 *
11218 * @return The ViewTreeObserver for this view's hierarchy.
11219 */
11220 public ViewTreeObserver getViewTreeObserver() {
11221 if (mAttachInfo != null) {
11222 return mAttachInfo.mTreeObserver;
11223 }
11224 if (mFloatingTreeObserver == null) {
11225 mFloatingTreeObserver = new ViewTreeObserver();
11226 }
11227 return mFloatingTreeObserver;
11228 }
11229
11230 /**
11231 * <p>Finds the topmost view in the current view hierarchy.</p>
11232 *
11233 * @return the topmost view containing this view
11234 */
11235 public View getRootView() {
11236 if (mAttachInfo != null) {
11237 final View v = mAttachInfo.mRootView;
11238 if (v != null) {
11239 return v;
11240 }
11241 }
Romain Guy8506ab42009-06-11 17:35:47 -070011242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011243 View parent = this;
11244
11245 while (parent.mParent != null && parent.mParent instanceof View) {
11246 parent = (View) parent.mParent;
11247 }
11248
11249 return parent;
11250 }
11251
11252 /**
11253 * <p>Computes the coordinates of this view on the screen. The argument
11254 * must be an array of two integers. After the method returns, the array
11255 * contains the x and y location in that order.</p>
11256 *
11257 * @param location an array of two integers in which to hold the coordinates
11258 */
11259 public void getLocationOnScreen(int[] location) {
11260 getLocationInWindow(location);
11261
11262 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070011263 if (info != null) {
11264 location[0] += info.mWindowLeft;
11265 location[1] += info.mWindowTop;
11266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011267 }
11268
11269 /**
11270 * <p>Computes the coordinates of this view in its window. The argument
11271 * must be an array of two integers. After the method returns, the array
11272 * contains the x and y location in that order.</p>
11273 *
11274 * @param location an array of two integers in which to hold the coordinates
11275 */
11276 public void getLocationInWindow(int[] location) {
11277 if (location == null || location.length < 2) {
11278 throw new IllegalArgumentException("location must be an array of "
11279 + "two integers");
11280 }
11281
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011282 location[0] = mLeft + (int) (mTranslationX + 0.5f);
11283 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011284
11285 ViewParent viewParent = mParent;
11286 while (viewParent instanceof View) {
11287 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011288 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
11289 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011290 viewParent = view.mParent;
11291 }
Romain Guy8506ab42009-06-11 17:35:47 -070011292
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011293 if (viewParent instanceof ViewAncestor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011294 // *cough*
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011295 final ViewAncestor vr = (ViewAncestor)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011296 location[1] -= vr.mCurScrollY;
11297 }
11298 }
11299
11300 /**
11301 * {@hide}
11302 * @param id the id of the view to be found
11303 * @return the view of the specified id, null if cannot be found
11304 */
11305 protected View findViewTraversal(int id) {
11306 if (id == mID) {
11307 return this;
11308 }
11309 return null;
11310 }
11311
11312 /**
11313 * {@hide}
11314 * @param tag the tag of the view to be found
11315 * @return the view of specified tag, null if cannot be found
11316 */
11317 protected View findViewWithTagTraversal(Object tag) {
11318 if (tag != null && tag.equals(mTag)) {
11319 return this;
11320 }
11321 return null;
11322 }
11323
11324 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011325 * {@hide}
11326 * @param predicate The predicate to evaluate.
11327 * @return The first view that matches the predicate or null.
11328 */
11329 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
11330 if (predicate.apply(this)) {
11331 return this;
11332 }
11333 return null;
11334 }
11335
11336 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011337 * Look for a child view with the given id. If this view has the given
11338 * id, return this view.
11339 *
11340 * @param id The id to search for.
11341 * @return The view that has the given id in the hierarchy or null
11342 */
11343 public final View findViewById(int id) {
11344 if (id < 0) {
11345 return null;
11346 }
11347 return findViewTraversal(id);
11348 }
11349
11350 /**
11351 * Look for a child view with the given tag. If this view has the given
11352 * tag, return this view.
11353 *
11354 * @param tag The tag to search for, using "tag.equals(getTag())".
11355 * @return The View that has the given tag in the hierarchy or null
11356 */
11357 public final View findViewWithTag(Object tag) {
11358 if (tag == null) {
11359 return null;
11360 }
11361 return findViewWithTagTraversal(tag);
11362 }
11363
11364 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011365 * {@hide}
11366 * Look for a child view that matches the specified predicate.
11367 * If this view matches the predicate, return this view.
11368 *
11369 * @param predicate The predicate to evaluate.
11370 * @return The first view that matches the predicate or null.
11371 */
11372 public final View findViewByPredicate(Predicate<View> predicate) {
11373 return findViewByPredicateTraversal(predicate);
11374 }
11375
11376 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011377 * Sets the identifier for this view. The identifier does not have to be
11378 * unique in this view's hierarchy. The identifier should be a positive
11379 * number.
11380 *
11381 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070011382 * @see #getId()
11383 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011384 *
11385 * @param id a number used to identify the view
11386 *
11387 * @attr ref android.R.styleable#View_id
11388 */
11389 public void setId(int id) {
11390 mID = id;
11391 }
11392
11393 /**
11394 * {@hide}
11395 *
11396 * @param isRoot true if the view belongs to the root namespace, false
11397 * otherwise
11398 */
11399 public void setIsRootNamespace(boolean isRoot) {
11400 if (isRoot) {
11401 mPrivateFlags |= IS_ROOT_NAMESPACE;
11402 } else {
11403 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
11404 }
11405 }
11406
11407 /**
11408 * {@hide}
11409 *
11410 * @return true if the view belongs to the root namespace, false otherwise
11411 */
11412 public boolean isRootNamespace() {
11413 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
11414 }
11415
11416 /**
11417 * Returns this view's identifier.
11418 *
11419 * @return a positive integer used to identify the view or {@link #NO_ID}
11420 * if the view has no ID
11421 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011422 * @see #setId(int)
11423 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011424 * @attr ref android.R.styleable#View_id
11425 */
11426 @ViewDebug.CapturedViewProperty
11427 public int getId() {
11428 return mID;
11429 }
11430
11431 /**
11432 * Returns this view's tag.
11433 *
11434 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070011435 *
11436 * @see #setTag(Object)
11437 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011438 */
11439 @ViewDebug.ExportedProperty
11440 public Object getTag() {
11441 return mTag;
11442 }
11443
11444 /**
11445 * Sets the tag associated with this view. A tag can be used to mark
11446 * a view in its hierarchy and does not have to be unique within the
11447 * hierarchy. Tags can also be used to store data within a view without
11448 * resorting to another data structure.
11449 *
11450 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070011451 *
11452 * @see #getTag()
11453 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011454 */
11455 public void setTag(final Object tag) {
11456 mTag = tag;
11457 }
11458
11459 /**
Romain Guyd90a3312009-05-06 14:54:28 -070011460 * Returns the tag associated with this view and the specified key.
11461 *
11462 * @param key The key identifying the tag
11463 *
11464 * @return the Object stored in this view as a tag
11465 *
11466 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070011467 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070011468 */
11469 public Object getTag(int key) {
11470 SparseArray<Object> tags = null;
11471 synchronized (sTagsLock) {
11472 if (sTags != null) {
11473 tags = sTags.get(this);
11474 }
11475 }
11476
11477 if (tags != null) return tags.get(key);
11478 return null;
11479 }
11480
11481 /**
11482 * Sets a tag associated with this view and a key. A tag can be used
11483 * to mark a view in its hierarchy and does not have to be unique within
11484 * the hierarchy. Tags can also be used to store data within a view
11485 * without resorting to another data structure.
11486 *
11487 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070011488 * application to ensure it is unique (see the <a
11489 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
11490 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070011491 * the Android framework or not associated with any package will cause
11492 * an {@link IllegalArgumentException} to be thrown.
11493 *
11494 * @param key The key identifying the tag
11495 * @param tag An Object to tag the view with
11496 *
11497 * @throws IllegalArgumentException If they specified key is not valid
11498 *
11499 * @see #setTag(Object)
11500 * @see #getTag(int)
11501 */
11502 public void setTag(int key, final Object tag) {
11503 // If the package id is 0x00 or 0x01, it's either an undefined package
11504 // or a framework id
11505 if ((key >>> 24) < 2) {
11506 throw new IllegalArgumentException("The key must be an application-specific "
11507 + "resource id.");
11508 }
11509
11510 setTagInternal(this, key, tag);
11511 }
11512
11513 /**
11514 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
11515 * framework id.
11516 *
11517 * @hide
11518 */
11519 public void setTagInternal(int key, Object tag) {
11520 if ((key >>> 24) != 0x1) {
11521 throw new IllegalArgumentException("The key must be a framework-specific "
11522 + "resource id.");
11523 }
11524
Romain Guy8506ab42009-06-11 17:35:47 -070011525 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070011526 }
11527
11528 private static void setTagInternal(View view, int key, Object tag) {
11529 SparseArray<Object> tags = null;
11530 synchronized (sTagsLock) {
11531 if (sTags == null) {
11532 sTags = new WeakHashMap<View, SparseArray<Object>>();
11533 } else {
11534 tags = sTags.get(view);
11535 }
11536 }
11537
11538 if (tags == null) {
11539 tags = new SparseArray<Object>(2);
11540 synchronized (sTagsLock) {
11541 sTags.put(view, tags);
11542 }
11543 }
11544
11545 tags.put(key, tag);
11546 }
11547
11548 /**
Romain Guy13922e02009-05-12 17:56:14 -070011549 * @param consistency The type of consistency. See ViewDebug for more information.
11550 *
11551 * @hide
11552 */
11553 protected boolean dispatchConsistencyCheck(int consistency) {
11554 return onConsistencyCheck(consistency);
11555 }
11556
11557 /**
11558 * Method that subclasses should implement to check their consistency. The type of
11559 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070011560 *
Romain Guy13922e02009-05-12 17:56:14 -070011561 * @param consistency The type of consistency. See ViewDebug for more information.
11562 *
11563 * @throws IllegalStateException if the view is in an inconsistent state.
11564 *
11565 * @hide
11566 */
11567 protected boolean onConsistencyCheck(int consistency) {
11568 boolean result = true;
11569
11570 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
11571 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
11572
11573 if (checkLayout) {
11574 if (getParent() == null) {
11575 result = false;
11576 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11577 "View " + this + " does not have a parent.");
11578 }
11579
11580 if (mAttachInfo == null) {
11581 result = false;
11582 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11583 "View " + this + " is not attached to a window.");
11584 }
11585 }
11586
11587 if (checkDrawing) {
11588 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
11589 // from their draw() method
11590
11591 if ((mPrivateFlags & DRAWN) != DRAWN &&
11592 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
11593 result = false;
11594 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11595 "View " + this + " was invalidated but its drawing cache is valid.");
11596 }
11597 }
11598
11599 return result;
11600 }
11601
11602 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011603 * Prints information about this view in the log output, with the tag
11604 * {@link #VIEW_LOG_TAG}.
11605 *
11606 * @hide
11607 */
11608 public void debug() {
11609 debug(0);
11610 }
11611
11612 /**
11613 * Prints information about this view in the log output, with the tag
11614 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
11615 * indentation defined by the <code>depth</code>.
11616 *
11617 * @param depth the indentation level
11618 *
11619 * @hide
11620 */
11621 protected void debug(int depth) {
11622 String output = debugIndent(depth - 1);
11623
11624 output += "+ " + this;
11625 int id = getId();
11626 if (id != -1) {
11627 output += " (id=" + id + ")";
11628 }
11629 Object tag = getTag();
11630 if (tag != null) {
11631 output += " (tag=" + tag + ")";
11632 }
11633 Log.d(VIEW_LOG_TAG, output);
11634
11635 if ((mPrivateFlags & FOCUSED) != 0) {
11636 output = debugIndent(depth) + " FOCUSED";
11637 Log.d(VIEW_LOG_TAG, output);
11638 }
11639
11640 output = debugIndent(depth);
11641 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
11642 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
11643 + "} ";
11644 Log.d(VIEW_LOG_TAG, output);
11645
11646 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
11647 || mPaddingBottom != 0) {
11648 output = debugIndent(depth);
11649 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
11650 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
11651 Log.d(VIEW_LOG_TAG, output);
11652 }
11653
11654 output = debugIndent(depth);
11655 output += "mMeasureWidth=" + mMeasuredWidth +
11656 " mMeasureHeight=" + mMeasuredHeight;
11657 Log.d(VIEW_LOG_TAG, output);
11658
11659 output = debugIndent(depth);
11660 if (mLayoutParams == null) {
11661 output += "BAD! no layout params";
11662 } else {
11663 output = mLayoutParams.debug(output);
11664 }
11665 Log.d(VIEW_LOG_TAG, output);
11666
11667 output = debugIndent(depth);
11668 output += "flags={";
11669 output += View.printFlags(mViewFlags);
11670 output += "}";
11671 Log.d(VIEW_LOG_TAG, output);
11672
11673 output = debugIndent(depth);
11674 output += "privateFlags={";
11675 output += View.printPrivateFlags(mPrivateFlags);
11676 output += "}";
11677 Log.d(VIEW_LOG_TAG, output);
11678 }
11679
11680 /**
11681 * Creates an string of whitespaces used for indentation.
11682 *
11683 * @param depth the indentation level
11684 * @return a String containing (depth * 2 + 3) * 2 white spaces
11685 *
11686 * @hide
11687 */
11688 protected static String debugIndent(int depth) {
11689 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
11690 for (int i = 0; i < (depth * 2) + 3; i++) {
11691 spaces.append(' ').append(' ');
11692 }
11693 return spaces.toString();
11694 }
11695
11696 /**
11697 * <p>Return the offset of the widget's text baseline from the widget's top
11698 * boundary. If this widget does not support baseline alignment, this
11699 * method returns -1. </p>
11700 *
11701 * @return the offset of the baseline within the widget's bounds or -1
11702 * if baseline alignment is not supported
11703 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011704 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011705 public int getBaseline() {
11706 return -1;
11707 }
11708
11709 /**
11710 * Call this when something has changed which has invalidated the
11711 * layout of this view. This will schedule a layout pass of the view
11712 * tree.
11713 */
11714 public void requestLayout() {
11715 if (ViewDebug.TRACE_HIERARCHY) {
11716 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
11717 }
11718
11719 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011720 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011721
11722 if (mParent != null && !mParent.isLayoutRequested()) {
11723 mParent.requestLayout();
11724 }
11725 }
11726
11727 /**
11728 * Forces this view to be laid out during the next layout pass.
11729 * This method does not call requestLayout() or forceLayout()
11730 * on the parent.
11731 */
11732 public void forceLayout() {
11733 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011734 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011735 }
11736
11737 /**
11738 * <p>
11739 * This is called to find out how big a view should be. The parent
11740 * supplies constraint information in the width and height parameters.
11741 * </p>
11742 *
11743 * <p>
11744 * The actual mesurement work of a view is performed in
11745 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
11746 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
11747 * </p>
11748 *
11749 *
11750 * @param widthMeasureSpec Horizontal space requirements as imposed by the
11751 * parent
11752 * @param heightMeasureSpec Vertical space requirements as imposed by the
11753 * parent
11754 *
11755 * @see #onMeasure(int, int)
11756 */
11757 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
11758 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
11759 widthMeasureSpec != mOldWidthMeasureSpec ||
11760 heightMeasureSpec != mOldHeightMeasureSpec) {
11761
11762 // first clears the measured dimension flag
11763 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
11764
11765 if (ViewDebug.TRACE_HIERARCHY) {
11766 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
11767 }
11768
11769 // measure ourselves, this should set the measured dimension flag back
11770 onMeasure(widthMeasureSpec, heightMeasureSpec);
11771
11772 // flag not set, setMeasuredDimension() was not invoked, we raise
11773 // an exception to warn the developer
11774 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
11775 throw new IllegalStateException("onMeasure() did not set the"
11776 + " measured dimension by calling"
11777 + " setMeasuredDimension()");
11778 }
11779
11780 mPrivateFlags |= LAYOUT_REQUIRED;
11781 }
11782
11783 mOldWidthMeasureSpec = widthMeasureSpec;
11784 mOldHeightMeasureSpec = heightMeasureSpec;
11785 }
11786
11787 /**
11788 * <p>
11789 * Measure the view and its content to determine the measured width and the
11790 * measured height. This method is invoked by {@link #measure(int, int)} and
11791 * should be overriden by subclasses to provide accurate and efficient
11792 * measurement of their contents.
11793 * </p>
11794 *
11795 * <p>
11796 * <strong>CONTRACT:</strong> When overriding this method, you
11797 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
11798 * measured width and height of this view. Failure to do so will trigger an
11799 * <code>IllegalStateException</code>, thrown by
11800 * {@link #measure(int, int)}. Calling the superclass'
11801 * {@link #onMeasure(int, int)} is a valid use.
11802 * </p>
11803 *
11804 * <p>
11805 * The base class implementation of measure defaults to the background size,
11806 * unless a larger size is allowed by the MeasureSpec. Subclasses should
11807 * override {@link #onMeasure(int, int)} to provide better measurements of
11808 * their content.
11809 * </p>
11810 *
11811 * <p>
11812 * If this method is overridden, it is the subclass's responsibility to make
11813 * sure the measured height and width are at least the view's minimum height
11814 * and width ({@link #getSuggestedMinimumHeight()} and
11815 * {@link #getSuggestedMinimumWidth()}).
11816 * </p>
11817 *
11818 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
11819 * The requirements are encoded with
11820 * {@link android.view.View.MeasureSpec}.
11821 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
11822 * The requirements are encoded with
11823 * {@link android.view.View.MeasureSpec}.
11824 *
11825 * @see #getMeasuredWidth()
11826 * @see #getMeasuredHeight()
11827 * @see #setMeasuredDimension(int, int)
11828 * @see #getSuggestedMinimumHeight()
11829 * @see #getSuggestedMinimumWidth()
11830 * @see android.view.View.MeasureSpec#getMode(int)
11831 * @see android.view.View.MeasureSpec#getSize(int)
11832 */
11833 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
11834 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
11835 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
11836 }
11837
11838 /**
11839 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
11840 * measured width and measured height. Failing to do so will trigger an
11841 * exception at measurement time.</p>
11842 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080011843 * @param measuredWidth The measured width of this view. May be a complex
11844 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11845 * {@link #MEASURED_STATE_TOO_SMALL}.
11846 * @param measuredHeight The measured height of this view. May be a complex
11847 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11848 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011849 */
11850 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
11851 mMeasuredWidth = measuredWidth;
11852 mMeasuredHeight = measuredHeight;
11853
11854 mPrivateFlags |= MEASURED_DIMENSION_SET;
11855 }
11856
11857 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080011858 * Merge two states as returned by {@link #getMeasuredState()}.
11859 * @param curState The current state as returned from a view or the result
11860 * of combining multiple views.
11861 * @param newState The new view state to combine.
11862 * @return Returns a new integer reflecting the combination of the two
11863 * states.
11864 */
11865 public static int combineMeasuredStates(int curState, int newState) {
11866 return curState | newState;
11867 }
11868
11869 /**
11870 * Version of {@link #resolveSizeAndState(int, int, int)}
11871 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
11872 */
11873 public static int resolveSize(int size, int measureSpec) {
11874 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
11875 }
11876
11877 /**
11878 * Utility to reconcile a desired size and state, with constraints imposed
11879 * by a MeasureSpec. Will take the desired size, unless a different size
11880 * is imposed by the constraints. The returned value is a compound integer,
11881 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
11882 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
11883 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011884 *
11885 * @param size How big the view wants to be
11886 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080011887 * @return Size information bit mask as defined by
11888 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011889 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080011890 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011891 int result = size;
11892 int specMode = MeasureSpec.getMode(measureSpec);
11893 int specSize = MeasureSpec.getSize(measureSpec);
11894 switch (specMode) {
11895 case MeasureSpec.UNSPECIFIED:
11896 result = size;
11897 break;
11898 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080011899 if (specSize < size) {
11900 result = specSize | MEASURED_STATE_TOO_SMALL;
11901 } else {
11902 result = size;
11903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011904 break;
11905 case MeasureSpec.EXACTLY:
11906 result = specSize;
11907 break;
11908 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080011909 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011910 }
11911
11912 /**
11913 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070011914 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011915 * by the MeasureSpec.
11916 *
11917 * @param size Default size for this view
11918 * @param measureSpec Constraints imposed by the parent
11919 * @return The size this view should be.
11920 */
11921 public static int getDefaultSize(int size, int measureSpec) {
11922 int result = size;
11923 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070011924 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011925
11926 switch (specMode) {
11927 case MeasureSpec.UNSPECIFIED:
11928 result = size;
11929 break;
11930 case MeasureSpec.AT_MOST:
11931 case MeasureSpec.EXACTLY:
11932 result = specSize;
11933 break;
11934 }
11935 return result;
11936 }
11937
11938 /**
11939 * Returns the suggested minimum height that the view should use. This
11940 * returns the maximum of the view's minimum height
11941 * and the background's minimum height
11942 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
11943 * <p>
11944 * When being used in {@link #onMeasure(int, int)}, the caller should still
11945 * ensure the returned height is within the requirements of the parent.
11946 *
11947 * @return The suggested minimum height of the view.
11948 */
11949 protected int getSuggestedMinimumHeight() {
11950 int suggestedMinHeight = mMinHeight;
11951
11952 if (mBGDrawable != null) {
11953 final int bgMinHeight = mBGDrawable.getMinimumHeight();
11954 if (suggestedMinHeight < bgMinHeight) {
11955 suggestedMinHeight = bgMinHeight;
11956 }
11957 }
11958
11959 return suggestedMinHeight;
11960 }
11961
11962 /**
11963 * Returns the suggested minimum width that the view should use. This
11964 * returns the maximum of the view's minimum width)
11965 * and the background's minimum width
11966 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
11967 * <p>
11968 * When being used in {@link #onMeasure(int, int)}, the caller should still
11969 * ensure the returned width is within the requirements of the parent.
11970 *
11971 * @return The suggested minimum width of the view.
11972 */
11973 protected int getSuggestedMinimumWidth() {
11974 int suggestedMinWidth = mMinWidth;
11975
11976 if (mBGDrawable != null) {
11977 final int bgMinWidth = mBGDrawable.getMinimumWidth();
11978 if (suggestedMinWidth < bgMinWidth) {
11979 suggestedMinWidth = bgMinWidth;
11980 }
11981 }
11982
11983 return suggestedMinWidth;
11984 }
11985
11986 /**
11987 * Sets the minimum height of the view. It is not guaranteed the view will
11988 * be able to achieve this minimum height (for example, if its parent layout
11989 * constrains it with less available height).
11990 *
11991 * @param minHeight The minimum height the view will try to be.
11992 */
11993 public void setMinimumHeight(int minHeight) {
11994 mMinHeight = minHeight;
11995 }
11996
11997 /**
11998 * Sets the minimum width of the view. It is not guaranteed the view will
11999 * be able to achieve this minimum width (for example, if its parent layout
12000 * constrains it with less available width).
12001 *
12002 * @param minWidth The minimum width the view will try to be.
12003 */
12004 public void setMinimumWidth(int minWidth) {
12005 mMinWidth = minWidth;
12006 }
12007
12008 /**
12009 * Get the animation currently associated with this view.
12010 *
12011 * @return The animation that is currently playing or
12012 * scheduled to play for this view.
12013 */
12014 public Animation getAnimation() {
12015 return mCurrentAnimation;
12016 }
12017
12018 /**
12019 * Start the specified animation now.
12020 *
12021 * @param animation the animation to start now
12022 */
12023 public void startAnimation(Animation animation) {
12024 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
12025 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012026 invalidateParentCaches();
12027 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012028 }
12029
12030 /**
12031 * Cancels any animations for this view.
12032 */
12033 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080012034 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080012035 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080012036 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012037 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012038 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012039 }
12040
12041 /**
12042 * Sets the next animation to play for this view.
12043 * If you want the animation to play immediately, use
12044 * startAnimation. This method provides allows fine-grained
12045 * control over the start time and invalidation, but you
12046 * must make sure that 1) the animation has a start time set, and
12047 * 2) the view will be invalidated when the animation is supposed to
12048 * start.
12049 *
12050 * @param animation The next animation, or null.
12051 */
12052 public void setAnimation(Animation animation) {
12053 mCurrentAnimation = animation;
12054 if (animation != null) {
12055 animation.reset();
12056 }
12057 }
12058
12059 /**
12060 * Invoked by a parent ViewGroup to notify the start of the animation
12061 * currently associated with this view. If you override this method,
12062 * always call super.onAnimationStart();
12063 *
12064 * @see #setAnimation(android.view.animation.Animation)
12065 * @see #getAnimation()
12066 */
12067 protected void onAnimationStart() {
12068 mPrivateFlags |= ANIMATION_STARTED;
12069 }
12070
12071 /**
12072 * Invoked by a parent ViewGroup to notify the end of the animation
12073 * currently associated with this view. If you override this method,
12074 * always call super.onAnimationEnd();
12075 *
12076 * @see #setAnimation(android.view.animation.Animation)
12077 * @see #getAnimation()
12078 */
12079 protected void onAnimationEnd() {
12080 mPrivateFlags &= ~ANIMATION_STARTED;
12081 }
12082
12083 /**
12084 * Invoked if there is a Transform that involves alpha. Subclass that can
12085 * draw themselves with the specified alpha should return true, and then
12086 * respect that alpha when their onDraw() is called. If this returns false
12087 * then the view may be redirected to draw into an offscreen buffer to
12088 * fulfill the request, which will look fine, but may be slower than if the
12089 * subclass handles it internally. The default implementation returns false.
12090 *
12091 * @param alpha The alpha (0..255) to apply to the view's drawing
12092 * @return true if the view can draw with the specified alpha.
12093 */
12094 protected boolean onSetAlpha(int alpha) {
12095 return false;
12096 }
12097
12098 /**
12099 * This is used by the RootView to perform an optimization when
12100 * the view hierarchy contains one or several SurfaceView.
12101 * SurfaceView is always considered transparent, but its children are not,
12102 * therefore all View objects remove themselves from the global transparent
12103 * region (passed as a parameter to this function).
12104 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012105 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012106 *
12107 * @return Returns true if the effective visibility of the view at this
12108 * point is opaque, regardless of the transparent region; returns false
12109 * if it is possible for underlying windows to be seen behind the view.
12110 *
12111 * {@hide}
12112 */
12113 public boolean gatherTransparentRegion(Region region) {
12114 final AttachInfo attachInfo = mAttachInfo;
12115 if (region != null && attachInfo != null) {
12116 final int pflags = mPrivateFlags;
12117 if ((pflags & SKIP_DRAW) == 0) {
12118 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
12119 // remove it from the transparent region.
12120 final int[] location = attachInfo.mTransparentLocation;
12121 getLocationInWindow(location);
12122 region.op(location[0], location[1], location[0] + mRight - mLeft,
12123 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
12124 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
12125 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
12126 // exists, so we remove the background drawable's non-transparent
12127 // parts from this transparent region.
12128 applyDrawableToTransparentRegion(mBGDrawable, region);
12129 }
12130 }
12131 return true;
12132 }
12133
12134 /**
12135 * Play a sound effect for this view.
12136 *
12137 * <p>The framework will play sound effects for some built in actions, such as
12138 * clicking, but you may wish to play these effects in your widget,
12139 * for instance, for internal navigation.
12140 *
12141 * <p>The sound effect will only be played if sound effects are enabled by the user, and
12142 * {@link #isSoundEffectsEnabled()} is true.
12143 *
12144 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
12145 */
12146 public void playSoundEffect(int soundConstant) {
12147 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
12148 return;
12149 }
12150 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
12151 }
12152
12153 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012154 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012155 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012156 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012157 *
12158 * <p>The framework will provide haptic feedback for some built in actions,
12159 * such as long presses, but you may wish to provide feedback for your
12160 * own widget.
12161 *
12162 * <p>The feedback will only be performed if
12163 * {@link #isHapticFeedbackEnabled()} is true.
12164 *
12165 * @param feedbackConstant One of the constants defined in
12166 * {@link HapticFeedbackConstants}
12167 */
12168 public boolean performHapticFeedback(int feedbackConstant) {
12169 return performHapticFeedback(feedbackConstant, 0);
12170 }
12171
12172 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012173 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012174 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012175 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012176 *
12177 * @param feedbackConstant One of the constants defined in
12178 * {@link HapticFeedbackConstants}
12179 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
12180 */
12181 public boolean performHapticFeedback(int feedbackConstant, int flags) {
12182 if (mAttachInfo == null) {
12183 return false;
12184 }
Romain Guyf607bdc2010-09-10 19:20:06 -070012185 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070012186 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012187 && !isHapticFeedbackEnabled()) {
12188 return false;
12189 }
Romain Guy812ccbe2010-06-01 14:07:24 -070012190 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
12191 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012192 }
12193
12194 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012195 * Request that the visibility of the status bar be changed.
Scott Mainec6331b2011-05-24 16:55:56 -070012196 * @param visibility Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080012197 */
12198 public void setSystemUiVisibility(int visibility) {
12199 if (visibility != mSystemUiVisibility) {
12200 mSystemUiVisibility = visibility;
12201 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12202 mParent.recomputeViewAttributes(this);
12203 }
12204 }
12205 }
12206
12207 /**
12208 * Returns the status bar visibility that this view has requested.
Scott Mainec6331b2011-05-24 16:55:56 -070012209 * @return Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080012210 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080012211 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080012212 return mSystemUiVisibility;
12213 }
12214
Scott Mainec6331b2011-05-24 16:55:56 -070012215 /**
12216 * Set a listener to receive callbacks when the visibility of the system bar changes.
12217 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
12218 */
Joe Onorato664644d2011-01-23 17:53:23 -080012219 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
12220 mOnSystemUiVisibilityChangeListener = l;
12221 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12222 mParent.recomputeViewAttributes(this);
12223 }
12224 }
12225
12226 /**
12227 */
12228 public void dispatchSystemUiVisibilityChanged(int visibility) {
12229 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080012230 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080012231 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080012232 }
12233 }
12234
12235 /**
Joe Malin32736f02011-01-19 16:14:20 -080012236 * Creates an image that the system displays during the drag and drop
12237 * operation. This is called a &quot;drag shadow&quot;. The default implementation
12238 * for a DragShadowBuilder based on a View returns an image that has exactly the same
12239 * appearance as the given View. The default also positions the center of the drag shadow
12240 * directly under the touch point. If no View is provided (the constructor with no parameters
12241 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
12242 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
12243 * default is an invisible drag shadow.
12244 * <p>
12245 * You are not required to use the View you provide to the constructor as the basis of the
12246 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
12247 * anything you want as the drag shadow.
12248 * </p>
12249 * <p>
12250 * You pass a DragShadowBuilder object to the system when you start the drag. The system
12251 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
12252 * size and position of the drag shadow. It uses this data to construct a
12253 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
12254 * so that your application can draw the shadow image in the Canvas.
12255 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012256 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012257 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070012258 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070012259
12260 /**
Joe Malin32736f02011-01-19 16:14:20 -080012261 * Constructs a shadow image builder based on a View. By default, the resulting drag
12262 * shadow will have the same appearance and dimensions as the View, with the touch point
12263 * over the center of the View.
12264 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070012265 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012266 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070012267 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070012268 }
12269
Christopher Tate17ed60c2011-01-18 12:50:26 -080012270 /**
12271 * Construct a shadow builder object with no associated View. This
12272 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
12273 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
12274 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080012275 * reference to any View object. If they are not overridden, then the result is an
12276 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080012277 */
12278 public DragShadowBuilder() {
12279 mView = new WeakReference<View>(null);
12280 }
12281
12282 /**
12283 * Returns the View object that had been passed to the
12284 * {@link #View.DragShadowBuilder(View)}
12285 * constructor. If that View parameter was {@code null} or if the
12286 * {@link #View.DragShadowBuilder()}
12287 * constructor was used to instantiate the builder object, this method will return
12288 * null.
12289 *
12290 * @return The View object associate with this builder object.
12291 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070012292 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070012293 final public View getView() {
12294 return mView.get();
12295 }
12296
Christopher Tate2c095f32010-10-04 14:13:40 -070012297 /**
Joe Malin32736f02011-01-19 16:14:20 -080012298 * Provides the metrics for the shadow image. These include the dimensions of
12299 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070012300 * be centered under the touch location while dragging.
12301 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012302 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080012303 * same as the dimensions of the View itself and centers the shadow under
12304 * the touch point.
12305 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012306 *
Joe Malin32736f02011-01-19 16:14:20 -080012307 * @param shadowSize A {@link android.graphics.Point} containing the width and height
12308 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
12309 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
12310 * image.
12311 *
12312 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
12313 * shadow image that should be underneath the touch point during the drag and drop
12314 * operation. Your application must set {@link android.graphics.Point#x} to the
12315 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070012316 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012317 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070012318 final View view = mView.get();
12319 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012320 shadowSize.set(view.getWidth(), view.getHeight());
12321 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070012322 } else {
12323 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
12324 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012325 }
12326
12327 /**
Joe Malin32736f02011-01-19 16:14:20 -080012328 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
12329 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012330 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070012331 *
Joe Malin32736f02011-01-19 16:14:20 -080012332 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070012333 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012334 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070012335 final View view = mView.get();
12336 if (view != null) {
12337 view.draw(canvas);
12338 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012339 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070012340 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012341 }
12342 }
12343
12344 /**
Joe Malin32736f02011-01-19 16:14:20 -080012345 * Starts a drag and drop operation. When your application calls this method, it passes a
12346 * {@link android.view.View.DragShadowBuilder} object to the system. The
12347 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
12348 * to get metrics for the drag shadow, and then calls the object's
12349 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
12350 * <p>
12351 * Once the system has the drag shadow, it begins the drag and drop operation by sending
12352 * drag events to all the View objects in your application that are currently visible. It does
12353 * this either by calling the View object's drag listener (an implementation of
12354 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
12355 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
12356 * Both are passed a {@link android.view.DragEvent} object that has a
12357 * {@link android.view.DragEvent#getAction()} value of
12358 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
12359 * </p>
12360 * <p>
12361 * Your application can invoke startDrag() on any attached View object. The View object does not
12362 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
12363 * be related to the View the user selected for dragging.
12364 * </p>
12365 * @param data A {@link android.content.ClipData} object pointing to the data to be
12366 * transferred by the drag and drop operation.
12367 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
12368 * drag shadow.
12369 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
12370 * drop operation. This Object is put into every DragEvent object sent by the system during the
12371 * current drag.
12372 * <p>
12373 * myLocalState is a lightweight mechanism for the sending information from the dragged View
12374 * to the target Views. For example, it can contain flags that differentiate between a
12375 * a copy operation and a move operation.
12376 * </p>
12377 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
12378 * so the parameter should be set to 0.
12379 * @return {@code true} if the method completes successfully, or
12380 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
12381 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070012382 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012383 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012384 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070012385 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012386 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070012387 }
12388 boolean okay = false;
12389
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012390 Point shadowSize = new Point();
12391 Point shadowTouchPoint = new Point();
12392 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070012393
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012394 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
12395 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
12396 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070012397 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012398
Chris Tatea32dcf72010-10-14 12:13:50 -070012399 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012400 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
12401 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070012402 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012403 Surface surface = new Surface();
12404 try {
12405 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012406 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070012407 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070012408 + " surface=" + surface);
12409 if (token != null) {
12410 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070012411 try {
Chris Tate6b391282010-10-14 15:48:59 -070012412 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012413 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070012414 } finally {
12415 surface.unlockCanvasAndPost(canvas);
12416 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012417
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012418 final ViewAncestor root = getViewAncestor();
Christopher Tate407b4e92010-11-30 17:14:08 -080012419
12420 // Cache the local state object for delivery with DragEvents
12421 root.setLocalDragState(myLocalState);
12422
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012423 // repurpose 'shadowSize' for the last touch point
12424 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070012425
Christopher Tatea53146c2010-09-07 11:57:52 -070012426 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012427 shadowSize.x, shadowSize.y,
12428 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070012429 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070012430 }
12431 } catch (Exception e) {
12432 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
12433 surface.destroy();
12434 }
12435
12436 return okay;
12437 }
12438
Christopher Tatea53146c2010-09-07 11:57:52 -070012439 /**
Joe Malin32736f02011-01-19 16:14:20 -080012440 * Handles drag events sent by the system following a call to
12441 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
12442 *<p>
12443 * When the system calls this method, it passes a
12444 * {@link android.view.DragEvent} object. A call to
12445 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
12446 * in DragEvent. The method uses these to determine what is happening in the drag and drop
12447 * operation.
12448 * @param event The {@link android.view.DragEvent} sent by the system.
12449 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
12450 * in DragEvent, indicating the type of drag event represented by this object.
12451 * @return {@code true} if the method was successful, otherwise {@code false}.
12452 * <p>
12453 * The method should return {@code true} in response to an action type of
12454 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
12455 * operation.
12456 * </p>
12457 * <p>
12458 * The method should also return {@code true} in response to an action type of
12459 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
12460 * {@code false} if it didn't.
12461 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012462 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070012463 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070012464 return false;
12465 }
12466
12467 /**
Joe Malin32736f02011-01-19 16:14:20 -080012468 * Detects if this View is enabled and has a drag event listener.
12469 * If both are true, then it calls the drag event listener with the
12470 * {@link android.view.DragEvent} it received. If the drag event listener returns
12471 * {@code true}, then dispatchDragEvent() returns {@code true}.
12472 * <p>
12473 * For all other cases, the method calls the
12474 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
12475 * method and returns its result.
12476 * </p>
12477 * <p>
12478 * This ensures that a drag event is always consumed, even if the View does not have a drag
12479 * event listener. However, if the View has a listener and the listener returns true, then
12480 * onDragEvent() is not called.
12481 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012482 */
12483 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080012484 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070012485 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
12486 && mOnDragListener.onDrag(this, event)) {
12487 return true;
12488 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012489 return onDragEvent(event);
12490 }
12491
Christopher Tate3d4bf172011-03-28 16:16:46 -070012492 boolean canAcceptDrag() {
12493 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
12494 }
12495
Christopher Tatea53146c2010-09-07 11:57:52 -070012496 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070012497 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
12498 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070012499 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070012500 */
12501 public void onCloseSystemDialogs(String reason) {
12502 }
Joe Malin32736f02011-01-19 16:14:20 -080012503
Dianne Hackbornffa42482009-09-23 22:20:11 -070012504 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012505 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070012506 * update a Region being computed for
12507 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012508 * that any non-transparent parts of the Drawable are removed from the
12509 * given transparent region.
12510 *
12511 * @param dr The Drawable whose transparency is to be applied to the region.
12512 * @param region A Region holding the current transparency information,
12513 * where any parts of the region that are set are considered to be
12514 * transparent. On return, this region will be modified to have the
12515 * transparency information reduced by the corresponding parts of the
12516 * Drawable that are not transparent.
12517 * {@hide}
12518 */
12519 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
12520 if (DBG) {
12521 Log.i("View", "Getting transparent region for: " + this);
12522 }
12523 final Region r = dr.getTransparentRegion();
12524 final Rect db = dr.getBounds();
12525 final AttachInfo attachInfo = mAttachInfo;
12526 if (r != null && attachInfo != null) {
12527 final int w = getRight()-getLeft();
12528 final int h = getBottom()-getTop();
12529 if (db.left > 0) {
12530 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
12531 r.op(0, 0, db.left, h, Region.Op.UNION);
12532 }
12533 if (db.right < w) {
12534 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
12535 r.op(db.right, 0, w, h, Region.Op.UNION);
12536 }
12537 if (db.top > 0) {
12538 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
12539 r.op(0, 0, w, db.top, Region.Op.UNION);
12540 }
12541 if (db.bottom < h) {
12542 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
12543 r.op(0, db.bottom, w, h, Region.Op.UNION);
12544 }
12545 final int[] location = attachInfo.mTransparentLocation;
12546 getLocationInWindow(location);
12547 r.translate(location[0], location[1]);
12548 region.op(r, Region.Op.INTERSECT);
12549 } else {
12550 region.op(db, Region.Op.DIFFERENCE);
12551 }
12552 }
12553
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012554 private void checkForLongClick(int delayOffset) {
12555 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
12556 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012557
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012558 if (mPendingCheckForLongPress == null) {
12559 mPendingCheckForLongPress = new CheckForLongPress();
12560 }
12561 mPendingCheckForLongPress.rememberWindowAttachCount();
12562 postDelayed(mPendingCheckForLongPress,
12563 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012565 }
12566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012567 /**
12568 * Inflate a view from an XML resource. This convenience method wraps the {@link
12569 * LayoutInflater} class, which provides a full range of options for view inflation.
12570 *
12571 * @param context The Context object for your activity or application.
12572 * @param resource The resource ID to inflate
12573 * @param root A view group that will be the parent. Used to properly inflate the
12574 * layout_* parameters.
12575 * @see LayoutInflater
12576 */
12577 public static View inflate(Context context, int resource, ViewGroup root) {
12578 LayoutInflater factory = LayoutInflater.from(context);
12579 return factory.inflate(resource, root);
12580 }
Romain Guy33e72ae2010-07-17 12:40:29 -070012581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012582 /**
Adam Powell637d3372010-08-25 14:37:03 -070012583 * Scroll the view with standard behavior for scrolling beyond the normal
12584 * content boundaries. Views that call this method should override
12585 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
12586 * results of an over-scroll operation.
12587 *
12588 * Views can use this method to handle any touch or fling-based scrolling.
12589 *
12590 * @param deltaX Change in X in pixels
12591 * @param deltaY Change in Y in pixels
12592 * @param scrollX Current X scroll value in pixels before applying deltaX
12593 * @param scrollY Current Y scroll value in pixels before applying deltaY
12594 * @param scrollRangeX Maximum content scroll range along the X axis
12595 * @param scrollRangeY Maximum content scroll range along the Y axis
12596 * @param maxOverScrollX Number of pixels to overscroll by in either direction
12597 * along the X axis.
12598 * @param maxOverScrollY Number of pixels to overscroll by in either direction
12599 * along the Y axis.
12600 * @param isTouchEvent true if this scroll operation is the result of a touch event.
12601 * @return true if scrolling was clamped to an over-scroll boundary along either
12602 * axis, false otherwise.
12603 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012604 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070012605 protected boolean overScrollBy(int deltaX, int deltaY,
12606 int scrollX, int scrollY,
12607 int scrollRangeX, int scrollRangeY,
12608 int maxOverScrollX, int maxOverScrollY,
12609 boolean isTouchEvent) {
12610 final int overScrollMode = mOverScrollMode;
12611 final boolean canScrollHorizontal =
12612 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
12613 final boolean canScrollVertical =
12614 computeVerticalScrollRange() > computeVerticalScrollExtent();
12615 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
12616 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
12617 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
12618 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
12619
12620 int newScrollX = scrollX + deltaX;
12621 if (!overScrollHorizontal) {
12622 maxOverScrollX = 0;
12623 }
12624
12625 int newScrollY = scrollY + deltaY;
12626 if (!overScrollVertical) {
12627 maxOverScrollY = 0;
12628 }
12629
12630 // Clamp values if at the limits and record
12631 final int left = -maxOverScrollX;
12632 final int right = maxOverScrollX + scrollRangeX;
12633 final int top = -maxOverScrollY;
12634 final int bottom = maxOverScrollY + scrollRangeY;
12635
12636 boolean clampedX = false;
12637 if (newScrollX > right) {
12638 newScrollX = right;
12639 clampedX = true;
12640 } else if (newScrollX < left) {
12641 newScrollX = left;
12642 clampedX = true;
12643 }
12644
12645 boolean clampedY = false;
12646 if (newScrollY > bottom) {
12647 newScrollY = bottom;
12648 clampedY = true;
12649 } else if (newScrollY < top) {
12650 newScrollY = top;
12651 clampedY = true;
12652 }
12653
12654 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
12655
12656 return clampedX || clampedY;
12657 }
12658
12659 /**
12660 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
12661 * respond to the results of an over-scroll operation.
12662 *
12663 * @param scrollX New X scroll value in pixels
12664 * @param scrollY New Y scroll value in pixels
12665 * @param clampedX True if scrollX was clamped to an over-scroll boundary
12666 * @param clampedY True if scrollY was clamped to an over-scroll boundary
12667 */
12668 protected void onOverScrolled(int scrollX, int scrollY,
12669 boolean clampedX, boolean clampedY) {
12670 // Intentionally empty.
12671 }
12672
12673 /**
12674 * Returns the over-scroll mode for this view. The result will be
12675 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12676 * (allow over-scrolling only if the view content is larger than the container),
12677 * or {@link #OVER_SCROLL_NEVER}.
12678 *
12679 * @return This view's over-scroll mode.
12680 */
12681 public int getOverScrollMode() {
12682 return mOverScrollMode;
12683 }
12684
12685 /**
12686 * Set the over-scroll mode for this view. Valid over-scroll modes are
12687 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12688 * (allow over-scrolling only if the view content is larger than the container),
12689 * or {@link #OVER_SCROLL_NEVER}.
12690 *
12691 * Setting the over-scroll mode of a view will have an effect only if the
12692 * view is capable of scrolling.
12693 *
12694 * @param overScrollMode The new over-scroll mode for this view.
12695 */
12696 public void setOverScrollMode(int overScrollMode) {
12697 if (overScrollMode != OVER_SCROLL_ALWAYS &&
12698 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
12699 overScrollMode != OVER_SCROLL_NEVER) {
12700 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
12701 }
12702 mOverScrollMode = overScrollMode;
12703 }
12704
12705 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012706 * Gets a scale factor that determines the distance the view should scroll
12707 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
12708 * @return The vertical scroll scale factor.
12709 * @hide
12710 */
12711 protected float getVerticalScrollFactor() {
12712 if (mVerticalScrollFactor == 0) {
12713 TypedValue outValue = new TypedValue();
12714 if (!mContext.getTheme().resolveAttribute(
12715 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
12716 throw new IllegalStateException(
12717 "Expected theme to define listPreferredItemHeight.");
12718 }
12719 mVerticalScrollFactor = outValue.getDimension(
12720 mContext.getResources().getDisplayMetrics());
12721 }
12722 return mVerticalScrollFactor;
12723 }
12724
12725 /**
12726 * Gets a scale factor that determines the distance the view should scroll
12727 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
12728 * @return The horizontal scroll scale factor.
12729 * @hide
12730 */
12731 protected float getHorizontalScrollFactor() {
12732 // TODO: Should use something else.
12733 return getVerticalScrollFactor();
12734 }
12735
Chet Haaseb39f0512011-05-24 14:36:40 -070012736 //
12737 // Properties
12738 //
12739 /**
12740 * A Property wrapper around the <code>alpha</code> functionality handled by the
12741 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
12742 */
12743 static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
12744 @Override
12745 public void setValue(View object, float value) {
12746 object.setAlpha(value);
12747 }
12748
12749 @Override
12750 public Float get(View object) {
12751 return object.getAlpha();
12752 }
12753 };
12754
12755 /**
12756 * A Property wrapper around the <code>translationX</code> functionality handled by the
12757 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
12758 */
12759 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
12760 @Override
12761 public void setValue(View object, float value) {
12762 object.setTranslationX(value);
12763 }
12764
12765 @Override
12766 public Float get(View object) {
12767 return object.getTranslationX();
12768 }
12769 };
12770
12771 /**
12772 * A Property wrapper around the <code>translationY</code> functionality handled by the
12773 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
12774 */
12775 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
12776 @Override
12777 public void setValue(View object, float value) {
12778 object.setTranslationY(value);
12779 }
12780
12781 @Override
12782 public Float get(View object) {
12783 return object.getTranslationY();
12784 }
12785 };
12786
12787 /**
12788 * A Property wrapper around the <code>x</code> functionality handled by the
12789 * {@link View#setX(float)} and {@link View#getX()} methods.
12790 */
12791 public static Property<View, Float> X = new FloatProperty<View>("x") {
12792 @Override
12793 public void setValue(View object, float value) {
12794 object.setX(value);
12795 }
12796
12797 @Override
12798 public Float get(View object) {
12799 return object.getX();
12800 }
12801 };
12802
12803 /**
12804 * A Property wrapper around the <code>y</code> functionality handled by the
12805 * {@link View#setY(float)} and {@link View#getY()} methods.
12806 */
12807 public static Property<View, Float> Y = new FloatProperty<View>("y") {
12808 @Override
12809 public void setValue(View object, float value) {
12810 object.setY(value);
12811 }
12812
12813 @Override
12814 public Float get(View object) {
12815 return object.getY();
12816 }
12817 };
12818
12819 /**
12820 * A Property wrapper around the <code>rotation</code> functionality handled by the
12821 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
12822 */
12823 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
12824 @Override
12825 public void setValue(View object, float value) {
12826 object.setRotation(value);
12827 }
12828
12829 @Override
12830 public Float get(View object) {
12831 return object.getRotation();
12832 }
12833 };
12834
12835 /**
12836 * A Property wrapper around the <code>rotationX</code> functionality handled by the
12837 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
12838 */
12839 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
12840 @Override
12841 public void setValue(View object, float value) {
12842 object.setRotationX(value);
12843 }
12844
12845 @Override
12846 public Float get(View object) {
12847 return object.getRotationX();
12848 }
12849 };
12850
12851 /**
12852 * A Property wrapper around the <code>rotationY</code> functionality handled by the
12853 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
12854 */
12855 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
12856 @Override
12857 public void setValue(View object, float value) {
12858 object.setRotationY(value);
12859 }
12860
12861 @Override
12862 public Float get(View object) {
12863 return object.getRotationY();
12864 }
12865 };
12866
12867 /**
12868 * A Property wrapper around the <code>scaleX</code> functionality handled by the
12869 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
12870 */
12871 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
12872 @Override
12873 public void setValue(View object, float value) {
12874 object.setScaleX(value);
12875 }
12876
12877 @Override
12878 public Float get(View object) {
12879 return object.getScaleX();
12880 }
12881 };
12882
12883 /**
12884 * A Property wrapper around the <code>scaleY</code> functionality handled by the
12885 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
12886 */
12887 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
12888 @Override
12889 public void setValue(View object, float value) {
12890 object.setScaleY(value);
12891 }
12892
12893 @Override
12894 public Float get(View object) {
12895 return object.getScaleY();
12896 }
12897 };
12898
Jeff Brown33bbfd22011-02-24 20:55:35 -080012899 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012900 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
12901 * Each MeasureSpec represents a requirement for either the width or the height.
12902 * A MeasureSpec is comprised of a size and a mode. There are three possible
12903 * modes:
12904 * <dl>
12905 * <dt>UNSPECIFIED</dt>
12906 * <dd>
12907 * The parent has not imposed any constraint on the child. It can be whatever size
12908 * it wants.
12909 * </dd>
12910 *
12911 * <dt>EXACTLY</dt>
12912 * <dd>
12913 * The parent has determined an exact size for the child. The child is going to be
12914 * given those bounds regardless of how big it wants to be.
12915 * </dd>
12916 *
12917 * <dt>AT_MOST</dt>
12918 * <dd>
12919 * The child can be as large as it wants up to the specified size.
12920 * </dd>
12921 * </dl>
12922 *
12923 * MeasureSpecs are implemented as ints to reduce object allocation. This class
12924 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
12925 */
12926 public static class MeasureSpec {
12927 private static final int MODE_SHIFT = 30;
12928 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
12929
12930 /**
12931 * Measure specification mode: The parent has not imposed any constraint
12932 * on the child. It can be whatever size it wants.
12933 */
12934 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
12935
12936 /**
12937 * Measure specification mode: The parent has determined an exact size
12938 * for the child. The child is going to be given those bounds regardless
12939 * of how big it wants to be.
12940 */
12941 public static final int EXACTLY = 1 << MODE_SHIFT;
12942
12943 /**
12944 * Measure specification mode: The child can be as large as it wants up
12945 * to the specified size.
12946 */
12947 public static final int AT_MOST = 2 << MODE_SHIFT;
12948
12949 /**
12950 * Creates a measure specification based on the supplied size and mode.
12951 *
12952 * The mode must always be one of the following:
12953 * <ul>
12954 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
12955 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
12956 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
12957 * </ul>
12958 *
12959 * @param size the size of the measure specification
12960 * @param mode the mode of the measure specification
12961 * @return the measure specification based on size and mode
12962 */
12963 public static int makeMeasureSpec(int size, int mode) {
12964 return size + mode;
12965 }
12966
12967 /**
12968 * Extracts the mode from the supplied measure specification.
12969 *
12970 * @param measureSpec the measure specification to extract the mode from
12971 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
12972 * {@link android.view.View.MeasureSpec#AT_MOST} or
12973 * {@link android.view.View.MeasureSpec#EXACTLY}
12974 */
12975 public static int getMode(int measureSpec) {
12976 return (measureSpec & MODE_MASK);
12977 }
12978
12979 /**
12980 * Extracts the size from the supplied measure specification.
12981 *
12982 * @param measureSpec the measure specification to extract the size from
12983 * @return the size in pixels defined in the supplied measure specification
12984 */
12985 public static int getSize(int measureSpec) {
12986 return (measureSpec & ~MODE_MASK);
12987 }
12988
12989 /**
12990 * Returns a String representation of the specified measure
12991 * specification.
12992 *
12993 * @param measureSpec the measure specification to convert to a String
12994 * @return a String with the following format: "MeasureSpec: MODE SIZE"
12995 */
12996 public static String toString(int measureSpec) {
12997 int mode = getMode(measureSpec);
12998 int size = getSize(measureSpec);
12999
13000 StringBuilder sb = new StringBuilder("MeasureSpec: ");
13001
13002 if (mode == UNSPECIFIED)
13003 sb.append("UNSPECIFIED ");
13004 else if (mode == EXACTLY)
13005 sb.append("EXACTLY ");
13006 else if (mode == AT_MOST)
13007 sb.append("AT_MOST ");
13008 else
13009 sb.append(mode).append(" ");
13010
13011 sb.append(size);
13012 return sb.toString();
13013 }
13014 }
13015
13016 class CheckForLongPress implements Runnable {
13017
13018 private int mOriginalWindowAttachCount;
13019
13020 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070013021 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013022 && mOriginalWindowAttachCount == mWindowAttachCount) {
13023 if (performLongClick()) {
13024 mHasPerformedLongPress = true;
13025 }
13026 }
13027 }
13028
13029 public void rememberWindowAttachCount() {
13030 mOriginalWindowAttachCount = mWindowAttachCount;
13031 }
13032 }
Joe Malin32736f02011-01-19 16:14:20 -080013033
Adam Powelle14579b2009-12-16 18:39:52 -080013034 private final class CheckForTap implements Runnable {
13035 public void run() {
13036 mPrivateFlags &= ~PREPRESSED;
13037 mPrivateFlags |= PRESSED;
13038 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013039 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080013040 }
13041 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013042
Adam Powella35d7682010-03-12 14:48:13 -080013043 private final class PerformClick implements Runnable {
13044 public void run() {
13045 performClick();
13046 }
13047 }
13048
Dianne Hackborn63042d62011-01-26 18:56:29 -080013049 /** @hide */
13050 public void hackTurnOffWindowResizeAnim(boolean off) {
13051 mAttachInfo.mTurnOffWindowResizeAnim = off;
13052 }
Joe Malin32736f02011-01-19 16:14:20 -080013053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013054 /**
Chet Haasea00f3862011-02-22 06:34:40 -080013055 * This method returns a ViewPropertyAnimator object, which can be used to animate
13056 * specific properties on this View.
13057 *
13058 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
13059 */
13060 public ViewPropertyAnimator animate() {
13061 if (mAnimator == null) {
13062 mAnimator = new ViewPropertyAnimator(this);
13063 }
13064 return mAnimator;
13065 }
13066
13067 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013068 * Interface definition for a callback to be invoked when a key event is
13069 * dispatched to this view. The callback will be invoked before the key
13070 * event is given to the view.
13071 */
13072 public interface OnKeyListener {
13073 /**
13074 * Called when a key is dispatched to a view. This allows listeners to
13075 * get a chance to respond before the target view.
13076 *
13077 * @param v The view the key has been dispatched to.
13078 * @param keyCode The code for the physical key that was pressed
13079 * @param event The KeyEvent object containing full information about
13080 * the event.
13081 * @return True if the listener has consumed the event, false otherwise.
13082 */
13083 boolean onKey(View v, int keyCode, KeyEvent event);
13084 }
13085
13086 /**
13087 * Interface definition for a callback to be invoked when a touch event is
13088 * dispatched to this view. The callback will be invoked before the touch
13089 * event is given to the view.
13090 */
13091 public interface OnTouchListener {
13092 /**
13093 * Called when a touch event is dispatched to a view. This allows listeners to
13094 * get a chance to respond before the target view.
13095 *
13096 * @param v The view the touch event has been dispatched to.
13097 * @param event The MotionEvent object containing full information about
13098 * the event.
13099 * @return True if the listener has consumed the event, false otherwise.
13100 */
13101 boolean onTouch(View v, MotionEvent event);
13102 }
13103
13104 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013105 * Interface definition for a callback to be invoked when a generic motion event is
13106 * dispatched to this view. The callback will be invoked before the generic motion
13107 * event is given to the view.
13108 */
13109 public interface OnGenericMotionListener {
13110 /**
13111 * Called when a generic motion event is dispatched to a view. This allows listeners to
13112 * get a chance to respond before the target view.
13113 *
13114 * @param v The view the generic motion event has been dispatched to.
13115 * @param event The MotionEvent object containing full information about
13116 * the event.
13117 * @return True if the listener has consumed the event, false otherwise.
13118 */
13119 boolean onGenericMotion(View v, MotionEvent event);
13120 }
13121
13122 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013123 * Interface definition for a callback to be invoked when a view has been clicked and held.
13124 */
13125 public interface OnLongClickListener {
13126 /**
13127 * Called when a view has been clicked and held.
13128 *
13129 * @param v The view that was clicked and held.
13130 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080013131 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013132 */
13133 boolean onLongClick(View v);
13134 }
13135
13136 /**
Chris Tate32affef2010-10-18 15:29:21 -070013137 * Interface definition for a callback to be invoked when a drag is being dispatched
13138 * to this view. The callback will be invoked before the hosting view's own
13139 * onDrag(event) method. If the listener wants to fall back to the hosting view's
13140 * onDrag(event) behavior, it should return 'false' from this callback.
13141 */
13142 public interface OnDragListener {
13143 /**
13144 * Called when a drag event is dispatched to a view. This allows listeners
13145 * to get a chance to override base View behavior.
13146 *
Joe Malin32736f02011-01-19 16:14:20 -080013147 * @param v The View that received the drag event.
13148 * @param event The {@link android.view.DragEvent} object for the drag event.
13149 * @return {@code true} if the drag event was handled successfully, or {@code false}
13150 * if the drag event was not handled. Note that {@code false} will trigger the View
13151 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070013152 */
13153 boolean onDrag(View v, DragEvent event);
13154 }
13155
13156 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013157 * Interface definition for a callback to be invoked when the focus state of
13158 * a view changed.
13159 */
13160 public interface OnFocusChangeListener {
13161 /**
13162 * Called when the focus state of a view has changed.
13163 *
13164 * @param v The view whose state has changed.
13165 * @param hasFocus The new focus state of v.
13166 */
13167 void onFocusChange(View v, boolean hasFocus);
13168 }
13169
13170 /**
13171 * Interface definition for a callback to be invoked when a view is clicked.
13172 */
13173 public interface OnClickListener {
13174 /**
13175 * Called when a view has been clicked.
13176 *
13177 * @param v The view that was clicked.
13178 */
13179 void onClick(View v);
13180 }
13181
13182 /**
13183 * Interface definition for a callback to be invoked when the context menu
13184 * for this view is being built.
13185 */
13186 public interface OnCreateContextMenuListener {
13187 /**
13188 * Called when the context menu for this view is being built. It is not
13189 * safe to hold onto the menu after this method returns.
13190 *
13191 * @param menu The context menu that is being built
13192 * @param v The view for which the context menu is being built
13193 * @param menuInfo Extra information about the item for which the
13194 * context menu should be shown. This information will vary
13195 * depending on the class of v.
13196 */
13197 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
13198 }
13199
Joe Onorato664644d2011-01-23 17:53:23 -080013200 /**
13201 * Interface definition for a callback to be invoked when the status bar changes
13202 * visibility.
13203 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070013204 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080013205 */
13206 public interface OnSystemUiVisibilityChangeListener {
13207 /**
13208 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070013209 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080013210 *
13211 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
13212 */
13213 public void onSystemUiVisibilityChange(int visibility);
13214 }
13215
Adam Powell4afd62b2011-02-18 15:02:18 -080013216 /**
13217 * Interface definition for a callback to be invoked when this view is attached
13218 * or detached from its window.
13219 */
13220 public interface OnAttachStateChangeListener {
13221 /**
13222 * Called when the view is attached to a window.
13223 * @param v The view that was attached
13224 */
13225 public void onViewAttachedToWindow(View v);
13226 /**
13227 * Called when the view is detached from a window.
13228 * @param v The view that was detached
13229 */
13230 public void onViewDetachedFromWindow(View v);
13231 }
13232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013233 private final class UnsetPressedState implements Runnable {
13234 public void run() {
13235 setPressed(false);
13236 }
13237 }
13238
13239 /**
13240 * Base class for derived classes that want to save and restore their own
13241 * state in {@link android.view.View#onSaveInstanceState()}.
13242 */
13243 public static class BaseSavedState extends AbsSavedState {
13244 /**
13245 * Constructor used when reading from a parcel. Reads the state of the superclass.
13246 *
13247 * @param source
13248 */
13249 public BaseSavedState(Parcel source) {
13250 super(source);
13251 }
13252
13253 /**
13254 * Constructor called by derived classes when creating their SavedState objects
13255 *
13256 * @param superState The state of the superclass of this view
13257 */
13258 public BaseSavedState(Parcelable superState) {
13259 super(superState);
13260 }
13261
13262 public static final Parcelable.Creator<BaseSavedState> CREATOR =
13263 new Parcelable.Creator<BaseSavedState>() {
13264 public BaseSavedState createFromParcel(Parcel in) {
13265 return new BaseSavedState(in);
13266 }
13267
13268 public BaseSavedState[] newArray(int size) {
13269 return new BaseSavedState[size];
13270 }
13271 };
13272 }
13273
13274 /**
13275 * A set of information given to a view when it is attached to its parent
13276 * window.
13277 */
13278 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013279 interface Callbacks {
13280 void playSoundEffect(int effectId);
13281 boolean performHapticFeedback(int effectId, boolean always);
13282 }
13283
13284 /**
13285 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
13286 * to a Handler. This class contains the target (View) to invalidate and
13287 * the coordinates of the dirty rectangle.
13288 *
13289 * For performance purposes, this class also implements a pool of up to
13290 * POOL_LIMIT objects that get reused. This reduces memory allocations
13291 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013292 */
Romain Guyd928d682009-03-31 17:52:16 -070013293 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013294 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070013295 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
13296 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070013297 public InvalidateInfo newInstance() {
13298 return new InvalidateInfo();
13299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013300
Romain Guyd928d682009-03-31 17:52:16 -070013301 public void onAcquired(InvalidateInfo element) {
13302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013303
Romain Guyd928d682009-03-31 17:52:16 -070013304 public void onReleased(InvalidateInfo element) {
13305 }
13306 }, POOL_LIMIT)
13307 );
13308
13309 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013310 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013311
13312 View target;
13313
13314 int left;
13315 int top;
13316 int right;
13317 int bottom;
13318
Romain Guyd928d682009-03-31 17:52:16 -070013319 public void setNextPoolable(InvalidateInfo element) {
13320 mNext = element;
13321 }
13322
13323 public InvalidateInfo getNextPoolable() {
13324 return mNext;
13325 }
13326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013327 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070013328 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013329 }
13330
13331 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070013332 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013333 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013334
13335 public boolean isPooled() {
13336 return mIsPooled;
13337 }
13338
13339 public void setPooled(boolean isPooled) {
13340 mIsPooled = isPooled;
13341 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013342 }
13343
13344 final IWindowSession mSession;
13345
13346 final IWindow mWindow;
13347
13348 final IBinder mWindowToken;
13349
13350 final Callbacks mRootCallbacks;
13351
Romain Guy59a12ca2011-06-09 17:48:21 -070013352 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080013353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013354 /**
13355 * The top view of the hierarchy.
13356 */
13357 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070013358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013359 IBinder mPanelParentWindowToken;
13360 Surface mSurface;
13361
Romain Guyb051e892010-09-28 19:09:36 -070013362 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013363 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070013364 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080013365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013366 /**
Romain Guy8506ab42009-06-11 17:35:47 -070013367 * Scale factor used by the compatibility mode
13368 */
13369 float mApplicationScale;
13370
13371 /**
13372 * Indicates whether the application is in compatibility mode
13373 */
13374 boolean mScalingRequired;
13375
13376 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013377 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080013378 */
13379 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080013380
Dianne Hackborn63042d62011-01-26 18:56:29 -080013381 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013382 * Left position of this view's window
13383 */
13384 int mWindowLeft;
13385
13386 /**
13387 * Top position of this view's window
13388 */
13389 int mWindowTop;
13390
13391 /**
Adam Powell26153a32010-11-08 15:22:27 -080013392 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070013393 */
Adam Powell26153a32010-11-08 15:22:27 -080013394 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070013395
13396 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013397 * For windows that are full-screen but using insets to layout inside
13398 * of the screen decorations, these are the current insets for the
13399 * content of the window.
13400 */
13401 final Rect mContentInsets = new Rect();
13402
13403 /**
13404 * 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 * actual visible parts of the window.
13407 */
13408 final Rect mVisibleInsets = new Rect();
13409
13410 /**
13411 * The internal insets given by this window. This value is
13412 * supplied by the client (through
13413 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
13414 * be given to the window manager when changed to be used in laying
13415 * out windows behind it.
13416 */
13417 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
13418 = new ViewTreeObserver.InternalInsetsInfo();
13419
13420 /**
13421 * All views in the window's hierarchy that serve as scroll containers,
13422 * used to determine if the window can be resized or must be panned
13423 * to adjust for a soft input area.
13424 */
13425 final ArrayList<View> mScrollContainers = new ArrayList<View>();
13426
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070013427 final KeyEvent.DispatcherState mKeyDispatchState
13428 = new KeyEvent.DispatcherState();
13429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013430 /**
13431 * Indicates whether the view's window currently has the focus.
13432 */
13433 boolean mHasWindowFocus;
13434
13435 /**
13436 * The current visibility of the window.
13437 */
13438 int mWindowVisibility;
13439
13440 /**
13441 * Indicates the time at which drawing started to occur.
13442 */
13443 long mDrawingTime;
13444
13445 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070013446 * Indicates whether or not ignoring the DIRTY_MASK flags.
13447 */
13448 boolean mIgnoreDirtyState;
13449
13450 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013451 * Indicates whether the view's window is currently in touch mode.
13452 */
13453 boolean mInTouchMode;
13454
13455 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013456 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013457 * the next time it performs a traversal
13458 */
13459 boolean mRecomputeGlobalAttributes;
13460
13461 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013462 * Set during a traveral if any views want to keep the screen on.
13463 */
13464 boolean mKeepScreenOn;
13465
13466 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013467 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
13468 */
13469 int mSystemUiVisibility;
13470
13471 /**
13472 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
13473 * attached.
13474 */
13475 boolean mHasSystemUiListeners;
13476
13477 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013478 * Set if the visibility of any views has changed.
13479 */
13480 boolean mViewVisibilityChanged;
13481
13482 /**
13483 * Set to true if a view has been scrolled.
13484 */
13485 boolean mViewScrollChanged;
13486
13487 /**
13488 * Global to the view hierarchy used as a temporary for dealing with
13489 * x/y points in the transparent region computations.
13490 */
13491 final int[] mTransparentLocation = new int[2];
13492
13493 /**
13494 * Global to the view hierarchy used as a temporary for dealing with
13495 * x/y points in the ViewGroup.invalidateChild implementation.
13496 */
13497 final int[] mInvalidateChildLocation = new int[2];
13498
Chet Haasec3aa3612010-06-17 08:50:37 -070013499
13500 /**
13501 * Global to the view hierarchy used as a temporary for dealing with
13502 * x/y location when view is transformed.
13503 */
13504 final float[] mTmpTransformLocation = new float[2];
13505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013506 /**
13507 * The view tree observer used to dispatch global events like
13508 * layout, pre-draw, touch mode change, etc.
13509 */
13510 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
13511
13512 /**
13513 * A Canvas used by the view hierarchy to perform bitmap caching.
13514 */
13515 Canvas mCanvas;
13516
13517 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013518 * A Handler supplied by a view's {@link android.view.ViewAncestor}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013519 * handler can be used to pump events in the UI events queue.
13520 */
13521 final Handler mHandler;
13522
13523 /**
13524 * Identifier for messages requesting the view to be invalidated.
13525 * Such messages should be sent to {@link #mHandler}.
13526 */
13527 static final int INVALIDATE_MSG = 0x1;
13528
13529 /**
13530 * Identifier for messages requesting the view to invalidate a region.
13531 * Such messages should be sent to {@link #mHandler}.
13532 */
13533 static final int INVALIDATE_RECT_MSG = 0x2;
13534
13535 /**
13536 * Temporary for use in computing invalidate rectangles while
13537 * calling up the hierarchy.
13538 */
13539 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070013540
13541 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070013542 * Temporary for use in computing hit areas with transformed views
13543 */
13544 final RectF mTmpTransformRect = new RectF();
13545
13546 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070013547 * Temporary list for use in collecting focusable descendents of a view.
13548 */
13549 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
13550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013551 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013552 * The id of the window for accessibility purposes.
13553 */
13554 int mAccessibilityWindowId = View.NO_ID;
13555
13556 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013557 * Creates a new set of attachment information with the specified
13558 * events handler and thread.
13559 *
13560 * @param handler the events handler the view must use
13561 */
13562 AttachInfo(IWindowSession session, IWindow window,
13563 Handler handler, Callbacks effectPlayer) {
13564 mSession = session;
13565 mWindow = window;
13566 mWindowToken = window.asBinder();
13567 mHandler = handler;
13568 mRootCallbacks = effectPlayer;
13569 }
13570 }
13571
13572 /**
13573 * <p>ScrollabilityCache holds various fields used by a View when scrolling
13574 * is supported. This avoids keeping too many unused fields in most
13575 * instances of View.</p>
13576 */
Mike Cleronf116bf82009-09-27 19:14:12 -070013577 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080013578
Mike Cleronf116bf82009-09-27 19:14:12 -070013579 /**
13580 * Scrollbars are not visible
13581 */
13582 public static final int OFF = 0;
13583
13584 /**
13585 * Scrollbars are visible
13586 */
13587 public static final int ON = 1;
13588
13589 /**
13590 * Scrollbars are fading away
13591 */
13592 public static final int FADING = 2;
13593
13594 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080013595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013596 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070013597 public int scrollBarDefaultDelayBeforeFade;
13598 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013599
13600 public int scrollBarSize;
13601 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070013602 public float[] interpolatorValues;
13603 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013604
13605 public final Paint paint;
13606 public final Matrix matrix;
13607 public Shader shader;
13608
Mike Cleronf116bf82009-09-27 19:14:12 -070013609 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
13610
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013611 private static final float[] OPAQUE = { 255 };
13612 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080013613
Mike Cleronf116bf82009-09-27 19:14:12 -070013614 /**
13615 * When fading should start. This time moves into the future every time
13616 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
13617 */
13618 public long fadeStartTime;
13619
13620
13621 /**
13622 * The current state of the scrollbars: ON, OFF, or FADING
13623 */
13624 public int state = OFF;
13625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013626 private int mLastColor;
13627
Mike Cleronf116bf82009-09-27 19:14:12 -070013628 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013629 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
13630 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070013631 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
13632 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013633
13634 paint = new Paint();
13635 matrix = new Matrix();
13636 // use use a height of 1, and then wack the matrix each time we
13637 // actually use it.
13638 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070013639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013640 paint.setShader(shader);
13641 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070013642 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013643 }
Romain Guy8506ab42009-06-11 17:35:47 -070013644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013645 public void setFadeColor(int color) {
13646 if (color != 0 && color != mLastColor) {
13647 mLastColor = color;
13648 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070013649
Romain Guye55e1a72009-08-27 10:42:26 -070013650 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
13651 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070013652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013653 paint.setShader(shader);
13654 // Restore the default transfer mode (src_over)
13655 paint.setXfermode(null);
13656 }
13657 }
Joe Malin32736f02011-01-19 16:14:20 -080013658
Mike Cleronf116bf82009-09-27 19:14:12 -070013659 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070013660 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070013661 if (now >= fadeStartTime) {
13662
13663 // the animation fades the scrollbars out by changing
13664 // the opacity (alpha) from fully opaque to fully
13665 // transparent
13666 int nextFrame = (int) now;
13667 int framesCount = 0;
13668
13669 Interpolator interpolator = scrollBarInterpolator;
13670
13671 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013672 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070013673
13674 // End transparent
13675 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013676 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070013677
13678 state = FADING;
13679
13680 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080013681 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070013682 }
13683 }
13684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013685 }
13686}