blob: 441cdc14db8a2d8e5f7267b53a5322fd697fded7 [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 /**
8454 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8455 * scrollbars are painted only if they have been awakened first.</p>
8456 *
8457 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008458 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008459 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008460 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008461 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008462 // scrollbars are drawn only when the animation is running
8463 final ScrollabilityCache cache = mScrollCache;
8464 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008465
Mike Cleronf116bf82009-09-27 19:14:12 -07008466 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008467
Mike Cleronf116bf82009-09-27 19:14:12 -07008468 if (state == ScrollabilityCache.OFF) {
8469 return;
8470 }
Joe Malin32736f02011-01-19 16:14:20 -08008471
Mike Cleronf116bf82009-09-27 19:14:12 -07008472 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008473
Mike Cleronf116bf82009-09-27 19:14:12 -07008474 if (state == ScrollabilityCache.FADING) {
8475 // We're fading -- get our fade interpolation
8476 if (cache.interpolatorValues == null) {
8477 cache.interpolatorValues = new float[1];
8478 }
Joe Malin32736f02011-01-19 16:14:20 -08008479
Mike Cleronf116bf82009-09-27 19:14:12 -07008480 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008481
Mike Cleronf116bf82009-09-27 19:14:12 -07008482 // Stops the animation if we're done
8483 if (cache.scrollBarInterpolator.timeToValues(values) ==
8484 Interpolator.Result.FREEZE_END) {
8485 cache.state = ScrollabilityCache.OFF;
8486 } else {
8487 cache.scrollBar.setAlpha(Math.round(values[0]));
8488 }
Joe Malin32736f02011-01-19 16:14:20 -08008489
8490 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008491 // drawing. We only want this when we're fading so that
8492 // we prevent excessive redraws
8493 invalidate = true;
8494 } else {
8495 // We're just on -- but we may have been fading before so
8496 // reset alpha
8497 cache.scrollBar.setAlpha(255);
8498 }
8499
Joe Malin32736f02011-01-19 16:14:20 -08008500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008501 final int viewFlags = mViewFlags;
8502
8503 final boolean drawHorizontalScrollBar =
8504 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8505 final boolean drawVerticalScrollBar =
8506 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8507 && !isVerticalScrollBarHidden();
8508
8509 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8510 final int width = mRight - mLeft;
8511 final int height = mBottom - mTop;
8512
8513 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008514
Mike Reede8853fc2009-09-04 14:01:48 -04008515 final int scrollX = mScrollX;
8516 final int scrollY = mScrollY;
8517 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8518
Mike Cleronf116bf82009-09-27 19:14:12 -07008519 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008521 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008522 int size = scrollBar.getSize(false);
8523 if (size <= 0) {
8524 size = cache.scrollBarSize;
8525 }
8526
Mike Cleronf116bf82009-09-27 19:14:12 -07008527 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008528 computeHorizontalScrollOffset(),
8529 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008530 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008531 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008532 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008533 left = scrollX + (mPaddingLeft & inside);
8534 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8535 bottom = top + size;
8536 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8537 if (invalidate) {
8538 invalidate(left, top, right, bottom);
8539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008540 }
8541
8542 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008543 int size = scrollBar.getSize(true);
8544 if (size <= 0) {
8545 size = cache.scrollBarSize;
8546 }
8547
Mike Reede8853fc2009-09-04 14:01:48 -04008548 scrollBar.setParameters(computeVerticalScrollRange(),
8549 computeVerticalScrollOffset(),
8550 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008551 switch (mVerticalScrollbarPosition) {
8552 default:
8553 case SCROLLBAR_POSITION_DEFAULT:
8554 case SCROLLBAR_POSITION_RIGHT:
8555 left = scrollX + width - size - (mUserPaddingRight & inside);
8556 break;
8557 case SCROLLBAR_POSITION_LEFT:
8558 left = scrollX + (mUserPaddingLeft & inside);
8559 break;
8560 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008561 top = scrollY + (mPaddingTop & inside);
8562 right = left + size;
8563 bottom = scrollY + height - (mUserPaddingBottom & inside);
8564 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8565 if (invalidate) {
8566 invalidate(left, top, right, bottom);
8567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008568 }
8569 }
8570 }
8571 }
Romain Guy8506ab42009-06-11 17:35:47 -07008572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008573 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008574 * 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 -08008575 * FastScroller is visible.
8576 * @return whether to temporarily hide the vertical scrollbar
8577 * @hide
8578 */
8579 protected boolean isVerticalScrollBarHidden() {
8580 return false;
8581 }
8582
8583 /**
8584 * <p>Draw the horizontal scrollbar if
8585 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
8586 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008587 * @param canvas the canvas on which to draw the scrollbar
8588 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008589 *
8590 * @see #isHorizontalScrollBarEnabled()
8591 * @see #computeHorizontalScrollRange()
8592 * @see #computeHorizontalScrollExtent()
8593 * @see #computeHorizontalScrollOffset()
8594 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008595 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008596 */
Romain Guy8fb95422010-08-17 18:38:51 -07008597 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8598 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008599 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008600 scrollBar.draw(canvas);
8601 }
Mike Reede8853fc2009-09-04 14:01:48 -04008602
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008603 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008604 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8605 * returns true.</p>
8606 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008607 * @param canvas the canvas on which to draw the scrollbar
8608 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008609 *
8610 * @see #isVerticalScrollBarEnabled()
8611 * @see #computeVerticalScrollRange()
8612 * @see #computeVerticalScrollExtent()
8613 * @see #computeVerticalScrollOffset()
8614 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008615 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008616 */
Romain Guy8fb95422010-08-17 18:38:51 -07008617 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
8618 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04008619 scrollBar.setBounds(l, t, r, b);
8620 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008621 }
8622
8623 /**
8624 * Implement this to do your drawing.
8625 *
8626 * @param canvas the canvas on which the background will be drawn
8627 */
8628 protected void onDraw(Canvas canvas) {
8629 }
8630
8631 /*
8632 * Caller is responsible for calling requestLayout if necessary.
8633 * (This allows addViewInLayout to not request a new layout.)
8634 */
8635 void assignParent(ViewParent parent) {
8636 if (mParent == null) {
8637 mParent = parent;
8638 } else if (parent == null) {
8639 mParent = null;
8640 } else {
8641 throw new RuntimeException("view " + this + " being added, but"
8642 + " it already has a parent");
8643 }
8644 }
8645
8646 /**
8647 * This is called when the view is attached to a window. At this point it
8648 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008649 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
8650 * however it may be called any time before the first onDraw -- including
8651 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008652 *
8653 * @see #onDetachedFromWindow()
8654 */
8655 protected void onAttachedToWindow() {
8656 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
8657 mParent.requestTransparentRegion(this);
8658 }
Adam Powell8568c3a2010-04-19 14:26:11 -07008659 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
8660 initialAwakenScrollBars();
8661 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
8662 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08008663 jumpDrawablesToCurrentState();
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008664 resolveLayoutDirection();
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008665 }
Cibu Johny86666632010-02-22 13:01:02 -08008666
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008667 /**
8668 * Resolving the layout direction. LTR is set initially.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07008669 * We are supposing here that the parent directionality will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008670 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008671 private void resolveLayoutDirection() {
Cibu Johny86666632010-02-22 13:01:02 -08008672 mPrivateFlags2 &= ~RESOLVED_LAYOUT_RTL;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008673 switch (getLayoutDirection()) {
8674 case LAYOUT_DIRECTION_INHERIT:
Cibu Johny86666632010-02-22 13:01:02 -08008675 // If this is root view, no need to look at parent's layout dir.
8676 if (mParent != null && mParent instanceof ViewGroup &&
8677 ((ViewGroup) mParent).isLayoutRtl()) {
8678 mPrivateFlags2 |= RESOLVED_LAYOUT_RTL;
8679 }
8680 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008681 case LAYOUT_DIRECTION_RTL:
Cibu Johny86666632010-02-22 13:01:02 -08008682 mPrivateFlags2 |= RESOLVED_LAYOUT_RTL;
8683 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07008684 case LAYOUT_DIRECTION_LOCALE:
8685 if(isLayoutDirectionRtl(Locale.getDefault())) {
8686 mPrivateFlags2 |= RESOLVED_LAYOUT_RTL;
8687 }
8688 break;
8689 default:
8690 // Nothing to do, LTR by default
8691 }
8692 }
8693
8694 /**
8695 * Check if a Locale is corresponding to a RTL script.
8696 *
8697 * @param locale Locale to check
8698 * @return true if a Locale is corresponding to a RTL script.
8699 */
8700 private static boolean isLayoutDirectionRtl(Locale locale) {
8701 if (locale == null || locale.equals(Locale.ROOT)) return false;
8702 // Be careful: this code will need to be changed when vertical scripts will be supported
8703 // OR if ICU4C is updated to have the "likelySubtags" file
8704 switch(Character.getDirectionality(locale.getDisplayName(locale).charAt(0))) {
8705 case Character.DIRECTIONALITY_LEFT_TO_RIGHT:
8706 return false;
8707 case Character.DIRECTIONALITY_RIGHT_TO_LEFT:
8708 case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC:
8709 return true;
8710 default:
8711 return false;
Cibu Johny86666632010-02-22 13:01:02 -08008712 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008713 }
8714
8715 /**
8716 * This is called when the view is detached from a window. At this point it
8717 * no longer has a surface for drawing.
8718 *
8719 * @see #onAttachedToWindow()
8720 */
8721 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08008722 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08008723
Romain Guya440b002010-02-24 15:57:54 -08008724 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05008725 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08008726 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08008727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008728 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08008729
8730 if (mHardwareLayer != null) {
8731 mHardwareLayer.destroy();
8732 mHardwareLayer = null;
8733 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008734
Chet Haasedaf98e92011-01-10 14:10:36 -08008735 if (mDisplayList != null) {
8736 mDisplayList.invalidate();
8737 }
8738
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008739 if (mAttachInfo != null) {
8740 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
8741 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
8742 }
8743
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08008744 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008745 }
8746
8747 /**
8748 * @return The number of times this view has been attached to a window
8749 */
8750 protected int getWindowAttachCount() {
8751 return mWindowAttachCount;
8752 }
8753
8754 /**
8755 * Retrieve a unique token identifying the window this view is attached to.
8756 * @return Return the window's token for use in
8757 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
8758 */
8759 public IBinder getWindowToken() {
8760 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
8761 }
8762
8763 /**
8764 * Retrieve a unique token identifying the top-level "real" window of
8765 * the window that this view is attached to. That is, this is like
8766 * {@link #getWindowToken}, except if the window this view in is a panel
8767 * window (attached to another containing window), then the token of
8768 * the containing window is returned instead.
8769 *
8770 * @return Returns the associated window token, either
8771 * {@link #getWindowToken()} or the containing window's token.
8772 */
8773 public IBinder getApplicationWindowToken() {
8774 AttachInfo ai = mAttachInfo;
8775 if (ai != null) {
8776 IBinder appWindowToken = ai.mPanelParentWindowToken;
8777 if (appWindowToken == null) {
8778 appWindowToken = ai.mWindowToken;
8779 }
8780 return appWindowToken;
8781 }
8782 return null;
8783 }
8784
8785 /**
8786 * Retrieve private session object this view hierarchy is using to
8787 * communicate with the window manager.
8788 * @return the session object to communicate with the window manager
8789 */
8790 /*package*/ IWindowSession getWindowSession() {
8791 return mAttachInfo != null ? mAttachInfo.mSession : null;
8792 }
8793
8794 /**
8795 * @param info the {@link android.view.View.AttachInfo} to associated with
8796 * this view
8797 */
8798 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
8799 //System.out.println("Attached! " + this);
8800 mAttachInfo = info;
8801 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008802 // We will need to evaluate the drawable state at least once.
8803 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008804 if (mFloatingTreeObserver != null) {
8805 info.mTreeObserver.merge(mFloatingTreeObserver);
8806 mFloatingTreeObserver = null;
8807 }
8808 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
8809 mAttachInfo.mScrollContainers.add(this);
8810 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
8811 }
8812 performCollectViewAttributes(visibility);
8813 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08008814
8815 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8816 mOnAttachStateChangeListeners;
8817 if (listeners != null && listeners.size() > 0) {
8818 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8819 // perform the dispatching. The iterator is a safe guard against listeners that
8820 // could mutate the list by calling the various add/remove methods. This prevents
8821 // the array from being modified while we iterate it.
8822 for (OnAttachStateChangeListener listener : listeners) {
8823 listener.onViewAttachedToWindow(this);
8824 }
8825 }
8826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008827 int vis = info.mWindowVisibility;
8828 if (vis != GONE) {
8829 onWindowVisibilityChanged(vis);
8830 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008831 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
8832 // If nobody has evaluated the drawable state yet, then do it now.
8833 refreshDrawableState();
8834 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008835 }
8836
8837 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008838 AttachInfo info = mAttachInfo;
8839 if (info != null) {
8840 int vis = info.mWindowVisibility;
8841 if (vis != GONE) {
8842 onWindowVisibilityChanged(GONE);
8843 }
8844 }
8845
8846 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08008847
Adam Powell4afd62b2011-02-18 15:02:18 -08008848 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8849 mOnAttachStateChangeListeners;
8850 if (listeners != null && listeners.size() > 0) {
8851 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8852 // perform the dispatching. The iterator is a safe guard against listeners that
8853 // could mutate the list by calling the various add/remove methods. This prevents
8854 // the array from being modified while we iterate it.
8855 for (OnAttachStateChangeListener listener : listeners) {
8856 listener.onViewDetachedFromWindow(this);
8857 }
8858 }
8859
Romain Guy01d5edc2011-01-28 11:28:53 -08008860 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008861 mAttachInfo.mScrollContainers.remove(this);
8862 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
8863 }
Romain Guy01d5edc2011-01-28 11:28:53 -08008864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008865 mAttachInfo = null;
8866 }
8867
8868 /**
8869 * Store this view hierarchy's frozen state into the given container.
8870 *
8871 * @param container The SparseArray in which to save the view's state.
8872 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008873 * @see #restoreHierarchyState(android.util.SparseArray)
8874 * @see #dispatchSaveInstanceState(android.util.SparseArray)
8875 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008876 */
8877 public void saveHierarchyState(SparseArray<Parcelable> container) {
8878 dispatchSaveInstanceState(container);
8879 }
8880
8881 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008882 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
8883 * this view and its children. May be overridden to modify how freezing happens to a
8884 * 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 -08008885 *
8886 * @param container The SparseArray in which to save the view's state.
8887 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008888 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
8889 * @see #saveHierarchyState(android.util.SparseArray)
8890 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008891 */
8892 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
8893 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
8894 mPrivateFlags &= ~SAVE_STATE_CALLED;
8895 Parcelable state = onSaveInstanceState();
8896 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8897 throw new IllegalStateException(
8898 "Derived class did not call super.onSaveInstanceState()");
8899 }
8900 if (state != null) {
8901 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
8902 // + ": " + state);
8903 container.put(mID, state);
8904 }
8905 }
8906 }
8907
8908 /**
8909 * Hook allowing a view to generate a representation of its internal state
8910 * that can later be used to create a new instance with that same state.
8911 * This state should only contain information that is not persistent or can
8912 * not be reconstructed later. For example, you will never store your
8913 * current position on screen because that will be computed again when a
8914 * new instance of the view is placed in its view hierarchy.
8915 * <p>
8916 * Some examples of things you may store here: the current cursor position
8917 * in a text view (but usually not the text itself since that is stored in a
8918 * content provider or other persistent storage), the currently selected
8919 * item in a list view.
8920 *
8921 * @return Returns a Parcelable object containing the view's current dynamic
8922 * state, or null if there is nothing interesting to save. The
8923 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008924 * @see #onRestoreInstanceState(android.os.Parcelable)
8925 * @see #saveHierarchyState(android.util.SparseArray)
8926 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008927 * @see #setSaveEnabled(boolean)
8928 */
8929 protected Parcelable onSaveInstanceState() {
8930 mPrivateFlags |= SAVE_STATE_CALLED;
8931 return BaseSavedState.EMPTY_STATE;
8932 }
8933
8934 /**
8935 * Restore this view hierarchy's frozen state from the given container.
8936 *
8937 * @param container The SparseArray which holds previously frozen states.
8938 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008939 * @see #saveHierarchyState(android.util.SparseArray)
8940 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
8941 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008942 */
8943 public void restoreHierarchyState(SparseArray<Parcelable> container) {
8944 dispatchRestoreInstanceState(container);
8945 }
8946
8947 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008948 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
8949 * state for this view and its children. May be overridden to modify how restoring
8950 * happens to a view's children; for example, some views may want to not store state
8951 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008952 *
8953 * @param container The SparseArray which holds previously saved state.
8954 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008955 * @see #dispatchSaveInstanceState(android.util.SparseArray)
8956 * @see #restoreHierarchyState(android.util.SparseArray)
8957 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008958 */
8959 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
8960 if (mID != NO_ID) {
8961 Parcelable state = container.get(mID);
8962 if (state != null) {
8963 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
8964 // + ": " + state);
8965 mPrivateFlags &= ~SAVE_STATE_CALLED;
8966 onRestoreInstanceState(state);
8967 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8968 throw new IllegalStateException(
8969 "Derived class did not call super.onRestoreInstanceState()");
8970 }
8971 }
8972 }
8973 }
8974
8975 /**
8976 * Hook allowing a view to re-apply a representation of its internal state that had previously
8977 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
8978 * null state.
8979 *
8980 * @param state The frozen state that had previously been returned by
8981 * {@link #onSaveInstanceState}.
8982 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008983 * @see #onSaveInstanceState()
8984 * @see #restoreHierarchyState(android.util.SparseArray)
8985 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008986 */
8987 protected void onRestoreInstanceState(Parcelable state) {
8988 mPrivateFlags |= SAVE_STATE_CALLED;
8989 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08008990 throw new IllegalArgumentException("Wrong state class, expecting View State but "
8991 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08008992 + "when two views of different type have the same id in the same hierarchy. "
8993 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08008994 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008995 }
8996 }
8997
8998 /**
8999 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9000 *
9001 * @return the drawing start time in milliseconds
9002 */
9003 public long getDrawingTime() {
9004 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9005 }
9006
9007 /**
9008 * <p>Enables or disables the duplication of the parent's state into this view. When
9009 * duplication is enabled, this view gets its drawable state from its parent rather
9010 * than from its own internal properties.</p>
9011 *
9012 * <p>Note: in the current implementation, setting this property to true after the
9013 * view was added to a ViewGroup might have no effect at all. This property should
9014 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9015 *
9016 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9017 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009018 *
Gilles Debunnefb817032011-01-13 13:52:49 -08009019 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
9020 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009021 *
9022 * @param enabled True to enable duplication of the parent's drawable state, false
9023 * to disable it.
9024 *
9025 * @see #getDrawableState()
9026 * @see #isDuplicateParentStateEnabled()
9027 */
9028 public void setDuplicateParentStateEnabled(boolean enabled) {
9029 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
9030 }
9031
9032 /**
9033 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
9034 *
9035 * @return True if this view's drawable state is duplicated from the parent,
9036 * false otherwise
9037 *
9038 * @see #getDrawableState()
9039 * @see #setDuplicateParentStateEnabled(boolean)
9040 */
9041 public boolean isDuplicateParentStateEnabled() {
9042 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
9043 }
9044
9045 /**
Romain Guy171c5922011-01-06 10:04:23 -08009046 * <p>Specifies the type of layer backing this view. The layer can be
9047 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
9048 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009049 *
Romain Guy171c5922011-01-06 10:04:23 -08009050 * <p>A layer is associated with an optional {@link android.graphics.Paint}
9051 * instance that controls how the layer is composed on screen. The following
9052 * properties of the paint are taken into account when composing the layer:</p>
9053 * <ul>
9054 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
9055 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
9056 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
9057 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08009058 *
Romain Guy171c5922011-01-06 10:04:23 -08009059 * <p>If this view has an alpha value set to < 1.0 by calling
9060 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
9061 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
9062 * equivalent to setting a hardware layer on this view and providing a paint with
9063 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08009064 *
Romain Guy171c5922011-01-06 10:04:23 -08009065 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
9066 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
9067 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009068 *
Romain Guy171c5922011-01-06 10:04:23 -08009069 * @param layerType The ype of layer to use with this view, must be one of
9070 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9071 * {@link #LAYER_TYPE_HARDWARE}
9072 * @param paint The paint used to compose the layer. This argument is optional
9073 * and can be null. It is ignored when the layer type is
9074 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08009075 *
9076 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08009077 * @see #LAYER_TYPE_NONE
9078 * @see #LAYER_TYPE_SOFTWARE
9079 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08009080 * @see #setAlpha(float)
9081 *
Romain Guy171c5922011-01-06 10:04:23 -08009082 * @attr ref android.R.styleable#View_layerType
9083 */
9084 public void setLayerType(int layerType, Paint paint) {
9085 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08009086 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08009087 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
9088 }
Chet Haasedaf98e92011-01-10 14:10:36 -08009089
Romain Guyd6cd5722011-01-17 14:42:41 -08009090 if (layerType == mLayerType) {
9091 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
9092 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009093 invalidateParentCaches();
9094 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08009095 }
9096 return;
9097 }
Romain Guy171c5922011-01-06 10:04:23 -08009098
9099 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08009100 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -07009101 case LAYER_TYPE_HARDWARE:
9102 if (mHardwareLayer != null) {
9103 mHardwareLayer.destroy();
9104 mHardwareLayer = null;
9105 }
9106 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -08009107 case LAYER_TYPE_SOFTWARE:
9108 if (mDrawingCache != null) {
9109 mDrawingCache.recycle();
9110 mDrawingCache = null;
9111 }
Joe Malin32736f02011-01-19 16:14:20 -08009112
Romain Guy6c319ca2011-01-11 14:29:25 -08009113 if (mUnscaledDrawingCache != null) {
9114 mUnscaledDrawingCache.recycle();
9115 mUnscaledDrawingCache = null;
9116 }
9117 break;
Romain Guy6c319ca2011-01-11 14:29:25 -08009118 default:
9119 break;
Romain Guy171c5922011-01-06 10:04:23 -08009120 }
9121
9122 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08009123 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
9124 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
9125 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08009126
Romain Guy0fd89bf2011-01-26 15:41:30 -08009127 invalidateParentCaches();
9128 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08009129 }
9130
9131 /**
9132 * Indicates what type of layer is currently associated with this view. By default
9133 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
9134 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
9135 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08009136 *
Romain Guy171c5922011-01-06 10:04:23 -08009137 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9138 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08009139 *
9140 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08009141 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08009142 * @see #LAYER_TYPE_NONE
9143 * @see #LAYER_TYPE_SOFTWARE
9144 * @see #LAYER_TYPE_HARDWARE
9145 */
9146 public int getLayerType() {
9147 return mLayerType;
9148 }
Joe Malin32736f02011-01-19 16:14:20 -08009149
Romain Guy6c319ca2011-01-11 14:29:25 -08009150 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08009151 * Forces this view's layer to be created and this view to be rendered
9152 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
9153 * invoking this method will have no effect.
9154 *
9155 * This method can for instance be used to render a view into its layer before
9156 * starting an animation. If this view is complex, rendering into the layer
9157 * before starting the animation will avoid skipping frames.
9158 *
9159 * @throws IllegalStateException If this view is not attached to a window
9160 *
9161 * @see #setLayerType(int, android.graphics.Paint)
9162 */
9163 public void buildLayer() {
9164 if (mLayerType == LAYER_TYPE_NONE) return;
9165
9166 if (mAttachInfo == null) {
9167 throw new IllegalStateException("This view must be attached to a window first");
9168 }
9169
9170 switch (mLayerType) {
9171 case LAYER_TYPE_HARDWARE:
9172 getHardwareLayer();
9173 break;
9174 case LAYER_TYPE_SOFTWARE:
9175 buildDrawingCache(true);
9176 break;
9177 }
9178 }
9179
9180 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08009181 * <p>Returns a hardware layer that can be used to draw this view again
9182 * without executing its draw method.</p>
9183 *
9184 * @return A HardwareLayer ready to render, or null if an error occurred.
9185 */
Romain Guy5e7f7662011-01-24 22:35:56 -08009186 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08009187 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
9188 return null;
9189 }
9190
9191 final int width = mRight - mLeft;
9192 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08009193
Romain Guy6c319ca2011-01-11 14:29:25 -08009194 if (width == 0 || height == 0) {
9195 return null;
9196 }
9197
9198 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
9199 if (mHardwareLayer == null) {
9200 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
9201 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08009202 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009203 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
9204 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08009205 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009206 }
9207
Romain Guy59a12ca2011-06-09 17:48:21 -07009208 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -08009209 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
9210 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009211 try {
9212 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08009213 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08009214 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009215
Chet Haase88172fe2011-03-07 17:36:33 -08009216 final int restoreCount = canvas.save();
9217
Romain Guy6c319ca2011-01-11 14:29:25 -08009218 computeScroll();
9219 canvas.translate(-mScrollX, -mScrollY);
9220
Romain Guy6c319ca2011-01-11 14:29:25 -08009221 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009222
Romain Guy6c319ca2011-01-11 14:29:25 -08009223 // Fast path for layouts with no backgrounds
9224 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9225 mPrivateFlags &= ~DIRTY_MASK;
9226 dispatchDraw(canvas);
9227 } else {
9228 draw(canvas);
9229 }
Joe Malin32736f02011-01-19 16:14:20 -08009230
Chet Haase88172fe2011-03-07 17:36:33 -08009231 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08009232 } finally {
9233 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08009234 mHardwareLayer.end(currentCanvas);
9235 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009236 }
9237 }
9238
9239 return mHardwareLayer;
9240 }
Romain Guy171c5922011-01-06 10:04:23 -08009241
9242 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009243 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
9244 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
9245 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
9246 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
9247 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
9248 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009249 *
Romain Guy171c5922011-01-06 10:04:23 -08009250 * <p>Enabling the drawing cache is similar to
9251 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08009252 * acceleration is turned off. When hardware acceleration is turned on, enabling the
9253 * drawing cache has no effect on rendering because the system uses a different mechanism
9254 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
9255 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
9256 * for information on how to enable software and hardware layers.</p>
9257 *
9258 * <p>This API can be used to manually generate
9259 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
9260 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009261 *
9262 * @param enabled true to enable the drawing cache, false otherwise
9263 *
9264 * @see #isDrawingCacheEnabled()
9265 * @see #getDrawingCache()
9266 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08009267 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009268 */
9269 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009270 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009271 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
9272 }
9273
9274 /**
9275 * <p>Indicates whether the drawing cache is enabled for this view.</p>
9276 *
9277 * @return true if the drawing cache is enabled
9278 *
9279 * @see #setDrawingCacheEnabled(boolean)
9280 * @see #getDrawingCache()
9281 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009282 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009283 public boolean isDrawingCacheEnabled() {
9284 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
9285 }
9286
9287 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08009288 * Debugging utility which recursively outputs the dirty state of a view and its
9289 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08009290 *
Chet Haasedaf98e92011-01-10 14:10:36 -08009291 * @hide
9292 */
Romain Guy676b1732011-02-14 14:45:33 -08009293 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08009294 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
9295 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
9296 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
9297 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
9298 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
9299 if (clear) {
9300 mPrivateFlags &= clearMask;
9301 }
9302 if (this instanceof ViewGroup) {
9303 ViewGroup parent = (ViewGroup) this;
9304 final int count = parent.getChildCount();
9305 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08009306 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08009307 child.outputDirtyFlags(indent + " ", clear, clearMask);
9308 }
9309 }
9310 }
9311
9312 /**
9313 * This method is used by ViewGroup to cause its children to restore or recreate their
9314 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
9315 * to recreate its own display list, which would happen if it went through the normal
9316 * draw/dispatchDraw mechanisms.
9317 *
9318 * @hide
9319 */
9320 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08009321
9322 /**
9323 * A view that is not attached or hardware accelerated cannot create a display list.
9324 * This method checks these conditions and returns the appropriate result.
9325 *
9326 * @return true if view has the ability to create a display list, false otherwise.
9327 *
9328 * @hide
9329 */
9330 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08009331 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08009332 }
Joe Malin32736f02011-01-19 16:14:20 -08009333
Chet Haasedaf98e92011-01-10 14:10:36 -08009334 /**
Romain Guyb051e892010-09-28 19:09:36 -07009335 * <p>Returns a display list that can be used to draw this view again
9336 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009337 *
Romain Guyb051e892010-09-28 19:09:36 -07009338 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08009339 *
9340 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07009341 */
Chet Haasedaf98e92011-01-10 14:10:36 -08009342 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08009343 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07009344 return null;
9345 }
9346
Chet Haasedaf98e92011-01-10 14:10:36 -08009347 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
9348 mDisplayList == null || !mDisplayList.isValid() ||
9349 mRecreateDisplayList)) {
9350 // Don't need to recreate the display list, just need to tell our
9351 // children to restore/recreate theirs
9352 if (mDisplayList != null && mDisplayList.isValid() &&
9353 !mRecreateDisplayList) {
9354 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9355 mPrivateFlags &= ~DIRTY_MASK;
9356 dispatchGetDisplayList();
9357
9358 return mDisplayList;
9359 }
9360
9361 // If we got here, we're recreating it. Mark it as such to ensure that
9362 // we copy in child display lists into ours in drawChild()
9363 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08009364 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009365 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
9366 // If we're creating a new display list, make sure our parent gets invalidated
9367 // since they will need to recreate their display list to account for this
9368 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08009369 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08009370 }
Romain Guyb051e892010-09-28 19:09:36 -07009371
9372 final HardwareCanvas canvas = mDisplayList.start();
9373 try {
9374 int width = mRight - mLeft;
9375 int height = mBottom - mTop;
9376
9377 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08009378 // The dirty rect should always be null for a display list
9379 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07009380
Chet Haase88172fe2011-03-07 17:36:33 -08009381 final int restoreCount = canvas.save();
9382
Chet Haasedaf98e92011-01-10 14:10:36 -08009383 computeScroll();
9384 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009385 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009386
Romain Guyb051e892010-09-28 19:09:36 -07009387 // Fast path for layouts with no backgrounds
9388 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9389 mPrivateFlags &= ~DIRTY_MASK;
9390 dispatchDraw(canvas);
9391 } else {
9392 draw(canvas);
9393 }
Joe Malin32736f02011-01-19 16:14:20 -08009394
Chet Haase88172fe2011-03-07 17:36:33 -08009395 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07009396 } finally {
9397 canvas.onPostDraw();
9398
9399 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07009400 }
Chet Haase77785f92011-01-25 23:22:09 -08009401 } else {
9402 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9403 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07009404 }
9405
9406 return mDisplayList;
9407 }
9408
9409 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009410 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009411 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009412 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009413 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009414 * @see #getDrawingCache(boolean)
9415 */
9416 public Bitmap getDrawingCache() {
9417 return getDrawingCache(false);
9418 }
9419
9420 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009421 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
9422 * is null when caching is disabled. If caching is enabled and the cache is not ready,
9423 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
9424 * draw from the cache when the cache is enabled. To benefit from the cache, you must
9425 * request the drawing cache by calling this method and draw it on screen if the
9426 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009427 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009428 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9429 * this method will create a bitmap of the same size as this view. Because this bitmap
9430 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9431 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9432 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9433 * size than the view. This implies that your application must be able to handle this
9434 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009435 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009436 * @param autoScale Indicates whether the generated bitmap should be scaled based on
9437 * the current density of the screen when the application is in compatibility
9438 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009439 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009440 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009441 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009442 * @see #setDrawingCacheEnabled(boolean)
9443 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07009444 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009445 * @see #destroyDrawingCache()
9446 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009447 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009448 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
9449 return null;
9450 }
9451 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009452 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009453 }
Romain Guy02890fd2010-08-06 17:58:44 -07009454 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009455 }
9456
9457 /**
9458 * <p>Frees the resources used by the drawing cache. If you call
9459 * {@link #buildDrawingCache()} manually without calling
9460 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9461 * should cleanup the cache with this method afterwards.</p>
9462 *
9463 * @see #setDrawingCacheEnabled(boolean)
9464 * @see #buildDrawingCache()
9465 * @see #getDrawingCache()
9466 */
9467 public void destroyDrawingCache() {
9468 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009469 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009470 mDrawingCache = null;
9471 }
Romain Guyfbd8f692009-06-26 14:51:58 -07009472 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009473 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07009474 mUnscaledDrawingCache = null;
9475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009476 }
9477
9478 /**
9479 * Setting a solid background color for the drawing cache's bitmaps will improve
9480 * perfromance and memory usage. Note, though that this should only be used if this
9481 * view will always be drawn on top of a solid color.
9482 *
9483 * @param color The background color to use for the drawing cache's bitmap
9484 *
9485 * @see #setDrawingCacheEnabled(boolean)
9486 * @see #buildDrawingCache()
9487 * @see #getDrawingCache()
9488 */
9489 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08009490 if (color != mDrawingCacheBackgroundColor) {
9491 mDrawingCacheBackgroundColor = color;
9492 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009494 }
9495
9496 /**
9497 * @see #setDrawingCacheBackgroundColor(int)
9498 *
9499 * @return The background color to used for the drawing cache's bitmap
9500 */
9501 public int getDrawingCacheBackgroundColor() {
9502 return mDrawingCacheBackgroundColor;
9503 }
9504
9505 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009506 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009507 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009508 * @see #buildDrawingCache(boolean)
9509 */
9510 public void buildDrawingCache() {
9511 buildDrawingCache(false);
9512 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08009513
Romain Guyfbd8f692009-06-26 14:51:58 -07009514 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009515 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
9516 *
9517 * <p>If you call {@link #buildDrawingCache()} manually without calling
9518 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9519 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009520 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009521 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9522 * this method will create a bitmap of the same size as this view. Because this bitmap
9523 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9524 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9525 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9526 * size than the view. This implies that your application must be able to handle this
9527 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009528 *
Romain Guy0d9275e2010-10-26 14:22:30 -07009529 * <p>You should avoid calling this method when hardware acceleration is enabled. If
9530 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08009531 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07009532 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009533 *
9534 * @see #getDrawingCache()
9535 * @see #destroyDrawingCache()
9536 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009537 public void buildDrawingCache(boolean autoScale) {
9538 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07009539 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009540 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009541
9542 if (ViewDebug.TRACE_HIERARCHY) {
9543 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
9544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009545
Romain Guy8506ab42009-06-11 17:35:47 -07009546 int width = mRight - mLeft;
9547 int height = mBottom - mTop;
9548
9549 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07009550 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07009551
Romain Guye1123222009-06-29 14:24:56 -07009552 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009553 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
9554 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07009555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009556
9557 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07009558 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08009559 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009560
9561 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07009562 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08009563 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009564 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
9565 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08009566 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009567 return;
9568 }
9569
9570 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07009571 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009572
9573 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009574 Bitmap.Config quality;
9575 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08009576 // Never pick ARGB_4444 because it looks awful
9577 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009578 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
9579 case DRAWING_CACHE_QUALITY_AUTO:
9580 quality = Bitmap.Config.ARGB_8888;
9581 break;
9582 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08009583 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009584 break;
9585 case DRAWING_CACHE_QUALITY_HIGH:
9586 quality = Bitmap.Config.ARGB_8888;
9587 break;
9588 default:
9589 quality = Bitmap.Config.ARGB_8888;
9590 break;
9591 }
9592 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07009593 // Optimization for translucent windows
9594 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08009595 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009596 }
9597
9598 // Try to cleanup memory
9599 if (bitmap != null) bitmap.recycle();
9600
9601 try {
9602 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07009603 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07009604 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07009605 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009606 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07009607 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009608 }
Adam Powell26153a32010-11-08 15:22:27 -08009609 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009610 } catch (OutOfMemoryError e) {
9611 // If there is not enough memory to create the bitmap cache, just
9612 // ignore the issue as bitmap caches are not required to draw the
9613 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07009614 if (autoScale) {
9615 mDrawingCache = null;
9616 } else {
9617 mUnscaledDrawingCache = null;
9618 }
Romain Guy0211a0a2011-02-14 16:34:59 -08009619 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009620 return;
9621 }
9622
9623 clear = drawingCacheBackgroundColor != 0;
9624 }
9625
9626 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009627 if (attachInfo != null) {
9628 canvas = attachInfo.mCanvas;
9629 if (canvas == null) {
9630 canvas = new Canvas();
9631 }
9632 canvas.setBitmap(bitmap);
9633 // Temporarily clobber the cached Canvas in case one of our children
9634 // is also using a drawing cache. Without this, the children would
9635 // steal the canvas by attaching their own bitmap to it and bad, bad
9636 // thing would happen (invisible views, corrupted drawings, etc.)
9637 attachInfo.mCanvas = null;
9638 } else {
9639 // This case should hopefully never or seldom happen
9640 canvas = new Canvas(bitmap);
9641 }
9642
9643 if (clear) {
9644 bitmap.eraseColor(drawingCacheBackgroundColor);
9645 }
9646
9647 computeScroll();
9648 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08009649
Romain Guye1123222009-06-29 14:24:56 -07009650 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009651 final float scale = attachInfo.mApplicationScale;
9652 canvas.scale(scale, scale);
9653 }
Joe Malin32736f02011-01-19 16:14:20 -08009654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009655 canvas.translate(-mScrollX, -mScrollY);
9656
Romain Guy5bcdff42009-05-14 21:27:18 -07009657 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08009658 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
9659 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07009660 mPrivateFlags |= DRAWING_CACHE_VALID;
9661 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009662
9663 // Fast path for layouts with no backgrounds
9664 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9665 if (ViewDebug.TRACE_HIERARCHY) {
9666 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9667 }
Romain Guy5bcdff42009-05-14 21:27:18 -07009668 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009669 dispatchDraw(canvas);
9670 } else {
9671 draw(canvas);
9672 }
9673
9674 canvas.restoreToCount(restoreCount);
9675
9676 if (attachInfo != null) {
9677 // Restore the cached Canvas for our siblings
9678 attachInfo.mCanvas = canvas;
9679 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009680 }
9681 }
9682
9683 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009684 * Create a snapshot of the view into a bitmap. We should probably make
9685 * some form of this public, but should think about the API.
9686 */
Romain Guy223ff5c2010-03-02 17:07:47 -08009687 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009688 int width = mRight - mLeft;
9689 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009690
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009691 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07009692 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009693 width = (int) ((width * scale) + 0.5f);
9694 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08009695
Romain Guy8c11e312009-09-14 15:15:30 -07009696 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009697 if (bitmap == null) {
9698 throw new OutOfMemoryError();
9699 }
9700
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009701 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08009702
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009703 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009704 if (attachInfo != null) {
9705 canvas = attachInfo.mCanvas;
9706 if (canvas == null) {
9707 canvas = new Canvas();
9708 }
9709 canvas.setBitmap(bitmap);
9710 // Temporarily clobber the cached Canvas in case one of our children
9711 // is also using a drawing cache. Without this, the children would
9712 // steal the canvas by attaching their own bitmap to it and bad, bad
9713 // things would happen (invisible views, corrupted drawings, etc.)
9714 attachInfo.mCanvas = null;
9715 } else {
9716 // This case should hopefully never or seldom happen
9717 canvas = new Canvas(bitmap);
9718 }
9719
Romain Guy5bcdff42009-05-14 21:27:18 -07009720 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009721 bitmap.eraseColor(backgroundColor);
9722 }
9723
9724 computeScroll();
9725 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009726 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009727 canvas.translate(-mScrollX, -mScrollY);
9728
Romain Guy5bcdff42009-05-14 21:27:18 -07009729 // Temporarily remove the dirty mask
9730 int flags = mPrivateFlags;
9731 mPrivateFlags &= ~DIRTY_MASK;
9732
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009733 // Fast path for layouts with no backgrounds
9734 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9735 dispatchDraw(canvas);
9736 } else {
9737 draw(canvas);
9738 }
9739
Romain Guy5bcdff42009-05-14 21:27:18 -07009740 mPrivateFlags = flags;
9741
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009742 canvas.restoreToCount(restoreCount);
9743
9744 if (attachInfo != null) {
9745 // Restore the cached Canvas for our siblings
9746 attachInfo.mCanvas = canvas;
9747 }
Romain Guy8506ab42009-06-11 17:35:47 -07009748
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009749 return bitmap;
9750 }
9751
9752 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009753 * Indicates whether this View is currently in edit mode. A View is usually
9754 * in edit mode when displayed within a developer tool. For instance, if
9755 * this View is being drawn by a visual user interface builder, this method
9756 * should return true.
9757 *
9758 * Subclasses should check the return value of this method to provide
9759 * different behaviors if their normal behavior might interfere with the
9760 * host environment. For instance: the class spawns a thread in its
9761 * constructor, the drawing code relies on device-specific features, etc.
9762 *
9763 * This method is usually checked in the drawing code of custom widgets.
9764 *
9765 * @return True if this View is in edit mode, false otherwise.
9766 */
9767 public boolean isInEditMode() {
9768 return false;
9769 }
9770
9771 /**
9772 * If the View draws content inside its padding and enables fading edges,
9773 * it needs to support padding offsets. Padding offsets are added to the
9774 * fading edges to extend the length of the fade so that it covers pixels
9775 * drawn inside the padding.
9776 *
9777 * Subclasses of this class should override this method if they need
9778 * to draw content inside the padding.
9779 *
9780 * @return True if padding offset must be applied, false otherwise.
9781 *
9782 * @see #getLeftPaddingOffset()
9783 * @see #getRightPaddingOffset()
9784 * @see #getTopPaddingOffset()
9785 * @see #getBottomPaddingOffset()
9786 *
9787 * @since CURRENT
9788 */
9789 protected boolean isPaddingOffsetRequired() {
9790 return false;
9791 }
9792
9793 /**
9794 * Amount by which to extend the left fading region. Called only when
9795 * {@link #isPaddingOffsetRequired()} returns true.
9796 *
9797 * @return The left padding offset in pixels.
9798 *
9799 * @see #isPaddingOffsetRequired()
9800 *
9801 * @since CURRENT
9802 */
9803 protected int getLeftPaddingOffset() {
9804 return 0;
9805 }
9806
9807 /**
9808 * Amount by which to extend the right fading region. Called only when
9809 * {@link #isPaddingOffsetRequired()} returns true.
9810 *
9811 * @return The right padding offset in pixels.
9812 *
9813 * @see #isPaddingOffsetRequired()
9814 *
9815 * @since CURRENT
9816 */
9817 protected int getRightPaddingOffset() {
9818 return 0;
9819 }
9820
9821 /**
9822 * Amount by which to extend the top fading region. Called only when
9823 * {@link #isPaddingOffsetRequired()} returns true.
9824 *
9825 * @return The top padding offset in pixels.
9826 *
9827 * @see #isPaddingOffsetRequired()
9828 *
9829 * @since CURRENT
9830 */
9831 protected int getTopPaddingOffset() {
9832 return 0;
9833 }
9834
9835 /**
9836 * Amount by which to extend the bottom fading region. Called only when
9837 * {@link #isPaddingOffsetRequired()} returns true.
9838 *
9839 * @return The bottom padding offset in pixels.
9840 *
9841 * @see #isPaddingOffsetRequired()
9842 *
9843 * @since CURRENT
9844 */
9845 protected int getBottomPaddingOffset() {
9846 return 0;
9847 }
9848
9849 /**
Romain Guy2bffd262010-09-12 17:40:02 -07009850 * <p>Indicates whether this view is attached to an hardware accelerated
9851 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009852 *
Romain Guy2bffd262010-09-12 17:40:02 -07009853 * <p>Even if this method returns true, it does not mean that every call
9854 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
9855 * accelerated {@link android.graphics.Canvas}. For instance, if this view
9856 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
9857 * window is hardware accelerated,
9858 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
9859 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009860 *
Romain Guy2bffd262010-09-12 17:40:02 -07009861 * @return True if the view is attached to a window and the window is
9862 * hardware accelerated; false in any other case.
9863 */
9864 public boolean isHardwareAccelerated() {
9865 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
9866 }
Joe Malin32736f02011-01-19 16:14:20 -08009867
Romain Guy2bffd262010-09-12 17:40:02 -07009868 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009869 * Manually render this view (and all of its children) to the given Canvas.
9870 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009871 * called. When implementing a view, implement
9872 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
9873 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009874 *
9875 * @param canvas The Canvas to which the View is rendered.
9876 */
9877 public void draw(Canvas canvas) {
9878 if (ViewDebug.TRACE_HIERARCHY) {
9879 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9880 }
9881
Romain Guy5bcdff42009-05-14 21:27:18 -07009882 final int privateFlags = mPrivateFlags;
9883 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
9884 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
9885 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07009886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009887 /*
9888 * Draw traversal performs several drawing steps which must be executed
9889 * in the appropriate order:
9890 *
9891 * 1. Draw the background
9892 * 2. If necessary, save the canvas' layers to prepare for fading
9893 * 3. Draw view's content
9894 * 4. Draw children
9895 * 5. If necessary, draw the fading edges and restore layers
9896 * 6. Draw decorations (scrollbars for instance)
9897 */
9898
9899 // Step 1, draw the background, if needed
9900 int saveCount;
9901
Romain Guy24443ea2009-05-11 11:56:30 -07009902 if (!dirtyOpaque) {
9903 final Drawable background = mBGDrawable;
9904 if (background != null) {
9905 final int scrollX = mScrollX;
9906 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009907
Romain Guy24443ea2009-05-11 11:56:30 -07009908 if (mBackgroundSizeChanged) {
9909 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
9910 mBackgroundSizeChanged = false;
9911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009912
Romain Guy24443ea2009-05-11 11:56:30 -07009913 if ((scrollX | scrollY) == 0) {
9914 background.draw(canvas);
9915 } else {
9916 canvas.translate(scrollX, scrollY);
9917 background.draw(canvas);
9918 canvas.translate(-scrollX, -scrollY);
9919 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009920 }
9921 }
9922
9923 // skip step 2 & 5 if possible (common case)
9924 final int viewFlags = mViewFlags;
9925 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
9926 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
9927 if (!verticalEdges && !horizontalEdges) {
9928 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009929 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009930
9931 // Step 4, draw the children
9932 dispatchDraw(canvas);
9933
9934 // Step 6, draw decorations (scrollbars)
9935 onDrawScrollBars(canvas);
9936
9937 // we're done...
9938 return;
9939 }
9940
9941 /*
9942 * Here we do the full fledged routine...
9943 * (this is an uncommon case where speed matters less,
9944 * this is why we repeat some of the tests that have been
9945 * done above)
9946 */
9947
9948 boolean drawTop = false;
9949 boolean drawBottom = false;
9950 boolean drawLeft = false;
9951 boolean drawRight = false;
9952
9953 float topFadeStrength = 0.0f;
9954 float bottomFadeStrength = 0.0f;
9955 float leftFadeStrength = 0.0f;
9956 float rightFadeStrength = 0.0f;
9957
9958 // Step 2, save the canvas' layers
9959 int paddingLeft = mPaddingLeft;
9960 int paddingTop = mPaddingTop;
9961
9962 final boolean offsetRequired = isPaddingOffsetRequired();
9963 if (offsetRequired) {
9964 paddingLeft += getLeftPaddingOffset();
9965 paddingTop += getTopPaddingOffset();
9966 }
9967
9968 int left = mScrollX + paddingLeft;
9969 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
9970 int top = mScrollY + paddingTop;
9971 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
9972
9973 if (offsetRequired) {
9974 right += getRightPaddingOffset();
9975 bottom += getBottomPaddingOffset();
9976 }
9977
9978 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009979 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
9980 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009981
9982 // clip the fade length if top and bottom fades overlap
9983 // overlapping fades produce odd-looking artifacts
9984 if (verticalEdges && (top + length > bottom - length)) {
9985 length = (bottom - top) / 2;
9986 }
9987
9988 // also clip horizontal fades if necessary
9989 if (horizontalEdges && (left + length > right - length)) {
9990 length = (right - left) / 2;
9991 }
9992
9993 if (verticalEdges) {
9994 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009995 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009996 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009997 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009998 }
9999
10000 if (horizontalEdges) {
10001 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010002 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010003 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010004 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010005 }
10006
10007 saveCount = canvas.getSaveCount();
10008
10009 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070010010 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010011 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
10012
10013 if (drawTop) {
10014 canvas.saveLayer(left, top, right, top + length, null, flags);
10015 }
10016
10017 if (drawBottom) {
10018 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
10019 }
10020
10021 if (drawLeft) {
10022 canvas.saveLayer(left, top, left + length, bottom, null, flags);
10023 }
10024
10025 if (drawRight) {
10026 canvas.saveLayer(right - length, top, right, bottom, null, flags);
10027 }
10028 } else {
10029 scrollabilityCache.setFadeColor(solidColor);
10030 }
10031
10032 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010033 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010034
10035 // Step 4, draw the children
10036 dispatchDraw(canvas);
10037
10038 // Step 5, draw the fade effect and restore layers
10039 final Paint p = scrollabilityCache.paint;
10040 final Matrix matrix = scrollabilityCache.matrix;
10041 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010042
10043 if (drawTop) {
10044 matrix.setScale(1, fadeHeight * topFadeStrength);
10045 matrix.postTranslate(left, top);
10046 fade.setLocalMatrix(matrix);
10047 canvas.drawRect(left, top, right, top + length, p);
10048 }
10049
10050 if (drawBottom) {
10051 matrix.setScale(1, fadeHeight * bottomFadeStrength);
10052 matrix.postRotate(180);
10053 matrix.postTranslate(left, bottom);
10054 fade.setLocalMatrix(matrix);
10055 canvas.drawRect(left, bottom - length, right, bottom, p);
10056 }
10057
10058 if (drawLeft) {
10059 matrix.setScale(1, fadeHeight * leftFadeStrength);
10060 matrix.postRotate(-90);
10061 matrix.postTranslate(left, top);
10062 fade.setLocalMatrix(matrix);
10063 canvas.drawRect(left, top, left + length, bottom, p);
10064 }
10065
10066 if (drawRight) {
10067 matrix.setScale(1, fadeHeight * rightFadeStrength);
10068 matrix.postRotate(90);
10069 matrix.postTranslate(right, top);
10070 fade.setLocalMatrix(matrix);
10071 canvas.drawRect(right - length, top, right, bottom, p);
10072 }
10073
10074 canvas.restoreToCount(saveCount);
10075
10076 // Step 6, draw decorations (scrollbars)
10077 onDrawScrollBars(canvas);
10078 }
10079
10080 /**
10081 * Override this if your view is known to always be drawn on top of a solid color background,
10082 * and needs to draw fading edges. Returning a non-zero color enables the view system to
10083 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
10084 * should be set to 0xFF.
10085 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010086 * @see #setVerticalFadingEdgeEnabled(boolean)
10087 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010088 *
10089 * @return The known solid color background for this view, or 0 if the color may vary
10090 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010091 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010092 public int getSolidColor() {
10093 return 0;
10094 }
10095
10096 /**
10097 * Build a human readable string representation of the specified view flags.
10098 *
10099 * @param flags the view flags to convert to a string
10100 * @return a String representing the supplied flags
10101 */
10102 private static String printFlags(int flags) {
10103 String output = "";
10104 int numFlags = 0;
10105 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
10106 output += "TAKES_FOCUS";
10107 numFlags++;
10108 }
10109
10110 switch (flags & VISIBILITY_MASK) {
10111 case INVISIBLE:
10112 if (numFlags > 0) {
10113 output += " ";
10114 }
10115 output += "INVISIBLE";
10116 // USELESS HERE numFlags++;
10117 break;
10118 case GONE:
10119 if (numFlags > 0) {
10120 output += " ";
10121 }
10122 output += "GONE";
10123 // USELESS HERE numFlags++;
10124 break;
10125 default:
10126 break;
10127 }
10128 return output;
10129 }
10130
10131 /**
10132 * Build a human readable string representation of the specified private
10133 * view flags.
10134 *
10135 * @param privateFlags the private view flags to convert to a string
10136 * @return a String representing the supplied flags
10137 */
10138 private static String printPrivateFlags(int privateFlags) {
10139 String output = "";
10140 int numFlags = 0;
10141
10142 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
10143 output += "WANTS_FOCUS";
10144 numFlags++;
10145 }
10146
10147 if ((privateFlags & FOCUSED) == FOCUSED) {
10148 if (numFlags > 0) {
10149 output += " ";
10150 }
10151 output += "FOCUSED";
10152 numFlags++;
10153 }
10154
10155 if ((privateFlags & SELECTED) == SELECTED) {
10156 if (numFlags > 0) {
10157 output += " ";
10158 }
10159 output += "SELECTED";
10160 numFlags++;
10161 }
10162
10163 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
10164 if (numFlags > 0) {
10165 output += " ";
10166 }
10167 output += "IS_ROOT_NAMESPACE";
10168 numFlags++;
10169 }
10170
10171 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
10172 if (numFlags > 0) {
10173 output += " ";
10174 }
10175 output += "HAS_BOUNDS";
10176 numFlags++;
10177 }
10178
10179 if ((privateFlags & DRAWN) == DRAWN) {
10180 if (numFlags > 0) {
10181 output += " ";
10182 }
10183 output += "DRAWN";
10184 // USELESS HERE numFlags++;
10185 }
10186 return output;
10187 }
10188
10189 /**
10190 * <p>Indicates whether or not this view's layout will be requested during
10191 * the next hierarchy layout pass.</p>
10192 *
10193 * @return true if the layout will be forced during next layout pass
10194 */
10195 public boolean isLayoutRequested() {
10196 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
10197 }
10198
10199 /**
Cibu Johny86666632010-02-22 13:01:02 -080010200 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
10201 * layout attribute and/or the inherited value from the parent.</p>
10202 *
10203 * @return true if the layout is right-to-left.
10204 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -070010205 @ViewDebug.ExportedProperty(category = "layout")
Cibu Johny86666632010-02-22 13:01:02 -080010206 public boolean isLayoutRtl() {
10207 return (mPrivateFlags2 & RESOLVED_LAYOUT_RTL) == RESOLVED_LAYOUT_RTL;
10208 }
10209
10210 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010211 * Assign a size and position to a view and all of its
10212 * descendants
10213 *
10214 * <p>This is the second phase of the layout mechanism.
10215 * (The first is measuring). In this phase, each parent calls
10216 * layout on all of its children to position them.
10217 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080010218 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010219 *
Chet Haase9c087442011-01-12 16:20:16 -080010220 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010221 * Derived classes with children should override
10222 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080010223 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010224 *
10225 * @param l Left position, relative to parent
10226 * @param t Top position, relative to parent
10227 * @param r Right position, relative to parent
10228 * @param b Bottom position, relative to parent
10229 */
Romain Guy5429e1d2010-09-07 12:38:00 -070010230 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080010231 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070010232 int oldL = mLeft;
10233 int oldT = mTop;
10234 int oldB = mBottom;
10235 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010236 boolean changed = setFrame(l, t, r, b);
10237 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
10238 if (ViewDebug.TRACE_HIERARCHY) {
10239 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
10240 }
10241
10242 onLayout(changed, l, t, r, b);
10243 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070010244
10245 if (mOnLayoutChangeListeners != null) {
10246 ArrayList<OnLayoutChangeListener> listenersCopy =
10247 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
10248 int numListeners = listenersCopy.size();
10249 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070010250 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070010251 }
10252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010253 }
10254 mPrivateFlags &= ~FORCE_LAYOUT;
10255 }
10256
10257 /**
10258 * Called from layout when this view should
10259 * assign a size and position to each of its children.
10260 *
10261 * Derived classes with children should override
10262 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070010263 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010264 * @param changed This is a new size or position for this view
10265 * @param left Left position, relative to parent
10266 * @param top Top position, relative to parent
10267 * @param right Right position, relative to parent
10268 * @param bottom Bottom position, relative to parent
10269 */
10270 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
10271 }
10272
10273 /**
10274 * Assign a size and position to this view.
10275 *
10276 * This is called from layout.
10277 *
10278 * @param left Left position, relative to parent
10279 * @param top Top position, relative to parent
10280 * @param right Right position, relative to parent
10281 * @param bottom Bottom position, relative to parent
10282 * @return true if the new size and position are different than the
10283 * previous ones
10284 * {@hide}
10285 */
10286 protected boolean setFrame(int left, int top, int right, int bottom) {
10287 boolean changed = false;
10288
10289 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070010290 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010291 + right + "," + bottom + ")");
10292 }
10293
10294 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
10295 changed = true;
10296
10297 // Remember our drawn bit
10298 int drawn = mPrivateFlags & DRAWN;
10299
10300 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -080010301 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010302
10303
10304 int oldWidth = mRight - mLeft;
10305 int oldHeight = mBottom - mTop;
10306
10307 mLeft = left;
10308 mTop = top;
10309 mRight = right;
10310 mBottom = bottom;
10311
10312 mPrivateFlags |= HAS_BOUNDS;
10313
10314 int newWidth = right - left;
10315 int newHeight = bottom - top;
10316
10317 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010318 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
10319 // A change in dimension means an auto-centered pivot point changes, too
10320 mMatrixDirty = true;
10321 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010322 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
10323 }
10324
10325 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
10326 // If we are visible, force the DRAWN bit to on so that
10327 // this invalidate will go through (at least to our parent).
10328 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010329 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010330 // the DRAWN bit.
10331 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010332 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -080010333 // parent display list may need to be recreated based on a change in the bounds
10334 // of any child
10335 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010336 }
10337
10338 // Reset drawn bit to original value (invalidate turns it off)
10339 mPrivateFlags |= drawn;
10340
10341 mBackgroundSizeChanged = true;
10342 }
10343 return changed;
10344 }
10345
10346 /**
10347 * Finalize inflating a view from XML. This is called as the last phase
10348 * of inflation, after all child views have been added.
10349 *
10350 * <p>Even if the subclass overrides onFinishInflate, they should always be
10351 * sure to call the super method, so that we get called.
10352 */
10353 protected void onFinishInflate() {
10354 }
10355
10356 /**
10357 * Returns the resources associated with this view.
10358 *
10359 * @return Resources object.
10360 */
10361 public Resources getResources() {
10362 return mResources;
10363 }
10364
10365 /**
10366 * Invalidates the specified Drawable.
10367 *
10368 * @param drawable the drawable to invalidate
10369 */
10370 public void invalidateDrawable(Drawable drawable) {
10371 if (verifyDrawable(drawable)) {
10372 final Rect dirty = drawable.getBounds();
10373 final int scrollX = mScrollX;
10374 final int scrollY = mScrollY;
10375
10376 invalidate(dirty.left + scrollX, dirty.top + scrollY,
10377 dirty.right + scrollX, dirty.bottom + scrollY);
10378 }
10379 }
10380
10381 /**
10382 * Schedules an action on a drawable to occur at a specified time.
10383 *
10384 * @param who the recipient of the action
10385 * @param what the action to run on the drawable
10386 * @param when the time at which the action must occur. Uses the
10387 * {@link SystemClock#uptimeMillis} timebase.
10388 */
10389 public void scheduleDrawable(Drawable who, Runnable what, long when) {
10390 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10391 mAttachInfo.mHandler.postAtTime(what, who, when);
10392 }
10393 }
10394
10395 /**
10396 * Cancels a scheduled action on a drawable.
10397 *
10398 * @param who the recipient of the action
10399 * @param what the action to cancel
10400 */
10401 public void unscheduleDrawable(Drawable who, Runnable what) {
10402 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10403 mAttachInfo.mHandler.removeCallbacks(what, who);
10404 }
10405 }
10406
10407 /**
10408 * Unschedule any events associated with the given Drawable. This can be
10409 * used when selecting a new Drawable into a view, so that the previous
10410 * one is completely unscheduled.
10411 *
10412 * @param who The Drawable to unschedule.
10413 *
10414 * @see #drawableStateChanged
10415 */
10416 public void unscheduleDrawable(Drawable who) {
10417 if (mAttachInfo != null) {
10418 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
10419 }
10420 }
10421
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070010422 /**
10423 * Check if a given Drawable is in RTL layout direction.
10424 *
10425 * @param who the recipient of the action
10426 */
10427 public boolean isLayoutRtl(Drawable who) {
10428 return (who == mBGDrawable) && isLayoutRtl();
10429 }
10430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010431 /**
10432 * If your view subclass is displaying its own Drawable objects, it should
10433 * override this function and return true for any Drawable it is
10434 * displaying. This allows animations for those drawables to be
10435 * scheduled.
10436 *
10437 * <p>Be sure to call through to the super class when overriding this
10438 * function.
10439 *
10440 * @param who The Drawable to verify. Return true if it is one you are
10441 * displaying, else return the result of calling through to the
10442 * super class.
10443 *
10444 * @return boolean If true than the Drawable is being displayed in the
10445 * view; else false and it is not allowed to animate.
10446 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010447 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
10448 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010449 */
10450 protected boolean verifyDrawable(Drawable who) {
10451 return who == mBGDrawable;
10452 }
10453
10454 /**
10455 * This function is called whenever the state of the view changes in such
10456 * a way that it impacts the state of drawables being shown.
10457 *
10458 * <p>Be sure to call through to the superclass when overriding this
10459 * function.
10460 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010461 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010462 */
10463 protected void drawableStateChanged() {
10464 Drawable d = mBGDrawable;
10465 if (d != null && d.isStateful()) {
10466 d.setState(getDrawableState());
10467 }
10468 }
10469
10470 /**
10471 * Call this to force a view to update its drawable state. This will cause
10472 * drawableStateChanged to be called on this view. Views that are interested
10473 * in the new state should call getDrawableState.
10474 *
10475 * @see #drawableStateChanged
10476 * @see #getDrawableState
10477 */
10478 public void refreshDrawableState() {
10479 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
10480 drawableStateChanged();
10481
10482 ViewParent parent = mParent;
10483 if (parent != null) {
10484 parent.childDrawableStateChanged(this);
10485 }
10486 }
10487
10488 /**
10489 * Return an array of resource IDs of the drawable states representing the
10490 * current state of the view.
10491 *
10492 * @return The current drawable state
10493 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010494 * @see Drawable#setState(int[])
10495 * @see #drawableStateChanged()
10496 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010497 */
10498 public final int[] getDrawableState() {
10499 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
10500 return mDrawableState;
10501 } else {
10502 mDrawableState = onCreateDrawableState(0);
10503 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
10504 return mDrawableState;
10505 }
10506 }
10507
10508 /**
10509 * Generate the new {@link android.graphics.drawable.Drawable} state for
10510 * this view. This is called by the view
10511 * system when the cached Drawable state is determined to be invalid. To
10512 * retrieve the current state, you should use {@link #getDrawableState}.
10513 *
10514 * @param extraSpace if non-zero, this is the number of extra entries you
10515 * would like in the returned array in which you can place your own
10516 * states.
10517 *
10518 * @return Returns an array holding the current {@link Drawable} state of
10519 * the view.
10520 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010521 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010522 */
10523 protected int[] onCreateDrawableState(int extraSpace) {
10524 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
10525 mParent instanceof View) {
10526 return ((View) mParent).onCreateDrawableState(extraSpace);
10527 }
10528
10529 int[] drawableState;
10530
10531 int privateFlags = mPrivateFlags;
10532
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010533 int viewStateIndex = 0;
10534 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
10535 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
10536 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070010537 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010538 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
10539 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070010540 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
10541 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010542 // This is set if HW acceleration is requested, even if the current
10543 // process doesn't allow it. This is just to allow app preview
10544 // windows to better match their app.
10545 viewStateIndex |= VIEW_STATE_ACCELERATED;
10546 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070010547 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010548
Christopher Tate3d4bf172011-03-28 16:16:46 -070010549 final int privateFlags2 = mPrivateFlags2;
10550 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
10551 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
10552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010553 drawableState = VIEW_STATE_SETS[viewStateIndex];
10554
10555 //noinspection ConstantIfStatement
10556 if (false) {
10557 Log.i("View", "drawableStateIndex=" + viewStateIndex);
10558 Log.i("View", toString()
10559 + " pressed=" + ((privateFlags & PRESSED) != 0)
10560 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
10561 + " fo=" + hasFocus()
10562 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010563 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010564 + ": " + Arrays.toString(drawableState));
10565 }
10566
10567 if (extraSpace == 0) {
10568 return drawableState;
10569 }
10570
10571 final int[] fullState;
10572 if (drawableState != null) {
10573 fullState = new int[drawableState.length + extraSpace];
10574 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
10575 } else {
10576 fullState = new int[extraSpace];
10577 }
10578
10579 return fullState;
10580 }
10581
10582 /**
10583 * Merge your own state values in <var>additionalState</var> into the base
10584 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070010585 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010586 *
10587 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070010588 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010589 * own additional state values.
10590 *
10591 * @param additionalState The additional state values you would like
10592 * added to <var>baseState</var>; this array is not modified.
10593 *
10594 * @return As a convenience, the <var>baseState</var> array you originally
10595 * passed into the function is returned.
10596 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010597 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010598 */
10599 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
10600 final int N = baseState.length;
10601 int i = N - 1;
10602 while (i >= 0 && baseState[i] == 0) {
10603 i--;
10604 }
10605 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
10606 return baseState;
10607 }
10608
10609 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070010610 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
10611 * on all Drawable objects associated with this view.
10612 */
10613 public void jumpDrawablesToCurrentState() {
10614 if (mBGDrawable != null) {
10615 mBGDrawable.jumpToCurrentState();
10616 }
10617 }
10618
10619 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010620 * Sets the background color for this view.
10621 * @param color the color of the background
10622 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010623 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010624 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070010625 if (mBGDrawable instanceof ColorDrawable) {
10626 ((ColorDrawable) mBGDrawable).setColor(color);
10627 } else {
10628 setBackgroundDrawable(new ColorDrawable(color));
10629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010630 }
10631
10632 /**
10633 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070010634 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010635 * @param resid The identifier of the resource.
10636 * @attr ref android.R.styleable#View_background
10637 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010638 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010639 public void setBackgroundResource(int resid) {
10640 if (resid != 0 && resid == mBackgroundResource) {
10641 return;
10642 }
10643
10644 Drawable d= null;
10645 if (resid != 0) {
10646 d = mResources.getDrawable(resid);
10647 }
10648 setBackgroundDrawable(d);
10649
10650 mBackgroundResource = resid;
10651 }
10652
10653 /**
10654 * Set the background to a given Drawable, or remove the background. If the
10655 * background has padding, this View's padding is set to the background's
10656 * padding. However, when a background is removed, this View's padding isn't
10657 * touched. If setting the padding is desired, please use
10658 * {@link #setPadding(int, int, int, int)}.
10659 *
10660 * @param d The Drawable to use as the background, or null to remove the
10661 * background
10662 */
10663 public void setBackgroundDrawable(Drawable d) {
10664 boolean requestLayout = false;
10665
10666 mBackgroundResource = 0;
10667
10668 /*
10669 * Regardless of whether we're setting a new background or not, we want
10670 * to clear the previous drawable.
10671 */
10672 if (mBGDrawable != null) {
10673 mBGDrawable.setCallback(null);
10674 unscheduleDrawable(mBGDrawable);
10675 }
10676
10677 if (d != null) {
10678 Rect padding = sThreadLocal.get();
10679 if (padding == null) {
10680 padding = new Rect();
10681 sThreadLocal.set(padding);
10682 }
10683 if (d.getPadding(padding)) {
10684 setPadding(padding.left, padding.top, padding.right, padding.bottom);
10685 }
10686
10687 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
10688 // if it has a different minimum size, we should layout again
10689 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
10690 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
10691 requestLayout = true;
10692 }
10693
10694 d.setCallback(this);
10695 if (d.isStateful()) {
10696 d.setState(getDrawableState());
10697 }
10698 d.setVisible(getVisibility() == VISIBLE, false);
10699 mBGDrawable = d;
10700
10701 if ((mPrivateFlags & SKIP_DRAW) != 0) {
10702 mPrivateFlags &= ~SKIP_DRAW;
10703 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
10704 requestLayout = true;
10705 }
10706 } else {
10707 /* Remove the background */
10708 mBGDrawable = null;
10709
10710 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
10711 /*
10712 * This view ONLY drew the background before and we're removing
10713 * the background, so now it won't draw anything
10714 * (hence we SKIP_DRAW)
10715 */
10716 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
10717 mPrivateFlags |= SKIP_DRAW;
10718 }
10719
10720 /*
10721 * When the background is set, we try to apply its padding to this
10722 * View. When the background is removed, we don't touch this View's
10723 * padding. This is noted in the Javadocs. Hence, we don't need to
10724 * requestLayout(), the invalidate() below is sufficient.
10725 */
10726
10727 // The old background's minimum size could have affected this
10728 // View's layout, so let's requestLayout
10729 requestLayout = true;
10730 }
10731
Romain Guy8f1344f52009-05-15 16:03:59 -070010732 computeOpaqueFlags();
10733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010734 if (requestLayout) {
10735 requestLayout();
10736 }
10737
10738 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010739 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010740 }
10741
10742 /**
10743 * Gets the background drawable
10744 * @return The drawable used as the background for this view, if any.
10745 */
10746 public Drawable getBackground() {
10747 return mBGDrawable;
10748 }
10749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010750 /**
10751 * Sets the padding. The view may add on the space required to display
10752 * the scrollbars, depending on the style and visibility of the scrollbars.
10753 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
10754 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
10755 * from the values set in this call.
10756 *
10757 * @attr ref android.R.styleable#View_padding
10758 * @attr ref android.R.styleable#View_paddingBottom
10759 * @attr ref android.R.styleable#View_paddingLeft
10760 * @attr ref android.R.styleable#View_paddingRight
10761 * @attr ref android.R.styleable#View_paddingTop
10762 * @param left the left padding in pixels
10763 * @param top the top padding in pixels
10764 * @param right the right padding in pixels
10765 * @param bottom the bottom padding in pixels
10766 */
10767 public void setPadding(int left, int top, int right, int bottom) {
10768 boolean changed = false;
10769
Adam Powell20232d02010-12-08 21:08:53 -080010770 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010771 mUserPaddingRight = right;
10772 mUserPaddingBottom = bottom;
10773
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010774 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070010775
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010776 // Common case is there are no scroll bars.
10777 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010778 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080010779 // TODO Determine what to do with SCROLLBAR_POSITION_DEFAULT based on RTL settings.
10780 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010781 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080010782 switch (mVerticalScrollbarPosition) {
10783 case SCROLLBAR_POSITION_DEFAULT:
10784 case SCROLLBAR_POSITION_RIGHT:
10785 right += offset;
10786 break;
10787 case SCROLLBAR_POSITION_LEFT:
10788 left += offset;
10789 break;
10790 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010791 }
Adam Powell20232d02010-12-08 21:08:53 -080010792 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010793 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
10794 ? 0 : getHorizontalScrollbarHeight();
10795 }
10796 }
Romain Guy8506ab42009-06-11 17:35:47 -070010797
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010798 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010799 changed = true;
10800 mPaddingLeft = left;
10801 }
10802 if (mPaddingTop != top) {
10803 changed = true;
10804 mPaddingTop = top;
10805 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010806 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010807 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010808 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010809 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010810 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010811 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010812 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010813 }
10814
10815 if (changed) {
10816 requestLayout();
10817 }
10818 }
10819
10820 /**
10821 * Returns the top padding of this view.
10822 *
10823 * @return the top padding in pixels
10824 */
10825 public int getPaddingTop() {
10826 return mPaddingTop;
10827 }
10828
10829 /**
10830 * Returns the bottom padding of this view. If there are inset and enabled
10831 * scrollbars, this value may include the space required to display the
10832 * scrollbars as well.
10833 *
10834 * @return the bottom padding in pixels
10835 */
10836 public int getPaddingBottom() {
10837 return mPaddingBottom;
10838 }
10839
10840 /**
10841 * Returns the left padding of this view. If there are inset and enabled
10842 * scrollbars, this value may include the space required to display the
10843 * scrollbars as well.
10844 *
10845 * @return the left padding in pixels
10846 */
10847 public int getPaddingLeft() {
10848 return mPaddingLeft;
10849 }
10850
10851 /**
10852 * Returns the right padding of this view. If there are inset and enabled
10853 * scrollbars, this value may include the space required to display the
10854 * scrollbars as well.
10855 *
10856 * @return the right padding in pixels
10857 */
10858 public int getPaddingRight() {
10859 return mPaddingRight;
10860 }
10861
10862 /**
10863 * Changes the selection state of this view. A view can be selected or not.
10864 * Note that selection is not the same as focus. Views are typically
10865 * selected in the context of an AdapterView like ListView or GridView;
10866 * the selected view is the view that is highlighted.
10867 *
10868 * @param selected true if the view must be selected, false otherwise
10869 */
10870 public void setSelected(boolean selected) {
10871 if (((mPrivateFlags & SELECTED) != 0) != selected) {
10872 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070010873 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080010874 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010875 refreshDrawableState();
10876 dispatchSetSelected(selected);
10877 }
10878 }
10879
10880 /**
10881 * Dispatch setSelected to all of this View's children.
10882 *
10883 * @see #setSelected(boolean)
10884 *
10885 * @param selected The new selected state
10886 */
10887 protected void dispatchSetSelected(boolean selected) {
10888 }
10889
10890 /**
10891 * Indicates the selection state of this view.
10892 *
10893 * @return true if the view is selected, false otherwise
10894 */
10895 @ViewDebug.ExportedProperty
10896 public boolean isSelected() {
10897 return (mPrivateFlags & SELECTED) != 0;
10898 }
10899
10900 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010901 * Changes the activated state of this view. A view can be activated or not.
10902 * Note that activation is not the same as selection. Selection is
10903 * a transient property, representing the view (hierarchy) the user is
10904 * currently interacting with. Activation is a longer-term state that the
10905 * user can move views in and out of. For example, in a list view with
10906 * single or multiple selection enabled, the views in the current selection
10907 * set are activated. (Um, yeah, we are deeply sorry about the terminology
10908 * here.) The activated state is propagated down to children of the view it
10909 * is set on.
10910 *
10911 * @param activated true if the view must be activated, false otherwise
10912 */
10913 public void setActivated(boolean activated) {
10914 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
10915 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010916 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010917 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070010918 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010919 }
10920 }
10921
10922 /**
10923 * Dispatch setActivated to all of this View's children.
10924 *
10925 * @see #setActivated(boolean)
10926 *
10927 * @param activated The new activated state
10928 */
10929 protected void dispatchSetActivated(boolean activated) {
10930 }
10931
10932 /**
10933 * Indicates the activation state of this view.
10934 *
10935 * @return true if the view is activated, false otherwise
10936 */
10937 @ViewDebug.ExportedProperty
10938 public boolean isActivated() {
10939 return (mPrivateFlags & ACTIVATED) != 0;
10940 }
10941
10942 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010943 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
10944 * observer can be used to get notifications when global events, like
10945 * layout, happen.
10946 *
10947 * The returned ViewTreeObserver observer is not guaranteed to remain
10948 * valid for the lifetime of this View. If the caller of this method keeps
10949 * a long-lived reference to ViewTreeObserver, it should always check for
10950 * the return value of {@link ViewTreeObserver#isAlive()}.
10951 *
10952 * @return The ViewTreeObserver for this view's hierarchy.
10953 */
10954 public ViewTreeObserver getViewTreeObserver() {
10955 if (mAttachInfo != null) {
10956 return mAttachInfo.mTreeObserver;
10957 }
10958 if (mFloatingTreeObserver == null) {
10959 mFloatingTreeObserver = new ViewTreeObserver();
10960 }
10961 return mFloatingTreeObserver;
10962 }
10963
10964 /**
10965 * <p>Finds the topmost view in the current view hierarchy.</p>
10966 *
10967 * @return the topmost view containing this view
10968 */
10969 public View getRootView() {
10970 if (mAttachInfo != null) {
10971 final View v = mAttachInfo.mRootView;
10972 if (v != null) {
10973 return v;
10974 }
10975 }
Romain Guy8506ab42009-06-11 17:35:47 -070010976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010977 View parent = this;
10978
10979 while (parent.mParent != null && parent.mParent instanceof View) {
10980 parent = (View) parent.mParent;
10981 }
10982
10983 return parent;
10984 }
10985
10986 /**
10987 * <p>Computes the coordinates of this view on the screen. The argument
10988 * must be an array of two integers. After the method returns, the array
10989 * contains the x and y location in that order.</p>
10990 *
10991 * @param location an array of two integers in which to hold the coordinates
10992 */
10993 public void getLocationOnScreen(int[] location) {
10994 getLocationInWindow(location);
10995
10996 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070010997 if (info != null) {
10998 location[0] += info.mWindowLeft;
10999 location[1] += info.mWindowTop;
11000 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011001 }
11002
11003 /**
11004 * <p>Computes the coordinates of this view in its window. The argument
11005 * must be an array of two integers. After the method returns, the array
11006 * contains the x and y location in that order.</p>
11007 *
11008 * @param location an array of two integers in which to hold the coordinates
11009 */
11010 public void getLocationInWindow(int[] location) {
11011 if (location == null || location.length < 2) {
11012 throw new IllegalArgumentException("location must be an array of "
11013 + "two integers");
11014 }
11015
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011016 location[0] = mLeft + (int) (mTranslationX + 0.5f);
11017 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011018
11019 ViewParent viewParent = mParent;
11020 while (viewParent instanceof View) {
11021 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011022 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
11023 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011024 viewParent = view.mParent;
11025 }
Romain Guy8506ab42009-06-11 17:35:47 -070011026
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011027 if (viewParent instanceof ViewAncestor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011028 // *cough*
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011029 final ViewAncestor vr = (ViewAncestor)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011030 location[1] -= vr.mCurScrollY;
11031 }
11032 }
11033
11034 /**
11035 * {@hide}
11036 * @param id the id of the view to be found
11037 * @return the view of the specified id, null if cannot be found
11038 */
11039 protected View findViewTraversal(int id) {
11040 if (id == mID) {
11041 return this;
11042 }
11043 return null;
11044 }
11045
11046 /**
11047 * {@hide}
11048 * @param tag the tag of the view to be found
11049 * @return the view of specified tag, null if cannot be found
11050 */
11051 protected View findViewWithTagTraversal(Object tag) {
11052 if (tag != null && tag.equals(mTag)) {
11053 return this;
11054 }
11055 return null;
11056 }
11057
11058 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011059 * {@hide}
11060 * @param predicate The predicate to evaluate.
11061 * @return The first view that matches the predicate or null.
11062 */
11063 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
11064 if (predicate.apply(this)) {
11065 return this;
11066 }
11067 return null;
11068 }
11069
11070 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011071 * Look for a child view with the given id. If this view has the given
11072 * id, return this view.
11073 *
11074 * @param id The id to search for.
11075 * @return The view that has the given id in the hierarchy or null
11076 */
11077 public final View findViewById(int id) {
11078 if (id < 0) {
11079 return null;
11080 }
11081 return findViewTraversal(id);
11082 }
11083
11084 /**
11085 * Look for a child view with the given tag. If this view has the given
11086 * tag, return this view.
11087 *
11088 * @param tag The tag to search for, using "tag.equals(getTag())".
11089 * @return The View that has the given tag in the hierarchy or null
11090 */
11091 public final View findViewWithTag(Object tag) {
11092 if (tag == null) {
11093 return null;
11094 }
11095 return findViewWithTagTraversal(tag);
11096 }
11097
11098 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011099 * {@hide}
11100 * Look for a child view that matches the specified predicate.
11101 * If this view matches the predicate, return this view.
11102 *
11103 * @param predicate The predicate to evaluate.
11104 * @return The first view that matches the predicate or null.
11105 */
11106 public final View findViewByPredicate(Predicate<View> predicate) {
11107 return findViewByPredicateTraversal(predicate);
11108 }
11109
11110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011111 * Sets the identifier for this view. The identifier does not have to be
11112 * unique in this view's hierarchy. The identifier should be a positive
11113 * number.
11114 *
11115 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070011116 * @see #getId()
11117 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011118 *
11119 * @param id a number used to identify the view
11120 *
11121 * @attr ref android.R.styleable#View_id
11122 */
11123 public void setId(int id) {
11124 mID = id;
11125 }
11126
11127 /**
11128 * {@hide}
11129 *
11130 * @param isRoot true if the view belongs to the root namespace, false
11131 * otherwise
11132 */
11133 public void setIsRootNamespace(boolean isRoot) {
11134 if (isRoot) {
11135 mPrivateFlags |= IS_ROOT_NAMESPACE;
11136 } else {
11137 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
11138 }
11139 }
11140
11141 /**
11142 * {@hide}
11143 *
11144 * @return true if the view belongs to the root namespace, false otherwise
11145 */
11146 public boolean isRootNamespace() {
11147 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
11148 }
11149
11150 /**
11151 * Returns this view's identifier.
11152 *
11153 * @return a positive integer used to identify the view or {@link #NO_ID}
11154 * if the view has no ID
11155 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011156 * @see #setId(int)
11157 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011158 * @attr ref android.R.styleable#View_id
11159 */
11160 @ViewDebug.CapturedViewProperty
11161 public int getId() {
11162 return mID;
11163 }
11164
11165 /**
11166 * Returns this view's tag.
11167 *
11168 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070011169 *
11170 * @see #setTag(Object)
11171 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011172 */
11173 @ViewDebug.ExportedProperty
11174 public Object getTag() {
11175 return mTag;
11176 }
11177
11178 /**
11179 * Sets the tag associated with this view. A tag can be used to mark
11180 * a view in its hierarchy and does not have to be unique within the
11181 * hierarchy. Tags can also be used to store data within a view without
11182 * resorting to another data structure.
11183 *
11184 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070011185 *
11186 * @see #getTag()
11187 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011188 */
11189 public void setTag(final Object tag) {
11190 mTag = tag;
11191 }
11192
11193 /**
Romain Guyd90a3312009-05-06 14:54:28 -070011194 * Returns the tag associated with this view and the specified key.
11195 *
11196 * @param key The key identifying the tag
11197 *
11198 * @return the Object stored in this view as a tag
11199 *
11200 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070011201 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070011202 */
11203 public Object getTag(int key) {
11204 SparseArray<Object> tags = null;
11205 synchronized (sTagsLock) {
11206 if (sTags != null) {
11207 tags = sTags.get(this);
11208 }
11209 }
11210
11211 if (tags != null) return tags.get(key);
11212 return null;
11213 }
11214
11215 /**
11216 * Sets a tag associated with this view and a key. A tag can be used
11217 * to mark a view in its hierarchy and does not have to be unique within
11218 * the hierarchy. Tags can also be used to store data within a view
11219 * without resorting to another data structure.
11220 *
11221 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070011222 * application to ensure it is unique (see the <a
11223 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
11224 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070011225 * the Android framework or not associated with any package will cause
11226 * an {@link IllegalArgumentException} to be thrown.
11227 *
11228 * @param key The key identifying the tag
11229 * @param tag An Object to tag the view with
11230 *
11231 * @throws IllegalArgumentException If they specified key is not valid
11232 *
11233 * @see #setTag(Object)
11234 * @see #getTag(int)
11235 */
11236 public void setTag(int key, final Object tag) {
11237 // If the package id is 0x00 or 0x01, it's either an undefined package
11238 // or a framework id
11239 if ((key >>> 24) < 2) {
11240 throw new IllegalArgumentException("The key must be an application-specific "
11241 + "resource id.");
11242 }
11243
11244 setTagInternal(this, key, tag);
11245 }
11246
11247 /**
11248 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
11249 * framework id.
11250 *
11251 * @hide
11252 */
11253 public void setTagInternal(int key, Object tag) {
11254 if ((key >>> 24) != 0x1) {
11255 throw new IllegalArgumentException("The key must be a framework-specific "
11256 + "resource id.");
11257 }
11258
Romain Guy8506ab42009-06-11 17:35:47 -070011259 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070011260 }
11261
11262 private static void setTagInternal(View view, int key, Object tag) {
11263 SparseArray<Object> tags = null;
11264 synchronized (sTagsLock) {
11265 if (sTags == null) {
11266 sTags = new WeakHashMap<View, SparseArray<Object>>();
11267 } else {
11268 tags = sTags.get(view);
11269 }
11270 }
11271
11272 if (tags == null) {
11273 tags = new SparseArray<Object>(2);
11274 synchronized (sTagsLock) {
11275 sTags.put(view, tags);
11276 }
11277 }
11278
11279 tags.put(key, tag);
11280 }
11281
11282 /**
Romain Guy13922e02009-05-12 17:56:14 -070011283 * @param consistency The type of consistency. See ViewDebug for more information.
11284 *
11285 * @hide
11286 */
11287 protected boolean dispatchConsistencyCheck(int consistency) {
11288 return onConsistencyCheck(consistency);
11289 }
11290
11291 /**
11292 * Method that subclasses should implement to check their consistency. The type of
11293 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070011294 *
Romain Guy13922e02009-05-12 17:56:14 -070011295 * @param consistency The type of consistency. See ViewDebug for more information.
11296 *
11297 * @throws IllegalStateException if the view is in an inconsistent state.
11298 *
11299 * @hide
11300 */
11301 protected boolean onConsistencyCheck(int consistency) {
11302 boolean result = true;
11303
11304 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
11305 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
11306
11307 if (checkLayout) {
11308 if (getParent() == null) {
11309 result = false;
11310 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11311 "View " + this + " does not have a parent.");
11312 }
11313
11314 if (mAttachInfo == null) {
11315 result = false;
11316 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11317 "View " + this + " is not attached to a window.");
11318 }
11319 }
11320
11321 if (checkDrawing) {
11322 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
11323 // from their draw() method
11324
11325 if ((mPrivateFlags & DRAWN) != DRAWN &&
11326 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
11327 result = false;
11328 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11329 "View " + this + " was invalidated but its drawing cache is valid.");
11330 }
11331 }
11332
11333 return result;
11334 }
11335
11336 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011337 * Prints information about this view in the log output, with the tag
11338 * {@link #VIEW_LOG_TAG}.
11339 *
11340 * @hide
11341 */
11342 public void debug() {
11343 debug(0);
11344 }
11345
11346 /**
11347 * Prints information about this view in the log output, with the tag
11348 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
11349 * indentation defined by the <code>depth</code>.
11350 *
11351 * @param depth the indentation level
11352 *
11353 * @hide
11354 */
11355 protected void debug(int depth) {
11356 String output = debugIndent(depth - 1);
11357
11358 output += "+ " + this;
11359 int id = getId();
11360 if (id != -1) {
11361 output += " (id=" + id + ")";
11362 }
11363 Object tag = getTag();
11364 if (tag != null) {
11365 output += " (tag=" + tag + ")";
11366 }
11367 Log.d(VIEW_LOG_TAG, output);
11368
11369 if ((mPrivateFlags & FOCUSED) != 0) {
11370 output = debugIndent(depth) + " FOCUSED";
11371 Log.d(VIEW_LOG_TAG, output);
11372 }
11373
11374 output = debugIndent(depth);
11375 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
11376 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
11377 + "} ";
11378 Log.d(VIEW_LOG_TAG, output);
11379
11380 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
11381 || mPaddingBottom != 0) {
11382 output = debugIndent(depth);
11383 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
11384 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
11385 Log.d(VIEW_LOG_TAG, output);
11386 }
11387
11388 output = debugIndent(depth);
11389 output += "mMeasureWidth=" + mMeasuredWidth +
11390 " mMeasureHeight=" + mMeasuredHeight;
11391 Log.d(VIEW_LOG_TAG, output);
11392
11393 output = debugIndent(depth);
11394 if (mLayoutParams == null) {
11395 output += "BAD! no layout params";
11396 } else {
11397 output = mLayoutParams.debug(output);
11398 }
11399 Log.d(VIEW_LOG_TAG, output);
11400
11401 output = debugIndent(depth);
11402 output += "flags={";
11403 output += View.printFlags(mViewFlags);
11404 output += "}";
11405 Log.d(VIEW_LOG_TAG, output);
11406
11407 output = debugIndent(depth);
11408 output += "privateFlags={";
11409 output += View.printPrivateFlags(mPrivateFlags);
11410 output += "}";
11411 Log.d(VIEW_LOG_TAG, output);
11412 }
11413
11414 /**
11415 * Creates an string of whitespaces used for indentation.
11416 *
11417 * @param depth the indentation level
11418 * @return a String containing (depth * 2 + 3) * 2 white spaces
11419 *
11420 * @hide
11421 */
11422 protected static String debugIndent(int depth) {
11423 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
11424 for (int i = 0; i < (depth * 2) + 3; i++) {
11425 spaces.append(' ').append(' ');
11426 }
11427 return spaces.toString();
11428 }
11429
11430 /**
11431 * <p>Return the offset of the widget's text baseline from the widget's top
11432 * boundary. If this widget does not support baseline alignment, this
11433 * method returns -1. </p>
11434 *
11435 * @return the offset of the baseline within the widget's bounds or -1
11436 * if baseline alignment is not supported
11437 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011438 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011439 public int getBaseline() {
11440 return -1;
11441 }
11442
11443 /**
11444 * Call this when something has changed which has invalidated the
11445 * layout of this view. This will schedule a layout pass of the view
11446 * tree.
11447 */
11448 public void requestLayout() {
11449 if (ViewDebug.TRACE_HIERARCHY) {
11450 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
11451 }
11452
11453 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011454 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011455
11456 if (mParent != null && !mParent.isLayoutRequested()) {
11457 mParent.requestLayout();
11458 }
11459 }
11460
11461 /**
11462 * Forces this view to be laid out during the next layout pass.
11463 * This method does not call requestLayout() or forceLayout()
11464 * on the parent.
11465 */
11466 public void forceLayout() {
11467 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011468 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011469 }
11470
11471 /**
11472 * <p>
11473 * This is called to find out how big a view should be. The parent
11474 * supplies constraint information in the width and height parameters.
11475 * </p>
11476 *
11477 * <p>
11478 * The actual mesurement work of a view is performed in
11479 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
11480 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
11481 * </p>
11482 *
11483 *
11484 * @param widthMeasureSpec Horizontal space requirements as imposed by the
11485 * parent
11486 * @param heightMeasureSpec Vertical space requirements as imposed by the
11487 * parent
11488 *
11489 * @see #onMeasure(int, int)
11490 */
11491 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
11492 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
11493 widthMeasureSpec != mOldWidthMeasureSpec ||
11494 heightMeasureSpec != mOldHeightMeasureSpec) {
11495
11496 // first clears the measured dimension flag
11497 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
11498
11499 if (ViewDebug.TRACE_HIERARCHY) {
11500 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
11501 }
11502
11503 // measure ourselves, this should set the measured dimension flag back
11504 onMeasure(widthMeasureSpec, heightMeasureSpec);
11505
11506 // flag not set, setMeasuredDimension() was not invoked, we raise
11507 // an exception to warn the developer
11508 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
11509 throw new IllegalStateException("onMeasure() did not set the"
11510 + " measured dimension by calling"
11511 + " setMeasuredDimension()");
11512 }
11513
11514 mPrivateFlags |= LAYOUT_REQUIRED;
11515 }
11516
11517 mOldWidthMeasureSpec = widthMeasureSpec;
11518 mOldHeightMeasureSpec = heightMeasureSpec;
11519 }
11520
11521 /**
11522 * <p>
11523 * Measure the view and its content to determine the measured width and the
11524 * measured height. This method is invoked by {@link #measure(int, int)} and
11525 * should be overriden by subclasses to provide accurate and efficient
11526 * measurement of their contents.
11527 * </p>
11528 *
11529 * <p>
11530 * <strong>CONTRACT:</strong> When overriding this method, you
11531 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
11532 * measured width and height of this view. Failure to do so will trigger an
11533 * <code>IllegalStateException</code>, thrown by
11534 * {@link #measure(int, int)}. Calling the superclass'
11535 * {@link #onMeasure(int, int)} is a valid use.
11536 * </p>
11537 *
11538 * <p>
11539 * The base class implementation of measure defaults to the background size,
11540 * unless a larger size is allowed by the MeasureSpec. Subclasses should
11541 * override {@link #onMeasure(int, int)} to provide better measurements of
11542 * their content.
11543 * </p>
11544 *
11545 * <p>
11546 * If this method is overridden, it is the subclass's responsibility to make
11547 * sure the measured height and width are at least the view's minimum height
11548 * and width ({@link #getSuggestedMinimumHeight()} and
11549 * {@link #getSuggestedMinimumWidth()}).
11550 * </p>
11551 *
11552 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
11553 * The requirements are encoded with
11554 * {@link android.view.View.MeasureSpec}.
11555 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
11556 * The requirements are encoded with
11557 * {@link android.view.View.MeasureSpec}.
11558 *
11559 * @see #getMeasuredWidth()
11560 * @see #getMeasuredHeight()
11561 * @see #setMeasuredDimension(int, int)
11562 * @see #getSuggestedMinimumHeight()
11563 * @see #getSuggestedMinimumWidth()
11564 * @see android.view.View.MeasureSpec#getMode(int)
11565 * @see android.view.View.MeasureSpec#getSize(int)
11566 */
11567 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
11568 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
11569 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
11570 }
11571
11572 /**
11573 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
11574 * measured width and measured height. Failing to do so will trigger an
11575 * exception at measurement time.</p>
11576 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080011577 * @param measuredWidth The measured width of this view. May be a complex
11578 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11579 * {@link #MEASURED_STATE_TOO_SMALL}.
11580 * @param measuredHeight The measured height of this view. May be a complex
11581 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11582 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011583 */
11584 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
11585 mMeasuredWidth = measuredWidth;
11586 mMeasuredHeight = measuredHeight;
11587
11588 mPrivateFlags |= MEASURED_DIMENSION_SET;
11589 }
11590
11591 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080011592 * Merge two states as returned by {@link #getMeasuredState()}.
11593 * @param curState The current state as returned from a view or the result
11594 * of combining multiple views.
11595 * @param newState The new view state to combine.
11596 * @return Returns a new integer reflecting the combination of the two
11597 * states.
11598 */
11599 public static int combineMeasuredStates(int curState, int newState) {
11600 return curState | newState;
11601 }
11602
11603 /**
11604 * Version of {@link #resolveSizeAndState(int, int, int)}
11605 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
11606 */
11607 public static int resolveSize(int size, int measureSpec) {
11608 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
11609 }
11610
11611 /**
11612 * Utility to reconcile a desired size and state, with constraints imposed
11613 * by a MeasureSpec. Will take the desired size, unless a different size
11614 * is imposed by the constraints. The returned value is a compound integer,
11615 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
11616 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
11617 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011618 *
11619 * @param size How big the view wants to be
11620 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080011621 * @return Size information bit mask as defined by
11622 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011623 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080011624 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011625 int result = size;
11626 int specMode = MeasureSpec.getMode(measureSpec);
11627 int specSize = MeasureSpec.getSize(measureSpec);
11628 switch (specMode) {
11629 case MeasureSpec.UNSPECIFIED:
11630 result = size;
11631 break;
11632 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080011633 if (specSize < size) {
11634 result = specSize | MEASURED_STATE_TOO_SMALL;
11635 } else {
11636 result = size;
11637 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011638 break;
11639 case MeasureSpec.EXACTLY:
11640 result = specSize;
11641 break;
11642 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080011643 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011644 }
11645
11646 /**
11647 * Utility to return a default size. Uses the supplied size if the
11648 * MeasureSpec imposed no contraints. Will get larger if allowed
11649 * by the MeasureSpec.
11650 *
11651 * @param size Default size for this view
11652 * @param measureSpec Constraints imposed by the parent
11653 * @return The size this view should be.
11654 */
11655 public static int getDefaultSize(int size, int measureSpec) {
11656 int result = size;
11657 int specMode = MeasureSpec.getMode(measureSpec);
11658 int specSize = MeasureSpec.getSize(measureSpec);
11659
11660 switch (specMode) {
11661 case MeasureSpec.UNSPECIFIED:
11662 result = size;
11663 break;
11664 case MeasureSpec.AT_MOST:
11665 case MeasureSpec.EXACTLY:
11666 result = specSize;
11667 break;
11668 }
11669 return result;
11670 }
11671
11672 /**
11673 * Returns the suggested minimum height that the view should use. This
11674 * returns the maximum of the view's minimum height
11675 * and the background's minimum height
11676 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
11677 * <p>
11678 * When being used in {@link #onMeasure(int, int)}, the caller should still
11679 * ensure the returned height is within the requirements of the parent.
11680 *
11681 * @return The suggested minimum height of the view.
11682 */
11683 protected int getSuggestedMinimumHeight() {
11684 int suggestedMinHeight = mMinHeight;
11685
11686 if (mBGDrawable != null) {
11687 final int bgMinHeight = mBGDrawable.getMinimumHeight();
11688 if (suggestedMinHeight < bgMinHeight) {
11689 suggestedMinHeight = bgMinHeight;
11690 }
11691 }
11692
11693 return suggestedMinHeight;
11694 }
11695
11696 /**
11697 * Returns the suggested minimum width that the view should use. This
11698 * returns the maximum of the view's minimum width)
11699 * and the background's minimum width
11700 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
11701 * <p>
11702 * When being used in {@link #onMeasure(int, int)}, the caller should still
11703 * ensure the returned width is within the requirements of the parent.
11704 *
11705 * @return The suggested minimum width of the view.
11706 */
11707 protected int getSuggestedMinimumWidth() {
11708 int suggestedMinWidth = mMinWidth;
11709
11710 if (mBGDrawable != null) {
11711 final int bgMinWidth = mBGDrawable.getMinimumWidth();
11712 if (suggestedMinWidth < bgMinWidth) {
11713 suggestedMinWidth = bgMinWidth;
11714 }
11715 }
11716
11717 return suggestedMinWidth;
11718 }
11719
11720 /**
11721 * Sets the minimum height of the view. It is not guaranteed the view will
11722 * be able to achieve this minimum height (for example, if its parent layout
11723 * constrains it with less available height).
11724 *
11725 * @param minHeight The minimum height the view will try to be.
11726 */
11727 public void setMinimumHeight(int minHeight) {
11728 mMinHeight = minHeight;
11729 }
11730
11731 /**
11732 * Sets the minimum width of the view. It is not guaranteed the view will
11733 * be able to achieve this minimum width (for example, if its parent layout
11734 * constrains it with less available width).
11735 *
11736 * @param minWidth The minimum width the view will try to be.
11737 */
11738 public void setMinimumWidth(int minWidth) {
11739 mMinWidth = minWidth;
11740 }
11741
11742 /**
11743 * Get the animation currently associated with this view.
11744 *
11745 * @return The animation that is currently playing or
11746 * scheduled to play for this view.
11747 */
11748 public Animation getAnimation() {
11749 return mCurrentAnimation;
11750 }
11751
11752 /**
11753 * Start the specified animation now.
11754 *
11755 * @param animation the animation to start now
11756 */
11757 public void startAnimation(Animation animation) {
11758 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
11759 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011760 invalidateParentCaches();
11761 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011762 }
11763
11764 /**
11765 * Cancels any animations for this view.
11766 */
11767 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080011768 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080011769 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080011770 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011771 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011772 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011773 }
11774
11775 /**
11776 * Sets the next animation to play for this view.
11777 * If you want the animation to play immediately, use
11778 * startAnimation. This method provides allows fine-grained
11779 * control over the start time and invalidation, but you
11780 * must make sure that 1) the animation has a start time set, and
11781 * 2) the view will be invalidated when the animation is supposed to
11782 * start.
11783 *
11784 * @param animation The next animation, or null.
11785 */
11786 public void setAnimation(Animation animation) {
11787 mCurrentAnimation = animation;
11788 if (animation != null) {
11789 animation.reset();
11790 }
11791 }
11792
11793 /**
11794 * Invoked by a parent ViewGroup to notify the start of the animation
11795 * currently associated with this view. If you override this method,
11796 * always call super.onAnimationStart();
11797 *
11798 * @see #setAnimation(android.view.animation.Animation)
11799 * @see #getAnimation()
11800 */
11801 protected void onAnimationStart() {
11802 mPrivateFlags |= ANIMATION_STARTED;
11803 }
11804
11805 /**
11806 * Invoked by a parent ViewGroup to notify the end of the animation
11807 * currently associated with this view. If you override this method,
11808 * always call super.onAnimationEnd();
11809 *
11810 * @see #setAnimation(android.view.animation.Animation)
11811 * @see #getAnimation()
11812 */
11813 protected void onAnimationEnd() {
11814 mPrivateFlags &= ~ANIMATION_STARTED;
11815 }
11816
11817 /**
11818 * Invoked if there is a Transform that involves alpha. Subclass that can
11819 * draw themselves with the specified alpha should return true, and then
11820 * respect that alpha when their onDraw() is called. If this returns false
11821 * then the view may be redirected to draw into an offscreen buffer to
11822 * fulfill the request, which will look fine, but may be slower than if the
11823 * subclass handles it internally. The default implementation returns false.
11824 *
11825 * @param alpha The alpha (0..255) to apply to the view's drawing
11826 * @return true if the view can draw with the specified alpha.
11827 */
11828 protected boolean onSetAlpha(int alpha) {
11829 return false;
11830 }
11831
11832 /**
11833 * This is used by the RootView to perform an optimization when
11834 * the view hierarchy contains one or several SurfaceView.
11835 * SurfaceView is always considered transparent, but its children are not,
11836 * therefore all View objects remove themselves from the global transparent
11837 * region (passed as a parameter to this function).
11838 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011839 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011840 *
11841 * @return Returns true if the effective visibility of the view at this
11842 * point is opaque, regardless of the transparent region; returns false
11843 * if it is possible for underlying windows to be seen behind the view.
11844 *
11845 * {@hide}
11846 */
11847 public boolean gatherTransparentRegion(Region region) {
11848 final AttachInfo attachInfo = mAttachInfo;
11849 if (region != null && attachInfo != null) {
11850 final int pflags = mPrivateFlags;
11851 if ((pflags & SKIP_DRAW) == 0) {
11852 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
11853 // remove it from the transparent region.
11854 final int[] location = attachInfo.mTransparentLocation;
11855 getLocationInWindow(location);
11856 region.op(location[0], location[1], location[0] + mRight - mLeft,
11857 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
11858 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
11859 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
11860 // exists, so we remove the background drawable's non-transparent
11861 // parts from this transparent region.
11862 applyDrawableToTransparentRegion(mBGDrawable, region);
11863 }
11864 }
11865 return true;
11866 }
11867
11868 /**
11869 * Play a sound effect for this view.
11870 *
11871 * <p>The framework will play sound effects for some built in actions, such as
11872 * clicking, but you may wish to play these effects in your widget,
11873 * for instance, for internal navigation.
11874 *
11875 * <p>The sound effect will only be played if sound effects are enabled by the user, and
11876 * {@link #isSoundEffectsEnabled()} is true.
11877 *
11878 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
11879 */
11880 public void playSoundEffect(int soundConstant) {
11881 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
11882 return;
11883 }
11884 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
11885 }
11886
11887 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011888 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011889 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011890 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011891 *
11892 * <p>The framework will provide haptic feedback for some built in actions,
11893 * such as long presses, but you may wish to provide feedback for your
11894 * own widget.
11895 *
11896 * <p>The feedback will only be performed if
11897 * {@link #isHapticFeedbackEnabled()} is true.
11898 *
11899 * @param feedbackConstant One of the constants defined in
11900 * {@link HapticFeedbackConstants}
11901 */
11902 public boolean performHapticFeedback(int feedbackConstant) {
11903 return performHapticFeedback(feedbackConstant, 0);
11904 }
11905
11906 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011907 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011908 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011909 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011910 *
11911 * @param feedbackConstant One of the constants defined in
11912 * {@link HapticFeedbackConstants}
11913 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
11914 */
11915 public boolean performHapticFeedback(int feedbackConstant, int flags) {
11916 if (mAttachInfo == null) {
11917 return false;
11918 }
Romain Guyf607bdc2010-09-10 19:20:06 -070011919 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070011920 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011921 && !isHapticFeedbackEnabled()) {
11922 return false;
11923 }
Romain Guy812ccbe2010-06-01 14:07:24 -070011924 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
11925 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011926 }
11927
11928 /**
Joe Onorato664644d2011-01-23 17:53:23 -080011929 * Request that the visibility of the status bar be changed.
Scott Mainec6331b2011-05-24 16:55:56 -070011930 * @param visibility Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080011931 */
11932 public void setSystemUiVisibility(int visibility) {
11933 if (visibility != mSystemUiVisibility) {
11934 mSystemUiVisibility = visibility;
11935 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11936 mParent.recomputeViewAttributes(this);
11937 }
11938 }
11939 }
11940
11941 /**
11942 * Returns the status bar visibility that this view has requested.
Scott Mainec6331b2011-05-24 16:55:56 -070011943 * @return Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080011944 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080011945 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080011946 return mSystemUiVisibility;
11947 }
11948
Scott Mainec6331b2011-05-24 16:55:56 -070011949 /**
11950 * Set a listener to receive callbacks when the visibility of the system bar changes.
11951 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
11952 */
Joe Onorato664644d2011-01-23 17:53:23 -080011953 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
11954 mOnSystemUiVisibilityChangeListener = l;
11955 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11956 mParent.recomputeViewAttributes(this);
11957 }
11958 }
11959
11960 /**
11961 */
11962 public void dispatchSystemUiVisibilityChanged(int visibility) {
11963 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080011964 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080011965 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080011966 }
11967 }
11968
11969 /**
Joe Malin32736f02011-01-19 16:14:20 -080011970 * Creates an image that the system displays during the drag and drop
11971 * operation. This is called a &quot;drag shadow&quot;. The default implementation
11972 * for a DragShadowBuilder based on a View returns an image that has exactly the same
11973 * appearance as the given View. The default also positions the center of the drag shadow
11974 * directly under the touch point. If no View is provided (the constructor with no parameters
11975 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
11976 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
11977 * default is an invisible drag shadow.
11978 * <p>
11979 * You are not required to use the View you provide to the constructor as the basis of the
11980 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
11981 * anything you want as the drag shadow.
11982 * </p>
11983 * <p>
11984 * You pass a DragShadowBuilder object to the system when you start the drag. The system
11985 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
11986 * size and position of the drag shadow. It uses this data to construct a
11987 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
11988 * so that your application can draw the shadow image in the Canvas.
11989 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011990 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011991 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070011992 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070011993
11994 /**
Joe Malin32736f02011-01-19 16:14:20 -080011995 * Constructs a shadow image builder based on a View. By default, the resulting drag
11996 * shadow will have the same appearance and dimensions as the View, with the touch point
11997 * over the center of the View.
11998 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070011999 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012000 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070012001 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070012002 }
12003
Christopher Tate17ed60c2011-01-18 12:50:26 -080012004 /**
12005 * Construct a shadow builder object with no associated View. This
12006 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
12007 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
12008 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080012009 * reference to any View object. If they are not overridden, then the result is an
12010 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080012011 */
12012 public DragShadowBuilder() {
12013 mView = new WeakReference<View>(null);
12014 }
12015
12016 /**
12017 * Returns the View object that had been passed to the
12018 * {@link #View.DragShadowBuilder(View)}
12019 * constructor. If that View parameter was {@code null} or if the
12020 * {@link #View.DragShadowBuilder()}
12021 * constructor was used to instantiate the builder object, this method will return
12022 * null.
12023 *
12024 * @return The View object associate with this builder object.
12025 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070012026 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070012027 final public View getView() {
12028 return mView.get();
12029 }
12030
Christopher Tate2c095f32010-10-04 14:13:40 -070012031 /**
Joe Malin32736f02011-01-19 16:14:20 -080012032 * Provides the metrics for the shadow image. These include the dimensions of
12033 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070012034 * be centered under the touch location while dragging.
12035 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012036 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080012037 * same as the dimensions of the View itself and centers the shadow under
12038 * the touch point.
12039 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012040 *
Joe Malin32736f02011-01-19 16:14:20 -080012041 * @param shadowSize A {@link android.graphics.Point} containing the width and height
12042 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
12043 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
12044 * image.
12045 *
12046 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
12047 * shadow image that should be underneath the touch point during the drag and drop
12048 * operation. Your application must set {@link android.graphics.Point#x} to the
12049 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070012050 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012051 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070012052 final View view = mView.get();
12053 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012054 shadowSize.set(view.getWidth(), view.getHeight());
12055 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070012056 } else {
12057 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
12058 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012059 }
12060
12061 /**
Joe Malin32736f02011-01-19 16:14:20 -080012062 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
12063 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012064 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070012065 *
Joe Malin32736f02011-01-19 16:14:20 -080012066 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070012067 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012068 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070012069 final View view = mView.get();
12070 if (view != null) {
12071 view.draw(canvas);
12072 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012073 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070012074 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012075 }
12076 }
12077
12078 /**
Joe Malin32736f02011-01-19 16:14:20 -080012079 * Starts a drag and drop operation. When your application calls this method, it passes a
12080 * {@link android.view.View.DragShadowBuilder} object to the system. The
12081 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
12082 * to get metrics for the drag shadow, and then calls the object's
12083 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
12084 * <p>
12085 * Once the system has the drag shadow, it begins the drag and drop operation by sending
12086 * drag events to all the View objects in your application that are currently visible. It does
12087 * this either by calling the View object's drag listener (an implementation of
12088 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
12089 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
12090 * Both are passed a {@link android.view.DragEvent} object that has a
12091 * {@link android.view.DragEvent#getAction()} value of
12092 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
12093 * </p>
12094 * <p>
12095 * Your application can invoke startDrag() on any attached View object. The View object does not
12096 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
12097 * be related to the View the user selected for dragging.
12098 * </p>
12099 * @param data A {@link android.content.ClipData} object pointing to the data to be
12100 * transferred by the drag and drop operation.
12101 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
12102 * drag shadow.
12103 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
12104 * drop operation. This Object is put into every DragEvent object sent by the system during the
12105 * current drag.
12106 * <p>
12107 * myLocalState is a lightweight mechanism for the sending information from the dragged View
12108 * to the target Views. For example, it can contain flags that differentiate between a
12109 * a copy operation and a move operation.
12110 * </p>
12111 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
12112 * so the parameter should be set to 0.
12113 * @return {@code true} if the method completes successfully, or
12114 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
12115 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070012116 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012117 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012118 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070012119 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012120 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070012121 }
12122 boolean okay = false;
12123
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012124 Point shadowSize = new Point();
12125 Point shadowTouchPoint = new Point();
12126 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070012127
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012128 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
12129 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
12130 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070012131 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012132
Chris Tatea32dcf72010-10-14 12:13:50 -070012133 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012134 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
12135 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070012136 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012137 Surface surface = new Surface();
12138 try {
12139 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012140 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070012141 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070012142 + " surface=" + surface);
12143 if (token != null) {
12144 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070012145 try {
Chris Tate6b391282010-10-14 15:48:59 -070012146 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012147 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070012148 } finally {
12149 surface.unlockCanvasAndPost(canvas);
12150 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012151
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012152 final ViewAncestor root = getViewAncestor();
Christopher Tate407b4e92010-11-30 17:14:08 -080012153
12154 // Cache the local state object for delivery with DragEvents
12155 root.setLocalDragState(myLocalState);
12156
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012157 // repurpose 'shadowSize' for the last touch point
12158 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070012159
Christopher Tatea53146c2010-09-07 11:57:52 -070012160 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012161 shadowSize.x, shadowSize.y,
12162 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070012163 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070012164 }
12165 } catch (Exception e) {
12166 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
12167 surface.destroy();
12168 }
12169
12170 return okay;
12171 }
12172
Christopher Tatea53146c2010-09-07 11:57:52 -070012173 /**
Joe Malin32736f02011-01-19 16:14:20 -080012174 * Handles drag events sent by the system following a call to
12175 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
12176 *<p>
12177 * When the system calls this method, it passes a
12178 * {@link android.view.DragEvent} object. A call to
12179 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
12180 * in DragEvent. The method uses these to determine what is happening in the drag and drop
12181 * operation.
12182 * @param event The {@link android.view.DragEvent} sent by the system.
12183 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
12184 * in DragEvent, indicating the type of drag event represented by this object.
12185 * @return {@code true} if the method was successful, otherwise {@code false}.
12186 * <p>
12187 * The method should return {@code true} in response to an action type of
12188 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
12189 * operation.
12190 * </p>
12191 * <p>
12192 * The method should also return {@code true} in response to an action type of
12193 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
12194 * {@code false} if it didn't.
12195 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012196 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070012197 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070012198 return false;
12199 }
12200
12201 /**
Joe Malin32736f02011-01-19 16:14:20 -080012202 * Detects if this View is enabled and has a drag event listener.
12203 * If both are true, then it calls the drag event listener with the
12204 * {@link android.view.DragEvent} it received. If the drag event listener returns
12205 * {@code true}, then dispatchDragEvent() returns {@code true}.
12206 * <p>
12207 * For all other cases, the method calls the
12208 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
12209 * method and returns its result.
12210 * </p>
12211 * <p>
12212 * This ensures that a drag event is always consumed, even if the View does not have a drag
12213 * event listener. However, if the View has a listener and the listener returns true, then
12214 * onDragEvent() is not called.
12215 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012216 */
12217 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080012218 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070012219 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
12220 && mOnDragListener.onDrag(this, event)) {
12221 return true;
12222 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012223 return onDragEvent(event);
12224 }
12225
Christopher Tate3d4bf172011-03-28 16:16:46 -070012226 boolean canAcceptDrag() {
12227 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
12228 }
12229
Christopher Tatea53146c2010-09-07 11:57:52 -070012230 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070012231 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
12232 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070012233 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070012234 */
12235 public void onCloseSystemDialogs(String reason) {
12236 }
Joe Malin32736f02011-01-19 16:14:20 -080012237
Dianne Hackbornffa42482009-09-23 22:20:11 -070012238 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012239 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070012240 * update a Region being computed for
12241 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012242 * that any non-transparent parts of the Drawable are removed from the
12243 * given transparent region.
12244 *
12245 * @param dr The Drawable whose transparency is to be applied to the region.
12246 * @param region A Region holding the current transparency information,
12247 * where any parts of the region that are set are considered to be
12248 * transparent. On return, this region will be modified to have the
12249 * transparency information reduced by the corresponding parts of the
12250 * Drawable that are not transparent.
12251 * {@hide}
12252 */
12253 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
12254 if (DBG) {
12255 Log.i("View", "Getting transparent region for: " + this);
12256 }
12257 final Region r = dr.getTransparentRegion();
12258 final Rect db = dr.getBounds();
12259 final AttachInfo attachInfo = mAttachInfo;
12260 if (r != null && attachInfo != null) {
12261 final int w = getRight()-getLeft();
12262 final int h = getBottom()-getTop();
12263 if (db.left > 0) {
12264 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
12265 r.op(0, 0, db.left, h, Region.Op.UNION);
12266 }
12267 if (db.right < w) {
12268 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
12269 r.op(db.right, 0, w, h, Region.Op.UNION);
12270 }
12271 if (db.top > 0) {
12272 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
12273 r.op(0, 0, w, db.top, Region.Op.UNION);
12274 }
12275 if (db.bottom < h) {
12276 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
12277 r.op(0, db.bottom, w, h, Region.Op.UNION);
12278 }
12279 final int[] location = attachInfo.mTransparentLocation;
12280 getLocationInWindow(location);
12281 r.translate(location[0], location[1]);
12282 region.op(r, Region.Op.INTERSECT);
12283 } else {
12284 region.op(db, Region.Op.DIFFERENCE);
12285 }
12286 }
12287
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012288 private void checkForLongClick(int delayOffset) {
12289 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
12290 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012291
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012292 if (mPendingCheckForLongPress == null) {
12293 mPendingCheckForLongPress = new CheckForLongPress();
12294 }
12295 mPendingCheckForLongPress.rememberWindowAttachCount();
12296 postDelayed(mPendingCheckForLongPress,
12297 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012298 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012299 }
12300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012301 /**
12302 * Inflate a view from an XML resource. This convenience method wraps the {@link
12303 * LayoutInflater} class, which provides a full range of options for view inflation.
12304 *
12305 * @param context The Context object for your activity or application.
12306 * @param resource The resource ID to inflate
12307 * @param root A view group that will be the parent. Used to properly inflate the
12308 * layout_* parameters.
12309 * @see LayoutInflater
12310 */
12311 public static View inflate(Context context, int resource, ViewGroup root) {
12312 LayoutInflater factory = LayoutInflater.from(context);
12313 return factory.inflate(resource, root);
12314 }
Romain Guy33e72ae2010-07-17 12:40:29 -070012315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012316 /**
Adam Powell637d3372010-08-25 14:37:03 -070012317 * Scroll the view with standard behavior for scrolling beyond the normal
12318 * content boundaries. Views that call this method should override
12319 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
12320 * results of an over-scroll operation.
12321 *
12322 * Views can use this method to handle any touch or fling-based scrolling.
12323 *
12324 * @param deltaX Change in X in pixels
12325 * @param deltaY Change in Y in pixels
12326 * @param scrollX Current X scroll value in pixels before applying deltaX
12327 * @param scrollY Current Y scroll value in pixels before applying deltaY
12328 * @param scrollRangeX Maximum content scroll range along the X axis
12329 * @param scrollRangeY Maximum content scroll range along the Y axis
12330 * @param maxOverScrollX Number of pixels to overscroll by in either direction
12331 * along the X axis.
12332 * @param maxOverScrollY Number of pixels to overscroll by in either direction
12333 * along the Y axis.
12334 * @param isTouchEvent true if this scroll operation is the result of a touch event.
12335 * @return true if scrolling was clamped to an over-scroll boundary along either
12336 * axis, false otherwise.
12337 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012338 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070012339 protected boolean overScrollBy(int deltaX, int deltaY,
12340 int scrollX, int scrollY,
12341 int scrollRangeX, int scrollRangeY,
12342 int maxOverScrollX, int maxOverScrollY,
12343 boolean isTouchEvent) {
12344 final int overScrollMode = mOverScrollMode;
12345 final boolean canScrollHorizontal =
12346 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
12347 final boolean canScrollVertical =
12348 computeVerticalScrollRange() > computeVerticalScrollExtent();
12349 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
12350 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
12351 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
12352 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
12353
12354 int newScrollX = scrollX + deltaX;
12355 if (!overScrollHorizontal) {
12356 maxOverScrollX = 0;
12357 }
12358
12359 int newScrollY = scrollY + deltaY;
12360 if (!overScrollVertical) {
12361 maxOverScrollY = 0;
12362 }
12363
12364 // Clamp values if at the limits and record
12365 final int left = -maxOverScrollX;
12366 final int right = maxOverScrollX + scrollRangeX;
12367 final int top = -maxOverScrollY;
12368 final int bottom = maxOverScrollY + scrollRangeY;
12369
12370 boolean clampedX = false;
12371 if (newScrollX > right) {
12372 newScrollX = right;
12373 clampedX = true;
12374 } else if (newScrollX < left) {
12375 newScrollX = left;
12376 clampedX = true;
12377 }
12378
12379 boolean clampedY = false;
12380 if (newScrollY > bottom) {
12381 newScrollY = bottom;
12382 clampedY = true;
12383 } else if (newScrollY < top) {
12384 newScrollY = top;
12385 clampedY = true;
12386 }
12387
12388 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
12389
12390 return clampedX || clampedY;
12391 }
12392
12393 /**
12394 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
12395 * respond to the results of an over-scroll operation.
12396 *
12397 * @param scrollX New X scroll value in pixels
12398 * @param scrollY New Y scroll value in pixels
12399 * @param clampedX True if scrollX was clamped to an over-scroll boundary
12400 * @param clampedY True if scrollY was clamped to an over-scroll boundary
12401 */
12402 protected void onOverScrolled(int scrollX, int scrollY,
12403 boolean clampedX, boolean clampedY) {
12404 // Intentionally empty.
12405 }
12406
12407 /**
12408 * Returns the over-scroll mode for this view. The result will be
12409 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12410 * (allow over-scrolling only if the view content is larger than the container),
12411 * or {@link #OVER_SCROLL_NEVER}.
12412 *
12413 * @return This view's over-scroll mode.
12414 */
12415 public int getOverScrollMode() {
12416 return mOverScrollMode;
12417 }
12418
12419 /**
12420 * Set the over-scroll mode for this view. Valid over-scroll modes are
12421 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12422 * (allow over-scrolling only if the view content is larger than the container),
12423 * or {@link #OVER_SCROLL_NEVER}.
12424 *
12425 * Setting the over-scroll mode of a view will have an effect only if the
12426 * view is capable of scrolling.
12427 *
12428 * @param overScrollMode The new over-scroll mode for this view.
12429 */
12430 public void setOverScrollMode(int overScrollMode) {
12431 if (overScrollMode != OVER_SCROLL_ALWAYS &&
12432 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
12433 overScrollMode != OVER_SCROLL_NEVER) {
12434 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
12435 }
12436 mOverScrollMode = overScrollMode;
12437 }
12438
12439 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012440 * Gets a scale factor that determines the distance the view should scroll
12441 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
12442 * @return The vertical scroll scale factor.
12443 * @hide
12444 */
12445 protected float getVerticalScrollFactor() {
12446 if (mVerticalScrollFactor == 0) {
12447 TypedValue outValue = new TypedValue();
12448 if (!mContext.getTheme().resolveAttribute(
12449 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
12450 throw new IllegalStateException(
12451 "Expected theme to define listPreferredItemHeight.");
12452 }
12453 mVerticalScrollFactor = outValue.getDimension(
12454 mContext.getResources().getDisplayMetrics());
12455 }
12456 return mVerticalScrollFactor;
12457 }
12458
12459 /**
12460 * Gets a scale factor that determines the distance the view should scroll
12461 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
12462 * @return The horizontal scroll scale factor.
12463 * @hide
12464 */
12465 protected float getHorizontalScrollFactor() {
12466 // TODO: Should use something else.
12467 return getVerticalScrollFactor();
12468 }
12469
Chet Haaseb39f0512011-05-24 14:36:40 -070012470 //
12471 // Properties
12472 //
12473 /**
12474 * A Property wrapper around the <code>alpha</code> functionality handled by the
12475 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
12476 */
12477 static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
12478 @Override
12479 public void setValue(View object, float value) {
12480 object.setAlpha(value);
12481 }
12482
12483 @Override
12484 public Float get(View object) {
12485 return object.getAlpha();
12486 }
12487 };
12488
12489 /**
12490 * A Property wrapper around the <code>translationX</code> functionality handled by the
12491 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
12492 */
12493 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
12494 @Override
12495 public void setValue(View object, float value) {
12496 object.setTranslationX(value);
12497 }
12498
12499 @Override
12500 public Float get(View object) {
12501 return object.getTranslationX();
12502 }
12503 };
12504
12505 /**
12506 * A Property wrapper around the <code>translationY</code> functionality handled by the
12507 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
12508 */
12509 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
12510 @Override
12511 public void setValue(View object, float value) {
12512 object.setTranslationY(value);
12513 }
12514
12515 @Override
12516 public Float get(View object) {
12517 return object.getTranslationY();
12518 }
12519 };
12520
12521 /**
12522 * A Property wrapper around the <code>x</code> functionality handled by the
12523 * {@link View#setX(float)} and {@link View#getX()} methods.
12524 */
12525 public static Property<View, Float> X = new FloatProperty<View>("x") {
12526 @Override
12527 public void setValue(View object, float value) {
12528 object.setX(value);
12529 }
12530
12531 @Override
12532 public Float get(View object) {
12533 return object.getX();
12534 }
12535 };
12536
12537 /**
12538 * A Property wrapper around the <code>y</code> functionality handled by the
12539 * {@link View#setY(float)} and {@link View#getY()} methods.
12540 */
12541 public static Property<View, Float> Y = new FloatProperty<View>("y") {
12542 @Override
12543 public void setValue(View object, float value) {
12544 object.setY(value);
12545 }
12546
12547 @Override
12548 public Float get(View object) {
12549 return object.getY();
12550 }
12551 };
12552
12553 /**
12554 * A Property wrapper around the <code>rotation</code> functionality handled by the
12555 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
12556 */
12557 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
12558 @Override
12559 public void setValue(View object, float value) {
12560 object.setRotation(value);
12561 }
12562
12563 @Override
12564 public Float get(View object) {
12565 return object.getRotation();
12566 }
12567 };
12568
12569 /**
12570 * A Property wrapper around the <code>rotationX</code> functionality handled by the
12571 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
12572 */
12573 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
12574 @Override
12575 public void setValue(View object, float value) {
12576 object.setRotationX(value);
12577 }
12578
12579 @Override
12580 public Float get(View object) {
12581 return object.getRotationX();
12582 }
12583 };
12584
12585 /**
12586 * A Property wrapper around the <code>rotationY</code> functionality handled by the
12587 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
12588 */
12589 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
12590 @Override
12591 public void setValue(View object, float value) {
12592 object.setRotationY(value);
12593 }
12594
12595 @Override
12596 public Float get(View object) {
12597 return object.getRotationY();
12598 }
12599 };
12600
12601 /**
12602 * A Property wrapper around the <code>scaleX</code> functionality handled by the
12603 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
12604 */
12605 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
12606 @Override
12607 public void setValue(View object, float value) {
12608 object.setScaleX(value);
12609 }
12610
12611 @Override
12612 public Float get(View object) {
12613 return object.getScaleX();
12614 }
12615 };
12616
12617 /**
12618 * A Property wrapper around the <code>scaleY</code> functionality handled by the
12619 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
12620 */
12621 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
12622 @Override
12623 public void setValue(View object, float value) {
12624 object.setScaleY(value);
12625 }
12626
12627 @Override
12628 public Float get(View object) {
12629 return object.getScaleY();
12630 }
12631 };
12632
Jeff Brown33bbfd22011-02-24 20:55:35 -080012633 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012634 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
12635 * Each MeasureSpec represents a requirement for either the width or the height.
12636 * A MeasureSpec is comprised of a size and a mode. There are three possible
12637 * modes:
12638 * <dl>
12639 * <dt>UNSPECIFIED</dt>
12640 * <dd>
12641 * The parent has not imposed any constraint on the child. It can be whatever size
12642 * it wants.
12643 * </dd>
12644 *
12645 * <dt>EXACTLY</dt>
12646 * <dd>
12647 * The parent has determined an exact size for the child. The child is going to be
12648 * given those bounds regardless of how big it wants to be.
12649 * </dd>
12650 *
12651 * <dt>AT_MOST</dt>
12652 * <dd>
12653 * The child can be as large as it wants up to the specified size.
12654 * </dd>
12655 * </dl>
12656 *
12657 * MeasureSpecs are implemented as ints to reduce object allocation. This class
12658 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
12659 */
12660 public static class MeasureSpec {
12661 private static final int MODE_SHIFT = 30;
12662 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
12663
12664 /**
12665 * Measure specification mode: The parent has not imposed any constraint
12666 * on the child. It can be whatever size it wants.
12667 */
12668 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
12669
12670 /**
12671 * Measure specification mode: The parent has determined an exact size
12672 * for the child. The child is going to be given those bounds regardless
12673 * of how big it wants to be.
12674 */
12675 public static final int EXACTLY = 1 << MODE_SHIFT;
12676
12677 /**
12678 * Measure specification mode: The child can be as large as it wants up
12679 * to the specified size.
12680 */
12681 public static final int AT_MOST = 2 << MODE_SHIFT;
12682
12683 /**
12684 * Creates a measure specification based on the supplied size and mode.
12685 *
12686 * The mode must always be one of the following:
12687 * <ul>
12688 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
12689 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
12690 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
12691 * </ul>
12692 *
12693 * @param size the size of the measure specification
12694 * @param mode the mode of the measure specification
12695 * @return the measure specification based on size and mode
12696 */
12697 public static int makeMeasureSpec(int size, int mode) {
12698 return size + mode;
12699 }
12700
12701 /**
12702 * Extracts the mode from the supplied measure specification.
12703 *
12704 * @param measureSpec the measure specification to extract the mode from
12705 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
12706 * {@link android.view.View.MeasureSpec#AT_MOST} or
12707 * {@link android.view.View.MeasureSpec#EXACTLY}
12708 */
12709 public static int getMode(int measureSpec) {
12710 return (measureSpec & MODE_MASK);
12711 }
12712
12713 /**
12714 * Extracts the size from the supplied measure specification.
12715 *
12716 * @param measureSpec the measure specification to extract the size from
12717 * @return the size in pixels defined in the supplied measure specification
12718 */
12719 public static int getSize(int measureSpec) {
12720 return (measureSpec & ~MODE_MASK);
12721 }
12722
12723 /**
12724 * Returns a String representation of the specified measure
12725 * specification.
12726 *
12727 * @param measureSpec the measure specification to convert to a String
12728 * @return a String with the following format: "MeasureSpec: MODE SIZE"
12729 */
12730 public static String toString(int measureSpec) {
12731 int mode = getMode(measureSpec);
12732 int size = getSize(measureSpec);
12733
12734 StringBuilder sb = new StringBuilder("MeasureSpec: ");
12735
12736 if (mode == UNSPECIFIED)
12737 sb.append("UNSPECIFIED ");
12738 else if (mode == EXACTLY)
12739 sb.append("EXACTLY ");
12740 else if (mode == AT_MOST)
12741 sb.append("AT_MOST ");
12742 else
12743 sb.append(mode).append(" ");
12744
12745 sb.append(size);
12746 return sb.toString();
12747 }
12748 }
12749
12750 class CheckForLongPress implements Runnable {
12751
12752 private int mOriginalWindowAttachCount;
12753
12754 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070012755 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012756 && mOriginalWindowAttachCount == mWindowAttachCount) {
12757 if (performLongClick()) {
12758 mHasPerformedLongPress = true;
12759 }
12760 }
12761 }
12762
12763 public void rememberWindowAttachCount() {
12764 mOriginalWindowAttachCount = mWindowAttachCount;
12765 }
12766 }
Joe Malin32736f02011-01-19 16:14:20 -080012767
Adam Powelle14579b2009-12-16 18:39:52 -080012768 private final class CheckForTap implements Runnable {
12769 public void run() {
12770 mPrivateFlags &= ~PREPRESSED;
12771 mPrivateFlags |= PRESSED;
12772 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012773 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080012774 }
12775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012776
Adam Powella35d7682010-03-12 14:48:13 -080012777 private final class PerformClick implements Runnable {
12778 public void run() {
12779 performClick();
12780 }
12781 }
12782
Dianne Hackborn63042d62011-01-26 18:56:29 -080012783 /** @hide */
12784 public void hackTurnOffWindowResizeAnim(boolean off) {
12785 mAttachInfo.mTurnOffWindowResizeAnim = off;
12786 }
Joe Malin32736f02011-01-19 16:14:20 -080012787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012788 /**
Chet Haasea00f3862011-02-22 06:34:40 -080012789 * This method returns a ViewPropertyAnimator object, which can be used to animate
12790 * specific properties on this View.
12791 *
12792 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
12793 */
12794 public ViewPropertyAnimator animate() {
12795 if (mAnimator == null) {
12796 mAnimator = new ViewPropertyAnimator(this);
12797 }
12798 return mAnimator;
12799 }
12800
12801 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012802 * Interface definition for a callback to be invoked when a key event is
12803 * dispatched to this view. The callback will be invoked before the key
12804 * event is given to the view.
12805 */
12806 public interface OnKeyListener {
12807 /**
12808 * Called when a key is dispatched to a view. This allows listeners to
12809 * get a chance to respond before the target view.
12810 *
12811 * @param v The view the key has been dispatched to.
12812 * @param keyCode The code for the physical key that was pressed
12813 * @param event The KeyEvent object containing full information about
12814 * the event.
12815 * @return True if the listener has consumed the event, false otherwise.
12816 */
12817 boolean onKey(View v, int keyCode, KeyEvent event);
12818 }
12819
12820 /**
12821 * Interface definition for a callback to be invoked when a touch event is
12822 * dispatched to this view. The callback will be invoked before the touch
12823 * event is given to the view.
12824 */
12825 public interface OnTouchListener {
12826 /**
12827 * Called when a touch event is dispatched to a view. This allows listeners to
12828 * get a chance to respond before the target view.
12829 *
12830 * @param v The view the touch event has been dispatched to.
12831 * @param event The MotionEvent object containing full information about
12832 * the event.
12833 * @return True if the listener has consumed the event, false otherwise.
12834 */
12835 boolean onTouch(View v, MotionEvent event);
12836 }
12837
12838 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012839 * Interface definition for a callback to be invoked when a generic motion event is
12840 * dispatched to this view. The callback will be invoked before the generic motion
12841 * event is given to the view.
12842 */
12843 public interface OnGenericMotionListener {
12844 /**
12845 * Called when a generic motion event is dispatched to a view. This allows listeners to
12846 * get a chance to respond before the target view.
12847 *
12848 * @param v The view the generic motion event has been dispatched to.
12849 * @param event The MotionEvent object containing full information about
12850 * the event.
12851 * @return True if the listener has consumed the event, false otherwise.
12852 */
12853 boolean onGenericMotion(View v, MotionEvent event);
12854 }
12855
12856 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012857 * Interface definition for a callback to be invoked when a view has been clicked and held.
12858 */
12859 public interface OnLongClickListener {
12860 /**
12861 * Called when a view has been clicked and held.
12862 *
12863 * @param v The view that was clicked and held.
12864 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080012865 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012866 */
12867 boolean onLongClick(View v);
12868 }
12869
12870 /**
Chris Tate32affef2010-10-18 15:29:21 -070012871 * Interface definition for a callback to be invoked when a drag is being dispatched
12872 * to this view. The callback will be invoked before the hosting view's own
12873 * onDrag(event) method. If the listener wants to fall back to the hosting view's
12874 * onDrag(event) behavior, it should return 'false' from this callback.
12875 */
12876 public interface OnDragListener {
12877 /**
12878 * Called when a drag event is dispatched to a view. This allows listeners
12879 * to get a chance to override base View behavior.
12880 *
Joe Malin32736f02011-01-19 16:14:20 -080012881 * @param v The View that received the drag event.
12882 * @param event The {@link android.view.DragEvent} object for the drag event.
12883 * @return {@code true} if the drag event was handled successfully, or {@code false}
12884 * if the drag event was not handled. Note that {@code false} will trigger the View
12885 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070012886 */
12887 boolean onDrag(View v, DragEvent event);
12888 }
12889
12890 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012891 * Interface definition for a callback to be invoked when the focus state of
12892 * a view changed.
12893 */
12894 public interface OnFocusChangeListener {
12895 /**
12896 * Called when the focus state of a view has changed.
12897 *
12898 * @param v The view whose state has changed.
12899 * @param hasFocus The new focus state of v.
12900 */
12901 void onFocusChange(View v, boolean hasFocus);
12902 }
12903
12904 /**
12905 * Interface definition for a callback to be invoked when a view is clicked.
12906 */
12907 public interface OnClickListener {
12908 /**
12909 * Called when a view has been clicked.
12910 *
12911 * @param v The view that was clicked.
12912 */
12913 void onClick(View v);
12914 }
12915
12916 /**
12917 * Interface definition for a callback to be invoked when the context menu
12918 * for this view is being built.
12919 */
12920 public interface OnCreateContextMenuListener {
12921 /**
12922 * Called when the context menu for this view is being built. It is not
12923 * safe to hold onto the menu after this method returns.
12924 *
12925 * @param menu The context menu that is being built
12926 * @param v The view for which the context menu is being built
12927 * @param menuInfo Extra information about the item for which the
12928 * context menu should be shown. This information will vary
12929 * depending on the class of v.
12930 */
12931 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
12932 }
12933
Joe Onorato664644d2011-01-23 17:53:23 -080012934 /**
12935 * Interface definition for a callback to be invoked when the status bar changes
12936 * visibility.
12937 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012938 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080012939 */
12940 public interface OnSystemUiVisibilityChangeListener {
12941 /**
12942 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070012943 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080012944 *
12945 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
12946 */
12947 public void onSystemUiVisibilityChange(int visibility);
12948 }
12949
Adam Powell4afd62b2011-02-18 15:02:18 -080012950 /**
12951 * Interface definition for a callback to be invoked when this view is attached
12952 * or detached from its window.
12953 */
12954 public interface OnAttachStateChangeListener {
12955 /**
12956 * Called when the view is attached to a window.
12957 * @param v The view that was attached
12958 */
12959 public void onViewAttachedToWindow(View v);
12960 /**
12961 * Called when the view is detached from a window.
12962 * @param v The view that was detached
12963 */
12964 public void onViewDetachedFromWindow(View v);
12965 }
12966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012967 private final class UnsetPressedState implements Runnable {
12968 public void run() {
12969 setPressed(false);
12970 }
12971 }
12972
12973 /**
12974 * Base class for derived classes that want to save and restore their own
12975 * state in {@link android.view.View#onSaveInstanceState()}.
12976 */
12977 public static class BaseSavedState extends AbsSavedState {
12978 /**
12979 * Constructor used when reading from a parcel. Reads the state of the superclass.
12980 *
12981 * @param source
12982 */
12983 public BaseSavedState(Parcel source) {
12984 super(source);
12985 }
12986
12987 /**
12988 * Constructor called by derived classes when creating their SavedState objects
12989 *
12990 * @param superState The state of the superclass of this view
12991 */
12992 public BaseSavedState(Parcelable superState) {
12993 super(superState);
12994 }
12995
12996 public static final Parcelable.Creator<BaseSavedState> CREATOR =
12997 new Parcelable.Creator<BaseSavedState>() {
12998 public BaseSavedState createFromParcel(Parcel in) {
12999 return new BaseSavedState(in);
13000 }
13001
13002 public BaseSavedState[] newArray(int size) {
13003 return new BaseSavedState[size];
13004 }
13005 };
13006 }
13007
13008 /**
13009 * A set of information given to a view when it is attached to its parent
13010 * window.
13011 */
13012 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013013 interface Callbacks {
13014 void playSoundEffect(int effectId);
13015 boolean performHapticFeedback(int effectId, boolean always);
13016 }
13017
13018 /**
13019 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
13020 * to a Handler. This class contains the target (View) to invalidate and
13021 * the coordinates of the dirty rectangle.
13022 *
13023 * For performance purposes, this class also implements a pool of up to
13024 * POOL_LIMIT objects that get reused. This reduces memory allocations
13025 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013026 */
Romain Guyd928d682009-03-31 17:52:16 -070013027 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013028 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070013029 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
13030 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070013031 public InvalidateInfo newInstance() {
13032 return new InvalidateInfo();
13033 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013034
Romain Guyd928d682009-03-31 17:52:16 -070013035 public void onAcquired(InvalidateInfo element) {
13036 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013037
Romain Guyd928d682009-03-31 17:52:16 -070013038 public void onReleased(InvalidateInfo element) {
13039 }
13040 }, POOL_LIMIT)
13041 );
13042
13043 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013044 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013045
13046 View target;
13047
13048 int left;
13049 int top;
13050 int right;
13051 int bottom;
13052
Romain Guyd928d682009-03-31 17:52:16 -070013053 public void setNextPoolable(InvalidateInfo element) {
13054 mNext = element;
13055 }
13056
13057 public InvalidateInfo getNextPoolable() {
13058 return mNext;
13059 }
13060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013061 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070013062 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013063 }
13064
13065 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070013066 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013067 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013068
13069 public boolean isPooled() {
13070 return mIsPooled;
13071 }
13072
13073 public void setPooled(boolean isPooled) {
13074 mIsPooled = isPooled;
13075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013076 }
13077
13078 final IWindowSession mSession;
13079
13080 final IWindow mWindow;
13081
13082 final IBinder mWindowToken;
13083
13084 final Callbacks mRootCallbacks;
13085
Romain Guy59a12ca2011-06-09 17:48:21 -070013086 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080013087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013088 /**
13089 * The top view of the hierarchy.
13090 */
13091 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070013092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013093 IBinder mPanelParentWindowToken;
13094 Surface mSurface;
13095
Romain Guyb051e892010-09-28 19:09:36 -070013096 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013097 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070013098 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080013099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013100 /**
Romain Guy8506ab42009-06-11 17:35:47 -070013101 * Scale factor used by the compatibility mode
13102 */
13103 float mApplicationScale;
13104
13105 /**
13106 * Indicates whether the application is in compatibility mode
13107 */
13108 boolean mScalingRequired;
13109
13110 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013111 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080013112 */
13113 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080013114
Dianne Hackborn63042d62011-01-26 18:56:29 -080013115 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013116 * Left position of this view's window
13117 */
13118 int mWindowLeft;
13119
13120 /**
13121 * Top position of this view's window
13122 */
13123 int mWindowTop;
13124
13125 /**
Adam Powell26153a32010-11-08 15:22:27 -080013126 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070013127 */
Adam Powell26153a32010-11-08 15:22:27 -080013128 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070013129
13130 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013131 * For windows that are full-screen but using insets to layout inside
13132 * of the screen decorations, these are the current insets for the
13133 * content of the window.
13134 */
13135 final Rect mContentInsets = new Rect();
13136
13137 /**
13138 * For windows that are full-screen but using insets to layout inside
13139 * of the screen decorations, these are the current insets for the
13140 * actual visible parts of the window.
13141 */
13142 final Rect mVisibleInsets = new Rect();
13143
13144 /**
13145 * The internal insets given by this window. This value is
13146 * supplied by the client (through
13147 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
13148 * be given to the window manager when changed to be used in laying
13149 * out windows behind it.
13150 */
13151 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
13152 = new ViewTreeObserver.InternalInsetsInfo();
13153
13154 /**
13155 * All views in the window's hierarchy that serve as scroll containers,
13156 * used to determine if the window can be resized or must be panned
13157 * to adjust for a soft input area.
13158 */
13159 final ArrayList<View> mScrollContainers = new ArrayList<View>();
13160
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070013161 final KeyEvent.DispatcherState mKeyDispatchState
13162 = new KeyEvent.DispatcherState();
13163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013164 /**
13165 * Indicates whether the view's window currently has the focus.
13166 */
13167 boolean mHasWindowFocus;
13168
13169 /**
13170 * The current visibility of the window.
13171 */
13172 int mWindowVisibility;
13173
13174 /**
13175 * Indicates the time at which drawing started to occur.
13176 */
13177 long mDrawingTime;
13178
13179 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070013180 * Indicates whether or not ignoring the DIRTY_MASK flags.
13181 */
13182 boolean mIgnoreDirtyState;
13183
13184 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013185 * Indicates whether the view's window is currently in touch mode.
13186 */
13187 boolean mInTouchMode;
13188
13189 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013190 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013191 * the next time it performs a traversal
13192 */
13193 boolean mRecomputeGlobalAttributes;
13194
13195 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013196 * Set during a traveral if any views want to keep the screen on.
13197 */
13198 boolean mKeepScreenOn;
13199
13200 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013201 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
13202 */
13203 int mSystemUiVisibility;
13204
13205 /**
13206 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
13207 * attached.
13208 */
13209 boolean mHasSystemUiListeners;
13210
13211 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013212 * Set if the visibility of any views has changed.
13213 */
13214 boolean mViewVisibilityChanged;
13215
13216 /**
13217 * Set to true if a view has been scrolled.
13218 */
13219 boolean mViewScrollChanged;
13220
13221 /**
13222 * Global to the view hierarchy used as a temporary for dealing with
13223 * x/y points in the transparent region computations.
13224 */
13225 final int[] mTransparentLocation = new int[2];
13226
13227 /**
13228 * Global to the view hierarchy used as a temporary for dealing with
13229 * x/y points in the ViewGroup.invalidateChild implementation.
13230 */
13231 final int[] mInvalidateChildLocation = new int[2];
13232
Chet Haasec3aa3612010-06-17 08:50:37 -070013233
13234 /**
13235 * Global to the view hierarchy used as a temporary for dealing with
13236 * x/y location when view is transformed.
13237 */
13238 final float[] mTmpTransformLocation = new float[2];
13239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013240 /**
13241 * The view tree observer used to dispatch global events like
13242 * layout, pre-draw, touch mode change, etc.
13243 */
13244 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
13245
13246 /**
13247 * A Canvas used by the view hierarchy to perform bitmap caching.
13248 */
13249 Canvas mCanvas;
13250
13251 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013252 * A Handler supplied by a view's {@link android.view.ViewAncestor}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013253 * handler can be used to pump events in the UI events queue.
13254 */
13255 final Handler mHandler;
13256
13257 /**
13258 * Identifier for messages requesting the view to be invalidated.
13259 * Such messages should be sent to {@link #mHandler}.
13260 */
13261 static final int INVALIDATE_MSG = 0x1;
13262
13263 /**
13264 * Identifier for messages requesting the view to invalidate a region.
13265 * Such messages should be sent to {@link #mHandler}.
13266 */
13267 static final int INVALIDATE_RECT_MSG = 0x2;
13268
13269 /**
13270 * Temporary for use in computing invalidate rectangles while
13271 * calling up the hierarchy.
13272 */
13273 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070013274
13275 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070013276 * Temporary for use in computing hit areas with transformed views
13277 */
13278 final RectF mTmpTransformRect = new RectF();
13279
13280 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070013281 * Temporary list for use in collecting focusable descendents of a view.
13282 */
13283 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
13284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013285 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013286 * The id of the window for accessibility purposes.
13287 */
13288 int mAccessibilityWindowId = View.NO_ID;
13289
13290 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013291 * Creates a new set of attachment information with the specified
13292 * events handler and thread.
13293 *
13294 * @param handler the events handler the view must use
13295 */
13296 AttachInfo(IWindowSession session, IWindow window,
13297 Handler handler, Callbacks effectPlayer) {
13298 mSession = session;
13299 mWindow = window;
13300 mWindowToken = window.asBinder();
13301 mHandler = handler;
13302 mRootCallbacks = effectPlayer;
13303 }
13304 }
13305
13306 /**
13307 * <p>ScrollabilityCache holds various fields used by a View when scrolling
13308 * is supported. This avoids keeping too many unused fields in most
13309 * instances of View.</p>
13310 */
Mike Cleronf116bf82009-09-27 19:14:12 -070013311 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080013312
Mike Cleronf116bf82009-09-27 19:14:12 -070013313 /**
13314 * Scrollbars are not visible
13315 */
13316 public static final int OFF = 0;
13317
13318 /**
13319 * Scrollbars are visible
13320 */
13321 public static final int ON = 1;
13322
13323 /**
13324 * Scrollbars are fading away
13325 */
13326 public static final int FADING = 2;
13327
13328 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080013329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013330 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070013331 public int scrollBarDefaultDelayBeforeFade;
13332 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013333
13334 public int scrollBarSize;
13335 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070013336 public float[] interpolatorValues;
13337 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013338
13339 public final Paint paint;
13340 public final Matrix matrix;
13341 public Shader shader;
13342
Mike Cleronf116bf82009-09-27 19:14:12 -070013343 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
13344
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013345 private static final float[] OPAQUE = { 255 };
13346 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080013347
Mike Cleronf116bf82009-09-27 19:14:12 -070013348 /**
13349 * When fading should start. This time moves into the future every time
13350 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
13351 */
13352 public long fadeStartTime;
13353
13354
13355 /**
13356 * The current state of the scrollbars: ON, OFF, or FADING
13357 */
13358 public int state = OFF;
13359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013360 private int mLastColor;
13361
Mike Cleronf116bf82009-09-27 19:14:12 -070013362 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013363 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
13364 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070013365 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
13366 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013367
13368 paint = new Paint();
13369 matrix = new Matrix();
13370 // use use a height of 1, and then wack the matrix each time we
13371 // actually use it.
13372 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070013373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013374 paint.setShader(shader);
13375 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070013376 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013377 }
Romain Guy8506ab42009-06-11 17:35:47 -070013378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013379 public void setFadeColor(int color) {
13380 if (color != 0 && color != mLastColor) {
13381 mLastColor = color;
13382 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070013383
Romain Guye55e1a72009-08-27 10:42:26 -070013384 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
13385 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070013386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013387 paint.setShader(shader);
13388 // Restore the default transfer mode (src_over)
13389 paint.setXfermode(null);
13390 }
13391 }
Joe Malin32736f02011-01-19 16:14:20 -080013392
Mike Cleronf116bf82009-09-27 19:14:12 -070013393 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070013394 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070013395 if (now >= fadeStartTime) {
13396
13397 // the animation fades the scrollbars out by changing
13398 // the opacity (alpha) from fully opaque to fully
13399 // transparent
13400 int nextFrame = (int) now;
13401 int framesCount = 0;
13402
13403 Interpolator interpolator = scrollBarInterpolator;
13404
13405 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013406 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070013407
13408 // End transparent
13409 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013410 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070013411
13412 state = FADING;
13413
13414 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080013415 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070013416 }
13417 }
13418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013419 }
13420}