blob: 622ab0fd62c164873a8402b5185b4176f0e10edc [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;
20import android.util.Property;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080021import com.android.internal.R;
22import com.android.internal.util.Predicate;
23import com.android.internal.view.menu.MenuBuilder;
24
Christopher Tatea53146c2010-09-07 11:57:52 -070025import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080027import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.res.Resources;
29import android.content.res.TypedArray;
30import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070031import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.graphics.Canvas;
Mike Cleronf116bf82009-09-27 19:14:12 -070033import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.LinearGradient;
35import android.graphics.Matrix;
36import android.graphics.Paint;
37import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070038import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.graphics.PorterDuff;
40import android.graphics.PorterDuffXfermode;
41import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070042import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.graphics.Region;
44import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.graphics.drawable.ColorDrawable;
46import android.graphics.drawable.Drawable;
47import android.os.Handler;
48import android.os.IBinder;
49import android.os.Message;
50import android.os.Parcel;
51import android.os.Parcelable;
52import android.os.RemoteException;
53import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070058import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.util.Pools;
60import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080061import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070063import android.view.accessibility.AccessibilityEvent;
64import android.view.accessibility.AccessibilityEventSource;
65import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070066import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070068import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070069import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.view.inputmethod.InputConnection;
71import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.widget.ScrollBarDrawable;
73
Christopher Tatea0374192010-10-05 13:06:41 -070074import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070075import java.lang.reflect.InvocationTargetException;
76import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import java.util.ArrayList;
78import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070079import java.util.Locale;
Romain Guyd90a3312009-05-06 14:54:28 -070080import java.util.WeakHashMap;
Adam Powell4afd62b2011-02-18 15:02:18 -080081import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
83/**
84 * <p>
85 * This class represents the basic building block for user interface components. A View
86 * occupies a rectangular area on the screen and is responsible for drawing and
87 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070088 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
90 * are invisible containers that hold other Views (or other ViewGroups) and define
91 * their layout properties.
92 * </p>
93 *
94 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070095 * <p>For an introduction to using this class to develop your
96 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -070098 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
100 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
101 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
102 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
103 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
104 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
105 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
106 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
107 * </p>
108 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700109 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 * <a name="Using"></a>
111 * <h3>Using Views</h3>
112 * <p>
113 * All of the views in a window are arranged in a single tree. You can add views
114 * either from code or by specifying a tree of views in one or more XML layout
115 * files. There are many specialized subclasses of views that act as controls or
116 * are capable of displaying text, images, or other content.
117 * </p>
118 * <p>
119 * Once you have created a tree of views, there are typically a few types of
120 * common operations you may wish to perform:
121 * <ul>
122 * <li><strong>Set properties:</strong> for example setting the text of a
123 * {@link android.widget.TextView}. The available properties and the methods
124 * that set them will vary among the different subclasses of views. Note that
125 * properties that are known at build time can be set in the XML layout
126 * files.</li>
127 * <li><strong>Set focus:</strong> The framework will handled moving focus in
128 * response to user input. To force focus to a specific view, call
129 * {@link #requestFocus}.</li>
130 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
131 * that will be notified when something interesting happens to the view. For
132 * example, all views will let you set a listener to be notified when the view
133 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700134 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
135 * Other view subclasses offer more specialized listeners. For example, a Button
136 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700138 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 * </ul>
140 * </p>
141 * <p><em>
142 * Note: The Android framework is responsible for measuring, laying out and
143 * drawing views. You should not call methods that perform these actions on
144 * views yourself unless you are actually implementing a
145 * {@link android.view.ViewGroup}.
146 * </em></p>
147 *
148 * <a name="Lifecycle"></a>
149 * <h3>Implementing a Custom View</h3>
150 *
151 * <p>
152 * To implement a custom view, you will usually begin by providing overrides for
153 * some of the standard methods that the framework calls on all views. You do
154 * not need to override all of these methods. In fact, you can start by just
155 * overriding {@link #onDraw(android.graphics.Canvas)}.
156 * <table border="2" width="85%" align="center" cellpadding="5">
157 * <thead>
158 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
159 * </thead>
160 *
161 * <tbody>
162 * <tr>
163 * <td rowspan="2">Creation</td>
164 * <td>Constructors</td>
165 * <td>There is a form of the constructor that are called when the view
166 * is created from code and a form that is called when the view is
167 * inflated from a layout file. The second form should parse and apply
168 * any attributes defined in the layout file.
169 * </td>
170 * </tr>
171 * <tr>
172 * <td><code>{@link #onFinishInflate()}</code></td>
173 * <td>Called after a view and all of its children has been inflated
174 * from XML.</td>
175 * </tr>
176 *
177 * <tr>
178 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700179 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 * <td>Called to determine the size requirements for this view and all
181 * of its children.
182 * </td>
183 * </tr>
184 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700185 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 * <td>Called when this view should assign a size and position to all
187 * of its children.
188 * </td>
189 * </tr>
190 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700191 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 * <td>Called when the size of this view has changed.
193 * </td>
194 * </tr>
195 *
196 * <tr>
197 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700198 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 * <td>Called when the view should render its content.
200 * </td>
201 * </tr>
202 *
203 * <tr>
204 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700205 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 * <td>Called when a new key event occurs.
207 * </td>
208 * </tr>
209 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700210 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 * <td>Called when a key up event occurs.
212 * </td>
213 * </tr>
214 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700215 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 * <td>Called when a trackball motion event occurs.
217 * </td>
218 * </tr>
219 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700220 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 * <td>Called when a touch screen motion event occurs.
222 * </td>
223 * </tr>
224 *
225 * <tr>
226 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700227 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 * <td>Called when the view gains or loses focus.
229 * </td>
230 * </tr>
231 *
232 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700233 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 * <td>Called when the window containing the view gains or loses focus.
235 * </td>
236 * </tr>
237 *
238 * <tr>
239 * <td rowspan="3">Attaching</td>
240 * <td><code>{@link #onAttachedToWindow()}</code></td>
241 * <td>Called when the view is attached to a window.
242 * </td>
243 * </tr>
244 *
245 * <tr>
246 * <td><code>{@link #onDetachedFromWindow}</code></td>
247 * <td>Called when the view is detached from its window.
248 * </td>
249 * </tr>
250 *
251 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700252 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 * <td>Called when the visibility of the window containing the view
254 * has changed.
255 * </td>
256 * </tr>
257 * </tbody>
258 *
259 * </table>
260 * </p>
261 *
262 * <a name="IDs"></a>
263 * <h3>IDs</h3>
264 * Views may have an integer id associated with them. These ids are typically
265 * assigned in the layout XML files, and are used to find specific views within
266 * the view tree. A common pattern is to:
267 * <ul>
268 * <li>Define a Button in the layout file and assign it a unique ID.
269 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700270 * &lt;Button
271 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 * android:layout_width="wrap_content"
273 * android:layout_height="wrap_content"
274 * android:text="@string/my_button_text"/&gt;
275 * </pre></li>
276 * <li>From the onCreate method of an Activity, find the Button
277 * <pre class="prettyprint">
278 * Button myButton = (Button) findViewById(R.id.my_button);
279 * </pre></li>
280 * </ul>
281 * <p>
282 * View IDs need not be unique throughout the tree, but it is good practice to
283 * ensure that they are at least unique within the part of the tree you are
284 * searching.
285 * </p>
286 *
287 * <a name="Position"></a>
288 * <h3>Position</h3>
289 * <p>
290 * The geometry of a view is that of a rectangle. A view has a location,
291 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
292 * two dimensions, expressed as a width and a height. The unit for location
293 * and dimensions is the pixel.
294 * </p>
295 *
296 * <p>
297 * It is possible to retrieve the location of a view by invoking the methods
298 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
299 * coordinate of the rectangle representing the view. The latter returns the
300 * top, or Y, coordinate of the rectangle representing the view. These methods
301 * both return the location of the view relative to its parent. For instance,
302 * when getLeft() returns 20, that means the view is located 20 pixels to the
303 * right of the left edge of its direct parent.
304 * </p>
305 *
306 * <p>
307 * In addition, several convenience methods are offered to avoid unnecessary
308 * computations, namely {@link #getRight()} and {@link #getBottom()}.
309 * These methods return the coordinates of the right and bottom edges of the
310 * rectangle representing the view. For instance, calling {@link #getRight()}
311 * is similar to the following computation: <code>getLeft() + getWidth()</code>
312 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
313 * </p>
314 *
315 * <a name="SizePaddingMargins"></a>
316 * <h3>Size, padding and margins</h3>
317 * <p>
318 * The size of a view is expressed with a width and a height. A view actually
319 * possess two pairs of width and height values.
320 * </p>
321 *
322 * <p>
323 * The first pair is known as <em>measured width</em> and
324 * <em>measured height</em>. These dimensions define how big a view wants to be
325 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
326 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
327 * and {@link #getMeasuredHeight()}.
328 * </p>
329 *
330 * <p>
331 * The second pair is simply known as <em>width</em> and <em>height</em>, or
332 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
333 * dimensions define the actual size of the view on screen, at drawing time and
334 * after layout. These values may, but do not have to, be different from the
335 * measured width and height. The width and height can be obtained by calling
336 * {@link #getWidth()} and {@link #getHeight()}.
337 * </p>
338 *
339 * <p>
340 * To measure its dimensions, a view takes into account its padding. The padding
341 * is expressed in pixels for the left, top, right and bottom parts of the view.
342 * Padding can be used to offset the content of the view by a specific amount of
343 * pixels. For instance, a left padding of 2 will push the view's content by
344 * 2 pixels to the right of the left edge. Padding can be set using the
345 * {@link #setPadding(int, int, int, int)} method and queried by calling
346 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
347 * {@link #getPaddingRight()} and {@link #getPaddingBottom()}.
348 * </p>
349 *
350 * <p>
351 * Even though a view can define a padding, it does not provide any support for
352 * margins. However, view groups provide such a support. Refer to
353 * {@link android.view.ViewGroup} and
354 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
355 * </p>
356 *
357 * <a name="Layout"></a>
358 * <h3>Layout</h3>
359 * <p>
360 * Layout is a two pass process: a measure pass and a layout pass. The measuring
361 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
362 * of the view tree. Each view pushes dimension specifications down the tree
363 * during the recursion. At the end of the measure pass, every view has stored
364 * its measurements. The second pass happens in
365 * {@link #layout(int,int,int,int)} and is also top-down. During
366 * this pass each parent is responsible for positioning all of its children
367 * using the sizes computed in the measure pass.
368 * </p>
369 *
370 * <p>
371 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
372 * {@link #getMeasuredHeight()} values must be set, along with those for all of
373 * that view's descendants. A view's measured width and measured height values
374 * must respect the constraints imposed by the view's parents. This guarantees
375 * that at the end of the measure pass, all parents accept all of their
376 * children's measurements. A parent view may call measure() more than once on
377 * its children. For example, the parent may measure each child once with
378 * unspecified dimensions to find out how big they want to be, then call
379 * measure() on them again with actual numbers if the sum of all the children's
380 * unconstrained sizes is too big or too small.
381 * </p>
382 *
383 * <p>
384 * The measure pass uses two classes to communicate dimensions. The
385 * {@link MeasureSpec} class is used by views to tell their parents how they
386 * want to be measured and positioned. The base LayoutParams class just
387 * describes how big the view wants to be for both width and height. For each
388 * dimension, it can specify one of:
389 * <ul>
390 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800391 * <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 -0800392 * (minus padding)
393 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
394 * enclose its content (plus padding).
395 * </ul>
396 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
397 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
398 * an X and Y value.
399 * </p>
400 *
401 * <p>
402 * MeasureSpecs are used to push requirements down the tree from parent to
403 * child. A MeasureSpec can be in one of three modes:
404 * <ul>
405 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
406 * of a child view. For example, a LinearLayout may call measure() on its child
407 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
408 * tall the child view wants to be given a width of 240 pixels.
409 * <li>EXACTLY: This is used by the parent to impose an exact size on the
410 * child. The child must use this size, and guarantee that all of its
411 * descendants will fit within this size.
412 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
413 * child. The child must gurantee that it and all of its descendants will fit
414 * within this size.
415 * </ul>
416 * </p>
417 *
418 * <p>
419 * To intiate a layout, call {@link #requestLayout}. This method is typically
420 * called by a view on itself when it believes that is can no longer fit within
421 * its current bounds.
422 * </p>
423 *
424 * <a name="Drawing"></a>
425 * <h3>Drawing</h3>
426 * <p>
427 * Drawing is handled by walking the tree and rendering each view that
428 * intersects the the invalid region. Because the tree is traversed in-order,
429 * this means that parents will draw before (i.e., behind) their children, with
430 * siblings drawn in the order they appear in the tree.
431 * If you set a background drawable for a View, then the View will draw it for you
432 * before calling back to its <code>onDraw()</code> method.
433 * </p>
434 *
435 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700436 * 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 -0800437 * </p>
438 *
439 * <p>
440 * To force a view to draw, call {@link #invalidate()}.
441 * </p>
442 *
443 * <a name="EventHandlingThreading"></a>
444 * <h3>Event Handling and Threading</h3>
445 * <p>
446 * The basic cycle of a view is as follows:
447 * <ol>
448 * <li>An event comes in and is dispatched to the appropriate view. The view
449 * handles the event and notifies any listeners.</li>
450 * <li>If in the course of processing the event, the view's bounds may need
451 * to be changed, the view will call {@link #requestLayout()}.</li>
452 * <li>Similarly, if in the course of processing the event the view's appearance
453 * may need to be changed, the view will call {@link #invalidate()}.</li>
454 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
455 * the framework will take care of measuring, laying out, and drawing the tree
456 * as appropriate.</li>
457 * </ol>
458 * </p>
459 *
460 * <p><em>Note: The entire view tree is single threaded. You must always be on
461 * the UI thread when calling any method on any view.</em>
462 * If you are doing work on other threads and want to update the state of a view
463 * from that thread, you should use a {@link Handler}.
464 * </p>
465 *
466 * <a name="FocusHandling"></a>
467 * <h3>Focus Handling</h3>
468 * <p>
469 * The framework will handle routine focus movement in response to user input.
470 * This includes changing the focus as views are removed or hidden, or as new
471 * views become available. Views indicate their willingness to take focus
472 * through the {@link #isFocusable} method. To change whether a view can take
473 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
474 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
475 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
476 * </p>
477 * <p>
478 * Focus movement is based on an algorithm which finds the nearest neighbor in a
479 * given direction. In rare cases, the default algorithm may not match the
480 * intended behavior of the developer. In these situations, you can provide
481 * explicit overrides by using these XML attributes in the layout file:
482 * <pre>
483 * nextFocusDown
484 * nextFocusLeft
485 * nextFocusRight
486 * nextFocusUp
487 * </pre>
488 * </p>
489 *
490 *
491 * <p>
492 * To get a particular view to take focus, call {@link #requestFocus()}.
493 * </p>
494 *
495 * <a name="TouchMode"></a>
496 * <h3>Touch Mode</h3>
497 * <p>
498 * When a user is navigating a user interface via directional keys such as a D-pad, it is
499 * necessary to give focus to actionable items such as buttons so the user can see
500 * what will take input. If the device has touch capabilities, however, and the user
501 * begins interacting with the interface by touching it, it is no longer necessary to
502 * always highlight, or give focus to, a particular view. This motivates a mode
503 * for interaction named 'touch mode'.
504 * </p>
505 * <p>
506 * For a touch capable device, once the user touches the screen, the device
507 * will enter touch mode. From this point onward, only views for which
508 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
509 * Other views that are touchable, like buttons, will not take focus when touched; they will
510 * only fire the on click listeners.
511 * </p>
512 * <p>
513 * Any time a user hits a directional key, such as a D-pad direction, the view device will
514 * exit touch mode, and find a view to take focus, so that the user may resume interacting
515 * with the user interface without touching the screen again.
516 * </p>
517 * <p>
518 * The touch mode state is maintained across {@link android.app.Activity}s. Call
519 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
520 * </p>
521 *
522 * <a name="Scrolling"></a>
523 * <h3>Scrolling</h3>
524 * <p>
525 * The framework provides basic support for views that wish to internally
526 * scroll their content. This includes keeping track of the X and Y scroll
527 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800528 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700529 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 * </p>
531 *
532 * <a name="Tags"></a>
533 * <h3>Tags</h3>
534 * <p>
535 * Unlike IDs, tags are not used to identify views. Tags are essentially an
536 * extra piece of information that can be associated with a view. They are most
537 * often used as a convenience to store data related to views in the views
538 * themselves rather than by putting them in a separate structure.
539 * </p>
540 *
541 * <a name="Animation"></a>
542 * <h3>Animation</h3>
543 * <p>
544 * You can attach an {@link Animation} object to a view using
545 * {@link #setAnimation(Animation)} or
546 * {@link #startAnimation(Animation)}. The animation can alter the scale,
547 * rotation, translation and alpha of a view over time. If the animation is
548 * attached to a view that has children, the animation will affect the entire
549 * subtree rooted by that node. When an animation is started, the framework will
550 * take care of redrawing the appropriate views until the animation completes.
551 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800552 * <p>
553 * Starting with Android 3.0, the preferred way of animating views is to use the
554 * {@link android.animation} package APIs.
555 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 *
Jeff Brown85a31762010-09-01 17:01:00 -0700557 * <a name="Security"></a>
558 * <h3>Security</h3>
559 * <p>
560 * Sometimes it is essential that an application be able to verify that an action
561 * is being performed with the full knowledge and consent of the user, such as
562 * granting a permission request, making a purchase or clicking on an advertisement.
563 * Unfortunately, a malicious application could try to spoof the user into
564 * performing these actions, unaware, by concealing the intended purpose of the view.
565 * As a remedy, the framework offers a touch filtering mechanism that can be used to
566 * improve the security of views that provide access to sensitive functionality.
567 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700568 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800569 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700570 * will discard touches that are received whenever the view's window is obscured by
571 * another visible window. As a result, the view will not receive touches whenever a
572 * toast, dialog or other window appears above the view's window.
573 * </p><p>
574 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700575 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
576 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700577 * </p>
578 *
Romain Guy171c5922011-01-06 10:04:23 -0800579 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700580 * @attr ref android.R.styleable#View_background
581 * @attr ref android.R.styleable#View_clickable
582 * @attr ref android.R.styleable#View_contentDescription
583 * @attr ref android.R.styleable#View_drawingCacheQuality
584 * @attr ref android.R.styleable#View_duplicateParentState
585 * @attr ref android.R.styleable#View_id
586 * @attr ref android.R.styleable#View_fadingEdge
587 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700588 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700590 * @attr ref android.R.styleable#View_isScrollContainer
591 * @attr ref android.R.styleable#View_focusable
592 * @attr ref android.R.styleable#View_focusableInTouchMode
593 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
594 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800595 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700596 * @attr ref android.R.styleable#View_longClickable
597 * @attr ref android.R.styleable#View_minHeight
598 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 * @attr ref android.R.styleable#View_nextFocusDown
600 * @attr ref android.R.styleable#View_nextFocusLeft
601 * @attr ref android.R.styleable#View_nextFocusRight
602 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700603 * @attr ref android.R.styleable#View_onClick
604 * @attr ref android.R.styleable#View_padding
605 * @attr ref android.R.styleable#View_paddingBottom
606 * @attr ref android.R.styleable#View_paddingLeft
607 * @attr ref android.R.styleable#View_paddingRight
608 * @attr ref android.R.styleable#View_paddingTop
609 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800610 * @attr ref android.R.styleable#View_rotation
611 * @attr ref android.R.styleable#View_rotationX
612 * @attr ref android.R.styleable#View_rotationY
613 * @attr ref android.R.styleable#View_scaleX
614 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 * @attr ref android.R.styleable#View_scrollX
616 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700617 * @attr ref android.R.styleable#View_scrollbarSize
618 * @attr ref android.R.styleable#View_scrollbarStyle
619 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700620 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
621 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
623 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 * @attr ref android.R.styleable#View_scrollbarThumbVertical
625 * @attr ref android.R.styleable#View_scrollbarTrackVertical
626 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
627 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700628 * @attr ref android.R.styleable#View_soundEffectsEnabled
629 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800630 * @attr ref android.R.styleable#View_transformPivotX
631 * @attr ref android.R.styleable#View_transformPivotY
632 * @attr ref android.R.styleable#View_translationX
633 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700634 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 *
636 * @see android.view.ViewGroup
637 */
Fabrice Di Meglio6a036402011-05-23 14:43:23 -0700638public class View implements Drawable.Callback2, KeyEvent.Callback, AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 private static final boolean DBG = false;
640
641 /**
642 * The logging tag used by this class with android.util.Log.
643 */
644 protected static final String VIEW_LOG_TAG = "View";
645
646 /**
647 * Used to mark a View that has no ID.
648 */
649 public static final int NO_ID = -1;
650
651 /**
652 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
653 * calling setFlags.
654 */
655 private static final int NOT_FOCUSABLE = 0x00000000;
656
657 /**
658 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
659 * setFlags.
660 */
661 private static final int FOCUSABLE = 0x00000001;
662
663 /**
664 * Mask for use with setFlags indicating bits used for focus.
665 */
666 private static final int FOCUSABLE_MASK = 0x00000001;
667
668 /**
669 * This view will adjust its padding to fit sytem windows (e.g. status bar)
670 */
671 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
672
673 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700674 * This view is visible. Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 */
676 public static final int VISIBLE = 0x00000000;
677
678 /**
679 * This view is invisible, but it still takes up space for layout purposes.
Romain Guy5c22a8c2011-05-13 11:48:45 -0700680 * Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 */
682 public static final int INVISIBLE = 0x00000004;
683
684 /**
685 * This view is invisible, and it doesn't take any space for layout
Romain Guy5c22a8c2011-05-13 11:48:45 -0700686 * purposes. Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 */
688 public static final int GONE = 0x00000008;
689
690 /**
691 * Mask for use with setFlags indicating bits used for visibility.
692 * {@hide}
693 */
694 static final int VISIBILITY_MASK = 0x0000000C;
695
696 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
697
698 /**
699 * This view is enabled. Intrepretation varies by subclass.
700 * Use with ENABLED_MASK when calling setFlags.
701 * {@hide}
702 */
703 static final int ENABLED = 0x00000000;
704
705 /**
706 * This view is disabled. Intrepretation varies by subclass.
707 * Use with ENABLED_MASK when calling setFlags.
708 * {@hide}
709 */
710 static final int DISABLED = 0x00000020;
711
712 /**
713 * Mask for use with setFlags indicating bits used for indicating whether
714 * this view is enabled
715 * {@hide}
716 */
717 static final int ENABLED_MASK = 0x00000020;
718
719 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700720 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
721 * called and further optimizations will be performed. It is okay to have
722 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 * {@hide}
724 */
725 static final int WILL_NOT_DRAW = 0x00000080;
726
727 /**
728 * Mask for use with setFlags indicating bits used for indicating whether
729 * this view is will draw
730 * {@hide}
731 */
732 static final int DRAW_MASK = 0x00000080;
733
734 /**
735 * <p>This view doesn't show scrollbars.</p>
736 * {@hide}
737 */
738 static final int SCROLLBARS_NONE = 0x00000000;
739
740 /**
741 * <p>This view shows horizontal scrollbars.</p>
742 * {@hide}
743 */
744 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
745
746 /**
747 * <p>This view shows vertical scrollbars.</p>
748 * {@hide}
749 */
750 static final int SCROLLBARS_VERTICAL = 0x00000200;
751
752 /**
753 * <p>Mask for use with setFlags indicating bits used for indicating which
754 * scrollbars are enabled.</p>
755 * {@hide}
756 */
757 static final int SCROLLBARS_MASK = 0x00000300;
758
Jeff Brown85a31762010-09-01 17:01:00 -0700759 /**
760 * Indicates that the view should filter touches when its window is obscured.
761 * Refer to the class comments for more information about this security feature.
762 * {@hide}
763 */
764 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
765
766 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767
768 /**
769 * <p>This view doesn't show fading edges.</p>
770 * {@hide}
771 */
772 static final int FADING_EDGE_NONE = 0x00000000;
773
774 /**
775 * <p>This view shows horizontal fading edges.</p>
776 * {@hide}
777 */
778 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
779
780 /**
781 * <p>This view shows vertical fading edges.</p>
782 * {@hide}
783 */
784 static final int FADING_EDGE_VERTICAL = 0x00002000;
785
786 /**
787 * <p>Mask for use with setFlags indicating bits used for indicating which
788 * fading edges are enabled.</p>
789 * {@hide}
790 */
791 static final int FADING_EDGE_MASK = 0x00003000;
792
793 /**
794 * <p>Indicates this view can be clicked. When clickable, a View reacts
795 * to clicks by notifying the OnClickListener.<p>
796 * {@hide}
797 */
798 static final int CLICKABLE = 0x00004000;
799
800 /**
801 * <p>Indicates this view is caching its drawing into a bitmap.</p>
802 * {@hide}
803 */
804 static final int DRAWING_CACHE_ENABLED = 0x00008000;
805
806 /**
807 * <p>Indicates that no icicle should be saved for this view.<p>
808 * {@hide}
809 */
810 static final int SAVE_DISABLED = 0x000010000;
811
812 /**
813 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
814 * property.</p>
815 * {@hide}
816 */
817 static final int SAVE_DISABLED_MASK = 0x000010000;
818
819 /**
820 * <p>Indicates that no drawing cache should ever be created for this view.<p>
821 * {@hide}
822 */
823 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
824
825 /**
826 * <p>Indicates this view can take / keep focus when int touch mode.</p>
827 * {@hide}
828 */
829 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
830
831 /**
832 * <p>Enables low quality mode for the drawing cache.</p>
833 */
834 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
835
836 /**
837 * <p>Enables high quality mode for the drawing cache.</p>
838 */
839 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
840
841 /**
842 * <p>Enables automatic quality mode for the drawing cache.</p>
843 */
844 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
845
846 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
847 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
848 };
849
850 /**
851 * <p>Mask for use with setFlags indicating bits used for the cache
852 * quality property.</p>
853 * {@hide}
854 */
855 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
856
857 /**
858 * <p>
859 * Indicates this view can be long clicked. When long clickable, a View
860 * reacts to long clicks by notifying the OnLongClickListener or showing a
861 * context menu.
862 * </p>
863 * {@hide}
864 */
865 static final int LONG_CLICKABLE = 0x00200000;
866
867 /**
868 * <p>Indicates that this view gets its drawable states from its direct parent
869 * and ignores its original internal states.</p>
870 *
871 * @hide
872 */
873 static final int DUPLICATE_PARENT_STATE = 0x00400000;
874
875 /**
876 * The scrollbar style to display the scrollbars inside the content area,
877 * without increasing the padding. The scrollbars will be overlaid with
878 * translucency on the view's content.
879 */
880 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
881
882 /**
883 * The scrollbar style to display the scrollbars inside the padded area,
884 * increasing the padding of the view. The scrollbars will not overlap the
885 * content area of the view.
886 */
887 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
888
889 /**
890 * The scrollbar style to display the scrollbars at the edge of the view,
891 * without increasing the padding. The scrollbars will be overlaid with
892 * translucency.
893 */
894 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
895
896 /**
897 * The scrollbar style to display the scrollbars at the edge of the view,
898 * increasing the padding of the view. The scrollbars will only overlap the
899 * background, if any.
900 */
901 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
902
903 /**
904 * Mask to check if the scrollbar style is overlay or inset.
905 * {@hide}
906 */
907 static final int SCROLLBARS_INSET_MASK = 0x01000000;
908
909 /**
910 * Mask to check if the scrollbar style is inside or outside.
911 * {@hide}
912 */
913 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
914
915 /**
916 * Mask for scrollbar style.
917 * {@hide}
918 */
919 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
920
921 /**
922 * View flag indicating that the screen should remain on while the
923 * window containing this view is visible to the user. This effectively
924 * takes care of automatically setting the WindowManager's
925 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
926 */
927 public static final int KEEP_SCREEN_ON = 0x04000000;
928
929 /**
930 * View flag indicating whether this view should have sound effects enabled
931 * for events such as clicking and touching.
932 */
933 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
934
935 /**
936 * View flag indicating whether this view should have haptic feedback
937 * enabled for events such as long presses.
938 */
939 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
940
941 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700942 * <p>Indicates that the view hierarchy should stop saving state when
943 * it reaches this view. If state saving is initiated immediately at
944 * the view, it will be allowed.
945 * {@hide}
946 */
947 static final int PARENT_SAVE_DISABLED = 0x20000000;
948
949 /**
950 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
951 * {@hide}
952 */
953 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
954
955 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800956 * Horizontal direction of this view is from Left to Right.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700957 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800958 * {@hide}
959 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700960 public static final int LAYOUT_DIRECTION_LTR = 0x00000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800961
962 /**
963 * Horizontal direction of this view is from Right to Left.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700964 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800965 * {@hide}
966 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700967 public static final int LAYOUT_DIRECTION_RTL = 0x40000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800968
969 /**
970 * Horizontal direction of this view is inherited from its parent.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700971 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800972 * {@hide}
973 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700974 public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800975
976 /**
977 * Horizontal direction of this view is from deduced from the default language
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700978 * script for the locale. Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800979 * {@hide}
980 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700981 public static final int LAYOUT_DIRECTION_LOCALE = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800982
983 /**
984 * Mask for use with setFlags indicating bits used for horizontalDirection.
985 * {@hide}
986 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700987 static final int LAYOUT_DIRECTION_MASK = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800988
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700989 /*
990 * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct
991 * flag value.
992 * {@hide}
993 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700994 private static final int[] LAYOUT_DIRECTION_FLAGS = {LAYOUT_DIRECTION_LTR,
995 LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE};
Cibu Johny7632cb92010-02-22 13:01:02 -0800996
997 /**
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700998 * Default horizontalDirection.
999 * {@hide}
1000 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07001001 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001002
1003 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001004 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1005 * should add all focusable Views regardless if they are focusable in touch mode.
1006 */
1007 public static final int FOCUSABLES_ALL = 0x00000000;
1008
1009 /**
1010 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1011 * should add only Views focusable in touch mode.
1012 */
1013 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1014
1015 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001016 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 * item.
1018 */
1019 public static final int FOCUS_BACKWARD = 0x00000001;
1020
1021 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001022 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 * item.
1024 */
1025 public static final int FOCUS_FORWARD = 0x00000002;
1026
1027 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001028 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 */
1030 public static final int FOCUS_LEFT = 0x00000011;
1031
1032 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001033 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 */
1035 public static final int FOCUS_UP = 0x00000021;
1036
1037 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001038 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 */
1040 public static final int FOCUS_RIGHT = 0x00000042;
1041
1042 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001043 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 */
1045 public static final int FOCUS_DOWN = 0x00000082;
1046
1047 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001048 * Bits of {@link #getMeasuredWidthAndState()} and
1049 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1050 */
1051 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1052
1053 /**
1054 * Bits of {@link #getMeasuredWidthAndState()} and
1055 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1056 */
1057 public static final int MEASURED_STATE_MASK = 0xff000000;
1058
1059 /**
1060 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1061 * for functions that combine both width and height into a single int,
1062 * such as {@link #getMeasuredState()} and the childState argument of
1063 * {@link #resolveSizeAndState(int, int, int)}.
1064 */
1065 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1066
1067 /**
1068 * Bit of {@link #getMeasuredWidthAndState()} and
1069 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1070 * is smaller that the space the view would like to have.
1071 */
1072 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1073
1074 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 * Base View state sets
1076 */
1077 // Singles
1078 /**
1079 * Indicates the view has no states set. States are used with
1080 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1081 * view depending on its state.
1082 *
1083 * @see android.graphics.drawable.Drawable
1084 * @see #getDrawableState()
1085 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001086 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 /**
1088 * Indicates the view is enabled. States are used with
1089 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1090 * view depending on its state.
1091 *
1092 * @see android.graphics.drawable.Drawable
1093 * @see #getDrawableState()
1094 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001095 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 /**
1097 * Indicates the view is focused. States are used with
1098 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1099 * view depending on its state.
1100 *
1101 * @see android.graphics.drawable.Drawable
1102 * @see #getDrawableState()
1103 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001104 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 /**
1106 * Indicates the view is selected. States are used with
1107 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1108 * view depending on its state.
1109 *
1110 * @see android.graphics.drawable.Drawable
1111 * @see #getDrawableState()
1112 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001113 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 /**
1115 * Indicates the view is pressed. States are used with
1116 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1117 * view depending on its state.
1118 *
1119 * @see android.graphics.drawable.Drawable
1120 * @see #getDrawableState()
1121 * @hide
1122 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001123 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 /**
1125 * Indicates the view's window has focus. States are used with
1126 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1127 * view depending on its state.
1128 *
1129 * @see android.graphics.drawable.Drawable
1130 * @see #getDrawableState()
1131 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001132 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 // Doubles
1134 /**
1135 * Indicates the view is enabled and has the focus.
1136 *
1137 * @see #ENABLED_STATE_SET
1138 * @see #FOCUSED_STATE_SET
1139 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001140 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 /**
1142 * Indicates the view is enabled and selected.
1143 *
1144 * @see #ENABLED_STATE_SET
1145 * @see #SELECTED_STATE_SET
1146 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001147 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 /**
1149 * Indicates the view is enabled and that its window has focus.
1150 *
1151 * @see #ENABLED_STATE_SET
1152 * @see #WINDOW_FOCUSED_STATE_SET
1153 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001154 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 /**
1156 * Indicates the view is focused and selected.
1157 *
1158 * @see #FOCUSED_STATE_SET
1159 * @see #SELECTED_STATE_SET
1160 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001161 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 /**
1163 * Indicates the view has the focus and that its window has the focus.
1164 *
1165 * @see #FOCUSED_STATE_SET
1166 * @see #WINDOW_FOCUSED_STATE_SET
1167 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001168 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 /**
1170 * Indicates the view is selected and that its window has the focus.
1171 *
1172 * @see #SELECTED_STATE_SET
1173 * @see #WINDOW_FOCUSED_STATE_SET
1174 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001175 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 // Triples
1177 /**
1178 * Indicates the view is enabled, focused and selected.
1179 *
1180 * @see #ENABLED_STATE_SET
1181 * @see #FOCUSED_STATE_SET
1182 * @see #SELECTED_STATE_SET
1183 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001184 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 /**
1186 * Indicates the view is enabled, focused and its window has the focus.
1187 *
1188 * @see #ENABLED_STATE_SET
1189 * @see #FOCUSED_STATE_SET
1190 * @see #WINDOW_FOCUSED_STATE_SET
1191 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001192 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 /**
1194 * Indicates the view is enabled, selected and its window has the focus.
1195 *
1196 * @see #ENABLED_STATE_SET
1197 * @see #SELECTED_STATE_SET
1198 * @see #WINDOW_FOCUSED_STATE_SET
1199 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001200 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 /**
1202 * Indicates the view is focused, selected and its window has the focus.
1203 *
1204 * @see #FOCUSED_STATE_SET
1205 * @see #SELECTED_STATE_SET
1206 * @see #WINDOW_FOCUSED_STATE_SET
1207 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001208 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 /**
1210 * Indicates the view is enabled, focused, selected and its window
1211 * has the focus.
1212 *
1213 * @see #ENABLED_STATE_SET
1214 * @see #FOCUSED_STATE_SET
1215 * @see #SELECTED_STATE_SET
1216 * @see #WINDOW_FOCUSED_STATE_SET
1217 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001218 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 /**
1220 * Indicates the view is pressed and its window has the focus.
1221 *
1222 * @see #PRESSED_STATE_SET
1223 * @see #WINDOW_FOCUSED_STATE_SET
1224 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001225 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 /**
1227 * Indicates the view is pressed and selected.
1228 *
1229 * @see #PRESSED_STATE_SET
1230 * @see #SELECTED_STATE_SET
1231 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001232 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 /**
1234 * Indicates the view is pressed, selected and its window has the focus.
1235 *
1236 * @see #PRESSED_STATE_SET
1237 * @see #SELECTED_STATE_SET
1238 * @see #WINDOW_FOCUSED_STATE_SET
1239 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001240 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 /**
1242 * Indicates the view is pressed and focused.
1243 *
1244 * @see #PRESSED_STATE_SET
1245 * @see #FOCUSED_STATE_SET
1246 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001247 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 /**
1249 * Indicates the view is pressed, focused and its window has the focus.
1250 *
1251 * @see #PRESSED_STATE_SET
1252 * @see #FOCUSED_STATE_SET
1253 * @see #WINDOW_FOCUSED_STATE_SET
1254 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001255 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 /**
1257 * Indicates the view is pressed, focused and selected.
1258 *
1259 * @see #PRESSED_STATE_SET
1260 * @see #SELECTED_STATE_SET
1261 * @see #FOCUSED_STATE_SET
1262 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001263 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 /**
1265 * Indicates the view is pressed, focused, selected and its window has the focus.
1266 *
1267 * @see #PRESSED_STATE_SET
1268 * @see #FOCUSED_STATE_SET
1269 * @see #SELECTED_STATE_SET
1270 * @see #WINDOW_FOCUSED_STATE_SET
1271 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001272 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 /**
1274 * Indicates the view is pressed and enabled.
1275 *
1276 * @see #PRESSED_STATE_SET
1277 * @see #ENABLED_STATE_SET
1278 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001279 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 /**
1281 * Indicates the view is pressed, enabled and its window has the focus.
1282 *
1283 * @see #PRESSED_STATE_SET
1284 * @see #ENABLED_STATE_SET
1285 * @see #WINDOW_FOCUSED_STATE_SET
1286 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001287 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 /**
1289 * Indicates the view is pressed, enabled and selected.
1290 *
1291 * @see #PRESSED_STATE_SET
1292 * @see #ENABLED_STATE_SET
1293 * @see #SELECTED_STATE_SET
1294 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001295 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 /**
1297 * Indicates the view is pressed, enabled, selected and its window has the
1298 * focus.
1299 *
1300 * @see #PRESSED_STATE_SET
1301 * @see #ENABLED_STATE_SET
1302 * @see #SELECTED_STATE_SET
1303 * @see #WINDOW_FOCUSED_STATE_SET
1304 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001305 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 /**
1307 * Indicates the view is pressed, enabled and focused.
1308 *
1309 * @see #PRESSED_STATE_SET
1310 * @see #ENABLED_STATE_SET
1311 * @see #FOCUSED_STATE_SET
1312 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001313 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 /**
1315 * Indicates the view is pressed, enabled, focused and its window has the
1316 * focus.
1317 *
1318 * @see #PRESSED_STATE_SET
1319 * @see #ENABLED_STATE_SET
1320 * @see #FOCUSED_STATE_SET
1321 * @see #WINDOW_FOCUSED_STATE_SET
1322 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001323 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 /**
1325 * Indicates the view is pressed, enabled, focused and selected.
1326 *
1327 * @see #PRESSED_STATE_SET
1328 * @see #ENABLED_STATE_SET
1329 * @see #SELECTED_STATE_SET
1330 * @see #FOCUSED_STATE_SET
1331 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001332 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 /**
1334 * Indicates the view is pressed, enabled, focused, selected and its window
1335 * has the focus.
1336 *
1337 * @see #PRESSED_STATE_SET
1338 * @see #ENABLED_STATE_SET
1339 * @see #SELECTED_STATE_SET
1340 * @see #FOCUSED_STATE_SET
1341 * @see #WINDOW_FOCUSED_STATE_SET
1342 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001343 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344
1345 /**
1346 * The order here is very important to {@link #getDrawableState()}
1347 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001348 private static final int[][] VIEW_STATE_SETS;
1349
Romain Guyb051e892010-09-28 19:09:36 -07001350 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1351 static final int VIEW_STATE_SELECTED = 1 << 1;
1352 static final int VIEW_STATE_FOCUSED = 1 << 2;
1353 static final int VIEW_STATE_ENABLED = 1 << 3;
1354 static final int VIEW_STATE_PRESSED = 1 << 4;
1355 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001356 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001357 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001358 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1359 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001360
1361 static final int[] VIEW_STATE_IDS = new int[] {
1362 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1363 R.attr.state_selected, VIEW_STATE_SELECTED,
1364 R.attr.state_focused, VIEW_STATE_FOCUSED,
1365 R.attr.state_enabled, VIEW_STATE_ENABLED,
1366 R.attr.state_pressed, VIEW_STATE_PRESSED,
1367 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001368 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001369 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001370 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1371 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 };
1373
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001374 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001375 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1376 throw new IllegalStateException(
1377 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1378 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001379 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001380 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001381 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001382 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001383 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001384 orderedIds[i * 2] = viewState;
1385 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001386 }
1387 }
1388 }
Romain Guyb051e892010-09-28 19:09:36 -07001389 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1390 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1391 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001392 int numBits = Integer.bitCount(i);
1393 int[] set = new int[numBits];
1394 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001395 for (int j = 0; j < orderedIds.length; j += 2) {
1396 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001397 set[pos++] = orderedIds[j];
1398 }
1399 }
1400 VIEW_STATE_SETS[i] = set;
1401 }
1402
1403 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1404 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1405 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1406 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1407 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1408 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1409 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1410 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1411 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1412 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1413 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1414 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1415 | VIEW_STATE_FOCUSED];
1416 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1417 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1418 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1419 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1421 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1422 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1423 | VIEW_STATE_ENABLED];
1424 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1425 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1426 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1427 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1428 | VIEW_STATE_ENABLED];
1429 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1430 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1431 | VIEW_STATE_ENABLED];
1432 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1433 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1434 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1435
1436 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1437 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1438 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1439 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1440 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1441 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1442 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1443 | VIEW_STATE_PRESSED];
1444 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1445 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1446 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1447 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1448 | VIEW_STATE_PRESSED];
1449 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1451 | VIEW_STATE_PRESSED];
1452 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1453 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1454 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1455 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1457 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1459 | VIEW_STATE_PRESSED];
1460 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1462 | VIEW_STATE_PRESSED];
1463 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1464 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1465 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1466 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1467 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1468 | VIEW_STATE_PRESSED];
1469 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1470 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1471 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1472 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1473 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1474 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1475 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1476 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1477 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1478 | VIEW_STATE_PRESSED];
1479 }
1480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 * Temporary Rect currently for use in setBackground(). This will probably
1483 * be extended in the future to hold our own class with more than just
1484 * a Rect. :)
1485 */
1486 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001487
1488 /**
1489 * Map used to store views' tags.
1490 */
1491 private static WeakHashMap<View, SparseArray<Object>> sTags;
1492
1493 /**
1494 * Lock used to access sTags.
1495 */
1496 private static final Object sTagsLock = new Object();
1497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001499 * The next available accessiiblity id.
1500 */
1501 private static int sNextAccessibilityViewId;
1502
1503 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 * The animation currently associated with this view.
1505 * @hide
1506 */
1507 protected Animation mCurrentAnimation = null;
1508
1509 /**
1510 * Width as measured during measure pass.
1511 * {@hide}
1512 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001513 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001514 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515
1516 /**
1517 * Height as measured during measure pass.
1518 * {@hide}
1519 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001520 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001521 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522
1523 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001524 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1525 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1526 * its display list. This flag, used only when hw accelerated, allows us to clear the
1527 * flag while retaining this information until it's needed (at getDisplayList() time and
1528 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1529 *
1530 * {@hide}
1531 */
1532 boolean mRecreateDisplayList = false;
1533
1534 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 * The view's identifier.
1536 * {@hide}
1537 *
1538 * @see #setId(int)
1539 * @see #getId()
1540 */
1541 @ViewDebug.ExportedProperty(resolveId = true)
1542 int mID = NO_ID;
1543
1544 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001545 * The stable ID of this view for accessibility porposes.
1546 */
1547 int mAccessibilityViewId = NO_ID;
1548
1549 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 * The view's tag.
1551 * {@hide}
1552 *
1553 * @see #setTag(Object)
1554 * @see #getTag()
1555 */
1556 protected Object mTag;
1557
1558 // for mPrivateFlags:
1559 /** {@hide} */
1560 static final int WANTS_FOCUS = 0x00000001;
1561 /** {@hide} */
1562 static final int FOCUSED = 0x00000002;
1563 /** {@hide} */
1564 static final int SELECTED = 0x00000004;
1565 /** {@hide} */
1566 static final int IS_ROOT_NAMESPACE = 0x00000008;
1567 /** {@hide} */
1568 static final int HAS_BOUNDS = 0x00000010;
1569 /** {@hide} */
1570 static final int DRAWN = 0x00000020;
1571 /**
1572 * When this flag is set, this view is running an animation on behalf of its
1573 * children and should therefore not cancel invalidate requests, even if they
1574 * lie outside of this view's bounds.
1575 *
1576 * {@hide}
1577 */
1578 static final int DRAW_ANIMATION = 0x00000040;
1579 /** {@hide} */
1580 static final int SKIP_DRAW = 0x00000080;
1581 /** {@hide} */
1582 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1583 /** {@hide} */
1584 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1585 /** {@hide} */
1586 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1587 /** {@hide} */
1588 static final int MEASURED_DIMENSION_SET = 0x00000800;
1589 /** {@hide} */
1590 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001591 /** {@hide} */
1592 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593
1594 private static final int PRESSED = 0x00004000;
1595
1596 /** {@hide} */
1597 static final int DRAWING_CACHE_VALID = 0x00008000;
1598 /**
1599 * Flag used to indicate that this view should be drawn once more (and only once
1600 * more) after its animation has completed.
1601 * {@hide}
1602 */
1603 static final int ANIMATION_STARTED = 0x00010000;
1604
1605 private static final int SAVE_STATE_CALLED = 0x00020000;
1606
1607 /**
1608 * Indicates that the View returned true when onSetAlpha() was called and that
1609 * the alpha must be restored.
1610 * {@hide}
1611 */
1612 static final int ALPHA_SET = 0x00040000;
1613
1614 /**
1615 * Set by {@link #setScrollContainer(boolean)}.
1616 */
1617 static final int SCROLL_CONTAINER = 0x00080000;
1618
1619 /**
1620 * Set by {@link #setScrollContainer(boolean)}.
1621 */
1622 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1623
1624 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001625 * View flag indicating whether this view was invalidated (fully or partially.)
1626 *
1627 * @hide
1628 */
1629 static final int DIRTY = 0x00200000;
1630
1631 /**
1632 * View flag indicating whether this view was invalidated by an opaque
1633 * invalidate request.
1634 *
1635 * @hide
1636 */
1637 static final int DIRTY_OPAQUE = 0x00400000;
1638
1639 /**
1640 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1641 *
1642 * @hide
1643 */
1644 static final int DIRTY_MASK = 0x00600000;
1645
1646 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001647 * Indicates whether the background is opaque.
1648 *
1649 * @hide
1650 */
1651 static final int OPAQUE_BACKGROUND = 0x00800000;
1652
1653 /**
1654 * Indicates whether the scrollbars are opaque.
1655 *
1656 * @hide
1657 */
1658 static final int OPAQUE_SCROLLBARS = 0x01000000;
1659
1660 /**
1661 * Indicates whether the view is opaque.
1662 *
1663 * @hide
1664 */
1665 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001666
Adam Powelle14579b2009-12-16 18:39:52 -08001667 /**
1668 * Indicates a prepressed state;
1669 * the short time between ACTION_DOWN and recognizing
1670 * a 'real' press. Prepressed is used to recognize quick taps
1671 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001672 *
Adam Powelle14579b2009-12-16 18:39:52 -08001673 * @hide
1674 */
1675 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001676
Adam Powellc9fbaab2010-02-16 17:16:19 -08001677 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001678 * Indicates whether the view is temporarily detached.
1679 *
1680 * @hide
1681 */
1682 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001683
Adam Powell8568c3a2010-04-19 14:26:11 -07001684 /**
1685 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001686 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001687 * @hide
1688 */
1689 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001690
1691 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001692 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1693 * @hide
1694 */
1695 private static final int HOVERED = 0x10000000;
1696
1697 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001698 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1699 * for transform operations
1700 *
1701 * @hide
1702 */
Adam Powellf37df072010-09-17 16:22:49 -07001703 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001704
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001705 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001706 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001707
Chet Haasefd2b0022010-08-06 13:08:56 -07001708 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001709 * Indicates that this view was specifically invalidated, not just dirtied because some
1710 * child view was invalidated. The flag is used to determine when we need to recreate
1711 * a view's display list (as opposed to just returning a reference to its existing
1712 * display list).
1713 *
1714 * @hide
1715 */
1716 static final int INVALIDATED = 0x80000000;
1717
Christopher Tate3d4bf172011-03-28 16:16:46 -07001718 /* Masks for mPrivateFlags2 */
1719
1720 /**
1721 * Indicates that this view has reported that it can accept the current drag's content.
1722 * Cleared when the drag operation concludes.
1723 * @hide
1724 */
1725 static final int DRAG_CAN_ACCEPT = 0x00000001;
1726
1727 /**
1728 * Indicates that this view is currently directly under the drag location in a
1729 * drag-and-drop operation involving content that it can accept. Cleared when
1730 * the drag exits the view, or when the drag operation concludes.
1731 * @hide
1732 */
1733 static final int DRAG_HOVERED = 0x00000002;
1734
Cibu Johny86666632010-02-22 13:01:02 -08001735 /**
1736 * Indicates whether the view is drawn in right-to-left direction.
1737 *
1738 * @hide
1739 */
1740 static final int RESOLVED_LAYOUT_RTL = 0x00000004;
1741
Christopher Tate3d4bf172011-03-28 16:16:46 -07001742 /* End of masks for mPrivateFlags2 */
1743
1744 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1745
Chet Haasedaf98e92011-01-10 14:10:36 -08001746 /**
Adam Powell637d3372010-08-25 14:37:03 -07001747 * Always allow a user to over-scroll this view, provided it is a
1748 * view that can scroll.
1749 *
1750 * @see #getOverScrollMode()
1751 * @see #setOverScrollMode(int)
1752 */
1753 public static final int OVER_SCROLL_ALWAYS = 0;
1754
1755 /**
1756 * Allow a user to over-scroll this view only if the content is large
1757 * enough to meaningfully scroll, provided it is a view that can scroll.
1758 *
1759 * @see #getOverScrollMode()
1760 * @see #setOverScrollMode(int)
1761 */
1762 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1763
1764 /**
1765 * Never allow a user to over-scroll this view.
1766 *
1767 * @see #getOverScrollMode()
1768 * @see #setOverScrollMode(int)
1769 */
1770 public static final int OVER_SCROLL_NEVER = 2;
1771
1772 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001773 * View has requested the status bar to be visible (the default).
1774 *
Joe Malin32736f02011-01-19 16:14:20 -08001775 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001776 */
1777 public static final int STATUS_BAR_VISIBLE = 0;
1778
1779 /**
Jeff Brown05dc66a2011-03-02 14:41:58 -08001780 * View has requested the status bar to be hidden.
Joe Onorato664644d2011-01-23 17:53:23 -08001781 *
Joe Malin32736f02011-01-19 16:14:20 -08001782 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001783 */
1784 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1785
1786 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001787 * @hide
1788 *
1789 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1790 * out of the public fields to keep the undefined bits out of the developer's way.
1791 *
1792 * Flag to make the status bar not expandable. Unless you also
1793 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1794 */
1795 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1796
1797 /**
1798 * @hide
1799 *
1800 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1801 * out of the public fields to keep the undefined bits out of the developer's way.
1802 *
1803 * Flag to hide notification icons and scrolling ticker text.
1804 */
1805 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1806
1807 /**
1808 * @hide
1809 *
1810 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1811 * out of the public fields to keep the undefined bits out of the developer's way.
1812 *
1813 * Flag to disable incoming notification alerts. This will not block
1814 * icons, but it will block sound, vibrating and other visual or aural notifications.
1815 */
1816 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1817
1818 /**
1819 * @hide
1820 *
1821 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1822 * out of the public fields to keep the undefined bits out of the developer's way.
1823 *
1824 * Flag to hide only the scrolling ticker. Note that
1825 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1826 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1827 */
1828 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
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 the center system info area.
1837 */
1838 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1839
1840 /**
1841 * @hide
1842 *
1843 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1844 * out of the public fields to keep the undefined bits out of the developer's way.
1845 *
1846 * Flag to hide only the navigation buttons. Don't use this
1847 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001848 *
1849 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001850 */
1851 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1852
1853 /**
1854 * @hide
1855 *
1856 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1857 * out of the public fields to keep the undefined bits out of the developer's way.
1858 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001859 * Flag to hide only the back button. Don't use this
1860 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1861 */
1862 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1863
1864 /**
1865 * @hide
1866 *
1867 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1868 * out of the public fields to keep the undefined bits out of the developer's way.
1869 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001870 * Flag to hide only the clock. You might use this if your activity has
1871 * its own clock making the status bar's clock redundant.
1872 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001873 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1874
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001875 /**
1876 * @hide
1877 */
1878 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001879
1880 /**
Adam Powell637d3372010-08-25 14:37:03 -07001881 * Controls the over-scroll mode for this view.
1882 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1883 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1884 * and {@link #OVER_SCROLL_NEVER}.
1885 */
1886 private int mOverScrollMode;
1887
1888 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 * The parent this view is attached to.
1890 * {@hide}
1891 *
1892 * @see #getParent()
1893 */
1894 protected ViewParent mParent;
1895
1896 /**
1897 * {@hide}
1898 */
1899 AttachInfo mAttachInfo;
1900
1901 /**
1902 * {@hide}
1903 */
Romain Guy809a7f62009-05-14 15:44:42 -07001904 @ViewDebug.ExportedProperty(flagMapping = {
1905 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1906 name = "FORCE_LAYOUT"),
1907 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1908 name = "LAYOUT_REQUIRED"),
1909 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001910 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001911 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1912 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1913 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1914 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1915 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001917 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918
1919 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001920 * This view's request for the visibility of the status bar.
1921 * @hide
1922 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001923 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001924 int mSystemUiVisibility;
1925
1926 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 * Count of how many windows this view has been attached to.
1928 */
1929 int mWindowAttachCount;
1930
1931 /**
1932 * The layout parameters associated with this view and used by the parent
1933 * {@link android.view.ViewGroup} to determine how this view should be
1934 * laid out.
1935 * {@hide}
1936 */
1937 protected ViewGroup.LayoutParams mLayoutParams;
1938
1939 /**
1940 * The view flags hold various views states.
1941 * {@hide}
1942 */
1943 @ViewDebug.ExportedProperty
1944 int mViewFlags;
1945
1946 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001947 * The transform matrix for the View. This transform is calculated internally
1948 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1949 * is used by default. Do *not* use this variable directly; instead call
1950 * getMatrix(), which will automatically recalculate the matrix if necessary
1951 * to get the correct matrix based on the latest rotation and scale properties.
1952 */
1953 private final Matrix mMatrix = new Matrix();
1954
1955 /**
1956 * The transform matrix for the View. This transform is calculated internally
1957 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1958 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001959 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001960 * to get the correct matrix based on the latest rotation and scale properties.
1961 */
1962 private Matrix mInverseMatrix;
1963
1964 /**
1965 * An internal variable that tracks whether we need to recalculate the
1966 * transform matrix, based on whether the rotation or scaleX/Y properties
1967 * have changed since the matrix was last calculated.
1968 */
Chet Haasea00f3862011-02-22 06:34:40 -08001969 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07001970
1971 /**
1972 * An internal variable that tracks whether we need to recalculate the
1973 * transform matrix, based on whether the rotation or scaleX/Y properties
1974 * have changed since the matrix was last calculated.
1975 */
1976 private boolean mInverseMatrixDirty = true;
1977
1978 /**
1979 * A variable that tracks whether we need to recalculate the
1980 * transform matrix, based on whether the rotation or scaleX/Y properties
1981 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001982 * is only valid after a call to updateMatrix() or to a function that
1983 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001984 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001985 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001986
1987 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001988 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
1989 */
1990 private Camera mCamera = null;
1991
1992 /**
1993 * This matrix is used when computing the matrix for 3D rotations.
1994 */
1995 private Matrix matrix3D = null;
1996
1997 /**
1998 * These prev values are used to recalculate a centered pivot point when necessary. The
1999 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2000 * set), so thes values are only used then as well.
2001 */
2002 private int mPrevWidth = -1;
2003 private int mPrevHeight = -1;
2004
Joe Malin32736f02011-01-19 16:14:20 -08002005 private boolean mLastIsOpaque;
2006
Chet Haasefd2b0022010-08-06 13:08:56 -07002007 /**
2008 * Convenience value to check for float values that are close enough to zero to be considered
2009 * zero.
2010 */
Romain Guy2542d192010-08-18 11:47:12 -07002011 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002012
2013 /**
2014 * The degrees rotation around the vertical axis through the pivot point.
2015 */
2016 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002017 float mRotationY = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002018
2019 /**
2020 * The degrees rotation around the horizontal axis through the pivot point.
2021 */
2022 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002023 float mRotationX = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002024
2025 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002026 * The degrees rotation around the pivot point.
2027 */
2028 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002029 float mRotation = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002030
2031 /**
Chet Haasedf030d22010-07-30 17:22:38 -07002032 * The amount of translation of the object away from its left property (post-layout).
2033 */
2034 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002035 float mTranslationX = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002036
2037 /**
2038 * The amount of translation of the object away from its top property (post-layout).
2039 */
2040 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002041 float mTranslationY = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002042
2043 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002044 * The amount of scale in the x direction around the pivot point. A
2045 * value of 1 means no scaling is applied.
2046 */
2047 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002048 float mScaleX = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002049
2050 /**
2051 * The amount of scale in the y direction around the pivot point. A
2052 * value of 1 means no scaling is applied.
2053 */
2054 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002055 float mScaleY = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002056
2057 /**
2058 * The amount of scale in the x direction around the pivot point. A
2059 * value of 1 means no scaling is applied.
2060 */
2061 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002062 float mPivotX = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002063
2064 /**
2065 * The amount of scale in the y direction around the pivot point. A
2066 * value of 1 means no scaling is applied.
2067 */
2068 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002069 float mPivotY = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002070
2071 /**
2072 * The opacity of the View. This is a value from 0 to 1, where 0 means
2073 * completely transparent and 1 means completely opaque.
2074 */
2075 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002076 float mAlpha = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002077
2078 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 * The distance in pixels from the left edge of this view's parent
2080 * to the left edge of this view.
2081 * {@hide}
2082 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002083 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 protected int mLeft;
2085 /**
2086 * The distance in pixels from the left edge of this view's parent
2087 * to the right edge of this view.
2088 * {@hide}
2089 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002090 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 protected int mRight;
2092 /**
2093 * The distance in pixels from the top edge of this view's parent
2094 * to the top edge of this view.
2095 * {@hide}
2096 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002097 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 protected int mTop;
2099 /**
2100 * The distance in pixels from the top edge of this view's parent
2101 * to the bottom edge of this view.
2102 * {@hide}
2103 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002104 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 protected int mBottom;
2106
2107 /**
2108 * The offset, in pixels, by which the content of this view is scrolled
2109 * horizontally.
2110 * {@hide}
2111 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002112 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 protected int mScrollX;
2114 /**
2115 * The offset, in pixels, by which the content of this view is scrolled
2116 * vertically.
2117 * {@hide}
2118 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002119 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 protected int mScrollY;
2121
2122 /**
2123 * The left padding in pixels, that is the distance in pixels between the
2124 * left edge of this view and the left edge of its content.
2125 * {@hide}
2126 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002127 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 protected int mPaddingLeft;
2129 /**
2130 * The right padding in pixels, that is the distance in pixels between the
2131 * right edge of this view and the right edge of its content.
2132 * {@hide}
2133 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002134 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 protected int mPaddingRight;
2136 /**
2137 * The top padding in pixels, that is the distance in pixels between the
2138 * top edge of this view and the top edge of its content.
2139 * {@hide}
2140 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002141 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 protected int mPaddingTop;
2143 /**
2144 * The bottom padding in pixels, that is the distance in pixels between the
2145 * bottom edge of this view and the bottom edge of its content.
2146 * {@hide}
2147 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002148 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 protected int mPaddingBottom;
2150
2151 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002152 * Briefly describes the view and is primarily used for accessibility support.
2153 */
2154 private CharSequence mContentDescription;
2155
2156 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 * Cache the paddingRight set by the user to append to the scrollbar's size.
2158 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002159 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 int mUserPaddingRight;
2161
2162 /**
2163 * Cache the paddingBottom set by the user to append to the scrollbar's size.
2164 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002165 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 int mUserPaddingBottom;
2167
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002168 /**
Adam Powell20232d02010-12-08 21:08:53 -08002169 * Cache the paddingLeft set by the user to append to the scrollbar's size.
2170 */
2171 @ViewDebug.ExportedProperty(category = "padding")
2172 int mUserPaddingLeft;
2173
2174 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002175 * @hide
2176 */
2177 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2178 /**
2179 * @hide
2180 */
2181 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182
2183 private Resources mResources = null;
2184
2185 private Drawable mBGDrawable;
2186
2187 private int mBackgroundResource;
2188 private boolean mBackgroundSizeChanged;
2189
2190 /**
2191 * Listener used to dispatch focus change events.
2192 * This field should be made private, so it is hidden from the SDK.
2193 * {@hide}
2194 */
2195 protected OnFocusChangeListener mOnFocusChangeListener;
2196
2197 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002198 * Listeners for layout change events.
2199 */
2200 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2201
2202 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002203 * Listeners for attach events.
2204 */
2205 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2206
2207 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 * Listener used to dispatch click events.
2209 * This field should be made private, so it is hidden from the SDK.
2210 * {@hide}
2211 */
2212 protected OnClickListener mOnClickListener;
2213
2214 /**
2215 * Listener used to dispatch long click events.
2216 * This field should be made private, so it is hidden from the SDK.
2217 * {@hide}
2218 */
2219 protected OnLongClickListener mOnLongClickListener;
2220
2221 /**
2222 * Listener used to build the context menu.
2223 * This field should be made private, so it is hidden from the SDK.
2224 * {@hide}
2225 */
2226 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2227
2228 private OnKeyListener mOnKeyListener;
2229
2230 private OnTouchListener mOnTouchListener;
2231
Jeff Brown33bbfd22011-02-24 20:55:35 -08002232 private OnGenericMotionListener mOnGenericMotionListener;
2233
Chris Tate32affef2010-10-18 15:29:21 -07002234 private OnDragListener mOnDragListener;
2235
Joe Onorato664644d2011-01-23 17:53:23 -08002236 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 /**
2239 * The application environment this view lives in.
2240 * This field should be made private, so it is hidden from the SDK.
2241 * {@hide}
2242 */
2243 protected Context mContext;
2244
2245 private ScrollabilityCache mScrollCache;
2246
2247 private int[] mDrawableState = null;
2248
Romain Guy0211a0a2011-02-14 16:34:59 -08002249 /**
2250 * Set to true when drawing cache is enabled and cannot be created.
2251 *
2252 * @hide
2253 */
2254 public boolean mCachingFailed;
2255
Romain Guy02890fd2010-08-06 17:58:44 -07002256 private Bitmap mDrawingCache;
2257 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002258 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002259 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260
2261 /**
2262 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2263 * the user may specify which view to go to next.
2264 */
2265 private int mNextFocusLeftId = View.NO_ID;
2266
2267 /**
2268 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2269 * the user may specify which view to go to next.
2270 */
2271 private int mNextFocusRightId = View.NO_ID;
2272
2273 /**
2274 * When this view has focus and the next focus is {@link #FOCUS_UP},
2275 * the user may specify which view to go to next.
2276 */
2277 private int mNextFocusUpId = View.NO_ID;
2278
2279 /**
2280 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2281 * the user may specify which view to go to next.
2282 */
2283 private int mNextFocusDownId = View.NO_ID;
2284
Jeff Brown4e6319b2010-12-13 10:36:51 -08002285 /**
2286 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2287 * the user may specify which view to go to next.
2288 */
2289 int mNextFocusForwardId = View.NO_ID;
2290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002292 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002293 private PerformClick mPerformClick;
Joe Malin32736f02011-01-19 16:14:20 -08002294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 private UnsetPressedState mUnsetPressedState;
2296
2297 /**
2298 * Whether the long press's action has been invoked. The tap's action is invoked on the
2299 * up event while a long press is invoked as soon as the long press duration is reached, so
2300 * a long press could be performed before the tap is checked, in which case the tap's action
2301 * should not be invoked.
2302 */
2303 private boolean mHasPerformedLongPress;
2304
2305 /**
2306 * The minimum height of the view. We'll try our best to have the height
2307 * of this view to at least this amount.
2308 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002309 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 private int mMinHeight;
2311
2312 /**
2313 * The minimum width of the view. We'll try our best to have the width
2314 * of this view to at least this amount.
2315 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002316 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317 private int mMinWidth;
2318
2319 /**
2320 * The delegate to handle touch events that are physically in this view
2321 * but should be handled by another view.
2322 */
2323 private TouchDelegate mTouchDelegate = null;
2324
2325 /**
2326 * Solid color to use as a background when creating the drawing cache. Enables
2327 * the cache to use 16 bit bitmaps instead of 32 bit.
2328 */
2329 private int mDrawingCacheBackgroundColor = 0;
2330
2331 /**
2332 * Special tree observer used when mAttachInfo is null.
2333 */
2334 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002335
Adam Powelle14579b2009-12-16 18:39:52 -08002336 /**
2337 * Cache the touch slop from the context that created the view.
2338 */
2339 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002342 * Object that handles automatic animation of view properties.
2343 */
2344 private ViewPropertyAnimator mAnimator = null;
2345
2346 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002347 * Flag indicating that a drag can cross window boundaries. When
2348 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2349 * with this flag set, all visible applications will be able to participate
2350 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002351 *
2352 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002353 */
2354 public static final int DRAG_FLAG_GLOBAL = 1;
2355
2356 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002357 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2358 */
2359 private float mVerticalScrollFactor;
2360
2361 /**
Adam Powell20232d02010-12-08 21:08:53 -08002362 * Position of the vertical scroll bar.
2363 */
2364 private int mVerticalScrollbarPosition;
2365
2366 /**
2367 * Position the scroll bar at the default position as determined by the system.
2368 */
2369 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2370
2371 /**
2372 * Position the scroll bar along the left edge.
2373 */
2374 public static final int SCROLLBAR_POSITION_LEFT = 1;
2375
2376 /**
2377 * Position the scroll bar along the right edge.
2378 */
2379 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2380
2381 /**
Romain Guy171c5922011-01-06 10:04:23 -08002382 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002383 *
2384 * @see #getLayerType()
2385 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002386 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002387 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002388 */
2389 public static final int LAYER_TYPE_NONE = 0;
2390
2391 /**
2392 * <p>Indicates that the view has a software layer. A software layer is backed
2393 * by a bitmap and causes the view to be rendered using Android's software
2394 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002395 *
Romain Guy171c5922011-01-06 10:04:23 -08002396 * <p>Software layers have various usages:</p>
2397 * <p>When the application is not using hardware acceleration, a software layer
2398 * is useful to apply a specific color filter and/or blending mode and/or
2399 * translucency to a view and all its children.</p>
2400 * <p>When the application is using hardware acceleration, a software layer
2401 * is useful to render drawing primitives not supported by the hardware
2402 * accelerated pipeline. It can also be used to cache a complex view tree
2403 * into a texture and reduce the complexity of drawing operations. For instance,
2404 * when animating a complex view tree with a translation, a software layer can
2405 * be used to render the view tree only once.</p>
2406 * <p>Software layers should be avoided when the affected view tree updates
2407 * often. Every update will require to re-render the software layer, which can
2408 * potentially be slow (particularly when hardware acceleration is turned on
2409 * since the layer will have to be uploaded into a hardware texture after every
2410 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002411 *
2412 * @see #getLayerType()
2413 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002414 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002415 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002416 */
2417 public static final int LAYER_TYPE_SOFTWARE = 1;
2418
2419 /**
2420 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2421 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2422 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2423 * rendering pipeline, but only if hardware acceleration is turned on for the
2424 * view hierarchy. When hardware acceleration is turned off, hardware layers
2425 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002426 *
Romain Guy171c5922011-01-06 10:04:23 -08002427 * <p>A hardware layer is useful to apply a specific color filter and/or
2428 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002429 * <p>A hardware layer can be used to cache a complex view tree into a
2430 * texture and reduce the complexity of drawing operations. For instance,
2431 * when animating a complex view tree with a translation, a hardware layer can
2432 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002433 * <p>A hardware layer can also be used to increase the rendering quality when
2434 * rotation transformations are applied on a view. It can also be used to
2435 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002436 *
2437 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002438 * @see #setLayerType(int, android.graphics.Paint)
2439 * @see #LAYER_TYPE_NONE
2440 * @see #LAYER_TYPE_SOFTWARE
2441 */
2442 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002443
Romain Guy3aaff3a2011-01-12 14:18:47 -08002444 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2445 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2446 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2447 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2448 })
Romain Guy171c5922011-01-06 10:04:23 -08002449 int mLayerType = LAYER_TYPE_NONE;
2450 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002451 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002452
2453 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002454 * Consistency verifier for debugging purposes.
2455 * @hide
2456 */
2457 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2458 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2459 new InputEventConsistencyVerifier(this, 0) : null;
2460
2461 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 * Simple constructor to use when creating a view from code.
2463 *
2464 * @param context The Context the view is running in, through which it can
2465 * access the current theme, resources, etc.
2466 */
2467 public View(Context context) {
2468 mContext = context;
2469 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002470 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002471 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002472 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 }
2474
2475 /**
2476 * Constructor that is called when inflating a view from XML. This is called
2477 * when a view is being constructed from an XML file, supplying attributes
2478 * that were specified in the XML file. This version uses a default style of
2479 * 0, so the only attribute values applied are those in the Context's Theme
2480 * and the given AttributeSet.
2481 *
2482 * <p>
2483 * The method onFinishInflate() will be called after all children have been
2484 * added.
2485 *
2486 * @param context The Context the view is running in, through which it can
2487 * access the current theme, resources, etc.
2488 * @param attrs The attributes of the XML tag that is inflating the view.
2489 * @see #View(Context, AttributeSet, int)
2490 */
2491 public View(Context context, AttributeSet attrs) {
2492 this(context, attrs, 0);
2493 }
2494
2495 /**
2496 * Perform inflation from XML and apply a class-specific base style. This
2497 * constructor of View allows subclasses to use their own base style when
2498 * they are inflating. For example, a Button class's constructor would call
2499 * this version of the super class constructor and supply
2500 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2501 * the theme's button style to modify all of the base view attributes (in
2502 * particular its background) as well as the Button class's attributes.
2503 *
2504 * @param context The Context the view is running in, through which it can
2505 * access the current theme, resources, etc.
2506 * @param attrs The attributes of the XML tag that is inflating the view.
2507 * @param defStyle The default style to apply to this view. If 0, no style
2508 * will be applied (beyond what is included in the theme). This may
2509 * either be an attribute resource, whose value will be retrieved
2510 * from the current theme, or an explicit style resource.
2511 * @see #View(Context, AttributeSet)
2512 */
2513 public View(Context context, AttributeSet attrs, int defStyle) {
2514 this(context);
2515
2516 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2517 defStyle, 0);
2518
2519 Drawable background = null;
2520
2521 int leftPadding = -1;
2522 int topPadding = -1;
2523 int rightPadding = -1;
2524 int bottomPadding = -1;
2525
2526 int padding = -1;
2527
2528 int viewFlagValues = 0;
2529 int viewFlagMasks = 0;
2530
2531 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 int x = 0;
2534 int y = 0;
2535
Chet Haase73066682010-11-29 15:55:32 -08002536 float tx = 0;
2537 float ty = 0;
2538 float rotation = 0;
2539 float rotationX = 0;
2540 float rotationY = 0;
2541 float sx = 1f;
2542 float sy = 1f;
2543 boolean transformSet = false;
2544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2546
Adam Powell637d3372010-08-25 14:37:03 -07002547 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 final int N = a.getIndexCount();
2549 for (int i = 0; i < N; i++) {
2550 int attr = a.getIndex(i);
2551 switch (attr) {
2552 case com.android.internal.R.styleable.View_background:
2553 background = a.getDrawable(attr);
2554 break;
2555 case com.android.internal.R.styleable.View_padding:
2556 padding = a.getDimensionPixelSize(attr, -1);
2557 break;
2558 case com.android.internal.R.styleable.View_paddingLeft:
2559 leftPadding = a.getDimensionPixelSize(attr, -1);
2560 break;
2561 case com.android.internal.R.styleable.View_paddingTop:
2562 topPadding = a.getDimensionPixelSize(attr, -1);
2563 break;
2564 case com.android.internal.R.styleable.View_paddingRight:
2565 rightPadding = a.getDimensionPixelSize(attr, -1);
2566 break;
2567 case com.android.internal.R.styleable.View_paddingBottom:
2568 bottomPadding = a.getDimensionPixelSize(attr, -1);
2569 break;
2570 case com.android.internal.R.styleable.View_scrollX:
2571 x = a.getDimensionPixelOffset(attr, 0);
2572 break;
2573 case com.android.internal.R.styleable.View_scrollY:
2574 y = a.getDimensionPixelOffset(attr, 0);
2575 break;
Chet Haase73066682010-11-29 15:55:32 -08002576 case com.android.internal.R.styleable.View_alpha:
2577 setAlpha(a.getFloat(attr, 1f));
2578 break;
2579 case com.android.internal.R.styleable.View_transformPivotX:
2580 setPivotX(a.getDimensionPixelOffset(attr, 0));
2581 break;
2582 case com.android.internal.R.styleable.View_transformPivotY:
2583 setPivotY(a.getDimensionPixelOffset(attr, 0));
2584 break;
2585 case com.android.internal.R.styleable.View_translationX:
2586 tx = a.getDimensionPixelOffset(attr, 0);
2587 transformSet = true;
2588 break;
2589 case com.android.internal.R.styleable.View_translationY:
2590 ty = a.getDimensionPixelOffset(attr, 0);
2591 transformSet = true;
2592 break;
2593 case com.android.internal.R.styleable.View_rotation:
2594 rotation = a.getFloat(attr, 0);
2595 transformSet = true;
2596 break;
2597 case com.android.internal.R.styleable.View_rotationX:
2598 rotationX = a.getFloat(attr, 0);
2599 transformSet = true;
2600 break;
2601 case com.android.internal.R.styleable.View_rotationY:
2602 rotationY = a.getFloat(attr, 0);
2603 transformSet = true;
2604 break;
2605 case com.android.internal.R.styleable.View_scaleX:
2606 sx = a.getFloat(attr, 1f);
2607 transformSet = true;
2608 break;
2609 case com.android.internal.R.styleable.View_scaleY:
2610 sy = a.getFloat(attr, 1f);
2611 transformSet = true;
2612 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 case com.android.internal.R.styleable.View_id:
2614 mID = a.getResourceId(attr, NO_ID);
2615 break;
2616 case com.android.internal.R.styleable.View_tag:
2617 mTag = a.getText(attr);
2618 break;
2619 case com.android.internal.R.styleable.View_fitsSystemWindows:
2620 if (a.getBoolean(attr, false)) {
2621 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2622 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2623 }
2624 break;
2625 case com.android.internal.R.styleable.View_focusable:
2626 if (a.getBoolean(attr, false)) {
2627 viewFlagValues |= FOCUSABLE;
2628 viewFlagMasks |= FOCUSABLE_MASK;
2629 }
2630 break;
2631 case com.android.internal.R.styleable.View_focusableInTouchMode:
2632 if (a.getBoolean(attr, false)) {
2633 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2634 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2635 }
2636 break;
2637 case com.android.internal.R.styleable.View_clickable:
2638 if (a.getBoolean(attr, false)) {
2639 viewFlagValues |= CLICKABLE;
2640 viewFlagMasks |= CLICKABLE;
2641 }
2642 break;
2643 case com.android.internal.R.styleable.View_longClickable:
2644 if (a.getBoolean(attr, false)) {
2645 viewFlagValues |= LONG_CLICKABLE;
2646 viewFlagMasks |= LONG_CLICKABLE;
2647 }
2648 break;
2649 case com.android.internal.R.styleable.View_saveEnabled:
2650 if (!a.getBoolean(attr, true)) {
2651 viewFlagValues |= SAVE_DISABLED;
2652 viewFlagMasks |= SAVE_DISABLED_MASK;
2653 }
2654 break;
2655 case com.android.internal.R.styleable.View_duplicateParentState:
2656 if (a.getBoolean(attr, false)) {
2657 viewFlagValues |= DUPLICATE_PARENT_STATE;
2658 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2659 }
2660 break;
2661 case com.android.internal.R.styleable.View_visibility:
2662 final int visibility = a.getInt(attr, 0);
2663 if (visibility != 0) {
2664 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2665 viewFlagMasks |= VISIBILITY_MASK;
2666 }
2667 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002668 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002669 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002670 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002671 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002672 final int layoutDirection = a.getInt(attr, -1);
2673 if (layoutDirection != -1) {
2674 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002675 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002676 // Set to default (LAYOUT_DIRECTION_INHERIT)
2677 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002678 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002679 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002680 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 case com.android.internal.R.styleable.View_drawingCacheQuality:
2682 final int cacheQuality = a.getInt(attr, 0);
2683 if (cacheQuality != 0) {
2684 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2685 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2686 }
2687 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002688 case com.android.internal.R.styleable.View_contentDescription:
2689 mContentDescription = a.getString(attr);
2690 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2692 if (!a.getBoolean(attr, true)) {
2693 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2694 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2695 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002696 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2698 if (!a.getBoolean(attr, true)) {
2699 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2700 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2701 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002702 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 case R.styleable.View_scrollbars:
2704 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2705 if (scrollbars != SCROLLBARS_NONE) {
2706 viewFlagValues |= scrollbars;
2707 viewFlagMasks |= SCROLLBARS_MASK;
2708 initializeScrollbars(a);
2709 }
2710 break;
2711 case R.styleable.View_fadingEdge:
2712 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2713 if (fadingEdge != FADING_EDGE_NONE) {
2714 viewFlagValues |= fadingEdge;
2715 viewFlagMasks |= FADING_EDGE_MASK;
2716 initializeFadingEdge(a);
2717 }
2718 break;
2719 case R.styleable.View_scrollbarStyle:
2720 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2721 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2722 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2723 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2724 }
2725 break;
2726 case R.styleable.View_isScrollContainer:
2727 setScrollContainer = true;
2728 if (a.getBoolean(attr, false)) {
2729 setScrollContainer(true);
2730 }
2731 break;
2732 case com.android.internal.R.styleable.View_keepScreenOn:
2733 if (a.getBoolean(attr, false)) {
2734 viewFlagValues |= KEEP_SCREEN_ON;
2735 viewFlagMasks |= KEEP_SCREEN_ON;
2736 }
2737 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002738 case R.styleable.View_filterTouchesWhenObscured:
2739 if (a.getBoolean(attr, false)) {
2740 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2741 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2742 }
2743 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 case R.styleable.View_nextFocusLeft:
2745 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2746 break;
2747 case R.styleable.View_nextFocusRight:
2748 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2749 break;
2750 case R.styleable.View_nextFocusUp:
2751 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2752 break;
2753 case R.styleable.View_nextFocusDown:
2754 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2755 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002756 case R.styleable.View_nextFocusForward:
2757 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2758 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 case R.styleable.View_minWidth:
2760 mMinWidth = a.getDimensionPixelSize(attr, 0);
2761 break;
2762 case R.styleable.View_minHeight:
2763 mMinHeight = a.getDimensionPixelSize(attr, 0);
2764 break;
Romain Guy9a817362009-05-01 10:57:14 -07002765 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002766 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002767 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002768 + "be used within a restricted context");
2769 }
2770
Romain Guy9a817362009-05-01 10:57:14 -07002771 final String handlerName = a.getString(attr);
2772 if (handlerName != null) {
2773 setOnClickListener(new OnClickListener() {
2774 private Method mHandler;
2775
2776 public void onClick(View v) {
2777 if (mHandler == null) {
2778 try {
2779 mHandler = getContext().getClass().getMethod(handlerName,
2780 View.class);
2781 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002782 int id = getId();
2783 String idText = id == NO_ID ? "" : " with id '"
2784 + getContext().getResources().getResourceEntryName(
2785 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002786 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002787 handlerName + "(View) in the activity "
2788 + getContext().getClass() + " for onClick handler"
2789 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002790 }
2791 }
2792
2793 try {
2794 mHandler.invoke(getContext(), View.this);
2795 } catch (IllegalAccessException e) {
2796 throw new IllegalStateException("Could not execute non "
2797 + "public method of the activity", e);
2798 } catch (InvocationTargetException e) {
2799 throw new IllegalStateException("Could not execute "
2800 + "method of the activity", e);
2801 }
2802 }
2803 });
2804 }
2805 break;
Adam Powell637d3372010-08-25 14:37:03 -07002806 case R.styleable.View_overScrollMode:
2807 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2808 break;
Adam Powell20232d02010-12-08 21:08:53 -08002809 case R.styleable.View_verticalScrollbarPosition:
2810 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2811 break;
Romain Guy171c5922011-01-06 10:04:23 -08002812 case R.styleable.View_layerType:
2813 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2814 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 }
2816 }
2817
Adam Powell637d3372010-08-25 14:37:03 -07002818 setOverScrollMode(overScrollMode);
2819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 if (background != null) {
2821 setBackgroundDrawable(background);
2822 }
2823
2824 if (padding >= 0) {
2825 leftPadding = padding;
2826 topPadding = padding;
2827 rightPadding = padding;
2828 bottomPadding = padding;
2829 }
2830
2831 // If the user specified the padding (either with android:padding or
2832 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2833 // use the default padding or the padding from the background drawable
2834 // (stored at this point in mPadding*)
2835 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2836 topPadding >= 0 ? topPadding : mPaddingTop,
2837 rightPadding >= 0 ? rightPadding : mPaddingRight,
2838 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2839
2840 if (viewFlagMasks != 0) {
2841 setFlags(viewFlagValues, viewFlagMasks);
2842 }
2843
2844 // Needs to be called after mViewFlags is set
2845 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2846 recomputePadding();
2847 }
2848
2849 if (x != 0 || y != 0) {
2850 scrollTo(x, y);
2851 }
2852
Chet Haase73066682010-11-29 15:55:32 -08002853 if (transformSet) {
2854 setTranslationX(tx);
2855 setTranslationY(ty);
2856 setRotation(rotation);
2857 setRotationX(rotationX);
2858 setRotationY(rotationY);
2859 setScaleX(sx);
2860 setScaleY(sy);
2861 }
2862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2864 setScrollContainer(true);
2865 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002866
2867 computeOpaqueFlags();
2868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 a.recycle();
2870 }
2871
2872 /**
2873 * Non-public constructor for use in testing
2874 */
2875 View() {
2876 }
2877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 /**
2879 * <p>
2880 * Initializes the fading edges from a given set of styled attributes. This
2881 * method should be called by subclasses that need fading edges and when an
2882 * instance of these subclasses is created programmatically rather than
2883 * being inflated from XML. This method is automatically called when the XML
2884 * is inflated.
2885 * </p>
2886 *
2887 * @param a the styled attributes set to initialize the fading edges from
2888 */
2889 protected void initializeFadingEdge(TypedArray a) {
2890 initScrollCache();
2891
2892 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2893 R.styleable.View_fadingEdgeLength,
2894 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2895 }
2896
2897 /**
2898 * Returns the size of the vertical faded edges used to indicate that more
2899 * content in this view is visible.
2900 *
2901 * @return The size in pixels of the vertical faded edge or 0 if vertical
2902 * faded edges are not enabled for this view.
2903 * @attr ref android.R.styleable#View_fadingEdgeLength
2904 */
2905 public int getVerticalFadingEdgeLength() {
2906 if (isVerticalFadingEdgeEnabled()) {
2907 ScrollabilityCache cache = mScrollCache;
2908 if (cache != null) {
2909 return cache.fadingEdgeLength;
2910 }
2911 }
2912 return 0;
2913 }
2914
2915 /**
2916 * Set the size of the faded edge used to indicate that more content in this
2917 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07002918 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
2919 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
2920 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 *
2922 * @param length The size in pixels of the faded edge used to indicate that more
2923 * content in this view is visible.
2924 */
2925 public void setFadingEdgeLength(int length) {
2926 initScrollCache();
2927 mScrollCache.fadingEdgeLength = length;
2928 }
2929
2930 /**
2931 * Returns the size of the horizontal faded edges used to indicate that more
2932 * content in this view is visible.
2933 *
2934 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2935 * faded edges are not enabled for this view.
2936 * @attr ref android.R.styleable#View_fadingEdgeLength
2937 */
2938 public int getHorizontalFadingEdgeLength() {
2939 if (isHorizontalFadingEdgeEnabled()) {
2940 ScrollabilityCache cache = mScrollCache;
2941 if (cache != null) {
2942 return cache.fadingEdgeLength;
2943 }
2944 }
2945 return 0;
2946 }
2947
2948 /**
2949 * Returns the width of the vertical scrollbar.
2950 *
2951 * @return The width in pixels of the vertical scrollbar or 0 if there
2952 * is no vertical scrollbar.
2953 */
2954 public int getVerticalScrollbarWidth() {
2955 ScrollabilityCache cache = mScrollCache;
2956 if (cache != null) {
2957 ScrollBarDrawable scrollBar = cache.scrollBar;
2958 if (scrollBar != null) {
2959 int size = scrollBar.getSize(true);
2960 if (size <= 0) {
2961 size = cache.scrollBarSize;
2962 }
2963 return size;
2964 }
2965 return 0;
2966 }
2967 return 0;
2968 }
2969
2970 /**
2971 * Returns the height of the horizontal scrollbar.
2972 *
2973 * @return The height in pixels of the horizontal scrollbar or 0 if
2974 * there is no horizontal scrollbar.
2975 */
2976 protected int getHorizontalScrollbarHeight() {
2977 ScrollabilityCache cache = mScrollCache;
2978 if (cache != null) {
2979 ScrollBarDrawable scrollBar = cache.scrollBar;
2980 if (scrollBar != null) {
2981 int size = scrollBar.getSize(false);
2982 if (size <= 0) {
2983 size = cache.scrollBarSize;
2984 }
2985 return size;
2986 }
2987 return 0;
2988 }
2989 return 0;
2990 }
2991
2992 /**
2993 * <p>
2994 * Initializes the scrollbars from a given set of styled attributes. This
2995 * method should be called by subclasses that need scrollbars and when an
2996 * instance of these subclasses is created programmatically rather than
2997 * being inflated from XML. This method is automatically called when the XML
2998 * is inflated.
2999 * </p>
3000 *
3001 * @param a the styled attributes set to initialize the scrollbars from
3002 */
3003 protected void initializeScrollbars(TypedArray a) {
3004 initScrollCache();
3005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003007
Mike Cleronf116bf82009-09-27 19:14:12 -07003008 if (scrollabilityCache.scrollBar == null) {
3009 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3010 }
Joe Malin32736f02011-01-19 16:14:20 -08003011
Romain Guy8bda2482010-03-02 11:42:11 -08003012 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013
Mike Cleronf116bf82009-09-27 19:14:12 -07003014 if (!fadeScrollbars) {
3015 scrollabilityCache.state = ScrollabilityCache.ON;
3016 }
3017 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003018
3019
Mike Cleronf116bf82009-09-27 19:14:12 -07003020 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3021 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3022 .getScrollBarFadeDuration());
3023 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3024 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3025 ViewConfiguration.getScrollDefaultDelay());
3026
Joe Malin32736f02011-01-19 16:14:20 -08003027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3029 com.android.internal.R.styleable.View_scrollbarSize,
3030 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3031
3032 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3033 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3034
3035 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3036 if (thumb != null) {
3037 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3038 }
3039
3040 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3041 false);
3042 if (alwaysDraw) {
3043 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3044 }
3045
3046 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3047 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3048
3049 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3050 if (thumb != null) {
3051 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3052 }
3053
3054 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3055 false);
3056 if (alwaysDraw) {
3057 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3058 }
3059
3060 // Re-apply user/background padding so that scrollbar(s) get added
3061 recomputePadding();
3062 }
3063
3064 /**
3065 * <p>
3066 * Initalizes the scrollability cache if necessary.
3067 * </p>
3068 */
3069 private void initScrollCache() {
3070 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003071 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 }
3073 }
3074
3075 /**
Adam Powell20232d02010-12-08 21:08:53 -08003076 * Set the position of the vertical scroll bar. Should be one of
3077 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3078 * {@link #SCROLLBAR_POSITION_RIGHT}.
3079 *
3080 * @param position Where the vertical scroll bar should be positioned.
3081 */
3082 public void setVerticalScrollbarPosition(int position) {
3083 if (mVerticalScrollbarPosition != position) {
3084 mVerticalScrollbarPosition = position;
3085 computeOpaqueFlags();
3086 recomputePadding();
3087 }
3088 }
3089
3090 /**
3091 * @return The position where the vertical scroll bar will show, if applicable.
3092 * @see #setVerticalScrollbarPosition(int)
3093 */
3094 public int getVerticalScrollbarPosition() {
3095 return mVerticalScrollbarPosition;
3096 }
3097
3098 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 * Register a callback to be invoked when focus of this view changed.
3100 *
3101 * @param l The callback that will run.
3102 */
3103 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3104 mOnFocusChangeListener = l;
3105 }
3106
3107 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003108 * Add a listener that will be called when the bounds of the view change due to
3109 * layout processing.
3110 *
3111 * @param listener The listener that will be called when layout bounds change.
3112 */
3113 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3114 if (mOnLayoutChangeListeners == null) {
3115 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3116 }
3117 mOnLayoutChangeListeners.add(listener);
3118 }
3119
3120 /**
3121 * Remove a listener for layout changes.
3122 *
3123 * @param listener The listener for layout bounds change.
3124 */
3125 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3126 if (mOnLayoutChangeListeners == null) {
3127 return;
3128 }
3129 mOnLayoutChangeListeners.remove(listener);
3130 }
3131
3132 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003133 * Add a listener for attach state changes.
3134 *
3135 * This listener will be called whenever this view is attached or detached
3136 * from a window. Remove the listener using
3137 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3138 *
3139 * @param listener Listener to attach
3140 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3141 */
3142 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3143 if (mOnAttachStateChangeListeners == null) {
3144 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3145 }
3146 mOnAttachStateChangeListeners.add(listener);
3147 }
3148
3149 /**
3150 * Remove a listener for attach state changes. The listener will receive no further
3151 * notification of window attach/detach events.
3152 *
3153 * @param listener Listener to remove
3154 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3155 */
3156 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3157 if (mOnAttachStateChangeListeners == null) {
3158 return;
3159 }
3160 mOnAttachStateChangeListeners.remove(listener);
3161 }
3162
3163 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003164 * Returns the focus-change callback registered for this view.
3165 *
3166 * @return The callback, or null if one is not registered.
3167 */
3168 public OnFocusChangeListener getOnFocusChangeListener() {
3169 return mOnFocusChangeListener;
3170 }
3171
3172 /**
3173 * Register a callback to be invoked when this view is clicked. If this view is not
3174 * clickable, it becomes clickable.
3175 *
3176 * @param l The callback that will run
3177 *
3178 * @see #setClickable(boolean)
3179 */
3180 public void setOnClickListener(OnClickListener l) {
3181 if (!isClickable()) {
3182 setClickable(true);
3183 }
3184 mOnClickListener = l;
3185 }
3186
3187 /**
3188 * Register a callback to be invoked when this view is clicked and held. If this view is not
3189 * long clickable, it becomes long clickable.
3190 *
3191 * @param l The callback that will run
3192 *
3193 * @see #setLongClickable(boolean)
3194 */
3195 public void setOnLongClickListener(OnLongClickListener l) {
3196 if (!isLongClickable()) {
3197 setLongClickable(true);
3198 }
3199 mOnLongClickListener = l;
3200 }
3201
3202 /**
3203 * Register a callback to be invoked when the context menu for this view is
3204 * being built. If this view is not long clickable, it becomes long clickable.
3205 *
3206 * @param l The callback that will run
3207 *
3208 */
3209 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3210 if (!isLongClickable()) {
3211 setLongClickable(true);
3212 }
3213 mOnCreateContextMenuListener = l;
3214 }
3215
3216 /**
3217 * Call this view's OnClickListener, if it is defined.
3218 *
3219 * @return True there was an assigned OnClickListener that was called, false
3220 * otherwise is returned.
3221 */
3222 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003223 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 if (mOnClickListener != null) {
3226 playSoundEffect(SoundEffectConstants.CLICK);
3227 mOnClickListener.onClick(this);
3228 return true;
3229 }
3230
3231 return false;
3232 }
3233
3234 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003235 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3236 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003238 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 */
3240 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003241 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 boolean handled = false;
3244 if (mOnLongClickListener != null) {
3245 handled = mOnLongClickListener.onLongClick(View.this);
3246 }
3247 if (!handled) {
3248 handled = showContextMenu();
3249 }
3250 if (handled) {
3251 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3252 }
3253 return handled;
3254 }
3255
3256 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003257 * Performs button-related actions during a touch down event.
3258 *
3259 * @param event The event.
3260 * @return True if the down was consumed.
3261 *
3262 * @hide
3263 */
3264 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3265 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3266 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3267 return true;
3268 }
3269 }
3270 return false;
3271 }
3272
3273 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003274 * Bring up the context menu for this view.
3275 *
3276 * @return Whether a context menu was displayed.
3277 */
3278 public boolean showContextMenu() {
3279 return getParent().showContextMenuForChild(this);
3280 }
3281
3282 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003283 * Bring up the context menu for this view, referring to the item under the specified point.
3284 *
3285 * @param x The referenced x coordinate.
3286 * @param y The referenced y coordinate.
3287 * @param metaState The keyboard modifiers that were pressed.
3288 * @return Whether a context menu was displayed.
3289 *
3290 * @hide
3291 */
3292 public boolean showContextMenu(float x, float y, int metaState) {
3293 return showContextMenu();
3294 }
3295
3296 /**
Adam Powell6e346362010-07-23 10:18:23 -07003297 * Start an action mode.
3298 *
3299 * @param callback Callback that will control the lifecycle of the action mode
3300 * @return The new action mode if it is started, null otherwise
3301 *
3302 * @see ActionMode
3303 */
3304 public ActionMode startActionMode(ActionMode.Callback callback) {
3305 return getParent().startActionModeForChild(this, callback);
3306 }
3307
3308 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 * Register a callback to be invoked when a key is pressed in this view.
3310 * @param l the key listener to attach to this view
3311 */
3312 public void setOnKeyListener(OnKeyListener l) {
3313 mOnKeyListener = l;
3314 }
3315
3316 /**
3317 * Register a callback to be invoked when a touch event is sent to this view.
3318 * @param l the touch listener to attach to this view
3319 */
3320 public void setOnTouchListener(OnTouchListener l) {
3321 mOnTouchListener = l;
3322 }
3323
3324 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003325 * Register a callback to be invoked when a generic motion event is sent to this view.
3326 * @param l the generic motion listener to attach to this view
3327 */
3328 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3329 mOnGenericMotionListener = l;
3330 }
3331
3332 /**
Joe Malin32736f02011-01-19 16:14:20 -08003333 * Register a drag event listener callback object for this View. The parameter is
3334 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3335 * View, the system calls the
3336 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3337 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003338 */
3339 public void setOnDragListener(OnDragListener l) {
3340 mOnDragListener = l;
3341 }
3342
3343 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003344 * Give this view focus. This will cause
3345 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346 *
3347 * Note: this does not check whether this {@link View} should get focus, it just
3348 * gives it focus no matter what. It should only be called internally by framework
3349 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3350 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003351 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3352 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 * focus moved when requestFocus() is called. It may not always
3354 * apply, in which case use the default View.FOCUS_DOWN.
3355 * @param previouslyFocusedRect The rectangle of the view that had focus
3356 * prior in this View's coordinate system.
3357 */
3358 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3359 if (DBG) {
3360 System.out.println(this + " requestFocus()");
3361 }
3362
3363 if ((mPrivateFlags & FOCUSED) == 0) {
3364 mPrivateFlags |= FOCUSED;
3365
3366 if (mParent != null) {
3367 mParent.requestChildFocus(this, this);
3368 }
3369
3370 onFocusChanged(true, direction, previouslyFocusedRect);
3371 refreshDrawableState();
3372 }
3373 }
3374
3375 /**
3376 * Request that a rectangle of this view be visible on the screen,
3377 * scrolling if necessary just enough.
3378 *
3379 * <p>A View should call this if it maintains some notion of which part
3380 * of its content is interesting. For example, a text editing view
3381 * should call this when its cursor moves.
3382 *
3383 * @param rectangle The rectangle.
3384 * @return Whether any parent scrolled.
3385 */
3386 public boolean requestRectangleOnScreen(Rect rectangle) {
3387 return requestRectangleOnScreen(rectangle, false);
3388 }
3389
3390 /**
3391 * Request that a rectangle of this view be visible on the screen,
3392 * scrolling if necessary just enough.
3393 *
3394 * <p>A View should call this if it maintains some notion of which part
3395 * of its content is interesting. For example, a text editing view
3396 * should call this when its cursor moves.
3397 *
3398 * <p>When <code>immediate</code> is set to true, scrolling will not be
3399 * animated.
3400 *
3401 * @param rectangle The rectangle.
3402 * @param immediate True to forbid animated scrolling, false otherwise
3403 * @return Whether any parent scrolled.
3404 */
3405 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3406 View child = this;
3407 ViewParent parent = mParent;
3408 boolean scrolled = false;
3409 while (parent != null) {
3410 scrolled |= parent.requestChildRectangleOnScreen(child,
3411 rectangle, immediate);
3412
3413 // offset rect so next call has the rectangle in the
3414 // coordinate system of its direct child.
3415 rectangle.offset(child.getLeft(), child.getTop());
3416 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3417
3418 if (!(parent instanceof View)) {
3419 break;
3420 }
Romain Guy8506ab42009-06-11 17:35:47 -07003421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003422 child = (View) parent;
3423 parent = child.getParent();
3424 }
3425 return scrolled;
3426 }
3427
3428 /**
3429 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003430 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003431 */
3432 public void clearFocus() {
3433 if (DBG) {
3434 System.out.println(this + " clearFocus()");
3435 }
3436
3437 if ((mPrivateFlags & FOCUSED) != 0) {
3438 mPrivateFlags &= ~FOCUSED;
3439
3440 if (mParent != null) {
3441 mParent.clearChildFocus(this);
3442 }
3443
3444 onFocusChanged(false, 0, null);
3445 refreshDrawableState();
3446 }
3447 }
3448
3449 /**
3450 * Called to clear the focus of a view that is about to be removed.
3451 * Doesn't call clearChildFocus, which prevents this view from taking
3452 * focus again before it has been removed from the parent
3453 */
3454 void clearFocusForRemoval() {
3455 if ((mPrivateFlags & FOCUSED) != 0) {
3456 mPrivateFlags &= ~FOCUSED;
3457
3458 onFocusChanged(false, 0, null);
3459 refreshDrawableState();
3460 }
3461 }
3462
3463 /**
3464 * Called internally by the view system when a new view is getting focus.
3465 * This is what clears the old focus.
3466 */
3467 void unFocus() {
3468 if (DBG) {
3469 System.out.println(this + " unFocus()");
3470 }
3471
3472 if ((mPrivateFlags & FOCUSED) != 0) {
3473 mPrivateFlags &= ~FOCUSED;
3474
3475 onFocusChanged(false, 0, null);
3476 refreshDrawableState();
3477 }
3478 }
3479
3480 /**
3481 * Returns true if this view has focus iteself, or is the ancestor of the
3482 * view that has focus.
3483 *
3484 * @return True if this view has or contains focus, false otherwise.
3485 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003486 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 public boolean hasFocus() {
3488 return (mPrivateFlags & FOCUSED) != 0;
3489 }
3490
3491 /**
3492 * Returns true if this view is focusable or if it contains a reachable View
3493 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3494 * is a View whose parents do not block descendants focus.
3495 *
3496 * Only {@link #VISIBLE} views are considered focusable.
3497 *
3498 * @return True if the view is focusable or if the view contains a focusable
3499 * View, false otherwise.
3500 *
3501 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3502 */
3503 public boolean hasFocusable() {
3504 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3505 }
3506
3507 /**
3508 * Called by the view system when the focus state of this view changes.
3509 * When the focus change event is caused by directional navigation, direction
3510 * and previouslyFocusedRect provide insight into where the focus is coming from.
3511 * When overriding, be sure to call up through to the super class so that
3512 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003513 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003514 * @param gainFocus True if the View has focus; false otherwise.
3515 * @param direction The direction focus has moved when requestFocus()
3516 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003517 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3518 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3519 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3521 * system, of the previously focused view. If applicable, this will be
3522 * passed in as finer grained information about where the focus is coming
3523 * from (in addition to direction). Will be <code>null</code> otherwise.
3524 */
3525 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003526 if (gainFocus) {
3527 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3528 }
3529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003530 InputMethodManager imm = InputMethodManager.peekInstance();
3531 if (!gainFocus) {
3532 if (isPressed()) {
3533 setPressed(false);
3534 }
3535 if (imm != null && mAttachInfo != null
3536 && mAttachInfo.mHasWindowFocus) {
3537 imm.focusOut(this);
3538 }
Romain Guya2431d02009-04-30 16:30:00 -07003539 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003540 } else if (imm != null && mAttachInfo != null
3541 && mAttachInfo.mHasWindowFocus) {
3542 imm.focusIn(this);
3543 }
Romain Guy8506ab42009-06-11 17:35:47 -07003544
Romain Guy0fd89bf2011-01-26 15:41:30 -08003545 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003546 if (mOnFocusChangeListener != null) {
3547 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3548 }
Joe Malin32736f02011-01-19 16:14:20 -08003549
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003550 if (mAttachInfo != null) {
3551 mAttachInfo.mKeyDispatchState.reset(this);
3552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 }
3554
3555 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003556 * Sends an accessibility event of the given type. If accessiiblity is
3557 * not enabled this method has no effect. The default implementation calls
3558 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3559 * to populate information about the event source (this View), then calls
3560 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3561 * populate the text content of the event source including its descendants,
3562 * and last calls
3563 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3564 * on its parent to resuest sending of the event to interested parties.
3565 *
3566 * @param eventType The type of the event to send.
3567 *
3568 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3569 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3570 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
svetoslavganov75986cf2009-05-14 22:28:01 -07003571 */
3572 public void sendAccessibilityEvent(int eventType) {
3573 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3574 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3575 }
3576 }
3577
3578 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003579 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3580 * takes as an argument an empty {@link AccessibilityEvent} and does not
3581 * perfrom a check whether accessibility is enabled.
3582 *
3583 * @param event The event to send.
3584 *
3585 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003586 */
3587 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003588 if (!isShown()) {
3589 return;
3590 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003591 onInitializeAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003592 dispatchPopulateAccessibilityEvent(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003593 // In the beginning we called #isShown(), so we know that getParent() is not null.
3594 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003595 }
3596
3597 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003598 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3599 * to its children for adding their text content to the event. Note that the
3600 * event text is populated in a separate dispatch path since we add to the
3601 * event not only the text of the source but also the text of all its descendants.
3602 * </p>
3603 * A typical implementation will call
3604 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3605 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3606 * on each child. Override this method if custom population of the event text
3607 * content is required.
svetoslavganov75986cf2009-05-14 22:28:01 -07003608 *
3609 * @param event The event.
3610 *
3611 * @return True if the event population was completed.
3612 */
3613 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003614 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003615 return false;
3616 }
3617
3618 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003619 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003620 * giving a chance to this View to populate the accessibility event with its
3621 * text content. While the implementation is free to modify other event
3622 * attributes this should be performed in
3623 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
3624 * <p>
3625 * Example: Adding formatted date string to an accessibility event in addition
3626 * to the text added by the super implementation.
3627 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003628 * public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3629 * super.onPopulateAccessibilityEvent(event);
3630 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
3631 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
3632 * mCurrentDate.getTimeInMillis(), flags);
3633 * event.getText().add(selectedDateUtterance);
3634 * }
3635 * </code></pre></p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003636 *
3637 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003638 *
3639 * @see #sendAccessibilityEvent(int)
3640 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003641 */
3642 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3643
3644 }
3645
3646 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003647 * Initializes an {@link AccessibilityEvent} with information about the
3648 * the type of the event and this View which is the event source. In other
3649 * words, the source of an accessibility event is the view whose state
3650 * change triggered firing the event.
3651 * <p>
3652 * Example: Setting the password property of an event in addition
3653 * to properties set by the super implementation.
3654 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003655 * public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3656 * super.onInitializeAccessibilityEvent(event);
3657 * event.setPassword(true);
3658 * }
3659 * </code></pre></p>
3660 * @param event The event to initialeze.
3661 *
3662 * @see #sendAccessibilityEvent(int)
3663 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3664 */
3665 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003666 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07003667 event.setClassName(getClass().getName());
3668 event.setPackageName(getContext().getPackageName());
3669 event.setEnabled(isEnabled());
3670 event.setContentDescription(mContentDescription);
3671
3672 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
3673 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3674 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
3675 event.setItemCount(focusablesTempList.size());
3676 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3677 focusablesTempList.clear();
3678 }
3679 }
3680
3681 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003682 * Returns an {@link AccessibilityNodeInfo} representing this view from the
3683 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
3684 * This method is responsible for obtaining an accessibility node info from a
3685 * pool of reusable instances and calling
3686 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
3687 * initialize the former.
3688 * <p>
3689 * Note: The client is responsible for recycling the obtained instance by calling
3690 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
3691 * </p>
3692 * @return A populated {@link AccessibilityNodeInfo}.
3693 *
3694 * @see AccessibilityNodeInfo
3695 */
3696 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
3697 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
3698 onInitializeAccessibilityNodeInfo(info);
3699 return info;
3700 }
3701
3702 /**
3703 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
3704 * The base implementation sets:
3705 * <ul>
3706 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
3707 * <li>{@link AccessibilityNodeInfo#setBounds(Rect)},</li>
3708 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
3709 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
3710 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
3711 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
3712 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
3713 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
3714 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
3715 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
3716 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
3717 * </ul>
3718 * <p>
3719 * Subclasses should override this method, call the super implementation,
3720 * and set additional attributes.
3721 * </p>
3722 * @param info The instance to initialize.
3723 */
3724 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
3725 Rect bounds = mAttachInfo.mTmpInvalRect;
3726 getDrawingRect(bounds);
3727 info.setBounds(bounds);
3728
3729 ViewParent parent = getParent();
3730 if (parent instanceof View) {
3731 View parentView = (View) parent;
3732 info.setParent(parentView);
3733 }
3734
3735 info.setPackageName(mContext.getPackageName());
3736 info.setClassName(getClass().getName());
3737 info.setContentDescription(getContentDescription());
3738
3739 info.setEnabled(isEnabled());
3740 info.setClickable(isClickable());
3741 info.setFocusable(isFocusable());
3742 info.setFocused(isFocused());
3743 info.setSelected(isSelected());
3744 info.setLongClickable(isLongClickable());
3745
3746 // TODO: These make sense only if we are in an AdapterView but all
3747 // views can be selected. Maybe from accessiiblity perspective
3748 // we should report as selectable view in an AdapterView.
3749 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
3750 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
3751
3752 if (isFocusable()) {
3753 if (isFocused()) {
3754 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
3755 } else {
3756 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
3757 }
3758 }
3759 }
3760
3761 /**
3762 * Gets the unique identifier of this view on the screen for accessibility purposes.
3763 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
3764 *
3765 * @return The view accessibility id.
3766 *
3767 * @hide
3768 */
3769 public int getAccessibilityViewId() {
3770 if (mAccessibilityViewId == NO_ID) {
3771 mAccessibilityViewId = sNextAccessibilityViewId++;
3772 }
3773 return mAccessibilityViewId;
3774 }
3775
3776 /**
3777 * Gets the unique identifier of the window in which this View reseides.
3778 *
3779 * @return The window accessibility id.
3780 *
3781 * @hide
3782 */
3783 public int getAccessibilityWindowId() {
3784 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
3785 }
3786
3787 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003788 * Gets the {@link View} description. It briefly describes the view and is
3789 * primarily used for accessibility support. Set this property to enable
3790 * better accessibility support for your application. This is especially
3791 * true for views that do not have textual representation (For example,
3792 * ImageButton).
3793 *
3794 * @return The content descriptiopn.
3795 *
3796 * @attr ref android.R.styleable#View_contentDescription
3797 */
3798 public CharSequence getContentDescription() {
3799 return mContentDescription;
3800 }
3801
3802 /**
3803 * Sets the {@link View} description. It briefly describes the view and is
3804 * primarily used for accessibility support. Set this property to enable
3805 * better accessibility support for your application. This is especially
3806 * true for views that do not have textual representation (For example,
3807 * ImageButton).
3808 *
3809 * @param contentDescription The content description.
3810 *
3811 * @attr ref android.R.styleable#View_contentDescription
3812 */
3813 public void setContentDescription(CharSequence contentDescription) {
3814 mContentDescription = contentDescription;
3815 }
3816
3817 /**
Romain Guya2431d02009-04-30 16:30:00 -07003818 * Invoked whenever this view loses focus, either by losing window focus or by losing
3819 * focus within its window. This method can be used to clear any state tied to the
3820 * focus. For instance, if a button is held pressed with the trackball and the window
3821 * loses focus, this method can be used to cancel the press.
3822 *
3823 * Subclasses of View overriding this method should always call super.onFocusLost().
3824 *
3825 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003826 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003827 *
3828 * @hide pending API council approval
3829 */
3830 protected void onFocusLost() {
3831 resetPressedState();
3832 }
3833
3834 private void resetPressedState() {
3835 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3836 return;
3837 }
3838
3839 if (isPressed()) {
3840 setPressed(false);
3841
3842 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05003843 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003844 }
3845 }
3846 }
3847
3848 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003849 * Returns true if this view has focus
3850 *
3851 * @return True if this view has focus, false otherwise.
3852 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003853 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003854 public boolean isFocused() {
3855 return (mPrivateFlags & FOCUSED) != 0;
3856 }
3857
3858 /**
3859 * Find the view in the hierarchy rooted at this view that currently has
3860 * focus.
3861 *
3862 * @return The view that currently has focus, or null if no focused view can
3863 * be found.
3864 */
3865 public View findFocus() {
3866 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
3867 }
3868
3869 /**
3870 * Change whether this view is one of the set of scrollable containers in
3871 * its window. This will be used to determine whether the window can
3872 * resize or must pan when a soft input area is open -- scrollable
3873 * containers allow the window to use resize mode since the container
3874 * will appropriately shrink.
3875 */
3876 public void setScrollContainer(boolean isScrollContainer) {
3877 if (isScrollContainer) {
3878 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
3879 mAttachInfo.mScrollContainers.add(this);
3880 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
3881 }
3882 mPrivateFlags |= SCROLL_CONTAINER;
3883 } else {
3884 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
3885 mAttachInfo.mScrollContainers.remove(this);
3886 }
3887 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
3888 }
3889 }
3890
3891 /**
3892 * Returns the quality of the drawing cache.
3893 *
3894 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3895 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3896 *
3897 * @see #setDrawingCacheQuality(int)
3898 * @see #setDrawingCacheEnabled(boolean)
3899 * @see #isDrawingCacheEnabled()
3900 *
3901 * @attr ref android.R.styleable#View_drawingCacheQuality
3902 */
3903 public int getDrawingCacheQuality() {
3904 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
3905 }
3906
3907 /**
3908 * Set the drawing cache quality of this view. This value is used only when the
3909 * drawing cache is enabled
3910 *
3911 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3912 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3913 *
3914 * @see #getDrawingCacheQuality()
3915 * @see #setDrawingCacheEnabled(boolean)
3916 * @see #isDrawingCacheEnabled()
3917 *
3918 * @attr ref android.R.styleable#View_drawingCacheQuality
3919 */
3920 public void setDrawingCacheQuality(int quality) {
3921 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
3922 }
3923
3924 /**
3925 * Returns whether the screen should remain on, corresponding to the current
3926 * value of {@link #KEEP_SCREEN_ON}.
3927 *
3928 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
3929 *
3930 * @see #setKeepScreenOn(boolean)
3931 *
3932 * @attr ref android.R.styleable#View_keepScreenOn
3933 */
3934 public boolean getKeepScreenOn() {
3935 return (mViewFlags & KEEP_SCREEN_ON) != 0;
3936 }
3937
3938 /**
3939 * Controls whether the screen should remain on, modifying the
3940 * value of {@link #KEEP_SCREEN_ON}.
3941 *
3942 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
3943 *
3944 * @see #getKeepScreenOn()
3945 *
3946 * @attr ref android.R.styleable#View_keepScreenOn
3947 */
3948 public void setKeepScreenOn(boolean keepScreenOn) {
3949 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
3950 }
3951
3952 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003953 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3954 * @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 -08003955 *
3956 * @attr ref android.R.styleable#View_nextFocusLeft
3957 */
3958 public int getNextFocusLeftId() {
3959 return mNextFocusLeftId;
3960 }
3961
3962 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003963 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3964 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
3965 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003966 *
3967 * @attr ref android.R.styleable#View_nextFocusLeft
3968 */
3969 public void setNextFocusLeftId(int nextFocusLeftId) {
3970 mNextFocusLeftId = nextFocusLeftId;
3971 }
3972
3973 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003974 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3975 * @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 -08003976 *
3977 * @attr ref android.R.styleable#View_nextFocusRight
3978 */
3979 public int getNextFocusRightId() {
3980 return mNextFocusRightId;
3981 }
3982
3983 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003984 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3985 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
3986 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003987 *
3988 * @attr ref android.R.styleable#View_nextFocusRight
3989 */
3990 public void setNextFocusRightId(int nextFocusRightId) {
3991 mNextFocusRightId = nextFocusRightId;
3992 }
3993
3994 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003995 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3996 * @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 -08003997 *
3998 * @attr ref android.R.styleable#View_nextFocusUp
3999 */
4000 public int getNextFocusUpId() {
4001 return mNextFocusUpId;
4002 }
4003
4004 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004005 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4006 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4007 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004008 *
4009 * @attr ref android.R.styleable#View_nextFocusUp
4010 */
4011 public void setNextFocusUpId(int nextFocusUpId) {
4012 mNextFocusUpId = nextFocusUpId;
4013 }
4014
4015 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004016 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4017 * @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 -08004018 *
4019 * @attr ref android.R.styleable#View_nextFocusDown
4020 */
4021 public int getNextFocusDownId() {
4022 return mNextFocusDownId;
4023 }
4024
4025 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004026 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4027 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4028 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004029 *
4030 * @attr ref android.R.styleable#View_nextFocusDown
4031 */
4032 public void setNextFocusDownId(int nextFocusDownId) {
4033 mNextFocusDownId = nextFocusDownId;
4034 }
4035
4036 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004037 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4038 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4039 *
4040 * @attr ref android.R.styleable#View_nextFocusForward
4041 */
4042 public int getNextFocusForwardId() {
4043 return mNextFocusForwardId;
4044 }
4045
4046 /**
4047 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4048 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4049 * decide automatically.
4050 *
4051 * @attr ref android.R.styleable#View_nextFocusForward
4052 */
4053 public void setNextFocusForwardId(int nextFocusForwardId) {
4054 mNextFocusForwardId = nextFocusForwardId;
4055 }
4056
4057 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004058 * Returns the visibility of this view and all of its ancestors
4059 *
4060 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4061 */
4062 public boolean isShown() {
4063 View current = this;
4064 //noinspection ConstantConditions
4065 do {
4066 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4067 return false;
4068 }
4069 ViewParent parent = current.mParent;
4070 if (parent == null) {
4071 return false; // We are not attached to the view root
4072 }
4073 if (!(parent instanceof View)) {
4074 return true;
4075 }
4076 current = (View) parent;
4077 } while (current != null);
4078
4079 return false;
4080 }
4081
4082 /**
4083 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4084 * is set
4085 *
4086 * @param insets Insets for system windows
4087 *
4088 * @return True if this view applied the insets, false otherwise
4089 */
4090 protected boolean fitSystemWindows(Rect insets) {
4091 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4092 mPaddingLeft = insets.left;
4093 mPaddingTop = insets.top;
4094 mPaddingRight = insets.right;
4095 mPaddingBottom = insets.bottom;
4096 requestLayout();
4097 return true;
4098 }
4099 return false;
4100 }
4101
4102 /**
4103 * Returns the visibility status for this view.
4104 *
4105 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4106 * @attr ref android.R.styleable#View_visibility
4107 */
4108 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004109 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4110 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4111 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 })
4113 public int getVisibility() {
4114 return mViewFlags & VISIBILITY_MASK;
4115 }
4116
4117 /**
4118 * Set the enabled state of this view.
4119 *
4120 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4121 * @attr ref android.R.styleable#View_visibility
4122 */
4123 @RemotableViewMethod
4124 public void setVisibility(int visibility) {
4125 setFlags(visibility, VISIBILITY_MASK);
4126 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4127 }
4128
4129 /**
4130 * Returns the enabled status for this view. The interpretation of the
4131 * enabled state varies by subclass.
4132 *
4133 * @return True if this view is enabled, false otherwise.
4134 */
4135 @ViewDebug.ExportedProperty
4136 public boolean isEnabled() {
4137 return (mViewFlags & ENABLED_MASK) == ENABLED;
4138 }
4139
4140 /**
4141 * Set the enabled state of this view. The interpretation of the enabled
4142 * state varies by subclass.
4143 *
4144 * @param enabled True if this view is enabled, false otherwise.
4145 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004146 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004147 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004148 if (enabled == isEnabled()) return;
4149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004150 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4151
4152 /*
4153 * The View most likely has to change its appearance, so refresh
4154 * the drawable state.
4155 */
4156 refreshDrawableState();
4157
4158 // Invalidate too, since the default behavior for views is to be
4159 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004160 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004161 }
4162
4163 /**
4164 * Set whether this view can receive the focus.
4165 *
4166 * Setting this to false will also ensure that this view is not focusable
4167 * in touch mode.
4168 *
4169 * @param focusable If true, this view can receive the focus.
4170 *
4171 * @see #setFocusableInTouchMode(boolean)
4172 * @attr ref android.R.styleable#View_focusable
4173 */
4174 public void setFocusable(boolean focusable) {
4175 if (!focusable) {
4176 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4177 }
4178 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4179 }
4180
4181 /**
4182 * Set whether this view can receive focus while in touch mode.
4183 *
4184 * Setting this to true will also ensure that this view is focusable.
4185 *
4186 * @param focusableInTouchMode If true, this view can receive the focus while
4187 * in touch mode.
4188 *
4189 * @see #setFocusable(boolean)
4190 * @attr ref android.R.styleable#View_focusableInTouchMode
4191 */
4192 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4193 // Focusable in touch mode should always be set before the focusable flag
4194 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4195 // which, in touch mode, will not successfully request focus on this view
4196 // because the focusable in touch mode flag is not set
4197 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4198 if (focusableInTouchMode) {
4199 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4200 }
4201 }
4202
4203 /**
4204 * Set whether this view should have sound effects enabled for events such as
4205 * clicking and touching.
4206 *
4207 * <p>You may wish to disable sound effects for a view if you already play sounds,
4208 * for instance, a dial key that plays dtmf tones.
4209 *
4210 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4211 * @see #isSoundEffectsEnabled()
4212 * @see #playSoundEffect(int)
4213 * @attr ref android.R.styleable#View_soundEffectsEnabled
4214 */
4215 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4216 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4217 }
4218
4219 /**
4220 * @return whether this view should have sound effects enabled for events such as
4221 * clicking and touching.
4222 *
4223 * @see #setSoundEffectsEnabled(boolean)
4224 * @see #playSoundEffect(int)
4225 * @attr ref android.R.styleable#View_soundEffectsEnabled
4226 */
4227 @ViewDebug.ExportedProperty
4228 public boolean isSoundEffectsEnabled() {
4229 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4230 }
4231
4232 /**
4233 * Set whether this view should have haptic feedback for events such as
4234 * long presses.
4235 *
4236 * <p>You may wish to disable haptic feedback if your view already controls
4237 * its own haptic feedback.
4238 *
4239 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4240 * @see #isHapticFeedbackEnabled()
4241 * @see #performHapticFeedback(int)
4242 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4243 */
4244 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4245 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4246 }
4247
4248 /**
4249 * @return whether this view should have haptic feedback enabled for events
4250 * long presses.
4251 *
4252 * @see #setHapticFeedbackEnabled(boolean)
4253 * @see #performHapticFeedback(int)
4254 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4255 */
4256 @ViewDebug.ExportedProperty
4257 public boolean isHapticFeedbackEnabled() {
4258 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4259 }
4260
4261 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004262 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004263 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004264 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4265 * {@link #LAYOUT_DIRECTION_RTL},
4266 * {@link #LAYOUT_DIRECTION_INHERIT} or
4267 * {@link #LAYOUT_DIRECTION_LOCALE}.
4268 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08004269 * @hide
4270 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004271 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004272 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4273 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4274 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4275 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004276 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004277 public int getLayoutDirection() {
4278 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004279 }
4280
4281 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004282 * Set the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004283 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004284 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4285 * {@link #LAYOUT_DIRECTION_RTL},
4286 * {@link #LAYOUT_DIRECTION_INHERIT} or
4287 * {@link #LAYOUT_DIRECTION_LOCALE}.
4288 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08004289 * @hide
4290 */
4291 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004292 public void setLayoutDirection(int layoutDirection) {
4293 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
Cibu Johny7632cb92010-02-22 13:01:02 -08004294 }
4295
4296 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004297 * If this view doesn't do any drawing on its own, set this flag to
4298 * allow further optimizations. By default, this flag is not set on
4299 * View, but could be set on some View subclasses such as ViewGroup.
4300 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004301 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4302 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004303 *
4304 * @param willNotDraw whether or not this View draw on its own
4305 */
4306 public void setWillNotDraw(boolean willNotDraw) {
4307 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4308 }
4309
4310 /**
4311 * Returns whether or not this View draws on its own.
4312 *
4313 * @return true if this view has nothing to draw, false otherwise
4314 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004315 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316 public boolean willNotDraw() {
4317 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4318 }
4319
4320 /**
4321 * When a View's drawing cache is enabled, drawing is redirected to an
4322 * offscreen bitmap. Some views, like an ImageView, must be able to
4323 * bypass this mechanism if they already draw a single bitmap, to avoid
4324 * unnecessary usage of the memory.
4325 *
4326 * @param willNotCacheDrawing true if this view does not cache its
4327 * drawing, false otherwise
4328 */
4329 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4330 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4331 }
4332
4333 /**
4334 * Returns whether or not this View can cache its drawing or not.
4335 *
4336 * @return true if this view does not cache its drawing, false otherwise
4337 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004338 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004339 public boolean willNotCacheDrawing() {
4340 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4341 }
4342
4343 /**
4344 * Indicates whether this view reacts to click events or not.
4345 *
4346 * @return true if the view is clickable, false otherwise
4347 *
4348 * @see #setClickable(boolean)
4349 * @attr ref android.R.styleable#View_clickable
4350 */
4351 @ViewDebug.ExportedProperty
4352 public boolean isClickable() {
4353 return (mViewFlags & CLICKABLE) == CLICKABLE;
4354 }
4355
4356 /**
4357 * Enables or disables click events for this view. When a view
4358 * is clickable it will change its state to "pressed" on every click.
4359 * Subclasses should set the view clickable to visually react to
4360 * user's clicks.
4361 *
4362 * @param clickable true to make the view clickable, false otherwise
4363 *
4364 * @see #isClickable()
4365 * @attr ref android.R.styleable#View_clickable
4366 */
4367 public void setClickable(boolean clickable) {
4368 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4369 }
4370
4371 /**
4372 * Indicates whether this view reacts to long click events or not.
4373 *
4374 * @return true if the view is long clickable, false otherwise
4375 *
4376 * @see #setLongClickable(boolean)
4377 * @attr ref android.R.styleable#View_longClickable
4378 */
4379 public boolean isLongClickable() {
4380 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4381 }
4382
4383 /**
4384 * Enables or disables long click events for this view. When a view is long
4385 * clickable it reacts to the user holding down the button for a longer
4386 * duration than a tap. This event can either launch the listener or a
4387 * context menu.
4388 *
4389 * @param longClickable true to make the view long clickable, false otherwise
4390 * @see #isLongClickable()
4391 * @attr ref android.R.styleable#View_longClickable
4392 */
4393 public void setLongClickable(boolean longClickable) {
4394 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4395 }
4396
4397 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004398 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399 *
4400 * @see #isClickable()
4401 * @see #setClickable(boolean)
4402 *
4403 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4404 * the View's internal state from a previously set "pressed" state.
4405 */
4406 public void setPressed(boolean pressed) {
4407 if (pressed) {
4408 mPrivateFlags |= PRESSED;
4409 } else {
4410 mPrivateFlags &= ~PRESSED;
4411 }
4412 refreshDrawableState();
4413 dispatchSetPressed(pressed);
4414 }
4415
4416 /**
4417 * Dispatch setPressed to all of this View's children.
4418 *
4419 * @see #setPressed(boolean)
4420 *
4421 * @param pressed The new pressed state
4422 */
4423 protected void dispatchSetPressed(boolean pressed) {
4424 }
4425
4426 /**
4427 * Indicates whether the view is currently in pressed state. Unless
4428 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4429 * the pressed state.
4430 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004431 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004432 * @see #isClickable()
4433 * @see #setClickable(boolean)
4434 *
4435 * @return true if the view is currently pressed, false otherwise
4436 */
4437 public boolean isPressed() {
4438 return (mPrivateFlags & PRESSED) == PRESSED;
4439 }
4440
4441 /**
4442 * Indicates whether this view will save its state (that is,
4443 * whether its {@link #onSaveInstanceState} method will be called).
4444 *
4445 * @return Returns true if the view state saving is enabled, else false.
4446 *
4447 * @see #setSaveEnabled(boolean)
4448 * @attr ref android.R.styleable#View_saveEnabled
4449 */
4450 public boolean isSaveEnabled() {
4451 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4452 }
4453
4454 /**
4455 * Controls whether the saving of this view's state is
4456 * enabled (that is, whether its {@link #onSaveInstanceState} method
4457 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004458 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004459 * for its state to be saved. This flag can only disable the
4460 * saving of this view; any child views may still have their state saved.
4461 *
4462 * @param enabled Set to false to <em>disable</em> state saving, or true
4463 * (the default) to allow it.
4464 *
4465 * @see #isSaveEnabled()
4466 * @see #setId(int)
4467 * @see #onSaveInstanceState()
4468 * @attr ref android.R.styleable#View_saveEnabled
4469 */
4470 public void setSaveEnabled(boolean enabled) {
4471 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4472 }
4473
Jeff Brown85a31762010-09-01 17:01:00 -07004474 /**
4475 * Gets whether the framework should discard touches when the view's
4476 * window is obscured by another visible window.
4477 * Refer to the {@link View} security documentation for more details.
4478 *
4479 * @return True if touch filtering is enabled.
4480 *
4481 * @see #setFilterTouchesWhenObscured(boolean)
4482 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4483 */
4484 @ViewDebug.ExportedProperty
4485 public boolean getFilterTouchesWhenObscured() {
4486 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4487 }
4488
4489 /**
4490 * Sets whether the framework should discard touches when the view's
4491 * window is obscured by another visible window.
4492 * Refer to the {@link View} security documentation for more details.
4493 *
4494 * @param enabled True if touch filtering should be enabled.
4495 *
4496 * @see #getFilterTouchesWhenObscured
4497 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4498 */
4499 public void setFilterTouchesWhenObscured(boolean enabled) {
4500 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4501 FILTER_TOUCHES_WHEN_OBSCURED);
4502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004503
4504 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004505 * Indicates whether the entire hierarchy under this view will save its
4506 * state when a state saving traversal occurs from its parent. The default
4507 * is true; if false, these views will not be saved unless
4508 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4509 *
4510 * @return Returns true if the view state saving from parent is enabled, else false.
4511 *
4512 * @see #setSaveFromParentEnabled(boolean)
4513 */
4514 public boolean isSaveFromParentEnabled() {
4515 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4516 }
4517
4518 /**
4519 * Controls whether the entire hierarchy under this view will save its
4520 * state when a state saving traversal occurs from its parent. The default
4521 * is true; if false, these views will not be saved unless
4522 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4523 *
4524 * @param enabled Set to false to <em>disable</em> state saving, or true
4525 * (the default) to allow it.
4526 *
4527 * @see #isSaveFromParentEnabled()
4528 * @see #setId(int)
4529 * @see #onSaveInstanceState()
4530 */
4531 public void setSaveFromParentEnabled(boolean enabled) {
4532 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4533 }
4534
4535
4536 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004537 * Returns whether this View is able to take focus.
4538 *
4539 * @return True if this view can take focus, or false otherwise.
4540 * @attr ref android.R.styleable#View_focusable
4541 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004542 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004543 public final boolean isFocusable() {
4544 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4545 }
4546
4547 /**
4548 * When a view is focusable, it may not want to take focus when in touch mode.
4549 * For example, a button would like focus when the user is navigating via a D-pad
4550 * so that the user can click on it, but once the user starts touching the screen,
4551 * the button shouldn't take focus
4552 * @return Whether the view is focusable in touch mode.
4553 * @attr ref android.R.styleable#View_focusableInTouchMode
4554 */
4555 @ViewDebug.ExportedProperty
4556 public final boolean isFocusableInTouchMode() {
4557 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4558 }
4559
4560 /**
4561 * Find the nearest view in the specified direction that can take focus.
4562 * This does not actually give focus to that view.
4563 *
4564 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4565 *
4566 * @return The nearest focusable in the specified direction, or null if none
4567 * can be found.
4568 */
4569 public View focusSearch(int direction) {
4570 if (mParent != null) {
4571 return mParent.focusSearch(this, direction);
4572 } else {
4573 return null;
4574 }
4575 }
4576
4577 /**
4578 * This method is the last chance for the focused view and its ancestors to
4579 * respond to an arrow key. This is called when the focused view did not
4580 * consume the key internally, nor could the view system find a new view in
4581 * the requested direction to give focus to.
4582 *
4583 * @param focused The currently focused view.
4584 * @param direction The direction focus wants to move. One of FOCUS_UP,
4585 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4586 * @return True if the this view consumed this unhandled move.
4587 */
4588 public boolean dispatchUnhandledMove(View focused, int direction) {
4589 return false;
4590 }
4591
4592 /**
4593 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004594 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004595 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004596 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4597 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004598 * @return The user specified next view, or null if there is none.
4599 */
4600 View findUserSetNextFocus(View root, int direction) {
4601 switch (direction) {
4602 case FOCUS_LEFT:
4603 if (mNextFocusLeftId == View.NO_ID) return null;
4604 return findViewShouldExist(root, mNextFocusLeftId);
4605 case FOCUS_RIGHT:
4606 if (mNextFocusRightId == View.NO_ID) return null;
4607 return findViewShouldExist(root, mNextFocusRightId);
4608 case FOCUS_UP:
4609 if (mNextFocusUpId == View.NO_ID) return null;
4610 return findViewShouldExist(root, mNextFocusUpId);
4611 case FOCUS_DOWN:
4612 if (mNextFocusDownId == View.NO_ID) return null;
4613 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004614 case FOCUS_FORWARD:
4615 if (mNextFocusForwardId == View.NO_ID) return null;
4616 return findViewShouldExist(root, mNextFocusForwardId);
4617 case FOCUS_BACKWARD: {
4618 final int id = mID;
4619 return root.findViewByPredicate(new Predicate<View>() {
4620 @Override
4621 public boolean apply(View t) {
4622 return t.mNextFocusForwardId == id;
4623 }
4624 });
4625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004626 }
4627 return null;
4628 }
4629
4630 private static View findViewShouldExist(View root, int childViewId) {
4631 View result = root.findViewById(childViewId);
4632 if (result == null) {
4633 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4634 + "by user for id " + childViewId);
4635 }
4636 return result;
4637 }
4638
4639 /**
4640 * Find and return all focusable views that are descendants of this view,
4641 * possibly including this view if it is focusable itself.
4642 *
4643 * @param direction The direction of the focus
4644 * @return A list of focusable views
4645 */
4646 public ArrayList<View> getFocusables(int direction) {
4647 ArrayList<View> result = new ArrayList<View>(24);
4648 addFocusables(result, direction);
4649 return result;
4650 }
4651
4652 /**
4653 * Add any focusable views that are descendants of this view (possibly
4654 * including this view if it is focusable itself) to views. If we are in touch mode,
4655 * only add views that are also focusable in touch mode.
4656 *
4657 * @param views Focusable views found so far
4658 * @param direction The direction of the focus
4659 */
4660 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004661 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004663
svetoslavganov75986cf2009-05-14 22:28:01 -07004664 /**
4665 * Adds any focusable views that are descendants of this view (possibly
4666 * including this view if it is focusable itself) to views. This method
4667 * adds all focusable views regardless if we are in touch mode or
4668 * only views focusable in touch mode if we are in touch mode depending on
4669 * the focusable mode paramater.
4670 *
4671 * @param views Focusable views found so far or null if all we are interested is
4672 * the number of focusables.
4673 * @param direction The direction of the focus.
4674 * @param focusableMode The type of focusables to be added.
4675 *
4676 * @see #FOCUSABLES_ALL
4677 * @see #FOCUSABLES_TOUCH_MODE
4678 */
4679 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4680 if (!isFocusable()) {
4681 return;
4682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004683
svetoslavganov75986cf2009-05-14 22:28:01 -07004684 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4685 isInTouchMode() && !isFocusableInTouchMode()) {
4686 return;
4687 }
4688
4689 if (views != null) {
4690 views.add(this);
4691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004692 }
4693
4694 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004695 * Finds the Views that contain given text. The containment is case insensitive.
4696 * As View's text is considered any text content that View renders.
4697 *
4698 * @param outViews The output list of matching Views.
4699 * @param text The text to match against.
4700 */
4701 public void findViewsWithText(ArrayList<View> outViews, CharSequence text) {
4702 }
4703
4704 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004705 * Find and return all touchable views that are descendants of this view,
4706 * possibly including this view if it is touchable itself.
4707 *
4708 * @return A list of touchable views
4709 */
4710 public ArrayList<View> getTouchables() {
4711 ArrayList<View> result = new ArrayList<View>();
4712 addTouchables(result);
4713 return result;
4714 }
4715
4716 /**
4717 * Add any touchable views that are descendants of this view (possibly
4718 * including this view if it is touchable itself) to views.
4719 *
4720 * @param views Touchable views found so far
4721 */
4722 public void addTouchables(ArrayList<View> views) {
4723 final int viewFlags = mViewFlags;
4724
4725 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4726 && (viewFlags & ENABLED_MASK) == ENABLED) {
4727 views.add(this);
4728 }
4729 }
4730
4731 /**
4732 * Call this to try to give focus to a specific view or to one of its
4733 * descendants.
4734 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004735 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4736 * false), or if it is focusable and it is not focusable in touch mode
4737 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004738 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004739 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004740 * have focus, and you want your parent to look for the next one.
4741 *
4742 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4743 * {@link #FOCUS_DOWN} and <code>null</code>.
4744 *
4745 * @return Whether this view or one of its descendants actually took focus.
4746 */
4747 public final boolean requestFocus() {
4748 return requestFocus(View.FOCUS_DOWN);
4749 }
4750
4751
4752 /**
4753 * Call this to try to give focus to a specific view or to one of its
4754 * descendants and give it a hint about what direction focus is heading.
4755 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004756 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4757 * false), or if it is focusable and it is not focusable in touch mode
4758 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004759 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004760 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004761 * have focus, and you want your parent to look for the next one.
4762 *
4763 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4764 * <code>null</code> set for the previously focused rectangle.
4765 *
4766 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4767 * @return Whether this view or one of its descendants actually took focus.
4768 */
4769 public final boolean requestFocus(int direction) {
4770 return requestFocus(direction, null);
4771 }
4772
4773 /**
4774 * Call this to try to give focus to a specific view or to one of its descendants
4775 * and give it hints about the direction and a specific rectangle that the focus
4776 * is coming from. The rectangle can help give larger views a finer grained hint
4777 * about where focus is coming from, and therefore, where to show selection, or
4778 * forward focus change internally.
4779 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004780 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4781 * false), or if it is focusable and it is not focusable in touch mode
4782 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004783 *
4784 * A View will not take focus if it is not visible.
4785 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004786 * A View will not take focus if one of its parents has
4787 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
4788 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004789 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004790 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004791 * have focus, and you want your parent to look for the next one.
4792 *
4793 * You may wish to override this method if your custom {@link View} has an internal
4794 * {@link View} that it wishes to forward the request to.
4795 *
4796 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4797 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
4798 * to give a finer grained hint about where focus is coming from. May be null
4799 * if there is no hint.
4800 * @return Whether this view or one of its descendants actually took focus.
4801 */
4802 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
4803 // need to be focusable
4804 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
4805 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4806 return false;
4807 }
4808
4809 // need to be focusable in touch mode if in touch mode
4810 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004811 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
4812 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004813 }
4814
4815 // need to not have any parents blocking us
4816 if (hasAncestorThatBlocksDescendantFocus()) {
4817 return false;
4818 }
4819
4820 handleFocusGainInternal(direction, previouslyFocusedRect);
4821 return true;
4822 }
4823
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004824 /** Gets the ViewAncestor, or null if not attached. */
4825 /*package*/ ViewAncestor getViewAncestor() {
Christopher Tate2c095f32010-10-04 14:13:40 -07004826 View root = getRootView();
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004827 return root != null ? (ViewAncestor)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07004828 }
4829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004830 /**
4831 * Call this to try to give focus to a specific view or to one of its descendants. This is a
4832 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
4833 * touch mode to request focus when they are touched.
4834 *
4835 * @return Whether this view or one of its descendants actually took focus.
4836 *
4837 * @see #isInTouchMode()
4838 *
4839 */
4840 public final boolean requestFocusFromTouch() {
4841 // Leave touch mode if we need to
4842 if (isInTouchMode()) {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004843 ViewAncestor viewRoot = getViewAncestor();
Christopher Tate2c095f32010-10-04 14:13:40 -07004844 if (viewRoot != null) {
4845 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004846 }
4847 }
4848 return requestFocus(View.FOCUS_DOWN);
4849 }
4850
4851 /**
4852 * @return Whether any ancestor of this view blocks descendant focus.
4853 */
4854 private boolean hasAncestorThatBlocksDescendantFocus() {
4855 ViewParent ancestor = mParent;
4856 while (ancestor instanceof ViewGroup) {
4857 final ViewGroup vgAncestor = (ViewGroup) ancestor;
4858 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
4859 return true;
4860 } else {
4861 ancestor = vgAncestor.getParent();
4862 }
4863 }
4864 return false;
4865 }
4866
4867 /**
Romain Guya440b002010-02-24 15:57:54 -08004868 * @hide
4869 */
4870 public void dispatchStartTemporaryDetach() {
4871 onStartTemporaryDetach();
4872 }
4873
4874 /**
4875 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004876 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
4877 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08004878 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004879 */
4880 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08004881 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08004882 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08004883 }
4884
4885 /**
4886 * @hide
4887 */
4888 public void dispatchFinishTemporaryDetach() {
4889 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004890 }
Romain Guy8506ab42009-06-11 17:35:47 -07004891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004892 /**
4893 * Called after {@link #onStartTemporaryDetach} when the container is done
4894 * changing the view.
4895 */
4896 public void onFinishTemporaryDetach() {
4897 }
Romain Guy8506ab42009-06-11 17:35:47 -07004898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004899 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004900 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
4901 * for this view's window. Returns null if the view is not currently attached
4902 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07004903 * just use the standard high-level event callbacks like
4904 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004905 */
4906 public KeyEvent.DispatcherState getKeyDispatcherState() {
4907 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
4908 }
Joe Malin32736f02011-01-19 16:14:20 -08004909
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004910 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004911 * Dispatch a key event before it is processed by any input method
4912 * associated with the view hierarchy. This can be used to intercept
4913 * key events in special situations before the IME consumes them; a
4914 * typical example would be handling the BACK key to update the application's
4915 * UI instead of allowing the IME to see it and close itself.
4916 *
4917 * @param event The key event to be dispatched.
4918 * @return True if the event was handled, false otherwise.
4919 */
4920 public boolean dispatchKeyEventPreIme(KeyEvent event) {
4921 return onKeyPreIme(event.getKeyCode(), event);
4922 }
4923
4924 /**
4925 * Dispatch a key event to the next view on the focus path. This path runs
4926 * from the top of the view tree down to the currently focused view. If this
4927 * view has focus, it will dispatch to itself. Otherwise it will dispatch
4928 * the next node down the focus path. This method also fires any key
4929 * listeners.
4930 *
4931 * @param event The key event to be dispatched.
4932 * @return True if the event was handled, false otherwise.
4933 */
4934 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004935 if (mInputEventConsistencyVerifier != null) {
4936 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
4937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004938
Jeff Brown21bc5c92011-02-28 18:27:14 -08004939 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07004940 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004941 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4942 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
4943 return true;
4944 }
4945
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004946 if (event.dispatch(this, mAttachInfo != null
4947 ? mAttachInfo.mKeyDispatchState : null, this)) {
4948 return true;
4949 }
4950
4951 if (mInputEventConsistencyVerifier != null) {
4952 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4953 }
4954 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004955 }
4956
4957 /**
4958 * Dispatches a key shortcut event.
4959 *
4960 * @param event The key event to be dispatched.
4961 * @return True if the event was handled by the view, false otherwise.
4962 */
4963 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
4964 return onKeyShortcut(event.getKeyCode(), event);
4965 }
4966
4967 /**
4968 * Pass the touch screen motion event down to the target view, or this
4969 * view if it is the target.
4970 *
4971 * @param event The motion event to be dispatched.
4972 * @return True if the event was handled by the view, false otherwise.
4973 */
4974 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004975 if (mInputEventConsistencyVerifier != null) {
4976 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
4977 }
4978
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004979 if (onFilterTouchEventForSecurity(event)) {
4980 //noinspection SimplifiableIfStatement
4981 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
4982 mOnTouchListener.onTouch(this, event)) {
4983 return true;
4984 }
4985
4986 if (onTouchEvent(event)) {
4987 return true;
4988 }
Jeff Brown85a31762010-09-01 17:01:00 -07004989 }
4990
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004991 if (mInputEventConsistencyVerifier != null) {
4992 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004993 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004994 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004995 }
4996
4997 /**
Jeff Brown85a31762010-09-01 17:01:00 -07004998 * Filter the touch event to apply security policies.
4999 *
5000 * @param event The motion event to be filtered.
5001 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005002 *
Jeff Brown85a31762010-09-01 17:01:00 -07005003 * @see #getFilterTouchesWhenObscured
5004 */
5005 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005006 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005007 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5008 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5009 // Window is obscured, drop this touch.
5010 return false;
5011 }
5012 return true;
5013 }
5014
5015 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005016 * Pass a trackball motion event down to the focused view.
5017 *
5018 * @param event The motion event to be dispatched.
5019 * @return True if the event was handled by the view, false otherwise.
5020 */
5021 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005022 if (mInputEventConsistencyVerifier != null) {
5023 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5024 }
5025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005026 //Log.i("view", "view=" + this + ", " + event.toString());
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005027 if (onTrackballEvent(event)) {
5028 return true;
5029 }
5030
5031 if (mInputEventConsistencyVerifier != null) {
5032 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5033 }
5034 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005035 }
5036
5037 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005038 * Dispatch a generic motion event.
5039 * <p>
5040 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5041 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005042 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005043 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005044 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005045 *
5046 * @param event The motion event to be dispatched.
5047 * @return True if the event was handled by the view, false otherwise.
5048 */
5049 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005050 if (mInputEventConsistencyVerifier != null) {
5051 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5052 }
5053
Jeff Browna032cc02011-03-07 16:56:21 -08005054 final int source = event.getSource();
5055 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5056 final int action = event.getAction();
5057 if (action == MotionEvent.ACTION_HOVER_ENTER
5058 || action == MotionEvent.ACTION_HOVER_MOVE
5059 || action == MotionEvent.ACTION_HOVER_EXIT) {
5060 if (dispatchHoverEvent(event)) {
5061 return true;
5062 }
5063 } else if (dispatchGenericPointerEvent(event)) {
5064 return true;
5065 }
5066 } else if (dispatchGenericFocusedEvent(event)) {
5067 return true;
5068 }
5069
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005070 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08005071 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5072 && mOnGenericMotionListener.onGenericMotion(this, event)) {
5073 return true;
5074 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005075
5076 if (onGenericMotionEvent(event)) {
5077 return true;
5078 }
5079
5080 if (mInputEventConsistencyVerifier != null) {
5081 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5082 }
5083 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005084 }
5085
5086 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005087 * Dispatch a hover event.
5088 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005089 * Do not call this method directly.
5090 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005091 * </p>
5092 *
5093 * @param event The motion event to be dispatched.
5094 * @return True if the event was handled by the view, false otherwise.
5095 * @hide
5096 */
5097 protected boolean dispatchHoverEvent(MotionEvent event) {
5098 return onHoverEvent(event);
5099 }
5100
5101 /**
5102 * Dispatch a generic motion event to the view under the first pointer.
5103 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005104 * Do not call this method directly.
5105 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005106 * </p>
5107 *
5108 * @param event The motion event to be dispatched.
5109 * @return True if the event was handled by the view, false otherwise.
5110 * @hide
5111 */
5112 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5113 return false;
5114 }
5115
5116 /**
5117 * Dispatch a generic motion event to the currently focused view.
5118 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005119 * Do not call this method directly.
5120 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005121 * </p>
5122 *
5123 * @param event The motion event to be dispatched.
5124 * @return True if the event was handled by the view, false otherwise.
5125 * @hide
5126 */
5127 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5128 return false;
5129 }
5130
5131 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005132 * Dispatch a pointer event.
5133 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005134 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5135 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5136 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005137 * and should not be expected to handle other pointing device features.
5138 * </p>
5139 *
5140 * @param event The motion event to be dispatched.
5141 * @return True if the event was handled by the view, false otherwise.
5142 * @hide
5143 */
5144 public final boolean dispatchPointerEvent(MotionEvent event) {
5145 if (event.isTouchEvent()) {
5146 return dispatchTouchEvent(event);
5147 } else {
5148 return dispatchGenericMotionEvent(event);
5149 }
5150 }
5151
5152 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005153 * Called when the window containing this view gains or loses window focus.
5154 * ViewGroups should override to route to their children.
5155 *
5156 * @param hasFocus True if the window containing this view now has focus,
5157 * false otherwise.
5158 */
5159 public void dispatchWindowFocusChanged(boolean hasFocus) {
5160 onWindowFocusChanged(hasFocus);
5161 }
5162
5163 /**
5164 * Called when the window containing this view gains or loses focus. Note
5165 * that this is separate from view focus: to receive key events, both
5166 * your view and its window must have focus. If a window is displayed
5167 * on top of yours that takes input focus, then your own window will lose
5168 * focus but the view focus will remain unchanged.
5169 *
5170 * @param hasWindowFocus True if the window containing this view now has
5171 * focus, false otherwise.
5172 */
5173 public void onWindowFocusChanged(boolean hasWindowFocus) {
5174 InputMethodManager imm = InputMethodManager.peekInstance();
5175 if (!hasWindowFocus) {
5176 if (isPressed()) {
5177 setPressed(false);
5178 }
5179 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5180 imm.focusOut(this);
5181 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005182 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005183 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005184 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005185 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5186 imm.focusIn(this);
5187 }
5188 refreshDrawableState();
5189 }
5190
5191 /**
5192 * Returns true if this view is in a window that currently has window focus.
5193 * Note that this is not the same as the view itself having focus.
5194 *
5195 * @return True if this view is in a window that currently has window focus.
5196 */
5197 public boolean hasWindowFocus() {
5198 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5199 }
5200
5201 /**
Adam Powell326d8082009-12-09 15:10:07 -08005202 * Dispatch a view visibility change down the view hierarchy.
5203 * ViewGroups should override to route to their children.
5204 * @param changedView The view whose visibility changed. Could be 'this' or
5205 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005206 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5207 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005208 */
5209 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5210 onVisibilityChanged(changedView, visibility);
5211 }
5212
5213 /**
5214 * Called when the visibility of the view or an ancestor of the view is changed.
5215 * @param changedView The view whose visibility changed. Could be 'this' or
5216 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005217 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5218 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005219 */
5220 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005221 if (visibility == VISIBLE) {
5222 if (mAttachInfo != null) {
5223 initialAwakenScrollBars();
5224 } else {
5225 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5226 }
5227 }
Adam Powell326d8082009-12-09 15:10:07 -08005228 }
5229
5230 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005231 * Dispatch a hint about whether this view is displayed. For instance, when
5232 * a View moves out of the screen, it might receives a display hint indicating
5233 * the view is not displayed. Applications should not <em>rely</em> on this hint
5234 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005235 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005236 * @param hint A hint about whether or not this view is displayed:
5237 * {@link #VISIBLE} or {@link #INVISIBLE}.
5238 */
5239 public void dispatchDisplayHint(int hint) {
5240 onDisplayHint(hint);
5241 }
5242
5243 /**
5244 * Gives this view a hint about whether is displayed or not. For instance, when
5245 * a View moves out of the screen, it might receives a display hint indicating
5246 * the view is not displayed. Applications should not <em>rely</em> on this hint
5247 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005248 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005249 * @param hint A hint about whether or not this view is displayed:
5250 * {@link #VISIBLE} or {@link #INVISIBLE}.
5251 */
5252 protected void onDisplayHint(int hint) {
5253 }
5254
5255 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005256 * Dispatch a window visibility change down the view hierarchy.
5257 * ViewGroups should override to route to their children.
5258 *
5259 * @param visibility The new visibility of the window.
5260 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005261 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005262 */
5263 public void dispatchWindowVisibilityChanged(int visibility) {
5264 onWindowVisibilityChanged(visibility);
5265 }
5266
5267 /**
5268 * Called when the window containing has change its visibility
5269 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5270 * that this tells you whether or not your window is being made visible
5271 * to the window manager; this does <em>not</em> tell you whether or not
5272 * your window is obscured by other windows on the screen, even if it
5273 * is itself visible.
5274 *
5275 * @param visibility The new visibility of the window.
5276 */
5277 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005278 if (visibility == VISIBLE) {
5279 initialAwakenScrollBars();
5280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005281 }
5282
5283 /**
5284 * Returns the current visibility of the window this view is attached to
5285 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5286 *
5287 * @return Returns the current visibility of the view's window.
5288 */
5289 public int getWindowVisibility() {
5290 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5291 }
5292
5293 /**
5294 * Retrieve the overall visible display size in which the window this view is
5295 * attached to has been positioned in. This takes into account screen
5296 * decorations above the window, for both cases where the window itself
5297 * is being position inside of them or the window is being placed under
5298 * then and covered insets are used for the window to position its content
5299 * inside. In effect, this tells you the available area where content can
5300 * be placed and remain visible to users.
5301 *
5302 * <p>This function requires an IPC back to the window manager to retrieve
5303 * the requested information, so should not be used in performance critical
5304 * code like drawing.
5305 *
5306 * @param outRect Filled in with the visible display frame. If the view
5307 * is not attached to a window, this is simply the raw display size.
5308 */
5309 public void getWindowVisibleDisplayFrame(Rect outRect) {
5310 if (mAttachInfo != null) {
5311 try {
5312 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5313 } catch (RemoteException e) {
5314 return;
5315 }
5316 // XXX This is really broken, and probably all needs to be done
5317 // in the window manager, and we need to know more about whether
5318 // we want the area behind or in front of the IME.
5319 final Rect insets = mAttachInfo.mVisibleInsets;
5320 outRect.left += insets.left;
5321 outRect.top += insets.top;
5322 outRect.right -= insets.right;
5323 outRect.bottom -= insets.bottom;
5324 return;
5325 }
5326 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005327 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005328 }
5329
5330 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005331 * Dispatch a notification about a resource configuration change down
5332 * the view hierarchy.
5333 * ViewGroups should override to route to their children.
5334 *
5335 * @param newConfig The new resource configuration.
5336 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005337 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005338 */
5339 public void dispatchConfigurationChanged(Configuration newConfig) {
5340 onConfigurationChanged(newConfig);
5341 }
5342
5343 /**
5344 * Called when the current configuration of the resources being used
5345 * by the application have changed. You can use this to decide when
5346 * to reload resources that can changed based on orientation and other
5347 * configuration characterstics. You only need to use this if you are
5348 * not relying on the normal {@link android.app.Activity} mechanism of
5349 * recreating the activity instance upon a configuration change.
5350 *
5351 * @param newConfig The new resource configuration.
5352 */
5353 protected void onConfigurationChanged(Configuration newConfig) {
5354 }
5355
5356 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005357 * Private function to aggregate all per-view attributes in to the view
5358 * root.
5359 */
5360 void dispatchCollectViewAttributes(int visibility) {
5361 performCollectViewAttributes(visibility);
5362 }
5363
5364 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005365 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005366 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5367 mAttachInfo.mKeepScreenOn = true;
5368 }
5369 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5370 if (mOnSystemUiVisibilityChangeListener != null) {
5371 mAttachInfo.mHasSystemUiListeners = true;
5372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005373 }
5374 }
5375
5376 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005377 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005378 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005379 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5380 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381 ai.mRecomputeGlobalAttributes = true;
5382 }
5383 }
5384 }
5385
5386 /**
5387 * Returns whether the device is currently in touch mode. Touch mode is entered
5388 * once the user begins interacting with the device by touch, and affects various
5389 * things like whether focus is always visible to the user.
5390 *
5391 * @return Whether the device is in touch mode.
5392 */
5393 @ViewDebug.ExportedProperty
5394 public boolean isInTouchMode() {
5395 if (mAttachInfo != null) {
5396 return mAttachInfo.mInTouchMode;
5397 } else {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005398 return ViewAncestor.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005399 }
5400 }
5401
5402 /**
5403 * Returns the context the view is running in, through which it can
5404 * access the current theme, resources, etc.
5405 *
5406 * @return The view's Context.
5407 */
5408 @ViewDebug.CapturedViewProperty
5409 public final Context getContext() {
5410 return mContext;
5411 }
5412
5413 /**
5414 * Handle a key event before it is processed by any input method
5415 * associated with the view hierarchy. This can be used to intercept
5416 * key events in special situations before the IME consumes them; a
5417 * typical example would be handling the BACK key to update the application's
5418 * UI instead of allowing the IME to see it and close itself.
5419 *
5420 * @param keyCode The value in event.getKeyCode().
5421 * @param event Description of the key event.
5422 * @return If you handled the event, return true. If you want to allow the
5423 * event to be handled by the next receiver, return false.
5424 */
5425 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5426 return false;
5427 }
5428
5429 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005430 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5431 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005432 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5433 * is released, if the view is enabled and clickable.
5434 *
5435 * @param keyCode A key code that represents the button pressed, from
5436 * {@link android.view.KeyEvent}.
5437 * @param event The KeyEvent object that defines the button action.
5438 */
5439 public boolean onKeyDown(int keyCode, KeyEvent event) {
5440 boolean result = false;
5441
5442 switch (keyCode) {
5443 case KeyEvent.KEYCODE_DPAD_CENTER:
5444 case KeyEvent.KEYCODE_ENTER: {
5445 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5446 return true;
5447 }
5448 // Long clickable items don't necessarily have to be clickable
5449 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5450 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5451 (event.getRepeatCount() == 0)) {
5452 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005453 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005454 return true;
5455 }
5456 break;
5457 }
5458 }
5459 return result;
5460 }
5461
5462 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005463 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5464 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5465 * the event).
5466 */
5467 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5468 return false;
5469 }
5470
5471 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005472 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5473 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005474 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5475 * {@link KeyEvent#KEYCODE_ENTER} is released.
5476 *
5477 * @param keyCode A key code that represents the button pressed, from
5478 * {@link android.view.KeyEvent}.
5479 * @param event The KeyEvent object that defines the button action.
5480 */
5481 public boolean onKeyUp(int keyCode, KeyEvent event) {
5482 boolean result = false;
5483
5484 switch (keyCode) {
5485 case KeyEvent.KEYCODE_DPAD_CENTER:
5486 case KeyEvent.KEYCODE_ENTER: {
5487 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5488 return true;
5489 }
5490 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5491 setPressed(false);
5492
5493 if (!mHasPerformedLongPress) {
5494 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005495 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005496
5497 result = performClick();
5498 }
5499 }
5500 break;
5501 }
5502 }
5503 return result;
5504 }
5505
5506 /**
5507 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5508 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5509 * the event).
5510 *
5511 * @param keyCode A key code that represents the button pressed, from
5512 * {@link android.view.KeyEvent}.
5513 * @param repeatCount The number of times the action was made.
5514 * @param event The KeyEvent object that defines the button action.
5515 */
5516 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5517 return false;
5518 }
5519
5520 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005521 * Called on the focused view when a key shortcut event is not handled.
5522 * Override this method to implement local key shortcuts for the View.
5523 * Key shortcuts can also be implemented by setting the
5524 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005525 *
5526 * @param keyCode The value in event.getKeyCode().
5527 * @param event Description of the key event.
5528 * @return If you handled the event, return true. If you want to allow the
5529 * event to be handled by the next receiver, return false.
5530 */
5531 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5532 return false;
5533 }
5534
5535 /**
5536 * Check whether the called view is a text editor, in which case it
5537 * would make sense to automatically display a soft input window for
5538 * it. Subclasses should override this if they implement
5539 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005540 * a call on that method would return a non-null InputConnection, and
5541 * they are really a first-class editor that the user would normally
5542 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005543 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005544 * <p>The default implementation always returns false. This does
5545 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5546 * will not be called or the user can not otherwise perform edits on your
5547 * view; it is just a hint to the system that this is not the primary
5548 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005549 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005550 * @return Returns true if this view is a text editor, else false.
5551 */
5552 public boolean onCheckIsTextEditor() {
5553 return false;
5554 }
Romain Guy8506ab42009-06-11 17:35:47 -07005555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005556 /**
5557 * Create a new InputConnection for an InputMethod to interact
5558 * with the view. The default implementation returns null, since it doesn't
5559 * support input methods. You can override this to implement such support.
5560 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005561 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005562 * <p>When implementing this, you probably also want to implement
5563 * {@link #onCheckIsTextEditor()} to indicate you will return a
5564 * non-null InputConnection.
5565 *
5566 * @param outAttrs Fill in with attribute information about the connection.
5567 */
5568 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5569 return null;
5570 }
5571
5572 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005573 * Called by the {@link android.view.inputmethod.InputMethodManager}
5574 * when a view who is not the current
5575 * input connection target is trying to make a call on the manager. The
5576 * default implementation returns false; you can override this to return
5577 * true for certain views if you are performing InputConnection proxying
5578 * to them.
5579 * @param view The View that is making the InputMethodManager call.
5580 * @return Return true to allow the call, false to reject.
5581 */
5582 public boolean checkInputConnectionProxy(View view) {
5583 return false;
5584 }
Romain Guy8506ab42009-06-11 17:35:47 -07005585
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005586 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005587 * Show the context menu for this view. It is not safe to hold on to the
5588 * menu after returning from this method.
5589 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005590 * You should normally not overload this method. Overload
5591 * {@link #onCreateContextMenu(ContextMenu)} or define an
5592 * {@link OnCreateContextMenuListener} to add items to the context menu.
5593 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005594 * @param menu The context menu to populate
5595 */
5596 public void createContextMenu(ContextMenu menu) {
5597 ContextMenuInfo menuInfo = getContextMenuInfo();
5598
5599 // Sets the current menu info so all items added to menu will have
5600 // my extra info set.
5601 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5602
5603 onCreateContextMenu(menu);
5604 if (mOnCreateContextMenuListener != null) {
5605 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5606 }
5607
5608 // Clear the extra information so subsequent items that aren't mine don't
5609 // have my extra info.
5610 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5611
5612 if (mParent != null) {
5613 mParent.createContextMenu(menu);
5614 }
5615 }
5616
5617 /**
5618 * Views should implement this if they have extra information to associate
5619 * with the context menu. The return result is supplied as a parameter to
5620 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5621 * callback.
5622 *
5623 * @return Extra information about the item for which the context menu
5624 * should be shown. This information will vary across different
5625 * subclasses of View.
5626 */
5627 protected ContextMenuInfo getContextMenuInfo() {
5628 return null;
5629 }
5630
5631 /**
5632 * Views should implement this if the view itself is going to add items to
5633 * the context menu.
5634 *
5635 * @param menu the context menu to populate
5636 */
5637 protected void onCreateContextMenu(ContextMenu menu) {
5638 }
5639
5640 /**
5641 * Implement this method to handle trackball motion events. The
5642 * <em>relative</em> movement of the trackball since the last event
5643 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5644 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5645 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5646 * they will often be fractional values, representing the more fine-grained
5647 * movement information available from a trackball).
5648 *
5649 * @param event The motion event.
5650 * @return True if the event was handled, false otherwise.
5651 */
5652 public boolean onTrackballEvent(MotionEvent event) {
5653 return false;
5654 }
5655
5656 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005657 * Implement this method to handle generic motion events.
5658 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005659 * Generic motion events describe joystick movements, mouse hovers, track pad
5660 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005661 * {@link MotionEvent#getSource() source} of the motion event specifies
5662 * the class of input that was received. Implementations of this method
5663 * must examine the bits in the source before processing the event.
5664 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005665 * </p><p>
5666 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5667 * are delivered to the view under the pointer. All other generic motion events are
5668 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005669 * </p>
5670 * <code>
5671 * public boolean onGenericMotionEvent(MotionEvent event) {
5672 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005673 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5674 * // process the joystick movement...
5675 * return true;
5676 * }
5677 * }
5678 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5679 * switch (event.getAction()) {
5680 * case MotionEvent.ACTION_HOVER_MOVE:
5681 * // process the mouse hover movement...
5682 * return true;
5683 * case MotionEvent.ACTION_SCROLL:
5684 * // process the scroll wheel movement...
5685 * return true;
5686 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005687 * }
5688 * return super.onGenericMotionEvent(event);
5689 * }
5690 * </code>
5691 *
5692 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08005693 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08005694 */
5695 public boolean onGenericMotionEvent(MotionEvent event) {
5696 return false;
5697 }
5698
5699 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005700 * Implement this method to handle hover events.
5701 * <p>
5702 * Hover events are pointer events with action {@link MotionEvent#ACTION_HOVER_ENTER},
5703 * {@link MotionEvent#ACTION_HOVER_MOVE}, or {@link MotionEvent#ACTION_HOVER_EXIT}.
5704 * </p><p>
5705 * The view receives hover enter as the pointer enters the bounds of the view and hover
5706 * exit as the pointer exits the bound of the view or just before the pointer goes down
Romain Guy5c22a8c2011-05-13 11:48:45 -07005707 * (which implies that {@link #onTouchEvent(MotionEvent)} will be called soon).
Jeff Browna032cc02011-03-07 16:56:21 -08005708 * </p><p>
5709 * If the view would like to handle the hover event itself and prevent its children
5710 * from receiving hover, it should return true from this method. If this method returns
5711 * true and a child has already received a hover enter event, the child will
5712 * automatically receive a hover exit event.
5713 * </p><p>
5714 * The default implementation sets the hovered state of the view if the view is
5715 * clickable.
5716 * </p>
5717 *
5718 * @param event The motion event that describes the hover.
5719 * @return True if this view handled the hover event and does not want its children
5720 * to receive the hover event.
5721 */
5722 public boolean onHoverEvent(MotionEvent event) {
Jeff Browna032cc02011-03-07 16:56:21 -08005723 switch (event.getAction()) {
5724 case MotionEvent.ACTION_HOVER_ENTER:
5725 setHovered(true);
5726 break;
5727
5728 case MotionEvent.ACTION_HOVER_EXIT:
5729 setHovered(false);
5730 break;
5731 }
5732
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005733 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08005734 }
5735
5736 /**
5737 * Returns true if the view is currently hovered.
5738 *
5739 * @return True if the view is currently hovered.
5740 */
5741 public boolean isHovered() {
5742 return (mPrivateFlags & HOVERED) != 0;
5743 }
5744
5745 /**
5746 * Sets whether the view is currently hovered.
5747 *
5748 * @param hovered True if the view is hovered.
5749 */
5750 public void setHovered(boolean hovered) {
5751 if (hovered) {
5752 if ((mPrivateFlags & HOVERED) == 0) {
5753 mPrivateFlags |= HOVERED;
5754 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005755 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Jeff Browna032cc02011-03-07 16:56:21 -08005756 }
5757 } else {
5758 if ((mPrivateFlags & HOVERED) != 0) {
5759 mPrivateFlags &= ~HOVERED;
5760 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005761 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Jeff Browna032cc02011-03-07 16:56:21 -08005762 }
5763 }
5764 }
5765
5766 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005767 * Implement this method to handle touch screen motion events.
5768 *
5769 * @param event The motion event.
5770 * @return True if the event was handled, false otherwise.
5771 */
5772 public boolean onTouchEvent(MotionEvent event) {
5773 final int viewFlags = mViewFlags;
5774
5775 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07005776 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
5777 mPrivateFlags &= ~PRESSED;
5778 refreshDrawableState();
5779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005780 // A disabled view that is clickable still consumes the touch
5781 // events, it just doesn't respond to them.
5782 return (((viewFlags & CLICKABLE) == CLICKABLE ||
5783 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
5784 }
5785
5786 if (mTouchDelegate != null) {
5787 if (mTouchDelegate.onTouchEvent(event)) {
5788 return true;
5789 }
5790 }
5791
5792 if (((viewFlags & CLICKABLE) == CLICKABLE ||
5793 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
5794 switch (event.getAction()) {
5795 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08005796 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
5797 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005798 // take focus if we don't have it already and we should in
5799 // touch mode.
5800 boolean focusTaken = false;
5801 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
5802 focusTaken = requestFocus();
5803 }
5804
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08005805 if (prepressed) {
5806 // The button is being released before we actually
5807 // showed it as pressed. Make it show the pressed
5808 // state now (before scheduling the click) to ensure
5809 // the user sees it.
5810 mPrivateFlags |= PRESSED;
5811 refreshDrawableState();
5812 }
Joe Malin32736f02011-01-19 16:14:20 -08005813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005814 if (!mHasPerformedLongPress) {
5815 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005816 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005817
5818 // Only perform take click actions if we were in the pressed state
5819 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08005820 // Use a Runnable and post this rather than calling
5821 // performClick directly. This lets other visual state
5822 // of the view update before click actions start.
5823 if (mPerformClick == null) {
5824 mPerformClick = new PerformClick();
5825 }
5826 if (!post(mPerformClick)) {
5827 performClick();
5828 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005829 }
5830 }
5831
5832 if (mUnsetPressedState == null) {
5833 mUnsetPressedState = new UnsetPressedState();
5834 }
5835
Adam Powelle14579b2009-12-16 18:39:52 -08005836 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08005837 postDelayed(mUnsetPressedState,
5838 ViewConfiguration.getPressedStateDuration());
5839 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005840 // If the post failed, unpress right now
5841 mUnsetPressedState.run();
5842 }
Adam Powelle14579b2009-12-16 18:39:52 -08005843 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005844 }
5845 break;
5846
5847 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08005848 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005849
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005850 if (performButtonActionOnTouchDown(event)) {
5851 break;
5852 }
5853
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005854 // Walk up the hierarchy to determine if we're inside a scrolling container.
5855 boolean isInScrollingContainer = false;
5856 ViewParent p = getParent();
5857 while (p != null && p instanceof ViewGroup) {
5858 if (((ViewGroup) p).shouldDelayChildPressedState()) {
5859 isInScrollingContainer = true;
5860 break;
5861 }
5862 p = p.getParent();
5863 }
5864
5865 // For views inside a scrolling container, delay the pressed feedback for
5866 // a short period in case this is a scroll.
5867 if (isInScrollingContainer) {
5868 mPrivateFlags |= PREPRESSED;
5869 if (mPendingCheckForTap == null) {
5870 mPendingCheckForTap = new CheckForTap();
5871 }
5872 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
5873 } else {
5874 // Not inside a scrolling container, so show the feedback right away
5875 mPrivateFlags |= PRESSED;
5876 refreshDrawableState();
5877 checkForLongClick(0);
5878 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005879 break;
5880
5881 case MotionEvent.ACTION_CANCEL:
5882 mPrivateFlags &= ~PRESSED;
5883 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08005884 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005885 break;
5886
5887 case MotionEvent.ACTION_MOVE:
5888 final int x = (int) event.getX();
5889 final int y = (int) event.getY();
5890
5891 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07005892 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005893 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08005894 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005895 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08005896 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05005897 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005898
5899 // Need to switch from pressed to not pressed
5900 mPrivateFlags &= ~PRESSED;
5901 refreshDrawableState();
5902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005903 }
5904 break;
5905 }
5906 return true;
5907 }
5908
5909 return false;
5910 }
5911
5912 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05005913 * Remove the longpress detection timer.
5914 */
5915 private void removeLongPressCallback() {
5916 if (mPendingCheckForLongPress != null) {
5917 removeCallbacks(mPendingCheckForLongPress);
5918 }
5919 }
Adam Powell3cb8b632011-01-21 15:34:14 -08005920
5921 /**
5922 * Remove the pending click action
5923 */
5924 private void removePerformClickCallback() {
5925 if (mPerformClick != null) {
5926 removeCallbacks(mPerformClick);
5927 }
5928 }
5929
Adam Powelle14579b2009-12-16 18:39:52 -08005930 /**
Romain Guya440b002010-02-24 15:57:54 -08005931 * Remove the prepress detection timer.
5932 */
5933 private void removeUnsetPressCallback() {
5934 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
5935 setPressed(false);
5936 removeCallbacks(mUnsetPressedState);
5937 }
5938 }
5939
5940 /**
Adam Powelle14579b2009-12-16 18:39:52 -08005941 * Remove the tap detection timer.
5942 */
5943 private void removeTapCallback() {
5944 if (mPendingCheckForTap != null) {
5945 mPrivateFlags &= ~PREPRESSED;
5946 removeCallbacks(mPendingCheckForTap);
5947 }
5948 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005949
5950 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005951 * Cancels a pending long press. Your subclass can use this if you
5952 * want the context menu to come up if the user presses and holds
5953 * at the same place, but you don't want it to come up if they press
5954 * and then move around enough to cause scrolling.
5955 */
5956 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005957 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08005958
5959 /*
5960 * The prepressed state handled by the tap callback is a display
5961 * construct, but the tap callback will post a long press callback
5962 * less its own timeout. Remove it here.
5963 */
5964 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005965 }
5966
5967 /**
5968 * Sets the TouchDelegate for this View.
5969 */
5970 public void setTouchDelegate(TouchDelegate delegate) {
5971 mTouchDelegate = delegate;
5972 }
5973
5974 /**
5975 * Gets the TouchDelegate for this View.
5976 */
5977 public TouchDelegate getTouchDelegate() {
5978 return mTouchDelegate;
5979 }
5980
5981 /**
5982 * Set flags controlling behavior of this view.
5983 *
5984 * @param flags Constant indicating the value which should be set
5985 * @param mask Constant indicating the bit range that should be changed
5986 */
5987 void setFlags(int flags, int mask) {
5988 int old = mViewFlags;
5989 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
5990
5991 int changed = mViewFlags ^ old;
5992 if (changed == 0) {
5993 return;
5994 }
5995 int privateFlags = mPrivateFlags;
5996
5997 /* Check if the FOCUSABLE bit has changed */
5998 if (((changed & FOCUSABLE_MASK) != 0) &&
5999 ((privateFlags & HAS_BOUNDS) !=0)) {
6000 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6001 && ((privateFlags & FOCUSED) != 0)) {
6002 /* Give up focus if we are no longer focusable */
6003 clearFocus();
6004 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6005 && ((privateFlags & FOCUSED) == 0)) {
6006 /*
6007 * Tell the view system that we are now available to take focus
6008 * if no one else already has it.
6009 */
6010 if (mParent != null) mParent.focusableViewAvailable(this);
6011 }
6012 }
6013
6014 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6015 if ((changed & VISIBILITY_MASK) != 0) {
6016 /*
6017 * If this view is becoming visible, set the DRAWN flag so that
6018 * the next invalidate() will not be skipped.
6019 */
6020 mPrivateFlags |= DRAWN;
6021
6022 needGlobalAttributesUpdate(true);
6023
6024 // a view becoming visible is worth notifying the parent
6025 // about in case nothing has focus. even if this specific view
6026 // isn't focusable, it may contain something that is, so let
6027 // the root view try to give this focus if nothing else does.
6028 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6029 mParent.focusableViewAvailable(this);
6030 }
6031 }
6032 }
6033
6034 /* Check if the GONE bit has changed */
6035 if ((changed & GONE) != 0) {
6036 needGlobalAttributesUpdate(false);
6037 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006038 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039
Romain Guyecd80ee2009-12-03 17:13:02 -08006040 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6041 if (hasFocus()) clearFocus();
6042 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006043 }
6044 if (mAttachInfo != null) {
6045 mAttachInfo.mViewVisibilityChanged = true;
6046 }
6047 }
6048
6049 /* Check if the VISIBLE bit has changed */
6050 if ((changed & INVISIBLE) != 0) {
6051 needGlobalAttributesUpdate(false);
Romain Guy0fd89bf2011-01-26 15:41:30 -08006052 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006053
6054 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6055 // root view becoming invisible shouldn't clear focus
6056 if (getRootView() != this) {
6057 clearFocus();
6058 }
6059 }
6060 if (mAttachInfo != null) {
6061 mAttachInfo.mViewVisibilityChanged = true;
6062 }
6063 }
6064
Adam Powell326d8082009-12-09 15:10:07 -08006065 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006066 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006067 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6068 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006069 }
Adam Powell326d8082009-12-09 15:10:07 -08006070 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6071 }
6072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006073 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6074 destroyDrawingCache();
6075 }
6076
6077 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6078 destroyDrawingCache();
6079 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006080 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006081 }
6082
6083 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6084 destroyDrawingCache();
6085 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6086 }
6087
6088 if ((changed & DRAW_MASK) != 0) {
6089 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6090 if (mBGDrawable != null) {
6091 mPrivateFlags &= ~SKIP_DRAW;
6092 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6093 } else {
6094 mPrivateFlags |= SKIP_DRAW;
6095 }
6096 } else {
6097 mPrivateFlags &= ~SKIP_DRAW;
6098 }
6099 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006100 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006101 }
6102
6103 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006104 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006105 mParent.recomputeViewAttributes(this);
6106 }
6107 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006108
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006109 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006110 requestLayout();
6111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006112 }
6113
6114 /**
6115 * Change the view's z order in the tree, so it's on top of other sibling
6116 * views
6117 */
6118 public void bringToFront() {
6119 if (mParent != null) {
6120 mParent.bringChildToFront(this);
6121 }
6122 }
6123
6124 /**
6125 * This is called in response to an internal scroll in this view (i.e., the
6126 * view scrolled its own contents). This is typically as a result of
6127 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6128 * called.
6129 *
6130 * @param l Current horizontal scroll origin.
6131 * @param t Current vertical scroll origin.
6132 * @param oldl Previous horizontal scroll origin.
6133 * @param oldt Previous vertical scroll origin.
6134 */
6135 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
6136 mBackgroundSizeChanged = true;
6137
6138 final AttachInfo ai = mAttachInfo;
6139 if (ai != null) {
6140 ai.mViewScrollChanged = true;
6141 }
6142 }
6143
6144 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006145 * Interface definition for a callback to be invoked when the layout bounds of a view
6146 * changes due to layout processing.
6147 */
6148 public interface OnLayoutChangeListener {
6149 /**
6150 * Called when the focus state of a view has changed.
6151 *
6152 * @param v The view whose state has changed.
6153 * @param left The new value of the view's left property.
6154 * @param top The new value of the view's top property.
6155 * @param right The new value of the view's right property.
6156 * @param bottom The new value of the view's bottom property.
6157 * @param oldLeft The previous value of the view's left property.
6158 * @param oldTop The previous value of the view's top property.
6159 * @param oldRight The previous value of the view's right property.
6160 * @param oldBottom The previous value of the view's bottom property.
6161 */
6162 void onLayoutChange(View v, int left, int top, int right, int bottom,
6163 int oldLeft, int oldTop, int oldRight, int oldBottom);
6164 }
6165
6166 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006167 * This is called during layout when the size of this view has changed. If
6168 * you were just added to the view hierarchy, you're called with the old
6169 * values of 0.
6170 *
6171 * @param w Current width of this view.
6172 * @param h Current height of this view.
6173 * @param oldw Old width of this view.
6174 * @param oldh Old height of this view.
6175 */
6176 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6177 }
6178
6179 /**
6180 * Called by draw to draw the child views. This may be overridden
6181 * by derived classes to gain control just before its children are drawn
6182 * (but after its own view has been drawn).
6183 * @param canvas the canvas on which to draw the view
6184 */
6185 protected void dispatchDraw(Canvas canvas) {
6186 }
6187
6188 /**
6189 * Gets the parent of this view. Note that the parent is a
6190 * ViewParent and not necessarily a View.
6191 *
6192 * @return Parent of this view.
6193 */
6194 public final ViewParent getParent() {
6195 return mParent;
6196 }
6197
6198 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006199 * Set the horizontal scrolled position of your view. This will cause a call to
6200 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6201 * invalidated.
6202 * @param value the x position to scroll to
6203 */
6204 public void setScrollX(int value) {
6205 scrollTo(value, mScrollY);
6206 }
6207
6208 /**
6209 * Set the vertical scrolled position of your view. This will cause a call to
6210 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6211 * invalidated.
6212 * @param value the y position to scroll to
6213 */
6214 public void setScrollY(int value) {
6215 scrollTo(mScrollX, value);
6216 }
6217
6218 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006219 * Return the scrolled left position of this view. This is the left edge of
6220 * the displayed part of your view. You do not need to draw any pixels
6221 * farther left, since those are outside of the frame of your view on
6222 * screen.
6223 *
6224 * @return The left edge of the displayed part of your view, in pixels.
6225 */
6226 public final int getScrollX() {
6227 return mScrollX;
6228 }
6229
6230 /**
6231 * Return the scrolled top position of this view. This is the top edge of
6232 * the displayed part of your view. You do not need to draw any pixels above
6233 * it, since those are outside of the frame of your view on screen.
6234 *
6235 * @return The top edge of the displayed part of your view, in pixels.
6236 */
6237 public final int getScrollY() {
6238 return mScrollY;
6239 }
6240
6241 /**
6242 * Return the width of the your view.
6243 *
6244 * @return The width of your view, in pixels.
6245 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006246 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006247 public final int getWidth() {
6248 return mRight - mLeft;
6249 }
6250
6251 /**
6252 * Return the height of your view.
6253 *
6254 * @return The height of your view, in pixels.
6255 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006256 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006257 public final int getHeight() {
6258 return mBottom - mTop;
6259 }
6260
6261 /**
6262 * Return the visible drawing bounds of your view. Fills in the output
6263 * rectangle with the values from getScrollX(), getScrollY(),
6264 * getWidth(), and getHeight().
6265 *
6266 * @param outRect The (scrolled) drawing bounds of the view.
6267 */
6268 public void getDrawingRect(Rect outRect) {
6269 outRect.left = mScrollX;
6270 outRect.top = mScrollY;
6271 outRect.right = mScrollX + (mRight - mLeft);
6272 outRect.bottom = mScrollY + (mBottom - mTop);
6273 }
6274
6275 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006276 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6277 * raw width component (that is the result is masked by
6278 * {@link #MEASURED_SIZE_MASK}).
6279 *
6280 * @return The raw measured width of this view.
6281 */
6282 public final int getMeasuredWidth() {
6283 return mMeasuredWidth & MEASURED_SIZE_MASK;
6284 }
6285
6286 /**
6287 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006288 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006289 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006290 * This should be used during measurement and layout calculations only. Use
6291 * {@link #getWidth()} to see how wide a view is after layout.
6292 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006293 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006294 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006295 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006296 return mMeasuredWidth;
6297 }
6298
6299 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006300 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6301 * raw width component (that is the result is masked by
6302 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006303 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006304 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006305 */
6306 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006307 return mMeasuredHeight & MEASURED_SIZE_MASK;
6308 }
6309
6310 /**
6311 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006312 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006313 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6314 * This should be used during measurement and layout calculations only. Use
6315 * {@link #getHeight()} to see how wide a view is after layout.
6316 *
6317 * @return The measured width of this view as a bit mask.
6318 */
6319 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006320 return mMeasuredHeight;
6321 }
6322
6323 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006324 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6325 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6326 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6327 * and the height component is at the shifted bits
6328 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6329 */
6330 public final int getMeasuredState() {
6331 return (mMeasuredWidth&MEASURED_STATE_MASK)
6332 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6333 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6334 }
6335
6336 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006337 * The transform matrix of this view, which is calculated based on the current
6338 * roation, scale, and pivot properties.
6339 *
6340 * @see #getRotation()
6341 * @see #getScaleX()
6342 * @see #getScaleY()
6343 * @see #getPivotX()
6344 * @see #getPivotY()
6345 * @return The current transform matrix for the view
6346 */
6347 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07006348 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07006349 return mMatrix;
6350 }
6351
6352 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006353 * Utility function to determine if the value is far enough away from zero to be
6354 * considered non-zero.
6355 * @param value A floating point value to check for zero-ness
6356 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6357 */
6358 private static boolean nonzero(float value) {
6359 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6360 }
6361
6362 /**
Jeff Brown86671742010-09-30 20:00:15 -07006363 * Returns true if the transform matrix is the identity matrix.
6364 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006365 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006366 * @return True if the transform matrix is the identity matrix, false otherwise.
6367 */
Jeff Brown86671742010-09-30 20:00:15 -07006368 final boolean hasIdentityMatrix() {
6369 updateMatrix();
6370 return mMatrixIsIdentity;
6371 }
6372
6373 /**
6374 * Recomputes the transform matrix if necessary.
6375 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006376 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07006377 if (mMatrixDirty) {
6378 // transform-related properties have changed since the last time someone
6379 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006380
6381 // Figure out if we need to update the pivot point
6382 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006383 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006384 mPrevWidth = mRight - mLeft;
6385 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08006386 mPivotX = mPrevWidth / 2f;
6387 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07006388 }
6389 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006390 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07006391 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
6392 mMatrix.setTranslate(mTranslationX, mTranslationY);
6393 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
6394 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
6395 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07006396 if (mCamera == null) {
6397 mCamera = new Camera();
6398 matrix3D = new Matrix();
6399 }
6400 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07006401 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08006402 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07006403 mCamera.getMatrix(matrix3D);
6404 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006405 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07006406 mMatrix.postConcat(matrix3D);
6407 mCamera.restore();
6408 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006409 mMatrixDirty = false;
6410 mMatrixIsIdentity = mMatrix.isIdentity();
6411 mInverseMatrixDirty = true;
6412 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006413 }
6414
6415 /**
6416 * Utility method to retrieve the inverse of the current mMatrix property.
6417 * We cache the matrix to avoid recalculating it when transform properties
6418 * have not changed.
6419 *
6420 * @return The inverse of the current matrix of this view.
6421 */
Jeff Brown86671742010-09-30 20:00:15 -07006422 final Matrix getInverseMatrix() {
6423 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07006424 if (mInverseMatrixDirty) {
6425 if (mInverseMatrix == null) {
6426 mInverseMatrix = new Matrix();
6427 }
6428 mMatrix.invert(mInverseMatrix);
6429 mInverseMatrixDirty = false;
6430 }
6431 return mInverseMatrix;
6432 }
6433
6434 /**
Romain Guya5364ee2011-02-24 14:46:04 -08006435 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
6436 * views are drawn) from the camera to this view. The camera's distance
6437 * affects 3D transformations, for instance rotations around the X and Y
6438 * axis. If the rotationX or rotationY properties are changed and this view is
6439 * large (more than half the size of the screen), it is recommended to always
6440 * use a camera distance that's greater than the height (X axis rotation) or
6441 * the width (Y axis rotation) of this view.</p>
6442 *
6443 * <p>The distance of the camera from the view plane can have an affect on the
6444 * perspective distortion of the view when it is rotated around the x or y axis.
6445 * For example, a large distance will result in a large viewing angle, and there
6446 * will not be much perspective distortion of the view as it rotates. A short
6447 * distance may cause much more perspective distortion upon rotation, and can
6448 * also result in some drawing artifacts if the rotated view ends up partially
6449 * behind the camera (which is why the recommendation is to use a distance at
6450 * least as far as the size of the view, if the view is to be rotated.)</p>
6451 *
6452 * <p>The distance is expressed in "depth pixels." The default distance depends
6453 * on the screen density. For instance, on a medium density display, the
6454 * default distance is 1280. On a high density display, the default distance
6455 * is 1920.</p>
6456 *
6457 * <p>If you want to specify a distance that leads to visually consistent
6458 * results across various densities, use the following formula:</p>
6459 * <pre>
6460 * float scale = context.getResources().getDisplayMetrics().density;
6461 * view.setCameraDistance(distance * scale);
6462 * </pre>
6463 *
6464 * <p>The density scale factor of a high density display is 1.5,
6465 * and 1920 = 1280 * 1.5.</p>
6466 *
6467 * @param distance The distance in "depth pixels", if negative the opposite
6468 * value is used
6469 *
6470 * @see #setRotationX(float)
6471 * @see #setRotationY(float)
6472 */
6473 public void setCameraDistance(float distance) {
6474 invalidateParentCaches();
6475 invalidate(false);
6476
6477 final float dpi = mResources.getDisplayMetrics().densityDpi;
6478 if (mCamera == null) {
6479 mCamera = new Camera();
6480 matrix3D = new Matrix();
6481 }
6482
6483 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6484 mMatrixDirty = true;
6485
6486 invalidate(false);
6487 }
6488
6489 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006490 * The degrees that the view is rotated around the pivot point.
6491 *
Romain Guya5364ee2011-02-24 14:46:04 -08006492 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006493 * @see #getPivotX()
6494 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006495 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006496 * @return The degrees of rotation.
6497 */
6498 public float getRotation() {
6499 return mRotation;
6500 }
6501
6502 /**
Chet Haase897247b2010-09-09 14:54:47 -07006503 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6504 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006505 *
6506 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006507 *
6508 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006509 * @see #getPivotX()
6510 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006511 * @see #setRotationX(float)
6512 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006513 *
6514 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006515 */
6516 public void setRotation(float rotation) {
6517 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006518 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006519 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006520 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006521 mRotation = rotation;
6522 mMatrixDirty = true;
6523 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006524 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006525 }
6526 }
6527
6528 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006529 * The degrees that the view is rotated around the vertical axis through the pivot point.
6530 *
6531 * @see #getPivotX()
6532 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006533 * @see #setRotationY(float)
6534 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006535 * @return The degrees of Y rotation.
6536 */
6537 public float getRotationY() {
6538 return mRotationY;
6539 }
6540
6541 /**
Chet Haase897247b2010-09-09 14:54:47 -07006542 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6543 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6544 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006545 *
6546 * When rotating large views, it is recommended to adjust the camera distance
6547 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006548 *
6549 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006550 *
6551 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006552 * @see #getPivotX()
6553 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006554 * @see #setRotation(float)
6555 * @see #setRotationX(float)
6556 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006557 *
6558 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006559 */
6560 public void setRotationY(float rotationY) {
6561 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006562 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006563 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006564 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006565 mRotationY = rotationY;
6566 mMatrixDirty = true;
6567 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006568 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006569 }
6570 }
6571
6572 /**
6573 * The degrees that the view is rotated around the horizontal axis through the pivot point.
6574 *
6575 * @see #getPivotX()
6576 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006577 * @see #setRotationX(float)
6578 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006579 * @return The degrees of X rotation.
6580 */
6581 public float getRotationX() {
6582 return mRotationX;
6583 }
6584
6585 /**
Chet Haase897247b2010-09-09 14:54:47 -07006586 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6587 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6588 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006589 *
6590 * When rotating large views, it is recommended to adjust the camera distance
6591 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006592 *
6593 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006594 *
6595 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006596 * @see #getPivotX()
6597 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006598 * @see #setRotation(float)
6599 * @see #setRotationY(float)
6600 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006601 *
6602 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006603 */
6604 public void setRotationX(float rotationX) {
6605 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006606 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006607 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006608 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006609 mRotationX = rotationX;
6610 mMatrixDirty = true;
6611 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006612 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006613 }
6614 }
6615
6616 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006617 * The amount that the view is scaled in x around the pivot point, as a proportion of
6618 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6619 *
Joe Onorato93162322010-09-16 15:42:01 -04006620 * <p>By default, this is 1.0f.
6621 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006622 * @see #getPivotX()
6623 * @see #getPivotY()
6624 * @return The scaling factor.
6625 */
6626 public float getScaleX() {
6627 return mScaleX;
6628 }
6629
6630 /**
6631 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6632 * the view's unscaled width. A value of 1 means that no scaling is applied.
6633 *
6634 * @param scaleX The scaling factor.
6635 * @see #getPivotX()
6636 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006637 *
6638 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006639 */
6640 public void setScaleX(float scaleX) {
6641 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006642 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006643 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006644 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006645 mScaleX = scaleX;
6646 mMatrixDirty = true;
6647 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006648 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006649 }
6650 }
6651
6652 /**
6653 * The amount that the view is scaled in y around the pivot point, as a proportion of
6654 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6655 *
Joe Onorato93162322010-09-16 15:42:01 -04006656 * <p>By default, this is 1.0f.
6657 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006658 * @see #getPivotX()
6659 * @see #getPivotY()
6660 * @return The scaling factor.
6661 */
6662 public float getScaleY() {
6663 return mScaleY;
6664 }
6665
6666 /**
6667 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
6668 * the view's unscaled width. A value of 1 means that no scaling is applied.
6669 *
6670 * @param scaleY The scaling factor.
6671 * @see #getPivotX()
6672 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006673 *
6674 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07006675 */
6676 public void setScaleY(float scaleY) {
6677 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006678 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006679 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006680 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006681 mScaleY = scaleY;
6682 mMatrixDirty = true;
6683 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006684 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006685 }
6686 }
6687
6688 /**
6689 * The x location of the point around which the view is {@link #setRotation(float) rotated}
6690 * and {@link #setScaleX(float) scaled}.
6691 *
6692 * @see #getRotation()
6693 * @see #getScaleX()
6694 * @see #getScaleY()
6695 * @see #getPivotY()
6696 * @return The x location of the pivot point.
6697 */
6698 public float getPivotX() {
6699 return mPivotX;
6700 }
6701
6702 /**
6703 * Sets the x location of the point around which the view is
6704 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07006705 * By default, the pivot point is centered on the object.
6706 * Setting this property disables this behavior and causes the view to use only the
6707 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006708 *
6709 * @param pivotX The x location of the pivot point.
6710 * @see #getRotation()
6711 * @see #getScaleX()
6712 * @see #getScaleY()
6713 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006714 *
6715 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07006716 */
6717 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006718 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006719 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006720 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006721 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006722 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006723 mPivotX = pivotX;
6724 mMatrixDirty = true;
6725 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006726 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006727 }
6728 }
6729
6730 /**
6731 * The y location of the point around which the view is {@link #setRotation(float) rotated}
6732 * and {@link #setScaleY(float) scaled}.
6733 *
6734 * @see #getRotation()
6735 * @see #getScaleX()
6736 * @see #getScaleY()
6737 * @see #getPivotY()
6738 * @return The y location of the pivot point.
6739 */
6740 public float getPivotY() {
6741 return mPivotY;
6742 }
6743
6744 /**
6745 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07006746 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
6747 * Setting this property disables this behavior and causes the view to use only the
6748 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006749 *
6750 * @param pivotY The y location of the pivot point.
6751 * @see #getRotation()
6752 * @see #getScaleX()
6753 * @see #getScaleY()
6754 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006755 *
6756 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07006757 */
6758 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006759 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006760 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006761 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006762 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006763 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006764 mPivotY = pivotY;
6765 mMatrixDirty = true;
6766 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006767 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006768 }
6769 }
6770
6771 /**
6772 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
6773 * completely transparent and 1 means the view is completely opaque.
6774 *
Joe Onorato93162322010-09-16 15:42:01 -04006775 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07006776 * @return The opacity of the view.
6777 */
6778 public float getAlpha() {
6779 return mAlpha;
6780 }
6781
6782 /**
Romain Guy171c5922011-01-06 10:04:23 -08006783 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
6784 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08006785 *
Romain Guy171c5922011-01-06 10:04:23 -08006786 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
6787 * responsible for applying the opacity itself. Otherwise, calling this method is
6788 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08006789 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07006790 *
6791 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08006792 *
Joe Malin32736f02011-01-19 16:14:20 -08006793 * @see #setLayerType(int, android.graphics.Paint)
6794 *
Chet Haase73066682010-11-29 15:55:32 -08006795 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07006796 */
6797 public void setAlpha(float alpha) {
6798 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006799 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07006800 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07006801 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006802 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006803 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07006804 } else {
Romain Guya3496a92010-10-12 11:53:24 -07006805 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006806 invalidate(false);
6807 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006808 }
6809
6810 /**
Chet Haasea00f3862011-02-22 06:34:40 -08006811 * Faster version of setAlpha() which performs the same steps except there are
6812 * no calls to invalidate(). The caller of this function should perform proper invalidation
6813 * on the parent and this object. The return value indicates whether the subclass handles
6814 * alpha (the return value for onSetAlpha()).
6815 *
6816 * @param alpha The new value for the alpha property
6817 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
6818 */
6819 boolean setAlphaNoInvalidation(float alpha) {
6820 mAlpha = alpha;
6821 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
6822 if (subclassHandlesAlpha) {
6823 mPrivateFlags |= ALPHA_SET;
6824 } else {
6825 mPrivateFlags &= ~ALPHA_SET;
6826 }
6827 return subclassHandlesAlpha;
6828 }
6829
6830 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006831 * Top position of this view relative to its parent.
6832 *
6833 * @return The top of this view, in pixels.
6834 */
6835 @ViewDebug.CapturedViewProperty
6836 public final int getTop() {
6837 return mTop;
6838 }
6839
6840 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006841 * Sets the top position of this view relative to its parent. This method is meant to be called
6842 * by the layout system and should not generally be called otherwise, because the property
6843 * may be changed at any time by the layout.
6844 *
6845 * @param top The top of this view, in pixels.
6846 */
6847 public final void setTop(int top) {
6848 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07006849 updateMatrix();
6850 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006851 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006852 int minTop;
6853 int yLoc;
6854 if (top < mTop) {
6855 minTop = top;
6856 yLoc = top - mTop;
6857 } else {
6858 minTop = mTop;
6859 yLoc = 0;
6860 }
Chet Haasee9140a72011-02-16 16:23:29 -08006861 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006862 }
6863 } else {
6864 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006865 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006866 }
6867
Chet Haaseed032702010-10-01 14:05:54 -07006868 int width = mRight - mLeft;
6869 int oldHeight = mBottom - mTop;
6870
Chet Haase21cd1382010-09-01 17:42:29 -07006871 mTop = top;
6872
Chet Haaseed032702010-10-01 14:05:54 -07006873 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6874
Chet Haase21cd1382010-09-01 17:42:29 -07006875 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006876 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6877 // A change in dimension means an auto-centered pivot point changes, too
6878 mMatrixDirty = true;
6879 }
Chet Haase21cd1382010-09-01 17:42:29 -07006880 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006881 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006882 }
Chet Haase55dbb652010-12-21 20:15:08 -08006883 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006884 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006885 }
6886 }
6887
6888 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006889 * Bottom position of this view relative to its parent.
6890 *
6891 * @return The bottom of this view, in pixels.
6892 */
6893 @ViewDebug.CapturedViewProperty
6894 public final int getBottom() {
6895 return mBottom;
6896 }
6897
6898 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08006899 * True if this view has changed since the last time being drawn.
6900 *
6901 * @return The dirty state of this view.
6902 */
6903 public boolean isDirty() {
6904 return (mPrivateFlags & DIRTY_MASK) != 0;
6905 }
6906
6907 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006908 * Sets the bottom position of this view relative to its parent. This method is meant to be
6909 * called by the layout system and should not generally be called otherwise, because the
6910 * property may be changed at any time by the layout.
6911 *
6912 * @param bottom The bottom of this view, in pixels.
6913 */
6914 public final void setBottom(int bottom) {
6915 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07006916 updateMatrix();
6917 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006918 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006919 int maxBottom;
6920 if (bottom < mBottom) {
6921 maxBottom = mBottom;
6922 } else {
6923 maxBottom = bottom;
6924 }
Chet Haasee9140a72011-02-16 16:23:29 -08006925 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006926 }
6927 } else {
6928 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006929 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006930 }
6931
Chet Haaseed032702010-10-01 14:05:54 -07006932 int width = mRight - mLeft;
6933 int oldHeight = mBottom - mTop;
6934
Chet Haase21cd1382010-09-01 17:42:29 -07006935 mBottom = bottom;
6936
Chet Haaseed032702010-10-01 14:05:54 -07006937 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6938
Chet Haase21cd1382010-09-01 17:42:29 -07006939 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006940 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6941 // A change in dimension means an auto-centered pivot point changes, too
6942 mMatrixDirty = true;
6943 }
Chet Haase21cd1382010-09-01 17:42:29 -07006944 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006945 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006946 }
Chet Haase55dbb652010-12-21 20:15:08 -08006947 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006948 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006949 }
6950 }
6951
6952 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006953 * Left position of this view relative to its parent.
6954 *
6955 * @return The left edge of this view, in pixels.
6956 */
6957 @ViewDebug.CapturedViewProperty
6958 public final int getLeft() {
6959 return mLeft;
6960 }
6961
6962 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006963 * Sets the left position of this view relative to its parent. This method is meant to be called
6964 * by the layout system and should not generally be called otherwise, because the property
6965 * may be changed at any time by the layout.
6966 *
6967 * @param left The bottom of this view, in pixels.
6968 */
6969 public final void setLeft(int left) {
6970 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07006971 updateMatrix();
6972 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006973 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006974 int minLeft;
6975 int xLoc;
6976 if (left < mLeft) {
6977 minLeft = left;
6978 xLoc = left - mLeft;
6979 } else {
6980 minLeft = mLeft;
6981 xLoc = 0;
6982 }
Chet Haasee9140a72011-02-16 16:23:29 -08006983 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006984 }
6985 } else {
6986 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006987 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006988 }
6989
Chet Haaseed032702010-10-01 14:05:54 -07006990 int oldWidth = mRight - mLeft;
6991 int height = mBottom - mTop;
6992
Chet Haase21cd1382010-09-01 17:42:29 -07006993 mLeft = left;
6994
Chet Haaseed032702010-10-01 14:05:54 -07006995 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6996
Chet Haase21cd1382010-09-01 17:42:29 -07006997 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006998 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6999 // A change in dimension means an auto-centered pivot point changes, too
7000 mMatrixDirty = true;
7001 }
Chet Haase21cd1382010-09-01 17:42:29 -07007002 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007003 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007004 }
Chet Haase55dbb652010-12-21 20:15:08 -08007005 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007006 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007007 }
7008 }
7009
7010 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007011 * Right position of this view relative to its parent.
7012 *
7013 * @return The right edge of this view, in pixels.
7014 */
7015 @ViewDebug.CapturedViewProperty
7016 public final int getRight() {
7017 return mRight;
7018 }
7019
7020 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007021 * Sets the right position of this view relative to its parent. This method is meant to be called
7022 * by the layout system and should not generally be called otherwise, because the property
7023 * may be changed at any time by the layout.
7024 *
7025 * @param right The bottom of this view, in pixels.
7026 */
7027 public final void setRight(int right) {
7028 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007029 updateMatrix();
7030 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007031 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007032 int maxRight;
7033 if (right < mRight) {
7034 maxRight = mRight;
7035 } else {
7036 maxRight = right;
7037 }
Chet Haasee9140a72011-02-16 16:23:29 -08007038 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007039 }
7040 } else {
7041 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007042 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007043 }
7044
Chet Haaseed032702010-10-01 14:05:54 -07007045 int oldWidth = mRight - mLeft;
7046 int height = mBottom - mTop;
7047
Chet Haase21cd1382010-09-01 17:42:29 -07007048 mRight = right;
7049
Chet Haaseed032702010-10-01 14:05:54 -07007050 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7051
Chet Haase21cd1382010-09-01 17:42:29 -07007052 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007053 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7054 // A change in dimension means an auto-centered pivot point changes, too
7055 mMatrixDirty = true;
7056 }
Chet Haase21cd1382010-09-01 17:42:29 -07007057 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007058 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007059 }
Chet Haase55dbb652010-12-21 20:15:08 -08007060 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007061 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007062 }
7063 }
7064
7065 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007066 * The visual x position of this view, in pixels. This is equivalent to the
7067 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007068 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007069 *
Chet Haasedf030d22010-07-30 17:22:38 -07007070 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007071 */
Chet Haasedf030d22010-07-30 17:22:38 -07007072 public float getX() {
7073 return mLeft + mTranslationX;
7074 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007075
Chet Haasedf030d22010-07-30 17:22:38 -07007076 /**
7077 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7078 * {@link #setTranslationX(float) translationX} property to be the difference between
7079 * the x value passed in and the current {@link #getLeft() left} property.
7080 *
7081 * @param x The visual x position of this view, in pixels.
7082 */
7083 public void setX(float x) {
7084 setTranslationX(x - mLeft);
7085 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007086
Chet Haasedf030d22010-07-30 17:22:38 -07007087 /**
7088 * The visual y position of this view, in pixels. This is equivalent to the
7089 * {@link #setTranslationY(float) translationY} property plus the current
7090 * {@link #getTop() top} property.
7091 *
7092 * @return The visual y position of this view, in pixels.
7093 */
7094 public float getY() {
7095 return mTop + mTranslationY;
7096 }
7097
7098 /**
7099 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7100 * {@link #setTranslationY(float) translationY} property to be the difference between
7101 * the y value passed in and the current {@link #getTop() top} property.
7102 *
7103 * @param y The visual y position of this view, in pixels.
7104 */
7105 public void setY(float y) {
7106 setTranslationY(y - mTop);
7107 }
7108
7109
7110 /**
7111 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7112 * This position is post-layout, in addition to wherever the object's
7113 * layout placed it.
7114 *
7115 * @return The horizontal position of this view relative to its left position, in pixels.
7116 */
7117 public float getTranslationX() {
7118 return mTranslationX;
7119 }
7120
7121 /**
7122 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7123 * This effectively positions the object post-layout, in addition to wherever the object's
7124 * layout placed it.
7125 *
7126 * @param translationX The horizontal position of this view relative to its left position,
7127 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007128 *
7129 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007130 */
7131 public void setTranslationX(float translationX) {
7132 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007133 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007134 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007135 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007136 mTranslationX = translationX;
7137 mMatrixDirty = true;
7138 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007139 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007140 }
7141 }
7142
7143 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007144 * The horizontal location of this view relative to its {@link #getTop() top} position.
7145 * This position is post-layout, in addition to wherever the object's
7146 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007147 *
Chet Haasedf030d22010-07-30 17:22:38 -07007148 * @return The vertical position of this view relative to its top position,
7149 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007150 */
Chet Haasedf030d22010-07-30 17:22:38 -07007151 public float getTranslationY() {
7152 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07007153 }
7154
7155 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007156 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7157 * This effectively positions the object post-layout, in addition to wherever the object's
7158 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007159 *
Chet Haasedf030d22010-07-30 17:22:38 -07007160 * @param translationY The vertical position of this view relative to its top position,
7161 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007162 *
7163 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007164 */
Chet Haasedf030d22010-07-30 17:22:38 -07007165 public void setTranslationY(float translationY) {
7166 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007167 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007168 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007169 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007170 mTranslationY = translationY;
7171 mMatrixDirty = true;
7172 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007173 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007174 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007175 }
7176
7177 /**
Romain Guyda489792011-02-03 01:05:15 -08007178 * @hide
7179 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007180 public void setFastTranslationX(float x) {
7181 mTranslationX = x;
7182 mMatrixDirty = true;
7183 }
7184
7185 /**
7186 * @hide
7187 */
7188 public void setFastTranslationY(float y) {
7189 mTranslationY = y;
7190 mMatrixDirty = true;
7191 }
7192
7193 /**
7194 * @hide
7195 */
Romain Guyda489792011-02-03 01:05:15 -08007196 public void setFastX(float x) {
7197 mTranslationX = x - mLeft;
7198 mMatrixDirty = true;
7199 }
7200
7201 /**
7202 * @hide
7203 */
7204 public void setFastY(float y) {
7205 mTranslationY = y - mTop;
7206 mMatrixDirty = true;
7207 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007208
Romain Guyda489792011-02-03 01:05:15 -08007209 /**
7210 * @hide
7211 */
7212 public void setFastScaleX(float x) {
7213 mScaleX = x;
7214 mMatrixDirty = true;
7215 }
7216
7217 /**
7218 * @hide
7219 */
7220 public void setFastScaleY(float y) {
7221 mScaleY = y;
7222 mMatrixDirty = true;
7223 }
7224
7225 /**
7226 * @hide
7227 */
7228 public void setFastAlpha(float alpha) {
7229 mAlpha = alpha;
7230 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007231
Romain Guyda489792011-02-03 01:05:15 -08007232 /**
7233 * @hide
7234 */
7235 public void setFastRotationY(float y) {
7236 mRotationY = y;
7237 mMatrixDirty = true;
7238 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007239
Romain Guyda489792011-02-03 01:05:15 -08007240 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007241 * Hit rectangle in parent's coordinates
7242 *
7243 * @param outRect The hit rectangle of the view.
7244 */
7245 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007246 updateMatrix();
7247 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007248 outRect.set(mLeft, mTop, mRight, mBottom);
7249 } else {
7250 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07007251 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07007252 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007253 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7254 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007255 }
7256 }
7257
7258 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007259 * Determines whether the given point, in local coordinates is inside the view.
7260 */
7261 /*package*/ final boolean pointInView(float localX, float localY) {
7262 return localX >= 0 && localX < (mRight - mLeft)
7263 && localY >= 0 && localY < (mBottom - mTop);
7264 }
7265
7266 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007267 * Utility method to determine whether the given point, in local coordinates,
7268 * is inside the view, where the area of the view is expanded by the slop factor.
7269 * This method is called while processing touch-move events to determine if the event
7270 * is still within the view.
7271 */
7272 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007273 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007274 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007275 }
7276
7277 /**
7278 * When a view has focus and the user navigates away from it, the next view is searched for
7279 * starting from the rectangle filled in by this method.
7280 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007281 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7282 * of the view. However, if your view maintains some idea of internal selection,
7283 * such as a cursor, or a selected row or column, you should override this method and
7284 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007285 *
7286 * @param r The rectangle to fill in, in this view's coordinates.
7287 */
7288 public void getFocusedRect(Rect r) {
7289 getDrawingRect(r);
7290 }
7291
7292 /**
7293 * If some part of this view is not clipped by any of its parents, then
7294 * return that area in r in global (root) coordinates. To convert r to local
7295 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7296 * -globalOffset.y)) If the view is completely clipped or translated out,
7297 * return false.
7298 *
7299 * @param r If true is returned, r holds the global coordinates of the
7300 * visible portion of this view.
7301 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7302 * between this view and its root. globalOffet may be null.
7303 * @return true if r is non-empty (i.e. part of the view is visible at the
7304 * root level.
7305 */
7306 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7307 int width = mRight - mLeft;
7308 int height = mBottom - mTop;
7309 if (width > 0 && height > 0) {
7310 r.set(0, 0, width, height);
7311 if (globalOffset != null) {
7312 globalOffset.set(-mScrollX, -mScrollY);
7313 }
7314 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7315 }
7316 return false;
7317 }
7318
7319 public final boolean getGlobalVisibleRect(Rect r) {
7320 return getGlobalVisibleRect(r, null);
7321 }
7322
7323 public final boolean getLocalVisibleRect(Rect r) {
7324 Point offset = new Point();
7325 if (getGlobalVisibleRect(r, offset)) {
7326 r.offset(-offset.x, -offset.y); // make r local
7327 return true;
7328 }
7329 return false;
7330 }
7331
7332 /**
7333 * Offset this view's vertical location by the specified number of pixels.
7334 *
7335 * @param offset the number of pixels to offset the view by
7336 */
7337 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007338 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007339 updateMatrix();
7340 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007341 final ViewParent p = mParent;
7342 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007343 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007344 int minTop;
7345 int maxBottom;
7346 int yLoc;
7347 if (offset < 0) {
7348 minTop = mTop + offset;
7349 maxBottom = mBottom;
7350 yLoc = offset;
7351 } else {
7352 minTop = mTop;
7353 maxBottom = mBottom + offset;
7354 yLoc = 0;
7355 }
7356 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
7357 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007358 }
7359 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007360 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007361 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007362
Chet Haasec3aa3612010-06-17 08:50:37 -07007363 mTop += offset;
7364 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007365
Chet Haasec3aa3612010-06-17 08:50:37 -07007366 if (!mMatrixIsIdentity) {
7367 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007368 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007369 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007370 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007371 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007372 }
7373
7374 /**
7375 * Offset this view's horizontal location by the specified amount of pixels.
7376 *
7377 * @param offset the numer of pixels to offset the view by
7378 */
7379 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007380 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007381 updateMatrix();
7382 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007383 final ViewParent p = mParent;
7384 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007385 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007386 int minLeft;
7387 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007388 if (offset < 0) {
7389 minLeft = mLeft + offset;
7390 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007391 } else {
7392 minLeft = mLeft;
7393 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007394 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007395 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07007396 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007397 }
7398 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007399 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007400 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007401
Chet Haasec3aa3612010-06-17 08:50:37 -07007402 mLeft += offset;
7403 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007404
Chet Haasec3aa3612010-06-17 08:50:37 -07007405 if (!mMatrixIsIdentity) {
7406 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007407 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007408 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007409 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007411 }
7412
7413 /**
7414 * Get the LayoutParams associated with this view. All views should have
7415 * layout parameters. These supply parameters to the <i>parent</i> of this
7416 * view specifying how it should be arranged. There are many subclasses of
7417 * ViewGroup.LayoutParams, and these correspond to the different subclasses
7418 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08007419 *
7420 * This method may return null if this View is not attached to a parent
7421 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
7422 * was not invoked successfully. When a View is attached to a parent
7423 * ViewGroup, this method must not return null.
7424 *
7425 * @return The LayoutParams associated with this view, or null if no
7426 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007427 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07007428 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007429 public ViewGroup.LayoutParams getLayoutParams() {
7430 return mLayoutParams;
7431 }
7432
7433 /**
7434 * Set the layout parameters associated with this view. These supply
7435 * parameters to the <i>parent</i> of this view specifying how it should be
7436 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
7437 * correspond to the different subclasses of ViewGroup that are responsible
7438 * for arranging their children.
7439 *
Romain Guy01c174b2011-02-22 11:51:06 -08007440 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007441 */
7442 public void setLayoutParams(ViewGroup.LayoutParams params) {
7443 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007444 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007445 }
7446 mLayoutParams = params;
7447 requestLayout();
7448 }
7449
7450 /**
7451 * Set the scrolled position of your view. This will cause a call to
7452 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7453 * invalidated.
7454 * @param x the x position to scroll to
7455 * @param y the y position to scroll to
7456 */
7457 public void scrollTo(int x, int y) {
7458 if (mScrollX != x || mScrollY != y) {
7459 int oldX = mScrollX;
7460 int oldY = mScrollY;
7461 mScrollX = x;
7462 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007463 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007464 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007465 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007466 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007468 }
7469 }
7470
7471 /**
7472 * Move the scrolled position of your view. This will cause a call to
7473 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7474 * invalidated.
7475 * @param x the amount of pixels to scroll by horizontally
7476 * @param y the amount of pixels to scroll by vertically
7477 */
7478 public void scrollBy(int x, int y) {
7479 scrollTo(mScrollX + x, mScrollY + y);
7480 }
7481
7482 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007483 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7484 * animation to fade the scrollbars out after a default delay. If a subclass
7485 * provides animated scrolling, the start delay should equal the duration
7486 * of the scrolling animation.</p>
7487 *
7488 * <p>The animation starts only if at least one of the scrollbars is
7489 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7490 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7491 * this method returns true, and false otherwise. If the animation is
7492 * started, this method calls {@link #invalidate()}; in that case the
7493 * caller should not call {@link #invalidate()}.</p>
7494 *
7495 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007496 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007497 *
7498 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7499 * and {@link #scrollTo(int, int)}.</p>
7500 *
7501 * @return true if the animation is played, false otherwise
7502 *
7503 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007504 * @see #scrollBy(int, int)
7505 * @see #scrollTo(int, int)
7506 * @see #isHorizontalScrollBarEnabled()
7507 * @see #isVerticalScrollBarEnabled()
7508 * @see #setHorizontalScrollBarEnabled(boolean)
7509 * @see #setVerticalScrollBarEnabled(boolean)
7510 */
7511 protected boolean awakenScrollBars() {
7512 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007513 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007514 }
7515
7516 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007517 * Trigger the scrollbars to draw.
7518 * This method differs from awakenScrollBars() only in its default duration.
7519 * initialAwakenScrollBars() will show the scroll bars for longer than
7520 * usual to give the user more of a chance to notice them.
7521 *
7522 * @return true if the animation is played, false otherwise.
7523 */
7524 private boolean initialAwakenScrollBars() {
7525 return mScrollCache != null &&
7526 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7527 }
7528
7529 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007530 * <p>
7531 * Trigger the scrollbars to draw. When invoked this method starts an
7532 * animation to fade the scrollbars out after a fixed delay. If a subclass
7533 * provides animated scrolling, the start delay should equal the duration of
7534 * the scrolling animation.
7535 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007536 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007537 * <p>
7538 * The animation starts only if at least one of the scrollbars is enabled,
7539 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7540 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7541 * this method returns true, and false otherwise. If the animation is
7542 * started, this method calls {@link #invalidate()}; in that case the caller
7543 * should not call {@link #invalidate()}.
7544 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007545 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007546 * <p>
7547 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007548 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007549 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007550 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007551 * @param startDelay the delay, in milliseconds, after which the animation
7552 * should start; when the delay is 0, the animation starts
7553 * immediately
7554 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007555 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007556 * @see #scrollBy(int, int)
7557 * @see #scrollTo(int, int)
7558 * @see #isHorizontalScrollBarEnabled()
7559 * @see #isVerticalScrollBarEnabled()
7560 * @see #setHorizontalScrollBarEnabled(boolean)
7561 * @see #setVerticalScrollBarEnabled(boolean)
7562 */
7563 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007564 return awakenScrollBars(startDelay, true);
7565 }
Joe Malin32736f02011-01-19 16:14:20 -08007566
Mike Cleron290947b2009-09-29 18:34:32 -07007567 /**
7568 * <p>
7569 * Trigger the scrollbars to draw. When invoked this method starts an
7570 * animation to fade the scrollbars out after a fixed delay. If a subclass
7571 * provides animated scrolling, the start delay should equal the duration of
7572 * the scrolling animation.
7573 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007574 *
Mike Cleron290947b2009-09-29 18:34:32 -07007575 * <p>
7576 * The animation starts only if at least one of the scrollbars is enabled,
7577 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7578 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7579 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08007580 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07007581 * is set to true; in that case the caller
7582 * should not call {@link #invalidate()}.
7583 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007584 *
Mike Cleron290947b2009-09-29 18:34:32 -07007585 * <p>
7586 * This method should be invoked everytime a subclass directly updates the
7587 * scroll parameters.
7588 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007589 *
Mike Cleron290947b2009-09-29 18:34:32 -07007590 * @param startDelay the delay, in milliseconds, after which the animation
7591 * should start; when the delay is 0, the animation starts
7592 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007593 *
Mike Cleron290947b2009-09-29 18:34:32 -07007594 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007595 *
Mike Cleron290947b2009-09-29 18:34:32 -07007596 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007597 *
Mike Cleron290947b2009-09-29 18:34:32 -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(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007606 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007607
Mike Cleronf116bf82009-09-27 19:14:12 -07007608 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7609 return false;
7610 }
7611
7612 if (scrollCache.scrollBar == null) {
7613 scrollCache.scrollBar = new ScrollBarDrawable();
7614 }
7615
7616 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7617
Mike Cleron290947b2009-09-29 18:34:32 -07007618 if (invalidate) {
7619 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007620 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007621 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007622
7623 if (scrollCache.state == ScrollabilityCache.OFF) {
7624 // FIXME: this is copied from WindowManagerService.
7625 // We should get this value from the system when it
7626 // is possible to do so.
7627 final int KEY_REPEAT_FIRST_DELAY = 750;
7628 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7629 }
7630
7631 // Tell mScrollCache when we should start fading. This may
7632 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007633 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007634 scrollCache.fadeStartTime = fadeStartTime;
7635 scrollCache.state = ScrollabilityCache.ON;
7636
7637 // Schedule our fader to run, unscheduling any old ones first
7638 if (mAttachInfo != null) {
7639 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7640 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7641 }
7642
7643 return true;
7644 }
7645
7646 return false;
7647 }
7648
7649 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007650 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07007651 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
7652 * in the future. This must be called from a UI thread. To call from a non-UI
7653 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007654 *
7655 * WARNING: This method is destructive to dirty.
7656 * @param dirty the rectangle representing the bounds of the dirty region
7657 */
7658 public void invalidate(Rect dirty) {
7659 if (ViewDebug.TRACE_HIERARCHY) {
7660 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7661 }
7662
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007663 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007664 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7665 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007666 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007667 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007668 final ViewParent p = mParent;
7669 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007670 //noinspection PointlessBooleanExpression,ConstantConditions
7671 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7672 if (p != null && ai != null && ai.mHardwareAccelerated) {
7673 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007674 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007675 p.invalidateChild(this, null);
7676 return;
7677 }
Romain Guyaf636eb2010-12-09 17:47:21 -08007678 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007679 if (p != null && ai != null) {
7680 final int scrollX = mScrollX;
7681 final int scrollY = mScrollY;
7682 final Rect r = ai.mTmpInvalRect;
7683 r.set(dirty.left - scrollX, dirty.top - scrollY,
7684 dirty.right - scrollX, dirty.bottom - scrollY);
7685 mParent.invalidateChild(this, r);
7686 }
7687 }
7688 }
7689
7690 /**
7691 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
7692 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007693 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
7694 * will be called at some point in the future. This must be called from
7695 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007696 * @param l the left position of the dirty region
7697 * @param t the top position of the dirty region
7698 * @param r the right position of the dirty region
7699 * @param b the bottom position of the dirty region
7700 */
7701 public void invalidate(int l, int t, int r, int b) {
7702 if (ViewDebug.TRACE_HIERARCHY) {
7703 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7704 }
7705
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007706 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007707 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7708 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007709 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007710 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007711 final ViewParent p = mParent;
7712 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007713 //noinspection PointlessBooleanExpression,ConstantConditions
7714 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7715 if (p != null && ai != null && ai.mHardwareAccelerated) {
7716 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007717 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007718 p.invalidateChild(this, null);
7719 return;
7720 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007721 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007722 if (p != null && ai != null && l < r && t < b) {
7723 final int scrollX = mScrollX;
7724 final int scrollY = mScrollY;
7725 final Rect tmpr = ai.mTmpInvalRect;
7726 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
7727 p.invalidateChild(this, tmpr);
7728 }
7729 }
7730 }
7731
7732 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07007733 * Invalidate the whole view. If the view is visible,
7734 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
7735 * the future. This must be called from a UI thread. To call from a non-UI thread,
7736 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007737 */
7738 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07007739 invalidate(true);
7740 }
Joe Malin32736f02011-01-19 16:14:20 -08007741
Chet Haaseed032702010-10-01 14:05:54 -07007742 /**
7743 * This is where the invalidate() work actually happens. A full invalidate()
7744 * causes the drawing cache to be invalidated, but this function can be called with
7745 * invalidateCache set to false to skip that invalidation step for cases that do not
7746 * need it (for example, a component that remains at the same dimensions with the same
7747 * content).
7748 *
7749 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
7750 * well. This is usually true for a full invalidate, but may be set to false if the
7751 * View's contents or dimensions have not changed.
7752 */
Romain Guy849d0a32011-02-01 17:20:48 -08007753 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007754 if (ViewDebug.TRACE_HIERARCHY) {
7755 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7756 }
7757
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007758 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08007759 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08007760 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
7761 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07007762 mPrivateFlags &= ~DRAWN;
7763 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08007764 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07007765 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7766 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007767 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07007768 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08007769 //noinspection PointlessBooleanExpression,ConstantConditions
7770 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7771 if (p != null && ai != null && ai.mHardwareAccelerated) {
7772 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007773 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007774 p.invalidateChild(this, null);
7775 return;
7776 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007777 }
Michael Jurkaebefea42010-11-15 16:04:01 -08007778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007779 if (p != null && ai != null) {
7780 final Rect r = ai.mTmpInvalRect;
7781 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7782 // Don't call invalidate -- we don't want to internally scroll
7783 // our own bounds
7784 p.invalidateChild(this, r);
7785 }
7786 }
7787 }
7788
7789 /**
Romain Guyda489792011-02-03 01:05:15 -08007790 * @hide
7791 */
7792 public void fastInvalidate() {
7793 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
7794 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7795 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
7796 if (mParent instanceof View) {
7797 ((View) mParent).mPrivateFlags |= INVALIDATED;
7798 }
7799 mPrivateFlags &= ~DRAWN;
7800 mPrivateFlags |= INVALIDATED;
7801 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07007802 if (mParent != null && mAttachInfo != null) {
7803 if (mAttachInfo.mHardwareAccelerated) {
7804 mParent.invalidateChild(this, null);
7805 } else {
7806 final Rect r = mAttachInfo.mTmpInvalRect;
7807 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7808 // Don't call invalidate -- we don't want to internally scroll
7809 // our own bounds
7810 mParent.invalidateChild(this, r);
7811 }
Romain Guyda489792011-02-03 01:05:15 -08007812 }
7813 }
7814 }
7815
7816 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08007817 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08007818 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7819 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08007820 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
7821 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08007822 *
7823 * @hide
7824 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08007825 protected void invalidateParentCaches() {
7826 if (mParent instanceof View) {
7827 ((View) mParent).mPrivateFlags |= INVALIDATED;
7828 }
7829 }
Joe Malin32736f02011-01-19 16:14:20 -08007830
Romain Guy0fd89bf2011-01-26 15:41:30 -08007831 /**
7832 * Used to indicate that the parent of this view should be invalidated. This functionality
7833 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7834 * which is necessary when various parent-managed properties of the view change, such as
7835 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
7836 * an invalidation event to the parent.
7837 *
7838 * @hide
7839 */
7840 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08007841 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007842 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08007843 }
7844 }
7845
7846 /**
Romain Guy24443ea2009-05-11 11:56:30 -07007847 * Indicates whether this View is opaque. An opaque View guarantees that it will
7848 * draw all the pixels overlapping its bounds using a fully opaque color.
7849 *
7850 * Subclasses of View should override this method whenever possible to indicate
7851 * whether an instance is opaque. Opaque Views are treated in a special way by
7852 * the View hierarchy, possibly allowing it to perform optimizations during
7853 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07007854 *
Romain Guy24443ea2009-05-11 11:56:30 -07007855 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07007856 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007857 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07007858 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07007859 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
7860 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07007861 }
7862
Adam Powell20232d02010-12-08 21:08:53 -08007863 /**
7864 * @hide
7865 */
7866 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07007867 // Opaque if:
7868 // - Has a background
7869 // - Background is opaque
7870 // - Doesn't have scrollbars or scrollbars are inside overlay
7871
7872 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
7873 mPrivateFlags |= OPAQUE_BACKGROUND;
7874 } else {
7875 mPrivateFlags &= ~OPAQUE_BACKGROUND;
7876 }
7877
7878 final int flags = mViewFlags;
7879 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
7880 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
7881 mPrivateFlags |= OPAQUE_SCROLLBARS;
7882 } else {
7883 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
7884 }
7885 }
7886
7887 /**
7888 * @hide
7889 */
7890 protected boolean hasOpaqueScrollbars() {
7891 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07007892 }
7893
7894 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007895 * @return A handler associated with the thread running the View. This
7896 * handler can be used to pump events in the UI events queue.
7897 */
7898 public Handler getHandler() {
7899 if (mAttachInfo != null) {
7900 return mAttachInfo.mHandler;
7901 }
7902 return null;
7903 }
7904
7905 /**
7906 * Causes the Runnable to be added to the message queue.
7907 * The runnable will be run on the user interface thread.
7908 *
7909 * @param action The Runnable that will be executed.
7910 *
7911 * @return Returns true if the Runnable was successfully placed in to the
7912 * message queue. Returns false on failure, usually because the
7913 * looper processing the message queue is exiting.
7914 */
7915 public boolean post(Runnable action) {
7916 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007917 AttachInfo attachInfo = mAttachInfo;
7918 if (attachInfo != null) {
7919 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007920 } else {
7921 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007922 ViewAncestor.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007923 return true;
7924 }
7925
7926 return handler.post(action);
7927 }
7928
7929 /**
7930 * Causes the Runnable to be added to the message queue, to be run
7931 * after the specified amount of time elapses.
7932 * The runnable will be run on the user interface thread.
7933 *
7934 * @param action The Runnable that will be executed.
7935 * @param delayMillis The delay (in milliseconds) until the Runnable
7936 * will be executed.
7937 *
7938 * @return true if the Runnable was successfully placed in to the
7939 * message queue. Returns false on failure, usually because the
7940 * looper processing the message queue is exiting. Note that a
7941 * result of true does not mean the Runnable will be processed --
7942 * if the looper is quit before the delivery time of the message
7943 * occurs then the message will be dropped.
7944 */
7945 public boolean postDelayed(Runnable action, long delayMillis) {
7946 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007947 AttachInfo attachInfo = mAttachInfo;
7948 if (attachInfo != null) {
7949 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007950 } else {
7951 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007952 ViewAncestor.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007953 return true;
7954 }
7955
7956 return handler.postDelayed(action, delayMillis);
7957 }
7958
7959 /**
7960 * Removes the specified Runnable from the message queue.
7961 *
7962 * @param action The Runnable to remove from the message handling queue
7963 *
7964 * @return true if this view could ask the Handler to remove the Runnable,
7965 * false otherwise. When the returned value is true, the Runnable
7966 * may or may not have been actually removed from the message queue
7967 * (for instance, if the Runnable was not in the queue already.)
7968 */
7969 public boolean removeCallbacks(Runnable action) {
7970 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007971 AttachInfo attachInfo = mAttachInfo;
7972 if (attachInfo != null) {
7973 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007974 } else {
7975 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007976 ViewAncestor.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007977 return true;
7978 }
7979
7980 handler.removeCallbacks(action);
7981 return true;
7982 }
7983
7984 /**
7985 * Cause an invalidate to happen on a subsequent cycle through the event loop.
7986 * Use this to invalidate the View from a non-UI thread.
7987 *
7988 * @see #invalidate()
7989 */
7990 public void postInvalidate() {
7991 postInvalidateDelayed(0);
7992 }
7993
7994 /**
7995 * Cause an invalidate of the specified area to happen on a subsequent cycle
7996 * through the event loop. Use this to invalidate the View from a non-UI thread.
7997 *
7998 * @param left The left coordinate of the rectangle to invalidate.
7999 * @param top The top coordinate of the rectangle to invalidate.
8000 * @param right The right coordinate of the rectangle to invalidate.
8001 * @param bottom The bottom coordinate of the rectangle to invalidate.
8002 *
8003 * @see #invalidate(int, int, int, int)
8004 * @see #invalidate(Rect)
8005 */
8006 public void postInvalidate(int left, int top, int right, int bottom) {
8007 postInvalidateDelayed(0, left, top, right, bottom);
8008 }
8009
8010 /**
8011 * Cause an invalidate to happen on a subsequent cycle through the event
8012 * loop. Waits for the specified amount of time.
8013 *
8014 * @param delayMilliseconds the duration in milliseconds to delay the
8015 * invalidation by
8016 */
8017 public void postInvalidateDelayed(long delayMilliseconds) {
8018 // We try only with the AttachInfo because there's no point in invalidating
8019 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008020 AttachInfo attachInfo = mAttachInfo;
8021 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008022 Message msg = Message.obtain();
8023 msg.what = AttachInfo.INVALIDATE_MSG;
8024 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008025 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008026 }
8027 }
8028
8029 /**
8030 * Cause an invalidate of the specified area to happen on a subsequent cycle
8031 * through the event loop. Waits for the specified amount of time.
8032 *
8033 * @param delayMilliseconds the duration in milliseconds to delay the
8034 * invalidation by
8035 * @param left The left coordinate of the rectangle to invalidate.
8036 * @param top The top coordinate of the rectangle to invalidate.
8037 * @param right The right coordinate of the rectangle to invalidate.
8038 * @param bottom The bottom coordinate of the rectangle to invalidate.
8039 */
8040 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8041 int right, int bottom) {
8042
8043 // We try only with the AttachInfo because there's no point in invalidating
8044 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008045 AttachInfo attachInfo = mAttachInfo;
8046 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008047 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8048 info.target = this;
8049 info.left = left;
8050 info.top = top;
8051 info.right = right;
8052 info.bottom = bottom;
8053
8054 final Message msg = Message.obtain();
8055 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8056 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008057 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008058 }
8059 }
8060
8061 /**
8062 * Called by a parent to request that a child update its values for mScrollX
8063 * and mScrollY if necessary. This will typically be done if the child is
8064 * animating a scroll using a {@link android.widget.Scroller Scroller}
8065 * object.
8066 */
8067 public void computeScroll() {
8068 }
8069
8070 /**
8071 * <p>Indicate whether the horizontal edges are faded when the view is
8072 * scrolled horizontally.</p>
8073 *
8074 * @return true if the horizontal edges should are faded on scroll, false
8075 * otherwise
8076 *
8077 * @see #setHorizontalFadingEdgeEnabled(boolean)
8078 * @attr ref android.R.styleable#View_fadingEdge
8079 */
8080 public boolean isHorizontalFadingEdgeEnabled() {
8081 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8082 }
8083
8084 /**
8085 * <p>Define whether the horizontal edges should be faded when this view
8086 * is scrolled horizontally.</p>
8087 *
8088 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8089 * be faded when the view is scrolled
8090 * horizontally
8091 *
8092 * @see #isHorizontalFadingEdgeEnabled()
8093 * @attr ref android.R.styleable#View_fadingEdge
8094 */
8095 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8096 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8097 if (horizontalFadingEdgeEnabled) {
8098 initScrollCache();
8099 }
8100
8101 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8102 }
8103 }
8104
8105 /**
8106 * <p>Indicate whether the vertical edges are faded when the view is
8107 * scrolled horizontally.</p>
8108 *
8109 * @return true if the vertical edges should are faded on scroll, false
8110 * otherwise
8111 *
8112 * @see #setVerticalFadingEdgeEnabled(boolean)
8113 * @attr ref android.R.styleable#View_fadingEdge
8114 */
8115 public boolean isVerticalFadingEdgeEnabled() {
8116 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8117 }
8118
8119 /**
8120 * <p>Define whether the vertical edges should be faded when this view
8121 * is scrolled vertically.</p>
8122 *
8123 * @param verticalFadingEdgeEnabled true if the vertical edges should
8124 * be faded when the view is scrolled
8125 * vertically
8126 *
8127 * @see #isVerticalFadingEdgeEnabled()
8128 * @attr ref android.R.styleable#View_fadingEdge
8129 */
8130 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8131 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8132 if (verticalFadingEdgeEnabled) {
8133 initScrollCache();
8134 }
8135
8136 mViewFlags ^= FADING_EDGE_VERTICAL;
8137 }
8138 }
8139
8140 /**
8141 * Returns the strength, or intensity, of the top faded edge. The strength is
8142 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8143 * returns 0.0 or 1.0 but no value in between.
8144 *
8145 * Subclasses should override this method to provide a smoother fade transition
8146 * when scrolling occurs.
8147 *
8148 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8149 */
8150 protected float getTopFadingEdgeStrength() {
8151 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8152 }
8153
8154 /**
8155 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8156 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8157 * returns 0.0 or 1.0 but no value in between.
8158 *
8159 * Subclasses should override this method to provide a smoother fade transition
8160 * when scrolling occurs.
8161 *
8162 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8163 */
8164 protected float getBottomFadingEdgeStrength() {
8165 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8166 computeVerticalScrollRange() ? 1.0f : 0.0f;
8167 }
8168
8169 /**
8170 * Returns the strength, or intensity, of the left faded edge. The strength is
8171 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8172 * returns 0.0 or 1.0 but no value in between.
8173 *
8174 * Subclasses should override this method to provide a smoother fade transition
8175 * when scrolling occurs.
8176 *
8177 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8178 */
8179 protected float getLeftFadingEdgeStrength() {
8180 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8181 }
8182
8183 /**
8184 * Returns the strength, or intensity, of the right faded edge. The strength is
8185 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8186 * returns 0.0 or 1.0 but no value in between.
8187 *
8188 * Subclasses should override this method to provide a smoother fade transition
8189 * when scrolling occurs.
8190 *
8191 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8192 */
8193 protected float getRightFadingEdgeStrength() {
8194 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8195 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8196 }
8197
8198 /**
8199 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8200 * scrollbar is not drawn by default.</p>
8201 *
8202 * @return true if the horizontal scrollbar should be painted, false
8203 * otherwise
8204 *
8205 * @see #setHorizontalScrollBarEnabled(boolean)
8206 */
8207 public boolean isHorizontalScrollBarEnabled() {
8208 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8209 }
8210
8211 /**
8212 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8213 * scrollbar is not drawn by default.</p>
8214 *
8215 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8216 * be painted
8217 *
8218 * @see #isHorizontalScrollBarEnabled()
8219 */
8220 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8221 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8222 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008223 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008224 recomputePadding();
8225 }
8226 }
8227
8228 /**
8229 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
8230 * scrollbar is not drawn by default.</p>
8231 *
8232 * @return true if the vertical scrollbar should be painted, false
8233 * otherwise
8234 *
8235 * @see #setVerticalScrollBarEnabled(boolean)
8236 */
8237 public boolean isVerticalScrollBarEnabled() {
8238 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
8239 }
8240
8241 /**
8242 * <p>Define whether the vertical scrollbar should be drawn or not. The
8243 * scrollbar is not drawn by default.</p>
8244 *
8245 * @param verticalScrollBarEnabled true if the vertical scrollbar should
8246 * be painted
8247 *
8248 * @see #isVerticalScrollBarEnabled()
8249 */
8250 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
8251 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
8252 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008253 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008254 recomputePadding();
8255 }
8256 }
8257
Adam Powell20232d02010-12-08 21:08:53 -08008258 /**
8259 * @hide
8260 */
8261 protected void recomputePadding() {
8262 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008263 }
Joe Malin32736f02011-01-19 16:14:20 -08008264
Mike Cleronfe81d382009-09-28 14:22:16 -07008265 /**
8266 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08008267 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008268 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08008269 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008270 */
8271 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
8272 initScrollCache();
8273 final ScrollabilityCache scrollabilityCache = mScrollCache;
8274 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008275 if (fadeScrollbars) {
8276 scrollabilityCache.state = ScrollabilityCache.OFF;
8277 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07008278 scrollabilityCache.state = ScrollabilityCache.ON;
8279 }
8280 }
Joe Malin32736f02011-01-19 16:14:20 -08008281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008282 /**
Joe Malin32736f02011-01-19 16:14:20 -08008283 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008284 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08008285 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008286 * @return true if scrollbar fading is enabled
8287 */
8288 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08008289 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008290 }
Joe Malin32736f02011-01-19 16:14:20 -08008291
Mike Cleron52f0a642009-09-28 18:21:37 -07008292 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008293 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
8294 * inset. When inset, they add to the padding of the view. And the scrollbars
8295 * can be drawn inside the padding area or on the edge of the view. For example,
8296 * if a view has a background drawable and you want to draw the scrollbars
8297 * inside the padding specified by the drawable, you can use
8298 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
8299 * appear at the edge of the view, ignoring the padding, then you can use
8300 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
8301 * @param style the style of the scrollbars. Should be one of
8302 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
8303 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
8304 * @see #SCROLLBARS_INSIDE_OVERLAY
8305 * @see #SCROLLBARS_INSIDE_INSET
8306 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8307 * @see #SCROLLBARS_OUTSIDE_INSET
8308 */
8309 public void setScrollBarStyle(int style) {
8310 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
8311 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07008312 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008313 recomputePadding();
8314 }
8315 }
8316
8317 /**
8318 * <p>Returns the current scrollbar style.</p>
8319 * @return the current scrollbar style
8320 * @see #SCROLLBARS_INSIDE_OVERLAY
8321 * @see #SCROLLBARS_INSIDE_INSET
8322 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8323 * @see #SCROLLBARS_OUTSIDE_INSET
8324 */
8325 public int getScrollBarStyle() {
8326 return mViewFlags & SCROLLBARS_STYLE_MASK;
8327 }
8328
8329 /**
8330 * <p>Compute the horizontal range that the horizontal scrollbar
8331 * represents.</p>
8332 *
8333 * <p>The range is expressed in arbitrary units that must be the same as the
8334 * units used by {@link #computeHorizontalScrollExtent()} and
8335 * {@link #computeHorizontalScrollOffset()}.</p>
8336 *
8337 * <p>The default range is the drawing width of this view.</p>
8338 *
8339 * @return the total horizontal range represented by the horizontal
8340 * scrollbar
8341 *
8342 * @see #computeHorizontalScrollExtent()
8343 * @see #computeHorizontalScrollOffset()
8344 * @see android.widget.ScrollBarDrawable
8345 */
8346 protected int computeHorizontalScrollRange() {
8347 return getWidth();
8348 }
8349
8350 /**
8351 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
8352 * within the horizontal range. This value is used to compute the position
8353 * of the thumb within the scrollbar's track.</p>
8354 *
8355 * <p>The range is expressed in arbitrary units that must be the same as the
8356 * units used by {@link #computeHorizontalScrollRange()} and
8357 * {@link #computeHorizontalScrollExtent()}.</p>
8358 *
8359 * <p>The default offset is the scroll offset of this view.</p>
8360 *
8361 * @return the horizontal offset of the scrollbar's thumb
8362 *
8363 * @see #computeHorizontalScrollRange()
8364 * @see #computeHorizontalScrollExtent()
8365 * @see android.widget.ScrollBarDrawable
8366 */
8367 protected int computeHorizontalScrollOffset() {
8368 return mScrollX;
8369 }
8370
8371 /**
8372 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
8373 * within the horizontal range. This value is used to compute the length
8374 * of the thumb within the scrollbar's track.</p>
8375 *
8376 * <p>The range is expressed in arbitrary units that must be the same as the
8377 * units used by {@link #computeHorizontalScrollRange()} and
8378 * {@link #computeHorizontalScrollOffset()}.</p>
8379 *
8380 * <p>The default extent is the drawing width of this view.</p>
8381 *
8382 * @return the horizontal extent of the scrollbar's thumb
8383 *
8384 * @see #computeHorizontalScrollRange()
8385 * @see #computeHorizontalScrollOffset()
8386 * @see android.widget.ScrollBarDrawable
8387 */
8388 protected int computeHorizontalScrollExtent() {
8389 return getWidth();
8390 }
8391
8392 /**
8393 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
8394 *
8395 * <p>The range is expressed in arbitrary units that must be the same as the
8396 * units used by {@link #computeVerticalScrollExtent()} and
8397 * {@link #computeVerticalScrollOffset()}.</p>
8398 *
8399 * @return the total vertical range represented by the vertical scrollbar
8400 *
8401 * <p>The default range is the drawing height of this view.</p>
8402 *
8403 * @see #computeVerticalScrollExtent()
8404 * @see #computeVerticalScrollOffset()
8405 * @see android.widget.ScrollBarDrawable
8406 */
8407 protected int computeVerticalScrollRange() {
8408 return getHeight();
8409 }
8410
8411 /**
8412 * <p>Compute the vertical offset of the vertical scrollbar's thumb
8413 * within the horizontal range. This value is used to compute the position
8414 * of the thumb within the scrollbar's track.</p>
8415 *
8416 * <p>The range is expressed in arbitrary units that must be the same as the
8417 * units used by {@link #computeVerticalScrollRange()} and
8418 * {@link #computeVerticalScrollExtent()}.</p>
8419 *
8420 * <p>The default offset is the scroll offset of this view.</p>
8421 *
8422 * @return the vertical offset of the scrollbar's thumb
8423 *
8424 * @see #computeVerticalScrollRange()
8425 * @see #computeVerticalScrollExtent()
8426 * @see android.widget.ScrollBarDrawable
8427 */
8428 protected int computeVerticalScrollOffset() {
8429 return mScrollY;
8430 }
8431
8432 /**
8433 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
8434 * within the vertical range. This value is used to compute the length
8435 * of the thumb within the scrollbar's track.</p>
8436 *
8437 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08008438 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008439 * {@link #computeVerticalScrollOffset()}.</p>
8440 *
8441 * <p>The default extent is the drawing height of this view.</p>
8442 *
8443 * @return the vertical extent of the scrollbar's thumb
8444 *
8445 * @see #computeVerticalScrollRange()
8446 * @see #computeVerticalScrollOffset()
8447 * @see android.widget.ScrollBarDrawable
8448 */
8449 protected int computeVerticalScrollExtent() {
8450 return getHeight();
8451 }
8452
8453 /**
Adam Powell69159442011-06-13 17:53:06 -07008454 * Check if this view can be scrolled horizontally in a certain direction.
8455 *
8456 * @param direction Negative to check scrolling left, positive to check scrolling right.
8457 * @return true if this view can be scrolled in the specified direction, false otherwise.
8458 */
8459 public boolean canScrollHorizontally(int direction) {
8460 final int offset = computeHorizontalScrollOffset();
8461 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
8462 if (range == 0) return false;
8463 if (direction < 0) {
8464 return offset > 0;
8465 } else {
8466 return offset < range - 1;
8467 }
8468 }
8469
8470 /**
8471 * Check if this view can be scrolled vertically in a certain direction.
8472 *
8473 * @param direction Negative to check scrolling up, positive to check scrolling down.
8474 * @return true if this view can be scrolled in the specified direction, false otherwise.
8475 */
8476 public boolean canScrollVertically(int direction) {
8477 final int offset = computeVerticalScrollOffset();
8478 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
8479 if (range == 0) return false;
8480 if (direction < 0) {
8481 return offset > 0;
8482 } else {
8483 return offset < range - 1;
8484 }
8485 }
8486
8487 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008488 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8489 * scrollbars are painted only if they have been awakened first.</p>
8490 *
8491 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008492 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008493 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008494 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008495 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008496 // scrollbars are drawn only when the animation is running
8497 final ScrollabilityCache cache = mScrollCache;
8498 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008499
Mike Cleronf116bf82009-09-27 19:14:12 -07008500 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008501
Mike Cleronf116bf82009-09-27 19:14:12 -07008502 if (state == ScrollabilityCache.OFF) {
8503 return;
8504 }
Joe Malin32736f02011-01-19 16:14:20 -08008505
Mike Cleronf116bf82009-09-27 19:14:12 -07008506 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008507
Mike Cleronf116bf82009-09-27 19:14:12 -07008508 if (state == ScrollabilityCache.FADING) {
8509 // We're fading -- get our fade interpolation
8510 if (cache.interpolatorValues == null) {
8511 cache.interpolatorValues = new float[1];
8512 }
Joe Malin32736f02011-01-19 16:14:20 -08008513
Mike Cleronf116bf82009-09-27 19:14:12 -07008514 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008515
Mike Cleronf116bf82009-09-27 19:14:12 -07008516 // Stops the animation if we're done
8517 if (cache.scrollBarInterpolator.timeToValues(values) ==
8518 Interpolator.Result.FREEZE_END) {
8519 cache.state = ScrollabilityCache.OFF;
8520 } else {
8521 cache.scrollBar.setAlpha(Math.round(values[0]));
8522 }
Joe Malin32736f02011-01-19 16:14:20 -08008523
8524 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008525 // drawing. We only want this when we're fading so that
8526 // we prevent excessive redraws
8527 invalidate = true;
8528 } else {
8529 // We're just on -- but we may have been fading before so
8530 // reset alpha
8531 cache.scrollBar.setAlpha(255);
8532 }
8533
Joe Malin32736f02011-01-19 16:14:20 -08008534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008535 final int viewFlags = mViewFlags;
8536
8537 final boolean drawHorizontalScrollBar =
8538 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8539 final boolean drawVerticalScrollBar =
8540 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8541 && !isVerticalScrollBarHidden();
8542
8543 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8544 final int width = mRight - mLeft;
8545 final int height = mBottom - mTop;
8546
8547 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008548
Mike Reede8853fc2009-09-04 14:01:48 -04008549 final int scrollX = mScrollX;
8550 final int scrollY = mScrollY;
8551 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8552
Mike Cleronf116bf82009-09-27 19:14:12 -07008553 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008555 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008556 int size = scrollBar.getSize(false);
8557 if (size <= 0) {
8558 size = cache.scrollBarSize;
8559 }
8560
Mike Cleronf116bf82009-09-27 19:14:12 -07008561 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008562 computeHorizontalScrollOffset(),
8563 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008564 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008565 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008566 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008567 left = scrollX + (mPaddingLeft & inside);
8568 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8569 bottom = top + size;
8570 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8571 if (invalidate) {
8572 invalidate(left, top, right, bottom);
8573 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008574 }
8575
8576 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008577 int size = scrollBar.getSize(true);
8578 if (size <= 0) {
8579 size = cache.scrollBarSize;
8580 }
8581
Mike Reede8853fc2009-09-04 14:01:48 -04008582 scrollBar.setParameters(computeVerticalScrollRange(),
8583 computeVerticalScrollOffset(),
8584 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008585 switch (mVerticalScrollbarPosition) {
8586 default:
8587 case SCROLLBAR_POSITION_DEFAULT:
8588 case SCROLLBAR_POSITION_RIGHT:
8589 left = scrollX + width - size - (mUserPaddingRight & inside);
8590 break;
8591 case SCROLLBAR_POSITION_LEFT:
8592 left = scrollX + (mUserPaddingLeft & inside);
8593 break;
8594 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008595 top = scrollY + (mPaddingTop & inside);
8596 right = left + size;
8597 bottom = scrollY + height - (mUserPaddingBottom & inside);
8598 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8599 if (invalidate) {
8600 invalidate(left, top, right, bottom);
8601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008602 }
8603 }
8604 }
8605 }
Romain Guy8506ab42009-06-11 17:35:47 -07008606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008607 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008608 * 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 -08008609 * FastScroller is visible.
8610 * @return whether to temporarily hide the vertical scrollbar
8611 * @hide
8612 */
8613 protected boolean isVerticalScrollBarHidden() {
8614 return false;
8615 }
8616
8617 /**
8618 * <p>Draw the horizontal scrollbar if
8619 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
8620 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008621 * @param canvas the canvas on which to draw the scrollbar
8622 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008623 *
8624 * @see #isHorizontalScrollBarEnabled()
8625 * @see #computeHorizontalScrollRange()
8626 * @see #computeHorizontalScrollExtent()
8627 * @see #computeHorizontalScrollOffset()
8628 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008629 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008630 */
Romain Guy8fb95422010-08-17 18:38:51 -07008631 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8632 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008633 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008634 scrollBar.draw(canvas);
8635 }
Mike Reede8853fc2009-09-04 14:01:48 -04008636
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008637 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008638 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8639 * returns true.</p>
8640 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008641 * @param canvas the canvas on which to draw the scrollbar
8642 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008643 *
8644 * @see #isVerticalScrollBarEnabled()
8645 * @see #computeVerticalScrollRange()
8646 * @see #computeVerticalScrollExtent()
8647 * @see #computeVerticalScrollOffset()
8648 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008649 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008650 */
Romain Guy8fb95422010-08-17 18:38:51 -07008651 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
8652 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04008653 scrollBar.setBounds(l, t, r, b);
8654 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008655 }
8656
8657 /**
8658 * Implement this to do your drawing.
8659 *
8660 * @param canvas the canvas on which the background will be drawn
8661 */
8662 protected void onDraw(Canvas canvas) {
8663 }
8664
8665 /*
8666 * Caller is responsible for calling requestLayout if necessary.
8667 * (This allows addViewInLayout to not request a new layout.)
8668 */
8669 void assignParent(ViewParent parent) {
8670 if (mParent == null) {
8671 mParent = parent;
8672 } else if (parent == null) {
8673 mParent = null;
8674 } else {
8675 throw new RuntimeException("view " + this + " being added, but"
8676 + " it already has a parent");
8677 }
8678 }
8679
8680 /**
8681 * This is called when the view is attached to a window. At this point it
8682 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008683 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
8684 * however it may be called any time before the first onDraw -- including
8685 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008686 *
8687 * @see #onDetachedFromWindow()
8688 */
8689 protected void onAttachedToWindow() {
8690 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
8691 mParent.requestTransparentRegion(this);
8692 }
Adam Powell8568c3a2010-04-19 14:26:11 -07008693 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
8694 initialAwakenScrollBars();
8695 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
8696 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08008697 jumpDrawablesToCurrentState();
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008698 resolveLayoutDirection();
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008699 }
Cibu Johny86666632010-02-22 13:01:02 -08008700
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008701 /**
8702 * Resolving the layout direction. LTR is set initially.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07008703 * We are supposing here that the parent directionality will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008704 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008705 private void resolveLayoutDirection() {
Cibu Johny86666632010-02-22 13:01:02 -08008706 mPrivateFlags2 &= ~RESOLVED_LAYOUT_RTL;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008707 switch (getLayoutDirection()) {
8708 case LAYOUT_DIRECTION_INHERIT:
Cibu Johny86666632010-02-22 13:01:02 -08008709 // If this is root view, no need to look at parent's layout dir.
8710 if (mParent != null && mParent instanceof ViewGroup &&
8711 ((ViewGroup) mParent).isLayoutRtl()) {
8712 mPrivateFlags2 |= RESOLVED_LAYOUT_RTL;
8713 }
8714 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008715 case LAYOUT_DIRECTION_RTL:
Cibu Johny86666632010-02-22 13:01:02 -08008716 mPrivateFlags2 |= RESOLVED_LAYOUT_RTL;
8717 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07008718 case LAYOUT_DIRECTION_LOCALE:
8719 if(isLayoutDirectionRtl(Locale.getDefault())) {
8720 mPrivateFlags2 |= RESOLVED_LAYOUT_RTL;
8721 }
8722 break;
8723 default:
8724 // Nothing to do, LTR by default
8725 }
8726 }
8727
8728 /**
8729 * Check if a Locale is corresponding to a RTL script.
8730 *
8731 * @param locale Locale to check
8732 * @return true if a Locale is corresponding to a RTL script.
8733 */
8734 private static boolean isLayoutDirectionRtl(Locale locale) {
8735 if (locale == null || locale.equals(Locale.ROOT)) return false;
8736 // Be careful: this code will need to be changed when vertical scripts will be supported
8737 // OR if ICU4C is updated to have the "likelySubtags" file
8738 switch(Character.getDirectionality(locale.getDisplayName(locale).charAt(0))) {
8739 case Character.DIRECTIONALITY_LEFT_TO_RIGHT:
8740 return false;
8741 case Character.DIRECTIONALITY_RIGHT_TO_LEFT:
8742 case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC:
8743 return true;
8744 default:
8745 return false;
Cibu Johny86666632010-02-22 13:01:02 -08008746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008747 }
8748
8749 /**
8750 * This is called when the view is detached from a window. At this point it
8751 * no longer has a surface for drawing.
8752 *
8753 * @see #onAttachedToWindow()
8754 */
8755 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08008756 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08008757
Romain Guya440b002010-02-24 15:57:54 -08008758 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05008759 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08008760 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08008761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008762 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08008763
8764 if (mHardwareLayer != null) {
8765 mHardwareLayer.destroy();
8766 mHardwareLayer = null;
8767 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008768
Chet Haasedaf98e92011-01-10 14:10:36 -08008769 if (mDisplayList != null) {
8770 mDisplayList.invalidate();
8771 }
8772
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008773 if (mAttachInfo != null) {
8774 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
8775 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
8776 }
8777
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08008778 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008779 }
8780
8781 /**
8782 * @return The number of times this view has been attached to a window
8783 */
8784 protected int getWindowAttachCount() {
8785 return mWindowAttachCount;
8786 }
8787
8788 /**
8789 * Retrieve a unique token identifying the window this view is attached to.
8790 * @return Return the window's token for use in
8791 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
8792 */
8793 public IBinder getWindowToken() {
8794 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
8795 }
8796
8797 /**
8798 * Retrieve a unique token identifying the top-level "real" window of
8799 * the window that this view is attached to. That is, this is like
8800 * {@link #getWindowToken}, except if the window this view in is a panel
8801 * window (attached to another containing window), then the token of
8802 * the containing window is returned instead.
8803 *
8804 * @return Returns the associated window token, either
8805 * {@link #getWindowToken()} or the containing window's token.
8806 */
8807 public IBinder getApplicationWindowToken() {
8808 AttachInfo ai = mAttachInfo;
8809 if (ai != null) {
8810 IBinder appWindowToken = ai.mPanelParentWindowToken;
8811 if (appWindowToken == null) {
8812 appWindowToken = ai.mWindowToken;
8813 }
8814 return appWindowToken;
8815 }
8816 return null;
8817 }
8818
8819 /**
8820 * Retrieve private session object this view hierarchy is using to
8821 * communicate with the window manager.
8822 * @return the session object to communicate with the window manager
8823 */
8824 /*package*/ IWindowSession getWindowSession() {
8825 return mAttachInfo != null ? mAttachInfo.mSession : null;
8826 }
8827
8828 /**
8829 * @param info the {@link android.view.View.AttachInfo} to associated with
8830 * this view
8831 */
8832 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
8833 //System.out.println("Attached! " + this);
8834 mAttachInfo = info;
8835 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008836 // We will need to evaluate the drawable state at least once.
8837 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008838 if (mFloatingTreeObserver != null) {
8839 info.mTreeObserver.merge(mFloatingTreeObserver);
8840 mFloatingTreeObserver = null;
8841 }
8842 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
8843 mAttachInfo.mScrollContainers.add(this);
8844 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
8845 }
8846 performCollectViewAttributes(visibility);
8847 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08008848
8849 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8850 mOnAttachStateChangeListeners;
8851 if (listeners != null && listeners.size() > 0) {
8852 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8853 // perform the dispatching. The iterator is a safe guard against listeners that
8854 // could mutate the list by calling the various add/remove methods. This prevents
8855 // the array from being modified while we iterate it.
8856 for (OnAttachStateChangeListener listener : listeners) {
8857 listener.onViewAttachedToWindow(this);
8858 }
8859 }
8860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008861 int vis = info.mWindowVisibility;
8862 if (vis != GONE) {
8863 onWindowVisibilityChanged(vis);
8864 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008865 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
8866 // If nobody has evaluated the drawable state yet, then do it now.
8867 refreshDrawableState();
8868 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008869 }
8870
8871 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008872 AttachInfo info = mAttachInfo;
8873 if (info != null) {
8874 int vis = info.mWindowVisibility;
8875 if (vis != GONE) {
8876 onWindowVisibilityChanged(GONE);
8877 }
8878 }
8879
8880 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08008881
Adam Powell4afd62b2011-02-18 15:02:18 -08008882 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8883 mOnAttachStateChangeListeners;
8884 if (listeners != null && listeners.size() > 0) {
8885 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8886 // perform the dispatching. The iterator is a safe guard against listeners that
8887 // could mutate the list by calling the various add/remove methods. This prevents
8888 // the array from being modified while we iterate it.
8889 for (OnAttachStateChangeListener listener : listeners) {
8890 listener.onViewDetachedFromWindow(this);
8891 }
8892 }
8893
Romain Guy01d5edc2011-01-28 11:28:53 -08008894 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008895 mAttachInfo.mScrollContainers.remove(this);
8896 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
8897 }
Romain Guy01d5edc2011-01-28 11:28:53 -08008898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008899 mAttachInfo = null;
8900 }
8901
8902 /**
8903 * Store this view hierarchy's frozen state into the given container.
8904 *
8905 * @param container The SparseArray in which to save the view's state.
8906 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008907 * @see #restoreHierarchyState(android.util.SparseArray)
8908 * @see #dispatchSaveInstanceState(android.util.SparseArray)
8909 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008910 */
8911 public void saveHierarchyState(SparseArray<Parcelable> container) {
8912 dispatchSaveInstanceState(container);
8913 }
8914
8915 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008916 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
8917 * this view and its children. May be overridden to modify how freezing happens to a
8918 * 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 -08008919 *
8920 * @param container The SparseArray in which to save the view's state.
8921 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008922 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
8923 * @see #saveHierarchyState(android.util.SparseArray)
8924 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008925 */
8926 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
8927 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
8928 mPrivateFlags &= ~SAVE_STATE_CALLED;
8929 Parcelable state = onSaveInstanceState();
8930 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8931 throw new IllegalStateException(
8932 "Derived class did not call super.onSaveInstanceState()");
8933 }
8934 if (state != null) {
8935 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
8936 // + ": " + state);
8937 container.put(mID, state);
8938 }
8939 }
8940 }
8941
8942 /**
8943 * Hook allowing a view to generate a representation of its internal state
8944 * that can later be used to create a new instance with that same state.
8945 * This state should only contain information that is not persistent or can
8946 * not be reconstructed later. For example, you will never store your
8947 * current position on screen because that will be computed again when a
8948 * new instance of the view is placed in its view hierarchy.
8949 * <p>
8950 * Some examples of things you may store here: the current cursor position
8951 * in a text view (but usually not the text itself since that is stored in a
8952 * content provider or other persistent storage), the currently selected
8953 * item in a list view.
8954 *
8955 * @return Returns a Parcelable object containing the view's current dynamic
8956 * state, or null if there is nothing interesting to save. The
8957 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008958 * @see #onRestoreInstanceState(android.os.Parcelable)
8959 * @see #saveHierarchyState(android.util.SparseArray)
8960 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008961 * @see #setSaveEnabled(boolean)
8962 */
8963 protected Parcelable onSaveInstanceState() {
8964 mPrivateFlags |= SAVE_STATE_CALLED;
8965 return BaseSavedState.EMPTY_STATE;
8966 }
8967
8968 /**
8969 * Restore this view hierarchy's frozen state from the given container.
8970 *
8971 * @param container The SparseArray which holds previously frozen states.
8972 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008973 * @see #saveHierarchyState(android.util.SparseArray)
8974 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
8975 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008976 */
8977 public void restoreHierarchyState(SparseArray<Parcelable> container) {
8978 dispatchRestoreInstanceState(container);
8979 }
8980
8981 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008982 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
8983 * state for this view and its children. May be overridden to modify how restoring
8984 * happens to a view's children; for example, some views may want to not store state
8985 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008986 *
8987 * @param container The SparseArray which holds previously saved state.
8988 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008989 * @see #dispatchSaveInstanceState(android.util.SparseArray)
8990 * @see #restoreHierarchyState(android.util.SparseArray)
8991 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008992 */
8993 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
8994 if (mID != NO_ID) {
8995 Parcelable state = container.get(mID);
8996 if (state != null) {
8997 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
8998 // + ": " + state);
8999 mPrivateFlags &= ~SAVE_STATE_CALLED;
9000 onRestoreInstanceState(state);
9001 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9002 throw new IllegalStateException(
9003 "Derived class did not call super.onRestoreInstanceState()");
9004 }
9005 }
9006 }
9007 }
9008
9009 /**
9010 * Hook allowing a view to re-apply a representation of its internal state that had previously
9011 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9012 * null state.
9013 *
9014 * @param state The frozen state that had previously been returned by
9015 * {@link #onSaveInstanceState}.
9016 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009017 * @see #onSaveInstanceState()
9018 * @see #restoreHierarchyState(android.util.SparseArray)
9019 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009020 */
9021 protected void onRestoreInstanceState(Parcelable state) {
9022 mPrivateFlags |= SAVE_STATE_CALLED;
9023 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009024 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9025 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009026 + "when two views of different type have the same id in the same hierarchy. "
9027 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009028 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009029 }
9030 }
9031
9032 /**
9033 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9034 *
9035 * @return the drawing start time in milliseconds
9036 */
9037 public long getDrawingTime() {
9038 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9039 }
9040
9041 /**
9042 * <p>Enables or disables the duplication of the parent's state into this view. When
9043 * duplication is enabled, this view gets its drawable state from its parent rather
9044 * than from its own internal properties.</p>
9045 *
9046 * <p>Note: in the current implementation, setting this property to true after the
9047 * view was added to a ViewGroup might have no effect at all. This property should
9048 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9049 *
9050 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9051 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009052 *
Gilles Debunnefb817032011-01-13 13:52:49 -08009053 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
9054 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009055 *
9056 * @param enabled True to enable duplication of the parent's drawable state, false
9057 * to disable it.
9058 *
9059 * @see #getDrawableState()
9060 * @see #isDuplicateParentStateEnabled()
9061 */
9062 public void setDuplicateParentStateEnabled(boolean enabled) {
9063 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
9064 }
9065
9066 /**
9067 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
9068 *
9069 * @return True if this view's drawable state is duplicated from the parent,
9070 * false otherwise
9071 *
9072 * @see #getDrawableState()
9073 * @see #setDuplicateParentStateEnabled(boolean)
9074 */
9075 public boolean isDuplicateParentStateEnabled() {
9076 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
9077 }
9078
9079 /**
Romain Guy171c5922011-01-06 10:04:23 -08009080 * <p>Specifies the type of layer backing this view. The layer can be
9081 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
9082 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009083 *
Romain Guy171c5922011-01-06 10:04:23 -08009084 * <p>A layer is associated with an optional {@link android.graphics.Paint}
9085 * instance that controls how the layer is composed on screen. The following
9086 * properties of the paint are taken into account when composing the layer:</p>
9087 * <ul>
9088 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
9089 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
9090 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
9091 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08009092 *
Romain Guy171c5922011-01-06 10:04:23 -08009093 * <p>If this view has an alpha value set to < 1.0 by calling
9094 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
9095 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
9096 * equivalent to setting a hardware layer on this view and providing a paint with
9097 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08009098 *
Romain Guy171c5922011-01-06 10:04:23 -08009099 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
9100 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
9101 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009102 *
Romain Guy171c5922011-01-06 10:04:23 -08009103 * @param layerType The ype of layer to use with this view, must be one of
9104 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9105 * {@link #LAYER_TYPE_HARDWARE}
9106 * @param paint The paint used to compose the layer. This argument is optional
9107 * and can be null. It is ignored when the layer type is
9108 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08009109 *
9110 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08009111 * @see #LAYER_TYPE_NONE
9112 * @see #LAYER_TYPE_SOFTWARE
9113 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08009114 * @see #setAlpha(float)
9115 *
Romain Guy171c5922011-01-06 10:04:23 -08009116 * @attr ref android.R.styleable#View_layerType
9117 */
9118 public void setLayerType(int layerType, Paint paint) {
9119 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08009120 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08009121 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
9122 }
Chet Haasedaf98e92011-01-10 14:10:36 -08009123
Romain Guyd6cd5722011-01-17 14:42:41 -08009124 if (layerType == mLayerType) {
9125 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
9126 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009127 invalidateParentCaches();
9128 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08009129 }
9130 return;
9131 }
Romain Guy171c5922011-01-06 10:04:23 -08009132
9133 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08009134 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -07009135 case LAYER_TYPE_HARDWARE:
9136 if (mHardwareLayer != null) {
9137 mHardwareLayer.destroy();
9138 mHardwareLayer = null;
9139 }
9140 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -08009141 case LAYER_TYPE_SOFTWARE:
9142 if (mDrawingCache != null) {
9143 mDrawingCache.recycle();
9144 mDrawingCache = null;
9145 }
Joe Malin32736f02011-01-19 16:14:20 -08009146
Romain Guy6c319ca2011-01-11 14:29:25 -08009147 if (mUnscaledDrawingCache != null) {
9148 mUnscaledDrawingCache.recycle();
9149 mUnscaledDrawingCache = null;
9150 }
9151 break;
Romain Guy6c319ca2011-01-11 14:29:25 -08009152 default:
9153 break;
Romain Guy171c5922011-01-06 10:04:23 -08009154 }
9155
9156 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08009157 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
9158 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
9159 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08009160
Romain Guy0fd89bf2011-01-26 15:41:30 -08009161 invalidateParentCaches();
9162 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08009163 }
9164
9165 /**
9166 * Indicates what type of layer is currently associated with this view. By default
9167 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
9168 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
9169 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08009170 *
Romain Guy171c5922011-01-06 10:04:23 -08009171 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9172 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08009173 *
9174 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08009175 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08009176 * @see #LAYER_TYPE_NONE
9177 * @see #LAYER_TYPE_SOFTWARE
9178 * @see #LAYER_TYPE_HARDWARE
9179 */
9180 public int getLayerType() {
9181 return mLayerType;
9182 }
Joe Malin32736f02011-01-19 16:14:20 -08009183
Romain Guy6c319ca2011-01-11 14:29:25 -08009184 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08009185 * Forces this view's layer to be created and this view to be rendered
9186 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
9187 * invoking this method will have no effect.
9188 *
9189 * This method can for instance be used to render a view into its layer before
9190 * starting an animation. If this view is complex, rendering into the layer
9191 * before starting the animation will avoid skipping frames.
9192 *
9193 * @throws IllegalStateException If this view is not attached to a window
9194 *
9195 * @see #setLayerType(int, android.graphics.Paint)
9196 */
9197 public void buildLayer() {
9198 if (mLayerType == LAYER_TYPE_NONE) return;
9199
9200 if (mAttachInfo == null) {
9201 throw new IllegalStateException("This view must be attached to a window first");
9202 }
9203
9204 switch (mLayerType) {
9205 case LAYER_TYPE_HARDWARE:
9206 getHardwareLayer();
9207 break;
9208 case LAYER_TYPE_SOFTWARE:
9209 buildDrawingCache(true);
9210 break;
9211 }
9212 }
9213
9214 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08009215 * <p>Returns a hardware layer that can be used to draw this view again
9216 * without executing its draw method.</p>
9217 *
9218 * @return A HardwareLayer ready to render, or null if an error occurred.
9219 */
Romain Guy5e7f7662011-01-24 22:35:56 -08009220 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08009221 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
9222 return null;
9223 }
9224
9225 final int width = mRight - mLeft;
9226 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08009227
Romain Guy6c319ca2011-01-11 14:29:25 -08009228 if (width == 0 || height == 0) {
9229 return null;
9230 }
9231
9232 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
9233 if (mHardwareLayer == null) {
9234 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
9235 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08009236 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009237 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
9238 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08009239 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009240 }
9241
Romain Guy59a12ca2011-06-09 17:48:21 -07009242 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -08009243 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
9244 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009245 try {
9246 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08009247 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08009248 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009249
Chet Haase88172fe2011-03-07 17:36:33 -08009250 final int restoreCount = canvas.save();
9251
Romain Guy6c319ca2011-01-11 14:29:25 -08009252 computeScroll();
9253 canvas.translate(-mScrollX, -mScrollY);
9254
Romain Guy6c319ca2011-01-11 14:29:25 -08009255 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009256
Romain Guy6c319ca2011-01-11 14:29:25 -08009257 // Fast path for layouts with no backgrounds
9258 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9259 mPrivateFlags &= ~DIRTY_MASK;
9260 dispatchDraw(canvas);
9261 } else {
9262 draw(canvas);
9263 }
Joe Malin32736f02011-01-19 16:14:20 -08009264
Chet Haase88172fe2011-03-07 17:36:33 -08009265 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08009266 } finally {
9267 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08009268 mHardwareLayer.end(currentCanvas);
9269 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009270 }
9271 }
9272
9273 return mHardwareLayer;
9274 }
Romain Guy171c5922011-01-06 10:04:23 -08009275
9276 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009277 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
9278 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
9279 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
9280 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
9281 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
9282 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009283 *
Romain Guy171c5922011-01-06 10:04:23 -08009284 * <p>Enabling the drawing cache is similar to
9285 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08009286 * acceleration is turned off. When hardware acceleration is turned on, enabling the
9287 * drawing cache has no effect on rendering because the system uses a different mechanism
9288 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
9289 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
9290 * for information on how to enable software and hardware layers.</p>
9291 *
9292 * <p>This API can be used to manually generate
9293 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
9294 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009295 *
9296 * @param enabled true to enable the drawing cache, false otherwise
9297 *
9298 * @see #isDrawingCacheEnabled()
9299 * @see #getDrawingCache()
9300 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08009301 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009302 */
9303 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009304 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009305 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
9306 }
9307
9308 /**
9309 * <p>Indicates whether the drawing cache is enabled for this view.</p>
9310 *
9311 * @return true if the drawing cache is enabled
9312 *
9313 * @see #setDrawingCacheEnabled(boolean)
9314 * @see #getDrawingCache()
9315 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009316 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009317 public boolean isDrawingCacheEnabled() {
9318 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
9319 }
9320
9321 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08009322 * Debugging utility which recursively outputs the dirty state of a view and its
9323 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08009324 *
Chet Haasedaf98e92011-01-10 14:10:36 -08009325 * @hide
9326 */
Romain Guy676b1732011-02-14 14:45:33 -08009327 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08009328 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
9329 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
9330 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
9331 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
9332 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
9333 if (clear) {
9334 mPrivateFlags &= clearMask;
9335 }
9336 if (this instanceof ViewGroup) {
9337 ViewGroup parent = (ViewGroup) this;
9338 final int count = parent.getChildCount();
9339 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08009340 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08009341 child.outputDirtyFlags(indent + " ", clear, clearMask);
9342 }
9343 }
9344 }
9345
9346 /**
9347 * This method is used by ViewGroup to cause its children to restore or recreate their
9348 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
9349 * to recreate its own display list, which would happen if it went through the normal
9350 * draw/dispatchDraw mechanisms.
9351 *
9352 * @hide
9353 */
9354 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08009355
9356 /**
9357 * A view that is not attached or hardware accelerated cannot create a display list.
9358 * This method checks these conditions and returns the appropriate result.
9359 *
9360 * @return true if view has the ability to create a display list, false otherwise.
9361 *
9362 * @hide
9363 */
9364 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08009365 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08009366 }
Joe Malin32736f02011-01-19 16:14:20 -08009367
Chet Haasedaf98e92011-01-10 14:10:36 -08009368 /**
Romain Guyb051e892010-09-28 19:09:36 -07009369 * <p>Returns a display list that can be used to draw this view again
9370 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009371 *
Romain Guyb051e892010-09-28 19:09:36 -07009372 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08009373 *
9374 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07009375 */
Chet Haasedaf98e92011-01-10 14:10:36 -08009376 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08009377 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07009378 return null;
9379 }
9380
Chet Haasedaf98e92011-01-10 14:10:36 -08009381 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
9382 mDisplayList == null || !mDisplayList.isValid() ||
9383 mRecreateDisplayList)) {
9384 // Don't need to recreate the display list, just need to tell our
9385 // children to restore/recreate theirs
9386 if (mDisplayList != null && mDisplayList.isValid() &&
9387 !mRecreateDisplayList) {
9388 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9389 mPrivateFlags &= ~DIRTY_MASK;
9390 dispatchGetDisplayList();
9391
9392 return mDisplayList;
9393 }
9394
9395 // If we got here, we're recreating it. Mark it as such to ensure that
9396 // we copy in child display lists into ours in drawChild()
9397 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08009398 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009399 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
9400 // If we're creating a new display list, make sure our parent gets invalidated
9401 // since they will need to recreate their display list to account for this
9402 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08009403 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08009404 }
Romain Guyb051e892010-09-28 19:09:36 -07009405
9406 final HardwareCanvas canvas = mDisplayList.start();
9407 try {
9408 int width = mRight - mLeft;
9409 int height = mBottom - mTop;
9410
9411 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08009412 // The dirty rect should always be null for a display list
9413 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07009414
Chet Haase88172fe2011-03-07 17:36:33 -08009415 final int restoreCount = canvas.save();
9416
Chet Haasedaf98e92011-01-10 14:10:36 -08009417 computeScroll();
9418 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009419 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009420
Romain Guyb051e892010-09-28 19:09:36 -07009421 // Fast path for layouts with no backgrounds
9422 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9423 mPrivateFlags &= ~DIRTY_MASK;
9424 dispatchDraw(canvas);
9425 } else {
9426 draw(canvas);
9427 }
Joe Malin32736f02011-01-19 16:14:20 -08009428
Chet Haase88172fe2011-03-07 17:36:33 -08009429 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07009430 } finally {
9431 canvas.onPostDraw();
9432
9433 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07009434 }
Chet Haase77785f92011-01-25 23:22:09 -08009435 } else {
9436 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9437 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07009438 }
9439
9440 return mDisplayList;
9441 }
9442
9443 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009444 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009445 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009446 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009447 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009448 * @see #getDrawingCache(boolean)
9449 */
9450 public Bitmap getDrawingCache() {
9451 return getDrawingCache(false);
9452 }
9453
9454 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009455 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
9456 * is null when caching is disabled. If caching is enabled and the cache is not ready,
9457 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
9458 * draw from the cache when the cache is enabled. To benefit from the cache, you must
9459 * request the drawing cache by calling this method and draw it on screen if the
9460 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009461 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009462 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9463 * this method will create a bitmap of the same size as this view. Because this bitmap
9464 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9465 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9466 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9467 * size than the view. This implies that your application must be able to handle this
9468 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009469 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009470 * @param autoScale Indicates whether the generated bitmap should be scaled based on
9471 * the current density of the screen when the application is in compatibility
9472 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009473 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009474 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009475 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009476 * @see #setDrawingCacheEnabled(boolean)
9477 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07009478 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009479 * @see #destroyDrawingCache()
9480 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009481 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009482 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
9483 return null;
9484 }
9485 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009486 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009487 }
Romain Guy02890fd2010-08-06 17:58:44 -07009488 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009489 }
9490
9491 /**
9492 * <p>Frees the resources used by the drawing cache. If you call
9493 * {@link #buildDrawingCache()} manually without calling
9494 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9495 * should cleanup the cache with this method afterwards.</p>
9496 *
9497 * @see #setDrawingCacheEnabled(boolean)
9498 * @see #buildDrawingCache()
9499 * @see #getDrawingCache()
9500 */
9501 public void destroyDrawingCache() {
9502 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009503 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009504 mDrawingCache = null;
9505 }
Romain Guyfbd8f692009-06-26 14:51:58 -07009506 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009507 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07009508 mUnscaledDrawingCache = null;
9509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009510 }
9511
9512 /**
9513 * Setting a solid background color for the drawing cache's bitmaps will improve
9514 * perfromance and memory usage. Note, though that this should only be used if this
9515 * view will always be drawn on top of a solid color.
9516 *
9517 * @param color The background color to use for the drawing cache's bitmap
9518 *
9519 * @see #setDrawingCacheEnabled(boolean)
9520 * @see #buildDrawingCache()
9521 * @see #getDrawingCache()
9522 */
9523 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08009524 if (color != mDrawingCacheBackgroundColor) {
9525 mDrawingCacheBackgroundColor = color;
9526 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009528 }
9529
9530 /**
9531 * @see #setDrawingCacheBackgroundColor(int)
9532 *
9533 * @return The background color to used for the drawing cache's bitmap
9534 */
9535 public int getDrawingCacheBackgroundColor() {
9536 return mDrawingCacheBackgroundColor;
9537 }
9538
9539 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009540 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009541 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009542 * @see #buildDrawingCache(boolean)
9543 */
9544 public void buildDrawingCache() {
9545 buildDrawingCache(false);
9546 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08009547
Romain Guyfbd8f692009-06-26 14:51:58 -07009548 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009549 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
9550 *
9551 * <p>If you call {@link #buildDrawingCache()} manually without calling
9552 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9553 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009554 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009555 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9556 * this method will create a bitmap of the same size as this view. Because this bitmap
9557 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9558 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9559 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9560 * size than the view. This implies that your application must be able to handle this
9561 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009562 *
Romain Guy0d9275e2010-10-26 14:22:30 -07009563 * <p>You should avoid calling this method when hardware acceleration is enabled. If
9564 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08009565 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07009566 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009567 *
9568 * @see #getDrawingCache()
9569 * @see #destroyDrawingCache()
9570 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009571 public void buildDrawingCache(boolean autoScale) {
9572 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07009573 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009574 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009575
9576 if (ViewDebug.TRACE_HIERARCHY) {
9577 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
9578 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009579
Romain Guy8506ab42009-06-11 17:35:47 -07009580 int width = mRight - mLeft;
9581 int height = mBottom - mTop;
9582
9583 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07009584 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07009585
Romain Guye1123222009-06-29 14:24:56 -07009586 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009587 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
9588 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07009589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009590
9591 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07009592 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08009593 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009594
9595 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07009596 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08009597 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009598 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
9599 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08009600 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009601 return;
9602 }
9603
9604 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07009605 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009606
9607 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009608 Bitmap.Config quality;
9609 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08009610 // Never pick ARGB_4444 because it looks awful
9611 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009612 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
9613 case DRAWING_CACHE_QUALITY_AUTO:
9614 quality = Bitmap.Config.ARGB_8888;
9615 break;
9616 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08009617 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009618 break;
9619 case DRAWING_CACHE_QUALITY_HIGH:
9620 quality = Bitmap.Config.ARGB_8888;
9621 break;
9622 default:
9623 quality = Bitmap.Config.ARGB_8888;
9624 break;
9625 }
9626 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07009627 // Optimization for translucent windows
9628 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08009629 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009630 }
9631
9632 // Try to cleanup memory
9633 if (bitmap != null) bitmap.recycle();
9634
9635 try {
9636 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07009637 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07009638 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07009639 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009640 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07009641 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009642 }
Adam Powell26153a32010-11-08 15:22:27 -08009643 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009644 } catch (OutOfMemoryError e) {
9645 // If there is not enough memory to create the bitmap cache, just
9646 // ignore the issue as bitmap caches are not required to draw the
9647 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07009648 if (autoScale) {
9649 mDrawingCache = null;
9650 } else {
9651 mUnscaledDrawingCache = null;
9652 }
Romain Guy0211a0a2011-02-14 16:34:59 -08009653 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009654 return;
9655 }
9656
9657 clear = drawingCacheBackgroundColor != 0;
9658 }
9659
9660 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009661 if (attachInfo != null) {
9662 canvas = attachInfo.mCanvas;
9663 if (canvas == null) {
9664 canvas = new Canvas();
9665 }
9666 canvas.setBitmap(bitmap);
9667 // Temporarily clobber the cached Canvas in case one of our children
9668 // is also using a drawing cache. Without this, the children would
9669 // steal the canvas by attaching their own bitmap to it and bad, bad
9670 // thing would happen (invisible views, corrupted drawings, etc.)
9671 attachInfo.mCanvas = null;
9672 } else {
9673 // This case should hopefully never or seldom happen
9674 canvas = new Canvas(bitmap);
9675 }
9676
9677 if (clear) {
9678 bitmap.eraseColor(drawingCacheBackgroundColor);
9679 }
9680
9681 computeScroll();
9682 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08009683
Romain Guye1123222009-06-29 14:24:56 -07009684 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009685 final float scale = attachInfo.mApplicationScale;
9686 canvas.scale(scale, scale);
9687 }
Joe Malin32736f02011-01-19 16:14:20 -08009688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009689 canvas.translate(-mScrollX, -mScrollY);
9690
Romain Guy5bcdff42009-05-14 21:27:18 -07009691 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08009692 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
9693 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07009694 mPrivateFlags |= DRAWING_CACHE_VALID;
9695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009696
9697 // Fast path for layouts with no backgrounds
9698 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9699 if (ViewDebug.TRACE_HIERARCHY) {
9700 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9701 }
Romain Guy5bcdff42009-05-14 21:27:18 -07009702 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009703 dispatchDraw(canvas);
9704 } else {
9705 draw(canvas);
9706 }
9707
9708 canvas.restoreToCount(restoreCount);
9709
9710 if (attachInfo != null) {
9711 // Restore the cached Canvas for our siblings
9712 attachInfo.mCanvas = canvas;
9713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009714 }
9715 }
9716
9717 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009718 * Create a snapshot of the view into a bitmap. We should probably make
9719 * some form of this public, but should think about the API.
9720 */
Romain Guy223ff5c2010-03-02 17:07:47 -08009721 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009722 int width = mRight - mLeft;
9723 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009724
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009725 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07009726 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009727 width = (int) ((width * scale) + 0.5f);
9728 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08009729
Romain Guy8c11e312009-09-14 15:15:30 -07009730 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009731 if (bitmap == null) {
9732 throw new OutOfMemoryError();
9733 }
9734
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009735 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08009736
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009737 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009738 if (attachInfo != null) {
9739 canvas = attachInfo.mCanvas;
9740 if (canvas == null) {
9741 canvas = new Canvas();
9742 }
9743 canvas.setBitmap(bitmap);
9744 // Temporarily clobber the cached Canvas in case one of our children
9745 // is also using a drawing cache. Without this, the children would
9746 // steal the canvas by attaching their own bitmap to it and bad, bad
9747 // things would happen (invisible views, corrupted drawings, etc.)
9748 attachInfo.mCanvas = null;
9749 } else {
9750 // This case should hopefully never or seldom happen
9751 canvas = new Canvas(bitmap);
9752 }
9753
Romain Guy5bcdff42009-05-14 21:27:18 -07009754 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009755 bitmap.eraseColor(backgroundColor);
9756 }
9757
9758 computeScroll();
9759 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009760 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009761 canvas.translate(-mScrollX, -mScrollY);
9762
Romain Guy5bcdff42009-05-14 21:27:18 -07009763 // Temporarily remove the dirty mask
9764 int flags = mPrivateFlags;
9765 mPrivateFlags &= ~DIRTY_MASK;
9766
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009767 // Fast path for layouts with no backgrounds
9768 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9769 dispatchDraw(canvas);
9770 } else {
9771 draw(canvas);
9772 }
9773
Romain Guy5bcdff42009-05-14 21:27:18 -07009774 mPrivateFlags = flags;
9775
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009776 canvas.restoreToCount(restoreCount);
9777
9778 if (attachInfo != null) {
9779 // Restore the cached Canvas for our siblings
9780 attachInfo.mCanvas = canvas;
9781 }
Romain Guy8506ab42009-06-11 17:35:47 -07009782
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009783 return bitmap;
9784 }
9785
9786 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009787 * Indicates whether this View is currently in edit mode. A View is usually
9788 * in edit mode when displayed within a developer tool. For instance, if
9789 * this View is being drawn by a visual user interface builder, this method
9790 * should return true.
9791 *
9792 * Subclasses should check the return value of this method to provide
9793 * different behaviors if their normal behavior might interfere with the
9794 * host environment. For instance: the class spawns a thread in its
9795 * constructor, the drawing code relies on device-specific features, etc.
9796 *
9797 * This method is usually checked in the drawing code of custom widgets.
9798 *
9799 * @return True if this View is in edit mode, false otherwise.
9800 */
9801 public boolean isInEditMode() {
9802 return false;
9803 }
9804
9805 /**
9806 * If the View draws content inside its padding and enables fading edges,
9807 * it needs to support padding offsets. Padding offsets are added to the
9808 * fading edges to extend the length of the fade so that it covers pixels
9809 * drawn inside the padding.
9810 *
9811 * Subclasses of this class should override this method if they need
9812 * to draw content inside the padding.
9813 *
9814 * @return True if padding offset must be applied, false otherwise.
9815 *
9816 * @see #getLeftPaddingOffset()
9817 * @see #getRightPaddingOffset()
9818 * @see #getTopPaddingOffset()
9819 * @see #getBottomPaddingOffset()
9820 *
9821 * @since CURRENT
9822 */
9823 protected boolean isPaddingOffsetRequired() {
9824 return false;
9825 }
9826
9827 /**
9828 * Amount by which to extend the left fading region. Called only when
9829 * {@link #isPaddingOffsetRequired()} returns true.
9830 *
9831 * @return The left padding offset in pixels.
9832 *
9833 * @see #isPaddingOffsetRequired()
9834 *
9835 * @since CURRENT
9836 */
9837 protected int getLeftPaddingOffset() {
9838 return 0;
9839 }
9840
9841 /**
9842 * Amount by which to extend the right fading region. Called only when
9843 * {@link #isPaddingOffsetRequired()} returns true.
9844 *
9845 * @return The right padding offset in pixels.
9846 *
9847 * @see #isPaddingOffsetRequired()
9848 *
9849 * @since CURRENT
9850 */
9851 protected int getRightPaddingOffset() {
9852 return 0;
9853 }
9854
9855 /**
9856 * Amount by which to extend the top fading region. Called only when
9857 * {@link #isPaddingOffsetRequired()} returns true.
9858 *
9859 * @return The top padding offset in pixels.
9860 *
9861 * @see #isPaddingOffsetRequired()
9862 *
9863 * @since CURRENT
9864 */
9865 protected int getTopPaddingOffset() {
9866 return 0;
9867 }
9868
9869 /**
9870 * Amount by which to extend the bottom fading region. Called only when
9871 * {@link #isPaddingOffsetRequired()} returns true.
9872 *
9873 * @return The bottom padding offset in pixels.
9874 *
9875 * @see #isPaddingOffsetRequired()
9876 *
9877 * @since CURRENT
9878 */
9879 protected int getBottomPaddingOffset() {
9880 return 0;
9881 }
9882
9883 /**
Romain Guy2bffd262010-09-12 17:40:02 -07009884 * <p>Indicates whether this view is attached to an hardware accelerated
9885 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009886 *
Romain Guy2bffd262010-09-12 17:40:02 -07009887 * <p>Even if this method returns true, it does not mean that every call
9888 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
9889 * accelerated {@link android.graphics.Canvas}. For instance, if this view
9890 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
9891 * window is hardware accelerated,
9892 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
9893 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009894 *
Romain Guy2bffd262010-09-12 17:40:02 -07009895 * @return True if the view is attached to a window and the window is
9896 * hardware accelerated; false in any other case.
9897 */
9898 public boolean isHardwareAccelerated() {
9899 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
9900 }
Joe Malin32736f02011-01-19 16:14:20 -08009901
Romain Guy2bffd262010-09-12 17:40:02 -07009902 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009903 * Manually render this view (and all of its children) to the given Canvas.
9904 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009905 * called. When implementing a view, implement
9906 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
9907 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009908 *
9909 * @param canvas The Canvas to which the View is rendered.
9910 */
9911 public void draw(Canvas canvas) {
9912 if (ViewDebug.TRACE_HIERARCHY) {
9913 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9914 }
9915
Romain Guy5bcdff42009-05-14 21:27:18 -07009916 final int privateFlags = mPrivateFlags;
9917 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
9918 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
9919 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07009920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009921 /*
9922 * Draw traversal performs several drawing steps which must be executed
9923 * in the appropriate order:
9924 *
9925 * 1. Draw the background
9926 * 2. If necessary, save the canvas' layers to prepare for fading
9927 * 3. Draw view's content
9928 * 4. Draw children
9929 * 5. If necessary, draw the fading edges and restore layers
9930 * 6. Draw decorations (scrollbars for instance)
9931 */
9932
9933 // Step 1, draw the background, if needed
9934 int saveCount;
9935
Romain Guy24443ea2009-05-11 11:56:30 -07009936 if (!dirtyOpaque) {
9937 final Drawable background = mBGDrawable;
9938 if (background != null) {
9939 final int scrollX = mScrollX;
9940 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009941
Romain Guy24443ea2009-05-11 11:56:30 -07009942 if (mBackgroundSizeChanged) {
9943 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
9944 mBackgroundSizeChanged = false;
9945 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009946
Romain Guy24443ea2009-05-11 11:56:30 -07009947 if ((scrollX | scrollY) == 0) {
9948 background.draw(canvas);
9949 } else {
9950 canvas.translate(scrollX, scrollY);
9951 background.draw(canvas);
9952 canvas.translate(-scrollX, -scrollY);
9953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009954 }
9955 }
9956
9957 // skip step 2 & 5 if possible (common case)
9958 final int viewFlags = mViewFlags;
9959 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
9960 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
9961 if (!verticalEdges && !horizontalEdges) {
9962 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009963 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009964
9965 // Step 4, draw the children
9966 dispatchDraw(canvas);
9967
9968 // Step 6, draw decorations (scrollbars)
9969 onDrawScrollBars(canvas);
9970
9971 // we're done...
9972 return;
9973 }
9974
9975 /*
9976 * Here we do the full fledged routine...
9977 * (this is an uncommon case where speed matters less,
9978 * this is why we repeat some of the tests that have been
9979 * done above)
9980 */
9981
9982 boolean drawTop = false;
9983 boolean drawBottom = false;
9984 boolean drawLeft = false;
9985 boolean drawRight = false;
9986
9987 float topFadeStrength = 0.0f;
9988 float bottomFadeStrength = 0.0f;
9989 float leftFadeStrength = 0.0f;
9990 float rightFadeStrength = 0.0f;
9991
9992 // Step 2, save the canvas' layers
9993 int paddingLeft = mPaddingLeft;
9994 int paddingTop = mPaddingTop;
9995
9996 final boolean offsetRequired = isPaddingOffsetRequired();
9997 if (offsetRequired) {
9998 paddingLeft += getLeftPaddingOffset();
9999 paddingTop += getTopPaddingOffset();
10000 }
10001
10002 int left = mScrollX + paddingLeft;
10003 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
10004 int top = mScrollY + paddingTop;
10005 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
10006
10007 if (offsetRequired) {
10008 right += getRightPaddingOffset();
10009 bottom += getBottomPaddingOffset();
10010 }
10011
10012 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010013 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
10014 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010015
10016 // clip the fade length if top and bottom fades overlap
10017 // overlapping fades produce odd-looking artifacts
10018 if (verticalEdges && (top + length > bottom - length)) {
10019 length = (bottom - top) / 2;
10020 }
10021
10022 // also clip horizontal fades if necessary
10023 if (horizontalEdges && (left + length > right - length)) {
10024 length = (right - left) / 2;
10025 }
10026
10027 if (verticalEdges) {
10028 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010029 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010030 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010031 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010032 }
10033
10034 if (horizontalEdges) {
10035 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010036 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010037 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010038 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010039 }
10040
10041 saveCount = canvas.getSaveCount();
10042
10043 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070010044 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010045 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
10046
10047 if (drawTop) {
10048 canvas.saveLayer(left, top, right, top + length, null, flags);
10049 }
10050
10051 if (drawBottom) {
10052 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
10053 }
10054
10055 if (drawLeft) {
10056 canvas.saveLayer(left, top, left + length, bottom, null, flags);
10057 }
10058
10059 if (drawRight) {
10060 canvas.saveLayer(right - length, top, right, bottom, null, flags);
10061 }
10062 } else {
10063 scrollabilityCache.setFadeColor(solidColor);
10064 }
10065
10066 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010067 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010068
10069 // Step 4, draw the children
10070 dispatchDraw(canvas);
10071
10072 // Step 5, draw the fade effect and restore layers
10073 final Paint p = scrollabilityCache.paint;
10074 final Matrix matrix = scrollabilityCache.matrix;
10075 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010076
10077 if (drawTop) {
10078 matrix.setScale(1, fadeHeight * topFadeStrength);
10079 matrix.postTranslate(left, top);
10080 fade.setLocalMatrix(matrix);
10081 canvas.drawRect(left, top, right, top + length, p);
10082 }
10083
10084 if (drawBottom) {
10085 matrix.setScale(1, fadeHeight * bottomFadeStrength);
10086 matrix.postRotate(180);
10087 matrix.postTranslate(left, bottom);
10088 fade.setLocalMatrix(matrix);
10089 canvas.drawRect(left, bottom - length, right, bottom, p);
10090 }
10091
10092 if (drawLeft) {
10093 matrix.setScale(1, fadeHeight * leftFadeStrength);
10094 matrix.postRotate(-90);
10095 matrix.postTranslate(left, top);
10096 fade.setLocalMatrix(matrix);
10097 canvas.drawRect(left, top, left + length, bottom, p);
10098 }
10099
10100 if (drawRight) {
10101 matrix.setScale(1, fadeHeight * rightFadeStrength);
10102 matrix.postRotate(90);
10103 matrix.postTranslate(right, top);
10104 fade.setLocalMatrix(matrix);
10105 canvas.drawRect(right - length, top, right, bottom, p);
10106 }
10107
10108 canvas.restoreToCount(saveCount);
10109
10110 // Step 6, draw decorations (scrollbars)
10111 onDrawScrollBars(canvas);
10112 }
10113
10114 /**
10115 * Override this if your view is known to always be drawn on top of a solid color background,
10116 * and needs to draw fading edges. Returning a non-zero color enables the view system to
10117 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
10118 * should be set to 0xFF.
10119 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010120 * @see #setVerticalFadingEdgeEnabled(boolean)
10121 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010122 *
10123 * @return The known solid color background for this view, or 0 if the color may vary
10124 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010125 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010126 public int getSolidColor() {
10127 return 0;
10128 }
10129
10130 /**
10131 * Build a human readable string representation of the specified view flags.
10132 *
10133 * @param flags the view flags to convert to a string
10134 * @return a String representing the supplied flags
10135 */
10136 private static String printFlags(int flags) {
10137 String output = "";
10138 int numFlags = 0;
10139 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
10140 output += "TAKES_FOCUS";
10141 numFlags++;
10142 }
10143
10144 switch (flags & VISIBILITY_MASK) {
10145 case INVISIBLE:
10146 if (numFlags > 0) {
10147 output += " ";
10148 }
10149 output += "INVISIBLE";
10150 // USELESS HERE numFlags++;
10151 break;
10152 case GONE:
10153 if (numFlags > 0) {
10154 output += " ";
10155 }
10156 output += "GONE";
10157 // USELESS HERE numFlags++;
10158 break;
10159 default:
10160 break;
10161 }
10162 return output;
10163 }
10164
10165 /**
10166 * Build a human readable string representation of the specified private
10167 * view flags.
10168 *
10169 * @param privateFlags the private view flags to convert to a string
10170 * @return a String representing the supplied flags
10171 */
10172 private static String printPrivateFlags(int privateFlags) {
10173 String output = "";
10174 int numFlags = 0;
10175
10176 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
10177 output += "WANTS_FOCUS";
10178 numFlags++;
10179 }
10180
10181 if ((privateFlags & FOCUSED) == FOCUSED) {
10182 if (numFlags > 0) {
10183 output += " ";
10184 }
10185 output += "FOCUSED";
10186 numFlags++;
10187 }
10188
10189 if ((privateFlags & SELECTED) == SELECTED) {
10190 if (numFlags > 0) {
10191 output += " ";
10192 }
10193 output += "SELECTED";
10194 numFlags++;
10195 }
10196
10197 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
10198 if (numFlags > 0) {
10199 output += " ";
10200 }
10201 output += "IS_ROOT_NAMESPACE";
10202 numFlags++;
10203 }
10204
10205 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
10206 if (numFlags > 0) {
10207 output += " ";
10208 }
10209 output += "HAS_BOUNDS";
10210 numFlags++;
10211 }
10212
10213 if ((privateFlags & DRAWN) == DRAWN) {
10214 if (numFlags > 0) {
10215 output += " ";
10216 }
10217 output += "DRAWN";
10218 // USELESS HERE numFlags++;
10219 }
10220 return output;
10221 }
10222
10223 /**
10224 * <p>Indicates whether or not this view's layout will be requested during
10225 * the next hierarchy layout pass.</p>
10226 *
10227 * @return true if the layout will be forced during next layout pass
10228 */
10229 public boolean isLayoutRequested() {
10230 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
10231 }
10232
10233 /**
Cibu Johny86666632010-02-22 13:01:02 -080010234 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
10235 * layout attribute and/or the inherited value from the parent.</p>
10236 *
10237 * @return true if the layout is right-to-left.
10238 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -070010239 @ViewDebug.ExportedProperty(category = "layout")
Cibu Johny86666632010-02-22 13:01:02 -080010240 public boolean isLayoutRtl() {
10241 return (mPrivateFlags2 & RESOLVED_LAYOUT_RTL) == RESOLVED_LAYOUT_RTL;
10242 }
10243
10244 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010245 * Assign a size and position to a view and all of its
10246 * descendants
10247 *
10248 * <p>This is the second phase of the layout mechanism.
10249 * (The first is measuring). In this phase, each parent calls
10250 * layout on all of its children to position them.
10251 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080010252 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010253 *
Chet Haase9c087442011-01-12 16:20:16 -080010254 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010255 * Derived classes with children should override
10256 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080010257 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010258 *
10259 * @param l Left position, relative to parent
10260 * @param t Top position, relative to parent
10261 * @param r Right position, relative to parent
10262 * @param b Bottom position, relative to parent
10263 */
Romain Guy5429e1d2010-09-07 12:38:00 -070010264 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080010265 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070010266 int oldL = mLeft;
10267 int oldT = mTop;
10268 int oldB = mBottom;
10269 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010270 boolean changed = setFrame(l, t, r, b);
10271 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
10272 if (ViewDebug.TRACE_HIERARCHY) {
10273 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
10274 }
10275
10276 onLayout(changed, l, t, r, b);
10277 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070010278
10279 if (mOnLayoutChangeListeners != null) {
10280 ArrayList<OnLayoutChangeListener> listenersCopy =
10281 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
10282 int numListeners = listenersCopy.size();
10283 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070010284 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070010285 }
10286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010287 }
10288 mPrivateFlags &= ~FORCE_LAYOUT;
10289 }
10290
10291 /**
10292 * Called from layout when this view should
10293 * assign a size and position to each of its children.
10294 *
10295 * Derived classes with children should override
10296 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070010297 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010298 * @param changed This is a new size or position for this view
10299 * @param left Left position, relative to parent
10300 * @param top Top position, relative to parent
10301 * @param right Right position, relative to parent
10302 * @param bottom Bottom position, relative to parent
10303 */
10304 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
10305 }
10306
10307 /**
10308 * Assign a size and position to this view.
10309 *
10310 * This is called from layout.
10311 *
10312 * @param left Left position, relative to parent
10313 * @param top Top position, relative to parent
10314 * @param right Right position, relative to parent
10315 * @param bottom Bottom position, relative to parent
10316 * @return true if the new size and position are different than the
10317 * previous ones
10318 * {@hide}
10319 */
10320 protected boolean setFrame(int left, int top, int right, int bottom) {
10321 boolean changed = false;
10322
10323 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070010324 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010325 + right + "," + bottom + ")");
10326 }
10327
10328 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
10329 changed = true;
10330
10331 // Remember our drawn bit
10332 int drawn = mPrivateFlags & DRAWN;
10333
10334 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -080010335 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010336
10337
10338 int oldWidth = mRight - mLeft;
10339 int oldHeight = mBottom - mTop;
10340
10341 mLeft = left;
10342 mTop = top;
10343 mRight = right;
10344 mBottom = bottom;
10345
10346 mPrivateFlags |= HAS_BOUNDS;
10347
10348 int newWidth = right - left;
10349 int newHeight = bottom - top;
10350
10351 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010352 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
10353 // A change in dimension means an auto-centered pivot point changes, too
10354 mMatrixDirty = true;
10355 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010356 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
10357 }
10358
10359 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
10360 // If we are visible, force the DRAWN bit to on so that
10361 // this invalidate will go through (at least to our parent).
10362 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010363 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010364 // the DRAWN bit.
10365 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010366 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -080010367 // parent display list may need to be recreated based on a change in the bounds
10368 // of any child
10369 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010370 }
10371
10372 // Reset drawn bit to original value (invalidate turns it off)
10373 mPrivateFlags |= drawn;
10374
10375 mBackgroundSizeChanged = true;
10376 }
10377 return changed;
10378 }
10379
10380 /**
10381 * Finalize inflating a view from XML. This is called as the last phase
10382 * of inflation, after all child views have been added.
10383 *
10384 * <p>Even if the subclass overrides onFinishInflate, they should always be
10385 * sure to call the super method, so that we get called.
10386 */
10387 protected void onFinishInflate() {
10388 }
10389
10390 /**
10391 * Returns the resources associated with this view.
10392 *
10393 * @return Resources object.
10394 */
10395 public Resources getResources() {
10396 return mResources;
10397 }
10398
10399 /**
10400 * Invalidates the specified Drawable.
10401 *
10402 * @param drawable the drawable to invalidate
10403 */
10404 public void invalidateDrawable(Drawable drawable) {
10405 if (verifyDrawable(drawable)) {
10406 final Rect dirty = drawable.getBounds();
10407 final int scrollX = mScrollX;
10408 final int scrollY = mScrollY;
10409
10410 invalidate(dirty.left + scrollX, dirty.top + scrollY,
10411 dirty.right + scrollX, dirty.bottom + scrollY);
10412 }
10413 }
10414
10415 /**
10416 * Schedules an action on a drawable to occur at a specified time.
10417 *
10418 * @param who the recipient of the action
10419 * @param what the action to run on the drawable
10420 * @param when the time at which the action must occur. Uses the
10421 * {@link SystemClock#uptimeMillis} timebase.
10422 */
10423 public void scheduleDrawable(Drawable who, Runnable what, long when) {
10424 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10425 mAttachInfo.mHandler.postAtTime(what, who, when);
10426 }
10427 }
10428
10429 /**
10430 * Cancels a scheduled action on a drawable.
10431 *
10432 * @param who the recipient of the action
10433 * @param what the action to cancel
10434 */
10435 public void unscheduleDrawable(Drawable who, Runnable what) {
10436 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10437 mAttachInfo.mHandler.removeCallbacks(what, who);
10438 }
10439 }
10440
10441 /**
10442 * Unschedule any events associated with the given Drawable. This can be
10443 * used when selecting a new Drawable into a view, so that the previous
10444 * one is completely unscheduled.
10445 *
10446 * @param who The Drawable to unschedule.
10447 *
10448 * @see #drawableStateChanged
10449 */
10450 public void unscheduleDrawable(Drawable who) {
10451 if (mAttachInfo != null) {
10452 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
10453 }
10454 }
10455
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070010456 /**
10457 * Check if a given Drawable is in RTL layout direction.
10458 *
10459 * @param who the recipient of the action
10460 */
10461 public boolean isLayoutRtl(Drawable who) {
10462 return (who == mBGDrawable) && isLayoutRtl();
10463 }
10464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010465 /**
10466 * If your view subclass is displaying its own Drawable objects, it should
10467 * override this function and return true for any Drawable it is
10468 * displaying. This allows animations for those drawables to be
10469 * scheduled.
10470 *
10471 * <p>Be sure to call through to the super class when overriding this
10472 * function.
10473 *
10474 * @param who The Drawable to verify. Return true if it is one you are
10475 * displaying, else return the result of calling through to the
10476 * super class.
10477 *
10478 * @return boolean If true than the Drawable is being displayed in the
10479 * view; else false and it is not allowed to animate.
10480 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010481 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
10482 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010483 */
10484 protected boolean verifyDrawable(Drawable who) {
10485 return who == mBGDrawable;
10486 }
10487
10488 /**
10489 * This function is called whenever the state of the view changes in such
10490 * a way that it impacts the state of drawables being shown.
10491 *
10492 * <p>Be sure to call through to the superclass when overriding this
10493 * function.
10494 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010495 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010496 */
10497 protected void drawableStateChanged() {
10498 Drawable d = mBGDrawable;
10499 if (d != null && d.isStateful()) {
10500 d.setState(getDrawableState());
10501 }
10502 }
10503
10504 /**
10505 * Call this to force a view to update its drawable state. This will cause
10506 * drawableStateChanged to be called on this view. Views that are interested
10507 * in the new state should call getDrawableState.
10508 *
10509 * @see #drawableStateChanged
10510 * @see #getDrawableState
10511 */
10512 public void refreshDrawableState() {
10513 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
10514 drawableStateChanged();
10515
10516 ViewParent parent = mParent;
10517 if (parent != null) {
10518 parent.childDrawableStateChanged(this);
10519 }
10520 }
10521
10522 /**
10523 * Return an array of resource IDs of the drawable states representing the
10524 * current state of the view.
10525 *
10526 * @return The current drawable state
10527 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010528 * @see Drawable#setState(int[])
10529 * @see #drawableStateChanged()
10530 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010531 */
10532 public final int[] getDrawableState() {
10533 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
10534 return mDrawableState;
10535 } else {
10536 mDrawableState = onCreateDrawableState(0);
10537 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
10538 return mDrawableState;
10539 }
10540 }
10541
10542 /**
10543 * Generate the new {@link android.graphics.drawable.Drawable} state for
10544 * this view. This is called by the view
10545 * system when the cached Drawable state is determined to be invalid. To
10546 * retrieve the current state, you should use {@link #getDrawableState}.
10547 *
10548 * @param extraSpace if non-zero, this is the number of extra entries you
10549 * would like in the returned array in which you can place your own
10550 * states.
10551 *
10552 * @return Returns an array holding the current {@link Drawable} state of
10553 * the view.
10554 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010555 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010556 */
10557 protected int[] onCreateDrawableState(int extraSpace) {
10558 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
10559 mParent instanceof View) {
10560 return ((View) mParent).onCreateDrawableState(extraSpace);
10561 }
10562
10563 int[] drawableState;
10564
10565 int privateFlags = mPrivateFlags;
10566
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010567 int viewStateIndex = 0;
10568 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
10569 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
10570 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070010571 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010572 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
10573 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070010574 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
10575 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010576 // This is set if HW acceleration is requested, even if the current
10577 // process doesn't allow it. This is just to allow app preview
10578 // windows to better match their app.
10579 viewStateIndex |= VIEW_STATE_ACCELERATED;
10580 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070010581 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010582
Christopher Tate3d4bf172011-03-28 16:16:46 -070010583 final int privateFlags2 = mPrivateFlags2;
10584 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
10585 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
10586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010587 drawableState = VIEW_STATE_SETS[viewStateIndex];
10588
10589 //noinspection ConstantIfStatement
10590 if (false) {
10591 Log.i("View", "drawableStateIndex=" + viewStateIndex);
10592 Log.i("View", toString()
10593 + " pressed=" + ((privateFlags & PRESSED) != 0)
10594 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
10595 + " fo=" + hasFocus()
10596 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010597 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010598 + ": " + Arrays.toString(drawableState));
10599 }
10600
10601 if (extraSpace == 0) {
10602 return drawableState;
10603 }
10604
10605 final int[] fullState;
10606 if (drawableState != null) {
10607 fullState = new int[drawableState.length + extraSpace];
10608 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
10609 } else {
10610 fullState = new int[extraSpace];
10611 }
10612
10613 return fullState;
10614 }
10615
10616 /**
10617 * Merge your own state values in <var>additionalState</var> into the base
10618 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070010619 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010620 *
10621 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070010622 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010623 * own additional state values.
10624 *
10625 * @param additionalState The additional state values you would like
10626 * added to <var>baseState</var>; this array is not modified.
10627 *
10628 * @return As a convenience, the <var>baseState</var> array you originally
10629 * passed into the function is returned.
10630 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010631 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010632 */
10633 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
10634 final int N = baseState.length;
10635 int i = N - 1;
10636 while (i >= 0 && baseState[i] == 0) {
10637 i--;
10638 }
10639 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
10640 return baseState;
10641 }
10642
10643 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070010644 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
10645 * on all Drawable objects associated with this view.
10646 */
10647 public void jumpDrawablesToCurrentState() {
10648 if (mBGDrawable != null) {
10649 mBGDrawable.jumpToCurrentState();
10650 }
10651 }
10652
10653 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010654 * Sets the background color for this view.
10655 * @param color the color of the background
10656 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010657 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010658 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070010659 if (mBGDrawable instanceof ColorDrawable) {
10660 ((ColorDrawable) mBGDrawable).setColor(color);
10661 } else {
10662 setBackgroundDrawable(new ColorDrawable(color));
10663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010664 }
10665
10666 /**
10667 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070010668 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010669 * @param resid The identifier of the resource.
10670 * @attr ref android.R.styleable#View_background
10671 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010672 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010673 public void setBackgroundResource(int resid) {
10674 if (resid != 0 && resid == mBackgroundResource) {
10675 return;
10676 }
10677
10678 Drawable d= null;
10679 if (resid != 0) {
10680 d = mResources.getDrawable(resid);
10681 }
10682 setBackgroundDrawable(d);
10683
10684 mBackgroundResource = resid;
10685 }
10686
10687 /**
10688 * Set the background to a given Drawable, or remove the background. If the
10689 * background has padding, this View's padding is set to the background's
10690 * padding. However, when a background is removed, this View's padding isn't
10691 * touched. If setting the padding is desired, please use
10692 * {@link #setPadding(int, int, int, int)}.
10693 *
10694 * @param d The Drawable to use as the background, or null to remove the
10695 * background
10696 */
10697 public void setBackgroundDrawable(Drawable d) {
10698 boolean requestLayout = false;
10699
10700 mBackgroundResource = 0;
10701
10702 /*
10703 * Regardless of whether we're setting a new background or not, we want
10704 * to clear the previous drawable.
10705 */
10706 if (mBGDrawable != null) {
10707 mBGDrawable.setCallback(null);
10708 unscheduleDrawable(mBGDrawable);
10709 }
10710
10711 if (d != null) {
10712 Rect padding = sThreadLocal.get();
10713 if (padding == null) {
10714 padding = new Rect();
10715 sThreadLocal.set(padding);
10716 }
10717 if (d.getPadding(padding)) {
10718 setPadding(padding.left, padding.top, padding.right, padding.bottom);
10719 }
10720
10721 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
10722 // if it has a different minimum size, we should layout again
10723 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
10724 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
10725 requestLayout = true;
10726 }
10727
10728 d.setCallback(this);
10729 if (d.isStateful()) {
10730 d.setState(getDrawableState());
10731 }
10732 d.setVisible(getVisibility() == VISIBLE, false);
10733 mBGDrawable = d;
10734
10735 if ((mPrivateFlags & SKIP_DRAW) != 0) {
10736 mPrivateFlags &= ~SKIP_DRAW;
10737 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
10738 requestLayout = true;
10739 }
10740 } else {
10741 /* Remove the background */
10742 mBGDrawable = null;
10743
10744 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
10745 /*
10746 * This view ONLY drew the background before and we're removing
10747 * the background, so now it won't draw anything
10748 * (hence we SKIP_DRAW)
10749 */
10750 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
10751 mPrivateFlags |= SKIP_DRAW;
10752 }
10753
10754 /*
10755 * When the background is set, we try to apply its padding to this
10756 * View. When the background is removed, we don't touch this View's
10757 * padding. This is noted in the Javadocs. Hence, we don't need to
10758 * requestLayout(), the invalidate() below is sufficient.
10759 */
10760
10761 // The old background's minimum size could have affected this
10762 // View's layout, so let's requestLayout
10763 requestLayout = true;
10764 }
10765
Romain Guy8f1344f52009-05-15 16:03:59 -070010766 computeOpaqueFlags();
10767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010768 if (requestLayout) {
10769 requestLayout();
10770 }
10771
10772 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010773 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010774 }
10775
10776 /**
10777 * Gets the background drawable
10778 * @return The drawable used as the background for this view, if any.
10779 */
10780 public Drawable getBackground() {
10781 return mBGDrawable;
10782 }
10783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010784 /**
10785 * Sets the padding. The view may add on the space required to display
10786 * the scrollbars, depending on the style and visibility of the scrollbars.
10787 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
10788 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
10789 * from the values set in this call.
10790 *
10791 * @attr ref android.R.styleable#View_padding
10792 * @attr ref android.R.styleable#View_paddingBottom
10793 * @attr ref android.R.styleable#View_paddingLeft
10794 * @attr ref android.R.styleable#View_paddingRight
10795 * @attr ref android.R.styleable#View_paddingTop
10796 * @param left the left padding in pixels
10797 * @param top the top padding in pixels
10798 * @param right the right padding in pixels
10799 * @param bottom the bottom padding in pixels
10800 */
10801 public void setPadding(int left, int top, int right, int bottom) {
10802 boolean changed = false;
10803
Adam Powell20232d02010-12-08 21:08:53 -080010804 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010805 mUserPaddingRight = right;
10806 mUserPaddingBottom = bottom;
10807
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010808 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070010809
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010810 // Common case is there are no scroll bars.
10811 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010812 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080010813 // TODO Determine what to do with SCROLLBAR_POSITION_DEFAULT based on RTL settings.
10814 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010815 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080010816 switch (mVerticalScrollbarPosition) {
10817 case SCROLLBAR_POSITION_DEFAULT:
10818 case SCROLLBAR_POSITION_RIGHT:
10819 right += offset;
10820 break;
10821 case SCROLLBAR_POSITION_LEFT:
10822 left += offset;
10823 break;
10824 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010825 }
Adam Powell20232d02010-12-08 21:08:53 -080010826 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010827 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
10828 ? 0 : getHorizontalScrollbarHeight();
10829 }
10830 }
Romain Guy8506ab42009-06-11 17:35:47 -070010831
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010832 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010833 changed = true;
10834 mPaddingLeft = left;
10835 }
10836 if (mPaddingTop != top) {
10837 changed = true;
10838 mPaddingTop = top;
10839 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010840 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010841 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010842 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010843 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010844 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010845 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010846 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010847 }
10848
10849 if (changed) {
10850 requestLayout();
10851 }
10852 }
10853
10854 /**
10855 * Returns the top padding of this view.
10856 *
10857 * @return the top padding in pixels
10858 */
10859 public int getPaddingTop() {
10860 return mPaddingTop;
10861 }
10862
10863 /**
10864 * Returns the bottom padding of this view. If there are inset and enabled
10865 * scrollbars, this value may include the space required to display the
10866 * scrollbars as well.
10867 *
10868 * @return the bottom padding in pixels
10869 */
10870 public int getPaddingBottom() {
10871 return mPaddingBottom;
10872 }
10873
10874 /**
10875 * Returns the left padding of this view. If there are inset and enabled
10876 * scrollbars, this value may include the space required to display the
10877 * scrollbars as well.
10878 *
10879 * @return the left padding in pixels
10880 */
10881 public int getPaddingLeft() {
10882 return mPaddingLeft;
10883 }
10884
10885 /**
10886 * Returns the right padding of this view. If there are inset and enabled
10887 * scrollbars, this value may include the space required to display the
10888 * scrollbars as well.
10889 *
10890 * @return the right padding in pixels
10891 */
10892 public int getPaddingRight() {
10893 return mPaddingRight;
10894 }
10895
10896 /**
10897 * Changes the selection state of this view. A view can be selected or not.
10898 * Note that selection is not the same as focus. Views are typically
10899 * selected in the context of an AdapterView like ListView or GridView;
10900 * the selected view is the view that is highlighted.
10901 *
10902 * @param selected true if the view must be selected, false otherwise
10903 */
10904 public void setSelected(boolean selected) {
10905 if (((mPrivateFlags & SELECTED) != 0) != selected) {
10906 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070010907 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080010908 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010909 refreshDrawableState();
10910 dispatchSetSelected(selected);
10911 }
10912 }
10913
10914 /**
10915 * Dispatch setSelected to all of this View's children.
10916 *
10917 * @see #setSelected(boolean)
10918 *
10919 * @param selected The new selected state
10920 */
10921 protected void dispatchSetSelected(boolean selected) {
10922 }
10923
10924 /**
10925 * Indicates the selection state of this view.
10926 *
10927 * @return true if the view is selected, false otherwise
10928 */
10929 @ViewDebug.ExportedProperty
10930 public boolean isSelected() {
10931 return (mPrivateFlags & SELECTED) != 0;
10932 }
10933
10934 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010935 * Changes the activated state of this view. A view can be activated or not.
10936 * Note that activation is not the same as selection. Selection is
10937 * a transient property, representing the view (hierarchy) the user is
10938 * currently interacting with. Activation is a longer-term state that the
10939 * user can move views in and out of. For example, in a list view with
10940 * single or multiple selection enabled, the views in the current selection
10941 * set are activated. (Um, yeah, we are deeply sorry about the terminology
10942 * here.) The activated state is propagated down to children of the view it
10943 * is set on.
10944 *
10945 * @param activated true if the view must be activated, false otherwise
10946 */
10947 public void setActivated(boolean activated) {
10948 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
10949 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010950 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010951 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070010952 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010953 }
10954 }
10955
10956 /**
10957 * Dispatch setActivated to all of this View's children.
10958 *
10959 * @see #setActivated(boolean)
10960 *
10961 * @param activated The new activated state
10962 */
10963 protected void dispatchSetActivated(boolean activated) {
10964 }
10965
10966 /**
10967 * Indicates the activation state of this view.
10968 *
10969 * @return true if the view is activated, false otherwise
10970 */
10971 @ViewDebug.ExportedProperty
10972 public boolean isActivated() {
10973 return (mPrivateFlags & ACTIVATED) != 0;
10974 }
10975
10976 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010977 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
10978 * observer can be used to get notifications when global events, like
10979 * layout, happen.
10980 *
10981 * The returned ViewTreeObserver observer is not guaranteed to remain
10982 * valid for the lifetime of this View. If the caller of this method keeps
10983 * a long-lived reference to ViewTreeObserver, it should always check for
10984 * the return value of {@link ViewTreeObserver#isAlive()}.
10985 *
10986 * @return The ViewTreeObserver for this view's hierarchy.
10987 */
10988 public ViewTreeObserver getViewTreeObserver() {
10989 if (mAttachInfo != null) {
10990 return mAttachInfo.mTreeObserver;
10991 }
10992 if (mFloatingTreeObserver == null) {
10993 mFloatingTreeObserver = new ViewTreeObserver();
10994 }
10995 return mFloatingTreeObserver;
10996 }
10997
10998 /**
10999 * <p>Finds the topmost view in the current view hierarchy.</p>
11000 *
11001 * @return the topmost view containing this view
11002 */
11003 public View getRootView() {
11004 if (mAttachInfo != null) {
11005 final View v = mAttachInfo.mRootView;
11006 if (v != null) {
11007 return v;
11008 }
11009 }
Romain Guy8506ab42009-06-11 17:35:47 -070011010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011011 View parent = this;
11012
11013 while (parent.mParent != null && parent.mParent instanceof View) {
11014 parent = (View) parent.mParent;
11015 }
11016
11017 return parent;
11018 }
11019
11020 /**
11021 * <p>Computes the coordinates of this view on the screen. The argument
11022 * must be an array of two integers. After the method returns, the array
11023 * contains the x and y location in that order.</p>
11024 *
11025 * @param location an array of two integers in which to hold the coordinates
11026 */
11027 public void getLocationOnScreen(int[] location) {
11028 getLocationInWindow(location);
11029
11030 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070011031 if (info != null) {
11032 location[0] += info.mWindowLeft;
11033 location[1] += info.mWindowTop;
11034 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011035 }
11036
11037 /**
11038 * <p>Computes the coordinates of this view in its window. The argument
11039 * must be an array of two integers. After the method returns, the array
11040 * contains the x and y location in that order.</p>
11041 *
11042 * @param location an array of two integers in which to hold the coordinates
11043 */
11044 public void getLocationInWindow(int[] location) {
11045 if (location == null || location.length < 2) {
11046 throw new IllegalArgumentException("location must be an array of "
11047 + "two integers");
11048 }
11049
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011050 location[0] = mLeft + (int) (mTranslationX + 0.5f);
11051 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011052
11053 ViewParent viewParent = mParent;
11054 while (viewParent instanceof View) {
11055 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011056 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
11057 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011058 viewParent = view.mParent;
11059 }
Romain Guy8506ab42009-06-11 17:35:47 -070011060
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011061 if (viewParent instanceof ViewAncestor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011062 // *cough*
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011063 final ViewAncestor vr = (ViewAncestor)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011064 location[1] -= vr.mCurScrollY;
11065 }
11066 }
11067
11068 /**
11069 * {@hide}
11070 * @param id the id of the view to be found
11071 * @return the view of the specified id, null if cannot be found
11072 */
11073 protected View findViewTraversal(int id) {
11074 if (id == mID) {
11075 return this;
11076 }
11077 return null;
11078 }
11079
11080 /**
11081 * {@hide}
11082 * @param tag the tag of the view to be found
11083 * @return the view of specified tag, null if cannot be found
11084 */
11085 protected View findViewWithTagTraversal(Object tag) {
11086 if (tag != null && tag.equals(mTag)) {
11087 return this;
11088 }
11089 return null;
11090 }
11091
11092 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011093 * {@hide}
11094 * @param predicate The predicate to evaluate.
11095 * @return The first view that matches the predicate or null.
11096 */
11097 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
11098 if (predicate.apply(this)) {
11099 return this;
11100 }
11101 return null;
11102 }
11103
11104 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011105 * Look for a child view with the given id. If this view has the given
11106 * id, return this view.
11107 *
11108 * @param id The id to search for.
11109 * @return The view that has the given id in the hierarchy or null
11110 */
11111 public final View findViewById(int id) {
11112 if (id < 0) {
11113 return null;
11114 }
11115 return findViewTraversal(id);
11116 }
11117
11118 /**
11119 * Look for a child view with the given tag. If this view has the given
11120 * tag, return this view.
11121 *
11122 * @param tag The tag to search for, using "tag.equals(getTag())".
11123 * @return The View that has the given tag in the hierarchy or null
11124 */
11125 public final View findViewWithTag(Object tag) {
11126 if (tag == null) {
11127 return null;
11128 }
11129 return findViewWithTagTraversal(tag);
11130 }
11131
11132 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011133 * {@hide}
11134 * Look for a child view that matches the specified predicate.
11135 * If this view matches the predicate, return this view.
11136 *
11137 * @param predicate The predicate to evaluate.
11138 * @return The first view that matches the predicate or null.
11139 */
11140 public final View findViewByPredicate(Predicate<View> predicate) {
11141 return findViewByPredicateTraversal(predicate);
11142 }
11143
11144 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011145 * Sets the identifier for this view. The identifier does not have to be
11146 * unique in this view's hierarchy. The identifier should be a positive
11147 * number.
11148 *
11149 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070011150 * @see #getId()
11151 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011152 *
11153 * @param id a number used to identify the view
11154 *
11155 * @attr ref android.R.styleable#View_id
11156 */
11157 public void setId(int id) {
11158 mID = id;
11159 }
11160
11161 /**
11162 * {@hide}
11163 *
11164 * @param isRoot true if the view belongs to the root namespace, false
11165 * otherwise
11166 */
11167 public void setIsRootNamespace(boolean isRoot) {
11168 if (isRoot) {
11169 mPrivateFlags |= IS_ROOT_NAMESPACE;
11170 } else {
11171 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
11172 }
11173 }
11174
11175 /**
11176 * {@hide}
11177 *
11178 * @return true if the view belongs to the root namespace, false otherwise
11179 */
11180 public boolean isRootNamespace() {
11181 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
11182 }
11183
11184 /**
11185 * Returns this view's identifier.
11186 *
11187 * @return a positive integer used to identify the view or {@link #NO_ID}
11188 * if the view has no ID
11189 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011190 * @see #setId(int)
11191 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011192 * @attr ref android.R.styleable#View_id
11193 */
11194 @ViewDebug.CapturedViewProperty
11195 public int getId() {
11196 return mID;
11197 }
11198
11199 /**
11200 * Returns this view's tag.
11201 *
11202 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070011203 *
11204 * @see #setTag(Object)
11205 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011206 */
11207 @ViewDebug.ExportedProperty
11208 public Object getTag() {
11209 return mTag;
11210 }
11211
11212 /**
11213 * Sets the tag associated with this view. A tag can be used to mark
11214 * a view in its hierarchy and does not have to be unique within the
11215 * hierarchy. Tags can also be used to store data within a view without
11216 * resorting to another data structure.
11217 *
11218 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070011219 *
11220 * @see #getTag()
11221 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011222 */
11223 public void setTag(final Object tag) {
11224 mTag = tag;
11225 }
11226
11227 /**
Romain Guyd90a3312009-05-06 14:54:28 -070011228 * Returns the tag associated with this view and the specified key.
11229 *
11230 * @param key The key identifying the tag
11231 *
11232 * @return the Object stored in this view as a tag
11233 *
11234 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070011235 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070011236 */
11237 public Object getTag(int key) {
11238 SparseArray<Object> tags = null;
11239 synchronized (sTagsLock) {
11240 if (sTags != null) {
11241 tags = sTags.get(this);
11242 }
11243 }
11244
11245 if (tags != null) return tags.get(key);
11246 return null;
11247 }
11248
11249 /**
11250 * Sets a tag associated with this view and a key. A tag can be used
11251 * to mark a view in its hierarchy and does not have to be unique within
11252 * the hierarchy. Tags can also be used to store data within a view
11253 * without resorting to another data structure.
11254 *
11255 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070011256 * application to ensure it is unique (see the <a
11257 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
11258 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070011259 * the Android framework or not associated with any package will cause
11260 * an {@link IllegalArgumentException} to be thrown.
11261 *
11262 * @param key The key identifying the tag
11263 * @param tag An Object to tag the view with
11264 *
11265 * @throws IllegalArgumentException If they specified key is not valid
11266 *
11267 * @see #setTag(Object)
11268 * @see #getTag(int)
11269 */
11270 public void setTag(int key, final Object tag) {
11271 // If the package id is 0x00 or 0x01, it's either an undefined package
11272 // or a framework id
11273 if ((key >>> 24) < 2) {
11274 throw new IllegalArgumentException("The key must be an application-specific "
11275 + "resource id.");
11276 }
11277
11278 setTagInternal(this, key, tag);
11279 }
11280
11281 /**
11282 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
11283 * framework id.
11284 *
11285 * @hide
11286 */
11287 public void setTagInternal(int key, Object tag) {
11288 if ((key >>> 24) != 0x1) {
11289 throw new IllegalArgumentException("The key must be a framework-specific "
11290 + "resource id.");
11291 }
11292
Romain Guy8506ab42009-06-11 17:35:47 -070011293 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070011294 }
11295
11296 private static void setTagInternal(View view, int key, Object tag) {
11297 SparseArray<Object> tags = null;
11298 synchronized (sTagsLock) {
11299 if (sTags == null) {
11300 sTags = new WeakHashMap<View, SparseArray<Object>>();
11301 } else {
11302 tags = sTags.get(view);
11303 }
11304 }
11305
11306 if (tags == null) {
11307 tags = new SparseArray<Object>(2);
11308 synchronized (sTagsLock) {
11309 sTags.put(view, tags);
11310 }
11311 }
11312
11313 tags.put(key, tag);
11314 }
11315
11316 /**
Romain Guy13922e02009-05-12 17:56:14 -070011317 * @param consistency The type of consistency. See ViewDebug for more information.
11318 *
11319 * @hide
11320 */
11321 protected boolean dispatchConsistencyCheck(int consistency) {
11322 return onConsistencyCheck(consistency);
11323 }
11324
11325 /**
11326 * Method that subclasses should implement to check their consistency. The type of
11327 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070011328 *
Romain Guy13922e02009-05-12 17:56:14 -070011329 * @param consistency The type of consistency. See ViewDebug for more information.
11330 *
11331 * @throws IllegalStateException if the view is in an inconsistent state.
11332 *
11333 * @hide
11334 */
11335 protected boolean onConsistencyCheck(int consistency) {
11336 boolean result = true;
11337
11338 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
11339 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
11340
11341 if (checkLayout) {
11342 if (getParent() == null) {
11343 result = false;
11344 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11345 "View " + this + " does not have a parent.");
11346 }
11347
11348 if (mAttachInfo == null) {
11349 result = false;
11350 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11351 "View " + this + " is not attached to a window.");
11352 }
11353 }
11354
11355 if (checkDrawing) {
11356 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
11357 // from their draw() method
11358
11359 if ((mPrivateFlags & DRAWN) != DRAWN &&
11360 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
11361 result = false;
11362 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11363 "View " + this + " was invalidated but its drawing cache is valid.");
11364 }
11365 }
11366
11367 return result;
11368 }
11369
11370 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011371 * Prints information about this view in the log output, with the tag
11372 * {@link #VIEW_LOG_TAG}.
11373 *
11374 * @hide
11375 */
11376 public void debug() {
11377 debug(0);
11378 }
11379
11380 /**
11381 * Prints information about this view in the log output, with the tag
11382 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
11383 * indentation defined by the <code>depth</code>.
11384 *
11385 * @param depth the indentation level
11386 *
11387 * @hide
11388 */
11389 protected void debug(int depth) {
11390 String output = debugIndent(depth - 1);
11391
11392 output += "+ " + this;
11393 int id = getId();
11394 if (id != -1) {
11395 output += " (id=" + id + ")";
11396 }
11397 Object tag = getTag();
11398 if (tag != null) {
11399 output += " (tag=" + tag + ")";
11400 }
11401 Log.d(VIEW_LOG_TAG, output);
11402
11403 if ((mPrivateFlags & FOCUSED) != 0) {
11404 output = debugIndent(depth) + " FOCUSED";
11405 Log.d(VIEW_LOG_TAG, output);
11406 }
11407
11408 output = debugIndent(depth);
11409 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
11410 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
11411 + "} ";
11412 Log.d(VIEW_LOG_TAG, output);
11413
11414 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
11415 || mPaddingBottom != 0) {
11416 output = debugIndent(depth);
11417 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
11418 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
11419 Log.d(VIEW_LOG_TAG, output);
11420 }
11421
11422 output = debugIndent(depth);
11423 output += "mMeasureWidth=" + mMeasuredWidth +
11424 " mMeasureHeight=" + mMeasuredHeight;
11425 Log.d(VIEW_LOG_TAG, output);
11426
11427 output = debugIndent(depth);
11428 if (mLayoutParams == null) {
11429 output += "BAD! no layout params";
11430 } else {
11431 output = mLayoutParams.debug(output);
11432 }
11433 Log.d(VIEW_LOG_TAG, output);
11434
11435 output = debugIndent(depth);
11436 output += "flags={";
11437 output += View.printFlags(mViewFlags);
11438 output += "}";
11439 Log.d(VIEW_LOG_TAG, output);
11440
11441 output = debugIndent(depth);
11442 output += "privateFlags={";
11443 output += View.printPrivateFlags(mPrivateFlags);
11444 output += "}";
11445 Log.d(VIEW_LOG_TAG, output);
11446 }
11447
11448 /**
11449 * Creates an string of whitespaces used for indentation.
11450 *
11451 * @param depth the indentation level
11452 * @return a String containing (depth * 2 + 3) * 2 white spaces
11453 *
11454 * @hide
11455 */
11456 protected static String debugIndent(int depth) {
11457 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
11458 for (int i = 0; i < (depth * 2) + 3; i++) {
11459 spaces.append(' ').append(' ');
11460 }
11461 return spaces.toString();
11462 }
11463
11464 /**
11465 * <p>Return the offset of the widget's text baseline from the widget's top
11466 * boundary. If this widget does not support baseline alignment, this
11467 * method returns -1. </p>
11468 *
11469 * @return the offset of the baseline within the widget's bounds or -1
11470 * if baseline alignment is not supported
11471 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011472 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011473 public int getBaseline() {
11474 return -1;
11475 }
11476
11477 /**
11478 * Call this when something has changed which has invalidated the
11479 * layout of this view. This will schedule a layout pass of the view
11480 * tree.
11481 */
11482 public void requestLayout() {
11483 if (ViewDebug.TRACE_HIERARCHY) {
11484 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
11485 }
11486
11487 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011488 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011489
11490 if (mParent != null && !mParent.isLayoutRequested()) {
11491 mParent.requestLayout();
11492 }
11493 }
11494
11495 /**
11496 * Forces this view to be laid out during the next layout pass.
11497 * This method does not call requestLayout() or forceLayout()
11498 * on the parent.
11499 */
11500 public void forceLayout() {
11501 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011502 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011503 }
11504
11505 /**
11506 * <p>
11507 * This is called to find out how big a view should be. The parent
11508 * supplies constraint information in the width and height parameters.
11509 * </p>
11510 *
11511 * <p>
11512 * The actual mesurement work of a view is performed in
11513 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
11514 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
11515 * </p>
11516 *
11517 *
11518 * @param widthMeasureSpec Horizontal space requirements as imposed by the
11519 * parent
11520 * @param heightMeasureSpec Vertical space requirements as imposed by the
11521 * parent
11522 *
11523 * @see #onMeasure(int, int)
11524 */
11525 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
11526 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
11527 widthMeasureSpec != mOldWidthMeasureSpec ||
11528 heightMeasureSpec != mOldHeightMeasureSpec) {
11529
11530 // first clears the measured dimension flag
11531 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
11532
11533 if (ViewDebug.TRACE_HIERARCHY) {
11534 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
11535 }
11536
11537 // measure ourselves, this should set the measured dimension flag back
11538 onMeasure(widthMeasureSpec, heightMeasureSpec);
11539
11540 // flag not set, setMeasuredDimension() was not invoked, we raise
11541 // an exception to warn the developer
11542 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
11543 throw new IllegalStateException("onMeasure() did not set the"
11544 + " measured dimension by calling"
11545 + " setMeasuredDimension()");
11546 }
11547
11548 mPrivateFlags |= LAYOUT_REQUIRED;
11549 }
11550
11551 mOldWidthMeasureSpec = widthMeasureSpec;
11552 mOldHeightMeasureSpec = heightMeasureSpec;
11553 }
11554
11555 /**
11556 * <p>
11557 * Measure the view and its content to determine the measured width and the
11558 * measured height. This method is invoked by {@link #measure(int, int)} and
11559 * should be overriden by subclasses to provide accurate and efficient
11560 * measurement of their contents.
11561 * </p>
11562 *
11563 * <p>
11564 * <strong>CONTRACT:</strong> When overriding this method, you
11565 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
11566 * measured width and height of this view. Failure to do so will trigger an
11567 * <code>IllegalStateException</code>, thrown by
11568 * {@link #measure(int, int)}. Calling the superclass'
11569 * {@link #onMeasure(int, int)} is a valid use.
11570 * </p>
11571 *
11572 * <p>
11573 * The base class implementation of measure defaults to the background size,
11574 * unless a larger size is allowed by the MeasureSpec. Subclasses should
11575 * override {@link #onMeasure(int, int)} to provide better measurements of
11576 * their content.
11577 * </p>
11578 *
11579 * <p>
11580 * If this method is overridden, it is the subclass's responsibility to make
11581 * sure the measured height and width are at least the view's minimum height
11582 * and width ({@link #getSuggestedMinimumHeight()} and
11583 * {@link #getSuggestedMinimumWidth()}).
11584 * </p>
11585 *
11586 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
11587 * The requirements are encoded with
11588 * {@link android.view.View.MeasureSpec}.
11589 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
11590 * The requirements are encoded with
11591 * {@link android.view.View.MeasureSpec}.
11592 *
11593 * @see #getMeasuredWidth()
11594 * @see #getMeasuredHeight()
11595 * @see #setMeasuredDimension(int, int)
11596 * @see #getSuggestedMinimumHeight()
11597 * @see #getSuggestedMinimumWidth()
11598 * @see android.view.View.MeasureSpec#getMode(int)
11599 * @see android.view.View.MeasureSpec#getSize(int)
11600 */
11601 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
11602 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
11603 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
11604 }
11605
11606 /**
11607 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
11608 * measured width and measured height. Failing to do so will trigger an
11609 * exception at measurement time.</p>
11610 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080011611 * @param measuredWidth The measured width of this view. May be a complex
11612 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11613 * {@link #MEASURED_STATE_TOO_SMALL}.
11614 * @param measuredHeight The measured height of this view. May be a complex
11615 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11616 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011617 */
11618 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
11619 mMeasuredWidth = measuredWidth;
11620 mMeasuredHeight = measuredHeight;
11621
11622 mPrivateFlags |= MEASURED_DIMENSION_SET;
11623 }
11624
11625 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080011626 * Merge two states as returned by {@link #getMeasuredState()}.
11627 * @param curState The current state as returned from a view or the result
11628 * of combining multiple views.
11629 * @param newState The new view state to combine.
11630 * @return Returns a new integer reflecting the combination of the two
11631 * states.
11632 */
11633 public static int combineMeasuredStates(int curState, int newState) {
11634 return curState | newState;
11635 }
11636
11637 /**
11638 * Version of {@link #resolveSizeAndState(int, int, int)}
11639 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
11640 */
11641 public static int resolveSize(int size, int measureSpec) {
11642 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
11643 }
11644
11645 /**
11646 * Utility to reconcile a desired size and state, with constraints imposed
11647 * by a MeasureSpec. Will take the desired size, unless a different size
11648 * is imposed by the constraints. The returned value is a compound integer,
11649 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
11650 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
11651 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011652 *
11653 * @param size How big the view wants to be
11654 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080011655 * @return Size information bit mask as defined by
11656 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011657 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080011658 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011659 int result = size;
11660 int specMode = MeasureSpec.getMode(measureSpec);
11661 int specSize = MeasureSpec.getSize(measureSpec);
11662 switch (specMode) {
11663 case MeasureSpec.UNSPECIFIED:
11664 result = size;
11665 break;
11666 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080011667 if (specSize < size) {
11668 result = specSize | MEASURED_STATE_TOO_SMALL;
11669 } else {
11670 result = size;
11671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011672 break;
11673 case MeasureSpec.EXACTLY:
11674 result = specSize;
11675 break;
11676 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080011677 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011678 }
11679
11680 /**
11681 * Utility to return a default size. Uses the supplied size if the
11682 * MeasureSpec imposed no contraints. Will get larger if allowed
11683 * by the MeasureSpec.
11684 *
11685 * @param size Default size for this view
11686 * @param measureSpec Constraints imposed by the parent
11687 * @return The size this view should be.
11688 */
11689 public static int getDefaultSize(int size, int measureSpec) {
11690 int result = size;
11691 int specMode = MeasureSpec.getMode(measureSpec);
11692 int specSize = MeasureSpec.getSize(measureSpec);
11693
11694 switch (specMode) {
11695 case MeasureSpec.UNSPECIFIED:
11696 result = size;
11697 break;
11698 case MeasureSpec.AT_MOST:
11699 case MeasureSpec.EXACTLY:
11700 result = specSize;
11701 break;
11702 }
11703 return result;
11704 }
11705
11706 /**
11707 * Returns the suggested minimum height that the view should use. This
11708 * returns the maximum of the view's minimum height
11709 * and the background's minimum height
11710 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
11711 * <p>
11712 * When being used in {@link #onMeasure(int, int)}, the caller should still
11713 * ensure the returned height is within the requirements of the parent.
11714 *
11715 * @return The suggested minimum height of the view.
11716 */
11717 protected int getSuggestedMinimumHeight() {
11718 int suggestedMinHeight = mMinHeight;
11719
11720 if (mBGDrawable != null) {
11721 final int bgMinHeight = mBGDrawable.getMinimumHeight();
11722 if (suggestedMinHeight < bgMinHeight) {
11723 suggestedMinHeight = bgMinHeight;
11724 }
11725 }
11726
11727 return suggestedMinHeight;
11728 }
11729
11730 /**
11731 * Returns the suggested minimum width that the view should use. This
11732 * returns the maximum of the view's minimum width)
11733 * and the background's minimum width
11734 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
11735 * <p>
11736 * When being used in {@link #onMeasure(int, int)}, the caller should still
11737 * ensure the returned width is within the requirements of the parent.
11738 *
11739 * @return The suggested minimum width of the view.
11740 */
11741 protected int getSuggestedMinimumWidth() {
11742 int suggestedMinWidth = mMinWidth;
11743
11744 if (mBGDrawable != null) {
11745 final int bgMinWidth = mBGDrawable.getMinimumWidth();
11746 if (suggestedMinWidth < bgMinWidth) {
11747 suggestedMinWidth = bgMinWidth;
11748 }
11749 }
11750
11751 return suggestedMinWidth;
11752 }
11753
11754 /**
11755 * Sets the minimum height of the view. It is not guaranteed the view will
11756 * be able to achieve this minimum height (for example, if its parent layout
11757 * constrains it with less available height).
11758 *
11759 * @param minHeight The minimum height the view will try to be.
11760 */
11761 public void setMinimumHeight(int minHeight) {
11762 mMinHeight = minHeight;
11763 }
11764
11765 /**
11766 * Sets the minimum width of the view. It is not guaranteed the view will
11767 * be able to achieve this minimum width (for example, if its parent layout
11768 * constrains it with less available width).
11769 *
11770 * @param minWidth The minimum width the view will try to be.
11771 */
11772 public void setMinimumWidth(int minWidth) {
11773 mMinWidth = minWidth;
11774 }
11775
11776 /**
11777 * Get the animation currently associated with this view.
11778 *
11779 * @return The animation that is currently playing or
11780 * scheduled to play for this view.
11781 */
11782 public Animation getAnimation() {
11783 return mCurrentAnimation;
11784 }
11785
11786 /**
11787 * Start the specified animation now.
11788 *
11789 * @param animation the animation to start now
11790 */
11791 public void startAnimation(Animation animation) {
11792 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
11793 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011794 invalidateParentCaches();
11795 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011796 }
11797
11798 /**
11799 * Cancels any animations for this view.
11800 */
11801 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080011802 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080011803 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080011804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011805 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011806 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011807 }
11808
11809 /**
11810 * Sets the next animation to play for this view.
11811 * If you want the animation to play immediately, use
11812 * startAnimation. This method provides allows fine-grained
11813 * control over the start time and invalidation, but you
11814 * must make sure that 1) the animation has a start time set, and
11815 * 2) the view will be invalidated when the animation is supposed to
11816 * start.
11817 *
11818 * @param animation The next animation, or null.
11819 */
11820 public void setAnimation(Animation animation) {
11821 mCurrentAnimation = animation;
11822 if (animation != null) {
11823 animation.reset();
11824 }
11825 }
11826
11827 /**
11828 * Invoked by a parent ViewGroup to notify the start of the animation
11829 * currently associated with this view. If you override this method,
11830 * always call super.onAnimationStart();
11831 *
11832 * @see #setAnimation(android.view.animation.Animation)
11833 * @see #getAnimation()
11834 */
11835 protected void onAnimationStart() {
11836 mPrivateFlags |= ANIMATION_STARTED;
11837 }
11838
11839 /**
11840 * Invoked by a parent ViewGroup to notify the end of the animation
11841 * currently associated with this view. If you override this method,
11842 * always call super.onAnimationEnd();
11843 *
11844 * @see #setAnimation(android.view.animation.Animation)
11845 * @see #getAnimation()
11846 */
11847 protected void onAnimationEnd() {
11848 mPrivateFlags &= ~ANIMATION_STARTED;
11849 }
11850
11851 /**
11852 * Invoked if there is a Transform that involves alpha. Subclass that can
11853 * draw themselves with the specified alpha should return true, and then
11854 * respect that alpha when their onDraw() is called. If this returns false
11855 * then the view may be redirected to draw into an offscreen buffer to
11856 * fulfill the request, which will look fine, but may be slower than if the
11857 * subclass handles it internally. The default implementation returns false.
11858 *
11859 * @param alpha The alpha (0..255) to apply to the view's drawing
11860 * @return true if the view can draw with the specified alpha.
11861 */
11862 protected boolean onSetAlpha(int alpha) {
11863 return false;
11864 }
11865
11866 /**
11867 * This is used by the RootView to perform an optimization when
11868 * the view hierarchy contains one or several SurfaceView.
11869 * SurfaceView is always considered transparent, but its children are not,
11870 * therefore all View objects remove themselves from the global transparent
11871 * region (passed as a parameter to this function).
11872 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011873 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011874 *
11875 * @return Returns true if the effective visibility of the view at this
11876 * point is opaque, regardless of the transparent region; returns false
11877 * if it is possible for underlying windows to be seen behind the view.
11878 *
11879 * {@hide}
11880 */
11881 public boolean gatherTransparentRegion(Region region) {
11882 final AttachInfo attachInfo = mAttachInfo;
11883 if (region != null && attachInfo != null) {
11884 final int pflags = mPrivateFlags;
11885 if ((pflags & SKIP_DRAW) == 0) {
11886 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
11887 // remove it from the transparent region.
11888 final int[] location = attachInfo.mTransparentLocation;
11889 getLocationInWindow(location);
11890 region.op(location[0], location[1], location[0] + mRight - mLeft,
11891 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
11892 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
11893 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
11894 // exists, so we remove the background drawable's non-transparent
11895 // parts from this transparent region.
11896 applyDrawableToTransparentRegion(mBGDrawable, region);
11897 }
11898 }
11899 return true;
11900 }
11901
11902 /**
11903 * Play a sound effect for this view.
11904 *
11905 * <p>The framework will play sound effects for some built in actions, such as
11906 * clicking, but you may wish to play these effects in your widget,
11907 * for instance, for internal navigation.
11908 *
11909 * <p>The sound effect will only be played if sound effects are enabled by the user, and
11910 * {@link #isSoundEffectsEnabled()} is true.
11911 *
11912 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
11913 */
11914 public void playSoundEffect(int soundConstant) {
11915 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
11916 return;
11917 }
11918 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
11919 }
11920
11921 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011922 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011923 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011924 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011925 *
11926 * <p>The framework will provide haptic feedback for some built in actions,
11927 * such as long presses, but you may wish to provide feedback for your
11928 * own widget.
11929 *
11930 * <p>The feedback will only be performed if
11931 * {@link #isHapticFeedbackEnabled()} is true.
11932 *
11933 * @param feedbackConstant One of the constants defined in
11934 * {@link HapticFeedbackConstants}
11935 */
11936 public boolean performHapticFeedback(int feedbackConstant) {
11937 return performHapticFeedback(feedbackConstant, 0);
11938 }
11939
11940 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011941 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011942 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011943 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011944 *
11945 * @param feedbackConstant One of the constants defined in
11946 * {@link HapticFeedbackConstants}
11947 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
11948 */
11949 public boolean performHapticFeedback(int feedbackConstant, int flags) {
11950 if (mAttachInfo == null) {
11951 return false;
11952 }
Romain Guyf607bdc2010-09-10 19:20:06 -070011953 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070011954 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011955 && !isHapticFeedbackEnabled()) {
11956 return false;
11957 }
Romain Guy812ccbe2010-06-01 14:07:24 -070011958 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
11959 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011960 }
11961
11962 /**
Joe Onorato664644d2011-01-23 17:53:23 -080011963 * Request that the visibility of the status bar be changed.
Scott Mainec6331b2011-05-24 16:55:56 -070011964 * @param visibility Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080011965 */
11966 public void setSystemUiVisibility(int visibility) {
11967 if (visibility != mSystemUiVisibility) {
11968 mSystemUiVisibility = visibility;
11969 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11970 mParent.recomputeViewAttributes(this);
11971 }
11972 }
11973 }
11974
11975 /**
11976 * Returns the status bar visibility that this view has requested.
Scott Mainec6331b2011-05-24 16:55:56 -070011977 * @return Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080011978 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080011979 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080011980 return mSystemUiVisibility;
11981 }
11982
Scott Mainec6331b2011-05-24 16:55:56 -070011983 /**
11984 * Set a listener to receive callbacks when the visibility of the system bar changes.
11985 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
11986 */
Joe Onorato664644d2011-01-23 17:53:23 -080011987 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
11988 mOnSystemUiVisibilityChangeListener = l;
11989 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11990 mParent.recomputeViewAttributes(this);
11991 }
11992 }
11993
11994 /**
11995 */
11996 public void dispatchSystemUiVisibilityChanged(int visibility) {
11997 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080011998 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080011999 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080012000 }
12001 }
12002
12003 /**
Joe Malin32736f02011-01-19 16:14:20 -080012004 * Creates an image that the system displays during the drag and drop
12005 * operation. This is called a &quot;drag shadow&quot;. The default implementation
12006 * for a DragShadowBuilder based on a View returns an image that has exactly the same
12007 * appearance as the given View. The default also positions the center of the drag shadow
12008 * directly under the touch point. If no View is provided (the constructor with no parameters
12009 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
12010 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
12011 * default is an invisible drag shadow.
12012 * <p>
12013 * You are not required to use the View you provide to the constructor as the basis of the
12014 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
12015 * anything you want as the drag shadow.
12016 * </p>
12017 * <p>
12018 * You pass a DragShadowBuilder object to the system when you start the drag. The system
12019 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
12020 * size and position of the drag shadow. It uses this data to construct a
12021 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
12022 * so that your application can draw the shadow image in the Canvas.
12023 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012024 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012025 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070012026 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070012027
12028 /**
Joe Malin32736f02011-01-19 16:14:20 -080012029 * Constructs a shadow image builder based on a View. By default, the resulting drag
12030 * shadow will have the same appearance and dimensions as the View, with the touch point
12031 * over the center of the View.
12032 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070012033 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012034 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070012035 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070012036 }
12037
Christopher Tate17ed60c2011-01-18 12:50:26 -080012038 /**
12039 * Construct a shadow builder object with no associated View. This
12040 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
12041 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
12042 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080012043 * reference to any View object. If they are not overridden, then the result is an
12044 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080012045 */
12046 public DragShadowBuilder() {
12047 mView = new WeakReference<View>(null);
12048 }
12049
12050 /**
12051 * Returns the View object that had been passed to the
12052 * {@link #View.DragShadowBuilder(View)}
12053 * constructor. If that View parameter was {@code null} or if the
12054 * {@link #View.DragShadowBuilder()}
12055 * constructor was used to instantiate the builder object, this method will return
12056 * null.
12057 *
12058 * @return The View object associate with this builder object.
12059 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070012060 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070012061 final public View getView() {
12062 return mView.get();
12063 }
12064
Christopher Tate2c095f32010-10-04 14:13:40 -070012065 /**
Joe Malin32736f02011-01-19 16:14:20 -080012066 * Provides the metrics for the shadow image. These include the dimensions of
12067 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070012068 * be centered under the touch location while dragging.
12069 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012070 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080012071 * same as the dimensions of the View itself and centers the shadow under
12072 * the touch point.
12073 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012074 *
Joe Malin32736f02011-01-19 16:14:20 -080012075 * @param shadowSize A {@link android.graphics.Point} containing the width and height
12076 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
12077 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
12078 * image.
12079 *
12080 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
12081 * shadow image that should be underneath the touch point during the drag and drop
12082 * operation. Your application must set {@link android.graphics.Point#x} to the
12083 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070012084 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012085 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070012086 final View view = mView.get();
12087 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012088 shadowSize.set(view.getWidth(), view.getHeight());
12089 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070012090 } else {
12091 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
12092 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012093 }
12094
12095 /**
Joe Malin32736f02011-01-19 16:14:20 -080012096 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
12097 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012098 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070012099 *
Joe Malin32736f02011-01-19 16:14:20 -080012100 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070012101 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012102 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070012103 final View view = mView.get();
12104 if (view != null) {
12105 view.draw(canvas);
12106 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012107 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070012108 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012109 }
12110 }
12111
12112 /**
Joe Malin32736f02011-01-19 16:14:20 -080012113 * Starts a drag and drop operation. When your application calls this method, it passes a
12114 * {@link android.view.View.DragShadowBuilder} object to the system. The
12115 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
12116 * to get metrics for the drag shadow, and then calls the object's
12117 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
12118 * <p>
12119 * Once the system has the drag shadow, it begins the drag and drop operation by sending
12120 * drag events to all the View objects in your application that are currently visible. It does
12121 * this either by calling the View object's drag listener (an implementation of
12122 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
12123 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
12124 * Both are passed a {@link android.view.DragEvent} object that has a
12125 * {@link android.view.DragEvent#getAction()} value of
12126 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
12127 * </p>
12128 * <p>
12129 * Your application can invoke startDrag() on any attached View object. The View object does not
12130 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
12131 * be related to the View the user selected for dragging.
12132 * </p>
12133 * @param data A {@link android.content.ClipData} object pointing to the data to be
12134 * transferred by the drag and drop operation.
12135 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
12136 * drag shadow.
12137 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
12138 * drop operation. This Object is put into every DragEvent object sent by the system during the
12139 * current drag.
12140 * <p>
12141 * myLocalState is a lightweight mechanism for the sending information from the dragged View
12142 * to the target Views. For example, it can contain flags that differentiate between a
12143 * a copy operation and a move operation.
12144 * </p>
12145 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
12146 * so the parameter should be set to 0.
12147 * @return {@code true} if the method completes successfully, or
12148 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
12149 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070012150 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012151 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012152 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070012153 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012154 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070012155 }
12156 boolean okay = false;
12157
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012158 Point shadowSize = new Point();
12159 Point shadowTouchPoint = new Point();
12160 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070012161
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012162 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
12163 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
12164 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070012165 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012166
Chris Tatea32dcf72010-10-14 12:13:50 -070012167 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012168 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
12169 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070012170 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012171 Surface surface = new Surface();
12172 try {
12173 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012174 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070012175 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070012176 + " surface=" + surface);
12177 if (token != null) {
12178 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070012179 try {
Chris Tate6b391282010-10-14 15:48:59 -070012180 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012181 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070012182 } finally {
12183 surface.unlockCanvasAndPost(canvas);
12184 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012185
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012186 final ViewAncestor root = getViewAncestor();
Christopher Tate407b4e92010-11-30 17:14:08 -080012187
12188 // Cache the local state object for delivery with DragEvents
12189 root.setLocalDragState(myLocalState);
12190
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012191 // repurpose 'shadowSize' for the last touch point
12192 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070012193
Christopher Tatea53146c2010-09-07 11:57:52 -070012194 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012195 shadowSize.x, shadowSize.y,
12196 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070012197 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070012198 }
12199 } catch (Exception e) {
12200 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
12201 surface.destroy();
12202 }
12203
12204 return okay;
12205 }
12206
Christopher Tatea53146c2010-09-07 11:57:52 -070012207 /**
Joe Malin32736f02011-01-19 16:14:20 -080012208 * Handles drag events sent by the system following a call to
12209 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
12210 *<p>
12211 * When the system calls this method, it passes a
12212 * {@link android.view.DragEvent} object. A call to
12213 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
12214 * in DragEvent. The method uses these to determine what is happening in the drag and drop
12215 * operation.
12216 * @param event The {@link android.view.DragEvent} sent by the system.
12217 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
12218 * in DragEvent, indicating the type of drag event represented by this object.
12219 * @return {@code true} if the method was successful, otherwise {@code false}.
12220 * <p>
12221 * The method should return {@code true} in response to an action type of
12222 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
12223 * operation.
12224 * </p>
12225 * <p>
12226 * The method should also return {@code true} in response to an action type of
12227 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
12228 * {@code false} if it didn't.
12229 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012230 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070012231 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070012232 return false;
12233 }
12234
12235 /**
Joe Malin32736f02011-01-19 16:14:20 -080012236 * Detects if this View is enabled and has a drag event listener.
12237 * If both are true, then it calls the drag event listener with the
12238 * {@link android.view.DragEvent} it received. If the drag event listener returns
12239 * {@code true}, then dispatchDragEvent() returns {@code true}.
12240 * <p>
12241 * For all other cases, the method calls the
12242 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
12243 * method and returns its result.
12244 * </p>
12245 * <p>
12246 * This ensures that a drag event is always consumed, even if the View does not have a drag
12247 * event listener. However, if the View has a listener and the listener returns true, then
12248 * onDragEvent() is not called.
12249 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012250 */
12251 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080012252 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070012253 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
12254 && mOnDragListener.onDrag(this, event)) {
12255 return true;
12256 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012257 return onDragEvent(event);
12258 }
12259
Christopher Tate3d4bf172011-03-28 16:16:46 -070012260 boolean canAcceptDrag() {
12261 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
12262 }
12263
Christopher Tatea53146c2010-09-07 11:57:52 -070012264 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070012265 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
12266 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070012267 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070012268 */
12269 public void onCloseSystemDialogs(String reason) {
12270 }
Joe Malin32736f02011-01-19 16:14:20 -080012271
Dianne Hackbornffa42482009-09-23 22:20:11 -070012272 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012273 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070012274 * update a Region being computed for
12275 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012276 * that any non-transparent parts of the Drawable are removed from the
12277 * given transparent region.
12278 *
12279 * @param dr The Drawable whose transparency is to be applied to the region.
12280 * @param region A Region holding the current transparency information,
12281 * where any parts of the region that are set are considered to be
12282 * transparent. On return, this region will be modified to have the
12283 * transparency information reduced by the corresponding parts of the
12284 * Drawable that are not transparent.
12285 * {@hide}
12286 */
12287 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
12288 if (DBG) {
12289 Log.i("View", "Getting transparent region for: " + this);
12290 }
12291 final Region r = dr.getTransparentRegion();
12292 final Rect db = dr.getBounds();
12293 final AttachInfo attachInfo = mAttachInfo;
12294 if (r != null && attachInfo != null) {
12295 final int w = getRight()-getLeft();
12296 final int h = getBottom()-getTop();
12297 if (db.left > 0) {
12298 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
12299 r.op(0, 0, db.left, h, Region.Op.UNION);
12300 }
12301 if (db.right < w) {
12302 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
12303 r.op(db.right, 0, w, h, Region.Op.UNION);
12304 }
12305 if (db.top > 0) {
12306 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
12307 r.op(0, 0, w, db.top, Region.Op.UNION);
12308 }
12309 if (db.bottom < h) {
12310 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
12311 r.op(0, db.bottom, w, h, Region.Op.UNION);
12312 }
12313 final int[] location = attachInfo.mTransparentLocation;
12314 getLocationInWindow(location);
12315 r.translate(location[0], location[1]);
12316 region.op(r, Region.Op.INTERSECT);
12317 } else {
12318 region.op(db, Region.Op.DIFFERENCE);
12319 }
12320 }
12321
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012322 private void checkForLongClick(int delayOffset) {
12323 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
12324 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012325
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012326 if (mPendingCheckForLongPress == null) {
12327 mPendingCheckForLongPress = new CheckForLongPress();
12328 }
12329 mPendingCheckForLongPress.rememberWindowAttachCount();
12330 postDelayed(mPendingCheckForLongPress,
12331 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012332 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012333 }
12334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012335 /**
12336 * Inflate a view from an XML resource. This convenience method wraps the {@link
12337 * LayoutInflater} class, which provides a full range of options for view inflation.
12338 *
12339 * @param context The Context object for your activity or application.
12340 * @param resource The resource ID to inflate
12341 * @param root A view group that will be the parent. Used to properly inflate the
12342 * layout_* parameters.
12343 * @see LayoutInflater
12344 */
12345 public static View inflate(Context context, int resource, ViewGroup root) {
12346 LayoutInflater factory = LayoutInflater.from(context);
12347 return factory.inflate(resource, root);
12348 }
Romain Guy33e72ae2010-07-17 12:40:29 -070012349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012350 /**
Adam Powell637d3372010-08-25 14:37:03 -070012351 * Scroll the view with standard behavior for scrolling beyond the normal
12352 * content boundaries. Views that call this method should override
12353 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
12354 * results of an over-scroll operation.
12355 *
12356 * Views can use this method to handle any touch or fling-based scrolling.
12357 *
12358 * @param deltaX Change in X in pixels
12359 * @param deltaY Change in Y in pixels
12360 * @param scrollX Current X scroll value in pixels before applying deltaX
12361 * @param scrollY Current Y scroll value in pixels before applying deltaY
12362 * @param scrollRangeX Maximum content scroll range along the X axis
12363 * @param scrollRangeY Maximum content scroll range along the Y axis
12364 * @param maxOverScrollX Number of pixels to overscroll by in either direction
12365 * along the X axis.
12366 * @param maxOverScrollY Number of pixels to overscroll by in either direction
12367 * along the Y axis.
12368 * @param isTouchEvent true if this scroll operation is the result of a touch event.
12369 * @return true if scrolling was clamped to an over-scroll boundary along either
12370 * axis, false otherwise.
12371 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012372 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070012373 protected boolean overScrollBy(int deltaX, int deltaY,
12374 int scrollX, int scrollY,
12375 int scrollRangeX, int scrollRangeY,
12376 int maxOverScrollX, int maxOverScrollY,
12377 boolean isTouchEvent) {
12378 final int overScrollMode = mOverScrollMode;
12379 final boolean canScrollHorizontal =
12380 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
12381 final boolean canScrollVertical =
12382 computeVerticalScrollRange() > computeVerticalScrollExtent();
12383 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
12384 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
12385 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
12386 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
12387
12388 int newScrollX = scrollX + deltaX;
12389 if (!overScrollHorizontal) {
12390 maxOverScrollX = 0;
12391 }
12392
12393 int newScrollY = scrollY + deltaY;
12394 if (!overScrollVertical) {
12395 maxOverScrollY = 0;
12396 }
12397
12398 // Clamp values if at the limits and record
12399 final int left = -maxOverScrollX;
12400 final int right = maxOverScrollX + scrollRangeX;
12401 final int top = -maxOverScrollY;
12402 final int bottom = maxOverScrollY + scrollRangeY;
12403
12404 boolean clampedX = false;
12405 if (newScrollX > right) {
12406 newScrollX = right;
12407 clampedX = true;
12408 } else if (newScrollX < left) {
12409 newScrollX = left;
12410 clampedX = true;
12411 }
12412
12413 boolean clampedY = false;
12414 if (newScrollY > bottom) {
12415 newScrollY = bottom;
12416 clampedY = true;
12417 } else if (newScrollY < top) {
12418 newScrollY = top;
12419 clampedY = true;
12420 }
12421
12422 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
12423
12424 return clampedX || clampedY;
12425 }
12426
12427 /**
12428 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
12429 * respond to the results of an over-scroll operation.
12430 *
12431 * @param scrollX New X scroll value in pixels
12432 * @param scrollY New Y scroll value in pixels
12433 * @param clampedX True if scrollX was clamped to an over-scroll boundary
12434 * @param clampedY True if scrollY was clamped to an over-scroll boundary
12435 */
12436 protected void onOverScrolled(int scrollX, int scrollY,
12437 boolean clampedX, boolean clampedY) {
12438 // Intentionally empty.
12439 }
12440
12441 /**
12442 * Returns the over-scroll mode for this view. The result will be
12443 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12444 * (allow over-scrolling only if the view content is larger than the container),
12445 * or {@link #OVER_SCROLL_NEVER}.
12446 *
12447 * @return This view's over-scroll mode.
12448 */
12449 public int getOverScrollMode() {
12450 return mOverScrollMode;
12451 }
12452
12453 /**
12454 * Set the over-scroll mode for this view. Valid over-scroll modes are
12455 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12456 * (allow over-scrolling only if the view content is larger than the container),
12457 * or {@link #OVER_SCROLL_NEVER}.
12458 *
12459 * Setting the over-scroll mode of a view will have an effect only if the
12460 * view is capable of scrolling.
12461 *
12462 * @param overScrollMode The new over-scroll mode for this view.
12463 */
12464 public void setOverScrollMode(int overScrollMode) {
12465 if (overScrollMode != OVER_SCROLL_ALWAYS &&
12466 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
12467 overScrollMode != OVER_SCROLL_NEVER) {
12468 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
12469 }
12470 mOverScrollMode = overScrollMode;
12471 }
12472
12473 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012474 * Gets a scale factor that determines the distance the view should scroll
12475 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
12476 * @return The vertical scroll scale factor.
12477 * @hide
12478 */
12479 protected float getVerticalScrollFactor() {
12480 if (mVerticalScrollFactor == 0) {
12481 TypedValue outValue = new TypedValue();
12482 if (!mContext.getTheme().resolveAttribute(
12483 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
12484 throw new IllegalStateException(
12485 "Expected theme to define listPreferredItemHeight.");
12486 }
12487 mVerticalScrollFactor = outValue.getDimension(
12488 mContext.getResources().getDisplayMetrics());
12489 }
12490 return mVerticalScrollFactor;
12491 }
12492
12493 /**
12494 * Gets a scale factor that determines the distance the view should scroll
12495 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
12496 * @return The horizontal scroll scale factor.
12497 * @hide
12498 */
12499 protected float getHorizontalScrollFactor() {
12500 // TODO: Should use something else.
12501 return getVerticalScrollFactor();
12502 }
12503
Chet Haaseb39f0512011-05-24 14:36:40 -070012504 //
12505 // Properties
12506 //
12507 /**
12508 * A Property wrapper around the <code>alpha</code> functionality handled by the
12509 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
12510 */
12511 static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
12512 @Override
12513 public void setValue(View object, float value) {
12514 object.setAlpha(value);
12515 }
12516
12517 @Override
12518 public Float get(View object) {
12519 return object.getAlpha();
12520 }
12521 };
12522
12523 /**
12524 * A Property wrapper around the <code>translationX</code> functionality handled by the
12525 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
12526 */
12527 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
12528 @Override
12529 public void setValue(View object, float value) {
12530 object.setTranslationX(value);
12531 }
12532
12533 @Override
12534 public Float get(View object) {
12535 return object.getTranslationX();
12536 }
12537 };
12538
12539 /**
12540 * A Property wrapper around the <code>translationY</code> functionality handled by the
12541 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
12542 */
12543 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
12544 @Override
12545 public void setValue(View object, float value) {
12546 object.setTranslationY(value);
12547 }
12548
12549 @Override
12550 public Float get(View object) {
12551 return object.getTranslationY();
12552 }
12553 };
12554
12555 /**
12556 * A Property wrapper around the <code>x</code> functionality handled by the
12557 * {@link View#setX(float)} and {@link View#getX()} methods.
12558 */
12559 public static Property<View, Float> X = new FloatProperty<View>("x") {
12560 @Override
12561 public void setValue(View object, float value) {
12562 object.setX(value);
12563 }
12564
12565 @Override
12566 public Float get(View object) {
12567 return object.getX();
12568 }
12569 };
12570
12571 /**
12572 * A Property wrapper around the <code>y</code> functionality handled by the
12573 * {@link View#setY(float)} and {@link View#getY()} methods.
12574 */
12575 public static Property<View, Float> Y = new FloatProperty<View>("y") {
12576 @Override
12577 public void setValue(View object, float value) {
12578 object.setY(value);
12579 }
12580
12581 @Override
12582 public Float get(View object) {
12583 return object.getY();
12584 }
12585 };
12586
12587 /**
12588 * A Property wrapper around the <code>rotation</code> functionality handled by the
12589 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
12590 */
12591 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
12592 @Override
12593 public void setValue(View object, float value) {
12594 object.setRotation(value);
12595 }
12596
12597 @Override
12598 public Float get(View object) {
12599 return object.getRotation();
12600 }
12601 };
12602
12603 /**
12604 * A Property wrapper around the <code>rotationX</code> functionality handled by the
12605 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
12606 */
12607 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
12608 @Override
12609 public void setValue(View object, float value) {
12610 object.setRotationX(value);
12611 }
12612
12613 @Override
12614 public Float get(View object) {
12615 return object.getRotationX();
12616 }
12617 };
12618
12619 /**
12620 * A Property wrapper around the <code>rotationY</code> functionality handled by the
12621 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
12622 */
12623 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
12624 @Override
12625 public void setValue(View object, float value) {
12626 object.setRotationY(value);
12627 }
12628
12629 @Override
12630 public Float get(View object) {
12631 return object.getRotationY();
12632 }
12633 };
12634
12635 /**
12636 * A Property wrapper around the <code>scaleX</code> functionality handled by the
12637 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
12638 */
12639 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
12640 @Override
12641 public void setValue(View object, float value) {
12642 object.setScaleX(value);
12643 }
12644
12645 @Override
12646 public Float get(View object) {
12647 return object.getScaleX();
12648 }
12649 };
12650
12651 /**
12652 * A Property wrapper around the <code>scaleY</code> functionality handled by the
12653 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
12654 */
12655 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
12656 @Override
12657 public void setValue(View object, float value) {
12658 object.setScaleY(value);
12659 }
12660
12661 @Override
12662 public Float get(View object) {
12663 return object.getScaleY();
12664 }
12665 };
12666
Jeff Brown33bbfd22011-02-24 20:55:35 -080012667 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012668 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
12669 * Each MeasureSpec represents a requirement for either the width or the height.
12670 * A MeasureSpec is comprised of a size and a mode. There are three possible
12671 * modes:
12672 * <dl>
12673 * <dt>UNSPECIFIED</dt>
12674 * <dd>
12675 * The parent has not imposed any constraint on the child. It can be whatever size
12676 * it wants.
12677 * </dd>
12678 *
12679 * <dt>EXACTLY</dt>
12680 * <dd>
12681 * The parent has determined an exact size for the child. The child is going to be
12682 * given those bounds regardless of how big it wants to be.
12683 * </dd>
12684 *
12685 * <dt>AT_MOST</dt>
12686 * <dd>
12687 * The child can be as large as it wants up to the specified size.
12688 * </dd>
12689 * </dl>
12690 *
12691 * MeasureSpecs are implemented as ints to reduce object allocation. This class
12692 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
12693 */
12694 public static class MeasureSpec {
12695 private static final int MODE_SHIFT = 30;
12696 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
12697
12698 /**
12699 * Measure specification mode: The parent has not imposed any constraint
12700 * on the child. It can be whatever size it wants.
12701 */
12702 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
12703
12704 /**
12705 * Measure specification mode: The parent has determined an exact size
12706 * for the child. The child is going to be given those bounds regardless
12707 * of how big it wants to be.
12708 */
12709 public static final int EXACTLY = 1 << MODE_SHIFT;
12710
12711 /**
12712 * Measure specification mode: The child can be as large as it wants up
12713 * to the specified size.
12714 */
12715 public static final int AT_MOST = 2 << MODE_SHIFT;
12716
12717 /**
12718 * Creates a measure specification based on the supplied size and mode.
12719 *
12720 * The mode must always be one of the following:
12721 * <ul>
12722 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
12723 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
12724 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
12725 * </ul>
12726 *
12727 * @param size the size of the measure specification
12728 * @param mode the mode of the measure specification
12729 * @return the measure specification based on size and mode
12730 */
12731 public static int makeMeasureSpec(int size, int mode) {
12732 return size + mode;
12733 }
12734
12735 /**
12736 * Extracts the mode from the supplied measure specification.
12737 *
12738 * @param measureSpec the measure specification to extract the mode from
12739 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
12740 * {@link android.view.View.MeasureSpec#AT_MOST} or
12741 * {@link android.view.View.MeasureSpec#EXACTLY}
12742 */
12743 public static int getMode(int measureSpec) {
12744 return (measureSpec & MODE_MASK);
12745 }
12746
12747 /**
12748 * Extracts the size from the supplied measure specification.
12749 *
12750 * @param measureSpec the measure specification to extract the size from
12751 * @return the size in pixels defined in the supplied measure specification
12752 */
12753 public static int getSize(int measureSpec) {
12754 return (measureSpec & ~MODE_MASK);
12755 }
12756
12757 /**
12758 * Returns a String representation of the specified measure
12759 * specification.
12760 *
12761 * @param measureSpec the measure specification to convert to a String
12762 * @return a String with the following format: "MeasureSpec: MODE SIZE"
12763 */
12764 public static String toString(int measureSpec) {
12765 int mode = getMode(measureSpec);
12766 int size = getSize(measureSpec);
12767
12768 StringBuilder sb = new StringBuilder("MeasureSpec: ");
12769
12770 if (mode == UNSPECIFIED)
12771 sb.append("UNSPECIFIED ");
12772 else if (mode == EXACTLY)
12773 sb.append("EXACTLY ");
12774 else if (mode == AT_MOST)
12775 sb.append("AT_MOST ");
12776 else
12777 sb.append(mode).append(" ");
12778
12779 sb.append(size);
12780 return sb.toString();
12781 }
12782 }
12783
12784 class CheckForLongPress implements Runnable {
12785
12786 private int mOriginalWindowAttachCount;
12787
12788 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070012789 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012790 && mOriginalWindowAttachCount == mWindowAttachCount) {
12791 if (performLongClick()) {
12792 mHasPerformedLongPress = true;
12793 }
12794 }
12795 }
12796
12797 public void rememberWindowAttachCount() {
12798 mOriginalWindowAttachCount = mWindowAttachCount;
12799 }
12800 }
Joe Malin32736f02011-01-19 16:14:20 -080012801
Adam Powelle14579b2009-12-16 18:39:52 -080012802 private final class CheckForTap implements Runnable {
12803 public void run() {
12804 mPrivateFlags &= ~PREPRESSED;
12805 mPrivateFlags |= PRESSED;
12806 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012807 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080012808 }
12809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012810
Adam Powella35d7682010-03-12 14:48:13 -080012811 private final class PerformClick implements Runnable {
12812 public void run() {
12813 performClick();
12814 }
12815 }
12816
Dianne Hackborn63042d62011-01-26 18:56:29 -080012817 /** @hide */
12818 public void hackTurnOffWindowResizeAnim(boolean off) {
12819 mAttachInfo.mTurnOffWindowResizeAnim = off;
12820 }
Joe Malin32736f02011-01-19 16:14:20 -080012821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012822 /**
Chet Haasea00f3862011-02-22 06:34:40 -080012823 * This method returns a ViewPropertyAnimator object, which can be used to animate
12824 * specific properties on this View.
12825 *
12826 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
12827 */
12828 public ViewPropertyAnimator animate() {
12829 if (mAnimator == null) {
12830 mAnimator = new ViewPropertyAnimator(this);
12831 }
12832 return mAnimator;
12833 }
12834
12835 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012836 * Interface definition for a callback to be invoked when a key event is
12837 * dispatched to this view. The callback will be invoked before the key
12838 * event is given to the view.
12839 */
12840 public interface OnKeyListener {
12841 /**
12842 * Called when a key is dispatched to a view. This allows listeners to
12843 * get a chance to respond before the target view.
12844 *
12845 * @param v The view the key has been dispatched to.
12846 * @param keyCode The code for the physical key that was pressed
12847 * @param event The KeyEvent object containing full information about
12848 * the event.
12849 * @return True if the listener has consumed the event, false otherwise.
12850 */
12851 boolean onKey(View v, int keyCode, KeyEvent event);
12852 }
12853
12854 /**
12855 * Interface definition for a callback to be invoked when a touch event is
12856 * dispatched to this view. The callback will be invoked before the touch
12857 * event is given to the view.
12858 */
12859 public interface OnTouchListener {
12860 /**
12861 * Called when a touch event is dispatched to a view. This allows listeners to
12862 * get a chance to respond before the target view.
12863 *
12864 * @param v The view the touch event has been dispatched to.
12865 * @param event The MotionEvent object containing full information about
12866 * the event.
12867 * @return True if the listener has consumed the event, false otherwise.
12868 */
12869 boolean onTouch(View v, MotionEvent event);
12870 }
12871
12872 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012873 * Interface definition for a callback to be invoked when a generic motion event is
12874 * dispatched to this view. The callback will be invoked before the generic motion
12875 * event is given to the view.
12876 */
12877 public interface OnGenericMotionListener {
12878 /**
12879 * Called when a generic motion event is dispatched to a view. This allows listeners to
12880 * get a chance to respond before the target view.
12881 *
12882 * @param v The view the generic motion event has been dispatched to.
12883 * @param event The MotionEvent object containing full information about
12884 * the event.
12885 * @return True if the listener has consumed the event, false otherwise.
12886 */
12887 boolean onGenericMotion(View v, MotionEvent event);
12888 }
12889
12890 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012891 * Interface definition for a callback to be invoked when a view has been clicked and held.
12892 */
12893 public interface OnLongClickListener {
12894 /**
12895 * Called when a view has been clicked and held.
12896 *
12897 * @param v The view that was clicked and held.
12898 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080012899 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012900 */
12901 boolean onLongClick(View v);
12902 }
12903
12904 /**
Chris Tate32affef2010-10-18 15:29:21 -070012905 * Interface definition for a callback to be invoked when a drag is being dispatched
12906 * to this view. The callback will be invoked before the hosting view's own
12907 * onDrag(event) method. If the listener wants to fall back to the hosting view's
12908 * onDrag(event) behavior, it should return 'false' from this callback.
12909 */
12910 public interface OnDragListener {
12911 /**
12912 * Called when a drag event is dispatched to a view. This allows listeners
12913 * to get a chance to override base View behavior.
12914 *
Joe Malin32736f02011-01-19 16:14:20 -080012915 * @param v The View that received the drag event.
12916 * @param event The {@link android.view.DragEvent} object for the drag event.
12917 * @return {@code true} if the drag event was handled successfully, or {@code false}
12918 * if the drag event was not handled. Note that {@code false} will trigger the View
12919 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070012920 */
12921 boolean onDrag(View v, DragEvent event);
12922 }
12923
12924 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012925 * Interface definition for a callback to be invoked when the focus state of
12926 * a view changed.
12927 */
12928 public interface OnFocusChangeListener {
12929 /**
12930 * Called when the focus state of a view has changed.
12931 *
12932 * @param v The view whose state has changed.
12933 * @param hasFocus The new focus state of v.
12934 */
12935 void onFocusChange(View v, boolean hasFocus);
12936 }
12937
12938 /**
12939 * Interface definition for a callback to be invoked when a view is clicked.
12940 */
12941 public interface OnClickListener {
12942 /**
12943 * Called when a view has been clicked.
12944 *
12945 * @param v The view that was clicked.
12946 */
12947 void onClick(View v);
12948 }
12949
12950 /**
12951 * Interface definition for a callback to be invoked when the context menu
12952 * for this view is being built.
12953 */
12954 public interface OnCreateContextMenuListener {
12955 /**
12956 * Called when the context menu for this view is being built. It is not
12957 * safe to hold onto the menu after this method returns.
12958 *
12959 * @param menu The context menu that is being built
12960 * @param v The view for which the context menu is being built
12961 * @param menuInfo Extra information about the item for which the
12962 * context menu should be shown. This information will vary
12963 * depending on the class of v.
12964 */
12965 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
12966 }
12967
Joe Onorato664644d2011-01-23 17:53:23 -080012968 /**
12969 * Interface definition for a callback to be invoked when the status bar changes
12970 * visibility.
12971 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012972 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080012973 */
12974 public interface OnSystemUiVisibilityChangeListener {
12975 /**
12976 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070012977 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080012978 *
12979 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
12980 */
12981 public void onSystemUiVisibilityChange(int visibility);
12982 }
12983
Adam Powell4afd62b2011-02-18 15:02:18 -080012984 /**
12985 * Interface definition for a callback to be invoked when this view is attached
12986 * or detached from its window.
12987 */
12988 public interface OnAttachStateChangeListener {
12989 /**
12990 * Called when the view is attached to a window.
12991 * @param v The view that was attached
12992 */
12993 public void onViewAttachedToWindow(View v);
12994 /**
12995 * Called when the view is detached from a window.
12996 * @param v The view that was detached
12997 */
12998 public void onViewDetachedFromWindow(View v);
12999 }
13000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013001 private final class UnsetPressedState implements Runnable {
13002 public void run() {
13003 setPressed(false);
13004 }
13005 }
13006
13007 /**
13008 * Base class for derived classes that want to save and restore their own
13009 * state in {@link android.view.View#onSaveInstanceState()}.
13010 */
13011 public static class BaseSavedState extends AbsSavedState {
13012 /**
13013 * Constructor used when reading from a parcel. Reads the state of the superclass.
13014 *
13015 * @param source
13016 */
13017 public BaseSavedState(Parcel source) {
13018 super(source);
13019 }
13020
13021 /**
13022 * Constructor called by derived classes when creating their SavedState objects
13023 *
13024 * @param superState The state of the superclass of this view
13025 */
13026 public BaseSavedState(Parcelable superState) {
13027 super(superState);
13028 }
13029
13030 public static final Parcelable.Creator<BaseSavedState> CREATOR =
13031 new Parcelable.Creator<BaseSavedState>() {
13032 public BaseSavedState createFromParcel(Parcel in) {
13033 return new BaseSavedState(in);
13034 }
13035
13036 public BaseSavedState[] newArray(int size) {
13037 return new BaseSavedState[size];
13038 }
13039 };
13040 }
13041
13042 /**
13043 * A set of information given to a view when it is attached to its parent
13044 * window.
13045 */
13046 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013047 interface Callbacks {
13048 void playSoundEffect(int effectId);
13049 boolean performHapticFeedback(int effectId, boolean always);
13050 }
13051
13052 /**
13053 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
13054 * to a Handler. This class contains the target (View) to invalidate and
13055 * the coordinates of the dirty rectangle.
13056 *
13057 * For performance purposes, this class also implements a pool of up to
13058 * POOL_LIMIT objects that get reused. This reduces memory allocations
13059 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013060 */
Romain Guyd928d682009-03-31 17:52:16 -070013061 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013062 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070013063 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
13064 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070013065 public InvalidateInfo newInstance() {
13066 return new InvalidateInfo();
13067 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013068
Romain Guyd928d682009-03-31 17:52:16 -070013069 public void onAcquired(InvalidateInfo element) {
13070 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013071
Romain Guyd928d682009-03-31 17:52:16 -070013072 public void onReleased(InvalidateInfo element) {
13073 }
13074 }, POOL_LIMIT)
13075 );
13076
13077 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013078 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013079
13080 View target;
13081
13082 int left;
13083 int top;
13084 int right;
13085 int bottom;
13086
Romain Guyd928d682009-03-31 17:52:16 -070013087 public void setNextPoolable(InvalidateInfo element) {
13088 mNext = element;
13089 }
13090
13091 public InvalidateInfo getNextPoolable() {
13092 return mNext;
13093 }
13094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013095 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070013096 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013097 }
13098
13099 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070013100 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013101 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013102
13103 public boolean isPooled() {
13104 return mIsPooled;
13105 }
13106
13107 public void setPooled(boolean isPooled) {
13108 mIsPooled = isPooled;
13109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013110 }
13111
13112 final IWindowSession mSession;
13113
13114 final IWindow mWindow;
13115
13116 final IBinder mWindowToken;
13117
13118 final Callbacks mRootCallbacks;
13119
Romain Guy59a12ca2011-06-09 17:48:21 -070013120 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080013121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013122 /**
13123 * The top view of the hierarchy.
13124 */
13125 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070013126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013127 IBinder mPanelParentWindowToken;
13128 Surface mSurface;
13129
Romain Guyb051e892010-09-28 19:09:36 -070013130 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013131 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070013132 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080013133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013134 /**
Romain Guy8506ab42009-06-11 17:35:47 -070013135 * Scale factor used by the compatibility mode
13136 */
13137 float mApplicationScale;
13138
13139 /**
13140 * Indicates whether the application is in compatibility mode
13141 */
13142 boolean mScalingRequired;
13143
13144 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013145 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080013146 */
13147 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080013148
Dianne Hackborn63042d62011-01-26 18:56:29 -080013149 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013150 * Left position of this view's window
13151 */
13152 int mWindowLeft;
13153
13154 /**
13155 * Top position of this view's window
13156 */
13157 int mWindowTop;
13158
13159 /**
Adam Powell26153a32010-11-08 15:22:27 -080013160 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070013161 */
Adam Powell26153a32010-11-08 15:22:27 -080013162 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070013163
13164 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013165 * For windows that are full-screen but using insets to layout inside
13166 * of the screen decorations, these are the current insets for the
13167 * content of the window.
13168 */
13169 final Rect mContentInsets = new Rect();
13170
13171 /**
13172 * For windows that are full-screen but using insets to layout inside
13173 * of the screen decorations, these are the current insets for the
13174 * actual visible parts of the window.
13175 */
13176 final Rect mVisibleInsets = new Rect();
13177
13178 /**
13179 * The internal insets given by this window. This value is
13180 * supplied by the client (through
13181 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
13182 * be given to the window manager when changed to be used in laying
13183 * out windows behind it.
13184 */
13185 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
13186 = new ViewTreeObserver.InternalInsetsInfo();
13187
13188 /**
13189 * All views in the window's hierarchy that serve as scroll containers,
13190 * used to determine if the window can be resized or must be panned
13191 * to adjust for a soft input area.
13192 */
13193 final ArrayList<View> mScrollContainers = new ArrayList<View>();
13194
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070013195 final KeyEvent.DispatcherState mKeyDispatchState
13196 = new KeyEvent.DispatcherState();
13197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013198 /**
13199 * Indicates whether the view's window currently has the focus.
13200 */
13201 boolean mHasWindowFocus;
13202
13203 /**
13204 * The current visibility of the window.
13205 */
13206 int mWindowVisibility;
13207
13208 /**
13209 * Indicates the time at which drawing started to occur.
13210 */
13211 long mDrawingTime;
13212
13213 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070013214 * Indicates whether or not ignoring the DIRTY_MASK flags.
13215 */
13216 boolean mIgnoreDirtyState;
13217
13218 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013219 * Indicates whether the view's window is currently in touch mode.
13220 */
13221 boolean mInTouchMode;
13222
13223 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013224 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013225 * the next time it performs a traversal
13226 */
13227 boolean mRecomputeGlobalAttributes;
13228
13229 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013230 * Set during a traveral if any views want to keep the screen on.
13231 */
13232 boolean mKeepScreenOn;
13233
13234 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013235 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
13236 */
13237 int mSystemUiVisibility;
13238
13239 /**
13240 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
13241 * attached.
13242 */
13243 boolean mHasSystemUiListeners;
13244
13245 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013246 * Set if the visibility of any views has changed.
13247 */
13248 boolean mViewVisibilityChanged;
13249
13250 /**
13251 * Set to true if a view has been scrolled.
13252 */
13253 boolean mViewScrollChanged;
13254
13255 /**
13256 * Global to the view hierarchy used as a temporary for dealing with
13257 * x/y points in the transparent region computations.
13258 */
13259 final int[] mTransparentLocation = new int[2];
13260
13261 /**
13262 * Global to the view hierarchy used as a temporary for dealing with
13263 * x/y points in the ViewGroup.invalidateChild implementation.
13264 */
13265 final int[] mInvalidateChildLocation = new int[2];
13266
Chet Haasec3aa3612010-06-17 08:50:37 -070013267
13268 /**
13269 * Global to the view hierarchy used as a temporary for dealing with
13270 * x/y location when view is transformed.
13271 */
13272 final float[] mTmpTransformLocation = new float[2];
13273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013274 /**
13275 * The view tree observer used to dispatch global events like
13276 * layout, pre-draw, touch mode change, etc.
13277 */
13278 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
13279
13280 /**
13281 * A Canvas used by the view hierarchy to perform bitmap caching.
13282 */
13283 Canvas mCanvas;
13284
13285 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013286 * A Handler supplied by a view's {@link android.view.ViewAncestor}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013287 * handler can be used to pump events in the UI events queue.
13288 */
13289 final Handler mHandler;
13290
13291 /**
13292 * Identifier for messages requesting the view to be invalidated.
13293 * Such messages should be sent to {@link #mHandler}.
13294 */
13295 static final int INVALIDATE_MSG = 0x1;
13296
13297 /**
13298 * Identifier for messages requesting the view to invalidate a region.
13299 * Such messages should be sent to {@link #mHandler}.
13300 */
13301 static final int INVALIDATE_RECT_MSG = 0x2;
13302
13303 /**
13304 * Temporary for use in computing invalidate rectangles while
13305 * calling up the hierarchy.
13306 */
13307 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070013308
13309 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070013310 * Temporary for use in computing hit areas with transformed views
13311 */
13312 final RectF mTmpTransformRect = new RectF();
13313
13314 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070013315 * Temporary list for use in collecting focusable descendents of a view.
13316 */
13317 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
13318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013319 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013320 * The id of the window for accessibility purposes.
13321 */
13322 int mAccessibilityWindowId = View.NO_ID;
13323
13324 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013325 * Creates a new set of attachment information with the specified
13326 * events handler and thread.
13327 *
13328 * @param handler the events handler the view must use
13329 */
13330 AttachInfo(IWindowSession session, IWindow window,
13331 Handler handler, Callbacks effectPlayer) {
13332 mSession = session;
13333 mWindow = window;
13334 mWindowToken = window.asBinder();
13335 mHandler = handler;
13336 mRootCallbacks = effectPlayer;
13337 }
13338 }
13339
13340 /**
13341 * <p>ScrollabilityCache holds various fields used by a View when scrolling
13342 * is supported. This avoids keeping too many unused fields in most
13343 * instances of View.</p>
13344 */
Mike Cleronf116bf82009-09-27 19:14:12 -070013345 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080013346
Mike Cleronf116bf82009-09-27 19:14:12 -070013347 /**
13348 * Scrollbars are not visible
13349 */
13350 public static final int OFF = 0;
13351
13352 /**
13353 * Scrollbars are visible
13354 */
13355 public static final int ON = 1;
13356
13357 /**
13358 * Scrollbars are fading away
13359 */
13360 public static final int FADING = 2;
13361
13362 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080013363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013364 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070013365 public int scrollBarDefaultDelayBeforeFade;
13366 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013367
13368 public int scrollBarSize;
13369 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070013370 public float[] interpolatorValues;
13371 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013372
13373 public final Paint paint;
13374 public final Matrix matrix;
13375 public Shader shader;
13376
Mike Cleronf116bf82009-09-27 19:14:12 -070013377 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
13378
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013379 private static final float[] OPAQUE = { 255 };
13380 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080013381
Mike Cleronf116bf82009-09-27 19:14:12 -070013382 /**
13383 * When fading should start. This time moves into the future every time
13384 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
13385 */
13386 public long fadeStartTime;
13387
13388
13389 /**
13390 * The current state of the scrollbars: ON, OFF, or FADING
13391 */
13392 public int state = OFF;
13393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013394 private int mLastColor;
13395
Mike Cleronf116bf82009-09-27 19:14:12 -070013396 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013397 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
13398 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070013399 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
13400 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013401
13402 paint = new Paint();
13403 matrix = new Matrix();
13404 // use use a height of 1, and then wack the matrix each time we
13405 // actually use it.
13406 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070013407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013408 paint.setShader(shader);
13409 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070013410 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013411 }
Romain Guy8506ab42009-06-11 17:35:47 -070013412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013413 public void setFadeColor(int color) {
13414 if (color != 0 && color != mLastColor) {
13415 mLastColor = color;
13416 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070013417
Romain Guye55e1a72009-08-27 10:42:26 -070013418 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
13419 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070013420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013421 paint.setShader(shader);
13422 // Restore the default transfer mode (src_over)
13423 paint.setXfermode(null);
13424 }
13425 }
Joe Malin32736f02011-01-19 16:14:20 -080013426
Mike Cleronf116bf82009-09-27 19:14:12 -070013427 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070013428 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070013429 if (now >= fadeStartTime) {
13430
13431 // the animation fades the scrollbars out by changing
13432 // the opacity (alpha) from fully opaque to fully
13433 // transparent
13434 int nextFrame = (int) now;
13435 int framesCount = 0;
13436
13437 Interpolator interpolator = scrollBarInterpolator;
13438
13439 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013440 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070013441
13442 // End transparent
13443 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013444 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070013445
13446 state = FADING;
13447
13448 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080013449 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070013450 }
13451 }
13452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013453 }
13454}