blob: 32c07420170ff667d22a74f82ad90da1dc9da85e [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
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080019import com.android.internal.R;
20import com.android.internal.util.Predicate;
21import com.android.internal.view.menu.MenuBuilder;
22
Christopher Tatea53146c2010-09-07 11:57:52 -070023import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080025import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.res.Resources;
27import android.content.res.TypedArray;
28import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070029import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.graphics.Canvas;
Mike Cleronf116bf82009-09-27 19:14:12 -070031import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.graphics.LinearGradient;
33import android.graphics.Matrix;
34import android.graphics.Paint;
35import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070036import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.graphics.PorterDuff;
38import android.graphics.PorterDuffXfermode;
39import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070040import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.graphics.Region;
42import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.graphics.drawable.ColorDrawable;
44import android.graphics.drawable.Drawable;
45import android.os.Handler;
46import android.os.IBinder;
47import android.os.Message;
48import android.os.Parcel;
49import android.os.Parcelable;
50import android.os.RemoteException;
51import android.os.SystemClock;
52import android.os.SystemProperties;
53import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070055import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070057import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.Pools;
59import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070062import android.view.accessibility.AccessibilityEvent;
63import android.view.accessibility.AccessibilityEventSource;
64import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070066import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070067import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.inputmethod.InputConnection;
69import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.widget.ScrollBarDrawable;
71
Christopher Tatea0374192010-10-05 13:06:41 -070072import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070073import java.lang.reflect.InvocationTargetException;
74import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import java.util.ArrayList;
76import java.util.Arrays;
Romain Guyd90a3312009-05-06 14:54:28 -070077import java.util.WeakHashMap;
Adam Powell4afd62b2011-02-18 15:02:18 -080078import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80/**
81 * <p>
82 * This class represents the basic building block for user interface components. A View
83 * occupies a rectangular area on the screen and is responsible for drawing and
84 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070085 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
87 * are invisible containers that hold other Views (or other ViewGroups) and define
88 * their layout properties.
89 * </p>
90 *
91 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070092 * <p>For an introduction to using this class to develop your
93 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -070095 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
97 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
98 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
99 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
100 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
101 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
102 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
103 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
104 * </p>
105 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700106 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * <a name="Using"></a>
108 * <h3>Using Views</h3>
109 * <p>
110 * All of the views in a window are arranged in a single tree. You can add views
111 * either from code or by specifying a tree of views in one or more XML layout
112 * files. There are many specialized subclasses of views that act as controls or
113 * are capable of displaying text, images, or other content.
114 * </p>
115 * <p>
116 * Once you have created a tree of views, there are typically a few types of
117 * common operations you may wish to perform:
118 * <ul>
119 * <li><strong>Set properties:</strong> for example setting the text of a
120 * {@link android.widget.TextView}. The available properties and the methods
121 * that set them will vary among the different subclasses of views. Note that
122 * properties that are known at build time can be set in the XML layout
123 * files.</li>
124 * <li><strong>Set focus:</strong> The framework will handled moving focus in
125 * response to user input. To force focus to a specific view, call
126 * {@link #requestFocus}.</li>
127 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
128 * that will be notified when something interesting happens to the view. For
129 * example, all views will let you set a listener to be notified when the view
130 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700131 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
132 * Other view subclasses offer more specialized listeners. For example, a Button
133 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700135 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 * </ul>
137 * </p>
138 * <p><em>
139 * Note: The Android framework is responsible for measuring, laying out and
140 * drawing views. You should not call methods that perform these actions on
141 * views yourself unless you are actually implementing a
142 * {@link android.view.ViewGroup}.
143 * </em></p>
144 *
145 * <a name="Lifecycle"></a>
146 * <h3>Implementing a Custom View</h3>
147 *
148 * <p>
149 * To implement a custom view, you will usually begin by providing overrides for
150 * some of the standard methods that the framework calls on all views. You do
151 * not need to override all of these methods. In fact, you can start by just
152 * overriding {@link #onDraw(android.graphics.Canvas)}.
153 * <table border="2" width="85%" align="center" cellpadding="5">
154 * <thead>
155 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
156 * </thead>
157 *
158 * <tbody>
159 * <tr>
160 * <td rowspan="2">Creation</td>
161 * <td>Constructors</td>
162 * <td>There is a form of the constructor that are called when the view
163 * is created from code and a form that is called when the view is
164 * inflated from a layout file. The second form should parse and apply
165 * any attributes defined in the layout file.
166 * </td>
167 * </tr>
168 * <tr>
169 * <td><code>{@link #onFinishInflate()}</code></td>
170 * <td>Called after a view and all of its children has been inflated
171 * from XML.</td>
172 * </tr>
173 *
174 * <tr>
175 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700176 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 * <td>Called to determine the size requirements for this view and all
178 * of its children.
179 * </td>
180 * </tr>
181 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700182 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 * <td>Called when this view should assign a size and position to all
184 * of its children.
185 * </td>
186 * </tr>
187 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700188 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 * <td>Called when the size of this view has changed.
190 * </td>
191 * </tr>
192 *
193 * <tr>
194 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700195 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 * <td>Called when the view should render its content.
197 * </td>
198 * </tr>
199 *
200 * <tr>
201 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700202 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 * <td>Called when a new key event occurs.
204 * </td>
205 * </tr>
206 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700207 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 * <td>Called when a key up event occurs.
209 * </td>
210 * </tr>
211 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700212 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 * <td>Called when a trackball motion event occurs.
214 * </td>
215 * </tr>
216 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700217 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 * <td>Called when a touch screen motion event occurs.
219 * </td>
220 * </tr>
221 *
222 * <tr>
223 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700224 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 * <td>Called when the view gains or loses focus.
226 * </td>
227 * </tr>
228 *
229 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700230 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 * <td>Called when the window containing the view gains or loses focus.
232 * </td>
233 * </tr>
234 *
235 * <tr>
236 * <td rowspan="3">Attaching</td>
237 * <td><code>{@link #onAttachedToWindow()}</code></td>
238 * <td>Called when the view is attached to a window.
239 * </td>
240 * </tr>
241 *
242 * <tr>
243 * <td><code>{@link #onDetachedFromWindow}</code></td>
244 * <td>Called when the view is detached from its window.
245 * </td>
246 * </tr>
247 *
248 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700249 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 * <td>Called when the visibility of the window containing the view
251 * has changed.
252 * </td>
253 * </tr>
254 * </tbody>
255 *
256 * </table>
257 * </p>
258 *
259 * <a name="IDs"></a>
260 * <h3>IDs</h3>
261 * Views may have an integer id associated with them. These ids are typically
262 * assigned in the layout XML files, and are used to find specific views within
263 * the view tree. A common pattern is to:
264 * <ul>
265 * <li>Define a Button in the layout file and assign it a unique ID.
266 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700267 * &lt;Button
268 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 * android:layout_width="wrap_content"
270 * android:layout_height="wrap_content"
271 * android:text="@string/my_button_text"/&gt;
272 * </pre></li>
273 * <li>From the onCreate method of an Activity, find the Button
274 * <pre class="prettyprint">
275 * Button myButton = (Button) findViewById(R.id.my_button);
276 * </pre></li>
277 * </ul>
278 * <p>
279 * View IDs need not be unique throughout the tree, but it is good practice to
280 * ensure that they are at least unique within the part of the tree you are
281 * searching.
282 * </p>
283 *
284 * <a name="Position"></a>
285 * <h3>Position</h3>
286 * <p>
287 * The geometry of a view is that of a rectangle. A view has a location,
288 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
289 * two dimensions, expressed as a width and a height. The unit for location
290 * and dimensions is the pixel.
291 * </p>
292 *
293 * <p>
294 * It is possible to retrieve the location of a view by invoking the methods
295 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
296 * coordinate of the rectangle representing the view. The latter returns the
297 * top, or Y, coordinate of the rectangle representing the view. These methods
298 * both return the location of the view relative to its parent. For instance,
299 * when getLeft() returns 20, that means the view is located 20 pixels to the
300 * right of the left edge of its direct parent.
301 * </p>
302 *
303 * <p>
304 * In addition, several convenience methods are offered to avoid unnecessary
305 * computations, namely {@link #getRight()} and {@link #getBottom()}.
306 * These methods return the coordinates of the right and bottom edges of the
307 * rectangle representing the view. For instance, calling {@link #getRight()}
308 * is similar to the following computation: <code>getLeft() + getWidth()</code>
309 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
310 * </p>
311 *
312 * <a name="SizePaddingMargins"></a>
313 * <h3>Size, padding and margins</h3>
314 * <p>
315 * The size of a view is expressed with a width and a height. A view actually
316 * possess two pairs of width and height values.
317 * </p>
318 *
319 * <p>
320 * The first pair is known as <em>measured width</em> and
321 * <em>measured height</em>. These dimensions define how big a view wants to be
322 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
323 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
324 * and {@link #getMeasuredHeight()}.
325 * </p>
326 *
327 * <p>
328 * The second pair is simply known as <em>width</em> and <em>height</em>, or
329 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
330 * dimensions define the actual size of the view on screen, at drawing time and
331 * after layout. These values may, but do not have to, be different from the
332 * measured width and height. The width and height can be obtained by calling
333 * {@link #getWidth()} and {@link #getHeight()}.
334 * </p>
335 *
336 * <p>
337 * To measure its dimensions, a view takes into account its padding. The padding
338 * is expressed in pixels for the left, top, right and bottom parts of the view.
339 * Padding can be used to offset the content of the view by a specific amount of
340 * pixels. For instance, a left padding of 2 will push the view's content by
341 * 2 pixels to the right of the left edge. Padding can be set using the
342 * {@link #setPadding(int, int, int, int)} method and queried by calling
343 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
344 * {@link #getPaddingRight()} and {@link #getPaddingBottom()}.
345 * </p>
346 *
347 * <p>
348 * Even though a view can define a padding, it does not provide any support for
349 * margins. However, view groups provide such a support. Refer to
350 * {@link android.view.ViewGroup} and
351 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
352 * </p>
353 *
354 * <a name="Layout"></a>
355 * <h3>Layout</h3>
356 * <p>
357 * Layout is a two pass process: a measure pass and a layout pass. The measuring
358 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
359 * of the view tree. Each view pushes dimension specifications down the tree
360 * during the recursion. At the end of the measure pass, every view has stored
361 * its measurements. The second pass happens in
362 * {@link #layout(int,int,int,int)} and is also top-down. During
363 * this pass each parent is responsible for positioning all of its children
364 * using the sizes computed in the measure pass.
365 * </p>
366 *
367 * <p>
368 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
369 * {@link #getMeasuredHeight()} values must be set, along with those for all of
370 * that view's descendants. A view's measured width and measured height values
371 * must respect the constraints imposed by the view's parents. This guarantees
372 * that at the end of the measure pass, all parents accept all of their
373 * children's measurements. A parent view may call measure() more than once on
374 * its children. For example, the parent may measure each child once with
375 * unspecified dimensions to find out how big they want to be, then call
376 * measure() on them again with actual numbers if the sum of all the children's
377 * unconstrained sizes is too big or too small.
378 * </p>
379 *
380 * <p>
381 * The measure pass uses two classes to communicate dimensions. The
382 * {@link MeasureSpec} class is used by views to tell their parents how they
383 * want to be measured and positioned. The base LayoutParams class just
384 * describes how big the view wants to be for both width and height. For each
385 * dimension, it can specify one of:
386 * <ul>
387 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800388 * <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 -0800389 * (minus padding)
390 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
391 * enclose its content (plus padding).
392 * </ul>
393 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
394 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
395 * an X and Y value.
396 * </p>
397 *
398 * <p>
399 * MeasureSpecs are used to push requirements down the tree from parent to
400 * child. A MeasureSpec can be in one of three modes:
401 * <ul>
402 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
403 * of a child view. For example, a LinearLayout may call measure() on its child
404 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
405 * tall the child view wants to be given a width of 240 pixels.
406 * <li>EXACTLY: This is used by the parent to impose an exact size on the
407 * child. The child must use this size, and guarantee that all of its
408 * descendants will fit within this size.
409 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
410 * child. The child must gurantee that it and all of its descendants will fit
411 * within this size.
412 * </ul>
413 * </p>
414 *
415 * <p>
416 * To intiate a layout, call {@link #requestLayout}. This method is typically
417 * called by a view on itself when it believes that is can no longer fit within
418 * its current bounds.
419 * </p>
420 *
421 * <a name="Drawing"></a>
422 * <h3>Drawing</h3>
423 * <p>
424 * Drawing is handled by walking the tree and rendering each view that
425 * intersects the the invalid region. Because the tree is traversed in-order,
426 * this means that parents will draw before (i.e., behind) their children, with
427 * siblings drawn in the order they appear in the tree.
428 * If you set a background drawable for a View, then the View will draw it for you
429 * before calling back to its <code>onDraw()</code> method.
430 * </p>
431 *
432 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700433 * 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 -0800434 * </p>
435 *
436 * <p>
437 * To force a view to draw, call {@link #invalidate()}.
438 * </p>
439 *
440 * <a name="EventHandlingThreading"></a>
441 * <h3>Event Handling and Threading</h3>
442 * <p>
443 * The basic cycle of a view is as follows:
444 * <ol>
445 * <li>An event comes in and is dispatched to the appropriate view. The view
446 * handles the event and notifies any listeners.</li>
447 * <li>If in the course of processing the event, the view's bounds may need
448 * to be changed, the view will call {@link #requestLayout()}.</li>
449 * <li>Similarly, if in the course of processing the event the view's appearance
450 * may need to be changed, the view will call {@link #invalidate()}.</li>
451 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
452 * the framework will take care of measuring, laying out, and drawing the tree
453 * as appropriate.</li>
454 * </ol>
455 * </p>
456 *
457 * <p><em>Note: The entire view tree is single threaded. You must always be on
458 * the UI thread when calling any method on any view.</em>
459 * If you are doing work on other threads and want to update the state of a view
460 * from that thread, you should use a {@link Handler}.
461 * </p>
462 *
463 * <a name="FocusHandling"></a>
464 * <h3>Focus Handling</h3>
465 * <p>
466 * The framework will handle routine focus movement in response to user input.
467 * This includes changing the focus as views are removed or hidden, or as new
468 * views become available. Views indicate their willingness to take focus
469 * through the {@link #isFocusable} method. To change whether a view can take
470 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
471 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
472 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
473 * </p>
474 * <p>
475 * Focus movement is based on an algorithm which finds the nearest neighbor in a
476 * given direction. In rare cases, the default algorithm may not match the
477 * intended behavior of the developer. In these situations, you can provide
478 * explicit overrides by using these XML attributes in the layout file:
479 * <pre>
480 * nextFocusDown
481 * nextFocusLeft
482 * nextFocusRight
483 * nextFocusUp
484 * </pre>
485 * </p>
486 *
487 *
488 * <p>
489 * To get a particular view to take focus, call {@link #requestFocus()}.
490 * </p>
491 *
492 * <a name="TouchMode"></a>
493 * <h3>Touch Mode</h3>
494 * <p>
495 * When a user is navigating a user interface via directional keys such as a D-pad, it is
496 * necessary to give focus to actionable items such as buttons so the user can see
497 * what will take input. If the device has touch capabilities, however, and the user
498 * begins interacting with the interface by touching it, it is no longer necessary to
499 * always highlight, or give focus to, a particular view. This motivates a mode
500 * for interaction named 'touch mode'.
501 * </p>
502 * <p>
503 * For a touch capable device, once the user touches the screen, the device
504 * will enter touch mode. From this point onward, only views for which
505 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
506 * Other views that are touchable, like buttons, will not take focus when touched; they will
507 * only fire the on click listeners.
508 * </p>
509 * <p>
510 * Any time a user hits a directional key, such as a D-pad direction, the view device will
511 * exit touch mode, and find a view to take focus, so that the user may resume interacting
512 * with the user interface without touching the screen again.
513 * </p>
514 * <p>
515 * The touch mode state is maintained across {@link android.app.Activity}s. Call
516 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
517 * </p>
518 *
519 * <a name="Scrolling"></a>
520 * <h3>Scrolling</h3>
521 * <p>
522 * The framework provides basic support for views that wish to internally
523 * scroll their content. This includes keeping track of the X and Y scroll
524 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800525 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700526 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 * </p>
528 *
529 * <a name="Tags"></a>
530 * <h3>Tags</h3>
531 * <p>
532 * Unlike IDs, tags are not used to identify views. Tags are essentially an
533 * extra piece of information that can be associated with a view. They are most
534 * often used as a convenience to store data related to views in the views
535 * themselves rather than by putting them in a separate structure.
536 * </p>
537 *
538 * <a name="Animation"></a>
539 * <h3>Animation</h3>
540 * <p>
541 * You can attach an {@link Animation} object to a view using
542 * {@link #setAnimation(Animation)} or
543 * {@link #startAnimation(Animation)}. The animation can alter the scale,
544 * rotation, translation and alpha of a view over time. If the animation is
545 * attached to a view that has children, the animation will affect the entire
546 * subtree rooted by that node. When an animation is started, the framework will
547 * take care of redrawing the appropriate views until the animation completes.
548 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800549 * <p>
550 * Starting with Android 3.0, the preferred way of animating views is to use the
551 * {@link android.animation} package APIs.
552 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 *
Jeff Brown85a31762010-09-01 17:01:00 -0700554 * <a name="Security"></a>
555 * <h3>Security</h3>
556 * <p>
557 * Sometimes it is essential that an application be able to verify that an action
558 * is being performed with the full knowledge and consent of the user, such as
559 * granting a permission request, making a purchase or clicking on an advertisement.
560 * Unfortunately, a malicious application could try to spoof the user into
561 * performing these actions, unaware, by concealing the intended purpose of the view.
562 * As a remedy, the framework offers a touch filtering mechanism that can be used to
563 * improve the security of views that provide access to sensitive functionality.
564 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700565 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800566 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700567 * will discard touches that are received whenever the view's window is obscured by
568 * another visible window. As a result, the view will not receive touches whenever a
569 * toast, dialog or other window appears above the view's window.
570 * </p><p>
571 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700572 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
573 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700574 * </p>
575 *
Romain Guy171c5922011-01-06 10:04:23 -0800576 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700577 * @attr ref android.R.styleable#View_background
578 * @attr ref android.R.styleable#View_clickable
579 * @attr ref android.R.styleable#View_contentDescription
580 * @attr ref android.R.styleable#View_drawingCacheQuality
581 * @attr ref android.R.styleable#View_duplicateParentState
582 * @attr ref android.R.styleable#View_id
583 * @attr ref android.R.styleable#View_fadingEdge
584 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700585 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700587 * @attr ref android.R.styleable#View_isScrollContainer
588 * @attr ref android.R.styleable#View_focusable
589 * @attr ref android.R.styleable#View_focusableInTouchMode
590 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
591 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800592 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700593 * @attr ref android.R.styleable#View_longClickable
594 * @attr ref android.R.styleable#View_minHeight
595 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 * @attr ref android.R.styleable#View_nextFocusDown
597 * @attr ref android.R.styleable#View_nextFocusLeft
598 * @attr ref android.R.styleable#View_nextFocusRight
599 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700600 * @attr ref android.R.styleable#View_onClick
601 * @attr ref android.R.styleable#View_padding
602 * @attr ref android.R.styleable#View_paddingBottom
603 * @attr ref android.R.styleable#View_paddingLeft
604 * @attr ref android.R.styleable#View_paddingRight
605 * @attr ref android.R.styleable#View_paddingTop
606 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800607 * @attr ref android.R.styleable#View_rotation
608 * @attr ref android.R.styleable#View_rotationX
609 * @attr ref android.R.styleable#View_rotationY
610 * @attr ref android.R.styleable#View_scaleX
611 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 * @attr ref android.R.styleable#View_scrollX
613 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700614 * @attr ref android.R.styleable#View_scrollbarSize
615 * @attr ref android.R.styleable#View_scrollbarStyle
616 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700617 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
618 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
620 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * @attr ref android.R.styleable#View_scrollbarThumbVertical
622 * @attr ref android.R.styleable#View_scrollbarTrackVertical
623 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
624 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700625 * @attr ref android.R.styleable#View_soundEffectsEnabled
626 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800627 * @attr ref android.R.styleable#View_transformPivotX
628 * @attr ref android.R.styleable#View_transformPivotY
629 * @attr ref android.R.styleable#View_translationX
630 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700631 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 *
633 * @see android.view.ViewGroup
634 */
svetoslavganov75986cf2009-05-14 22:28:01 -0700635public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 private static final boolean DBG = false;
637
638 /**
639 * The logging tag used by this class with android.util.Log.
640 */
641 protected static final String VIEW_LOG_TAG = "View";
642
643 /**
644 * Used to mark a View that has no ID.
645 */
646 public static final int NO_ID = -1;
647
648 /**
649 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
650 * calling setFlags.
651 */
652 private static final int NOT_FOCUSABLE = 0x00000000;
653
654 /**
655 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
656 * setFlags.
657 */
658 private static final int FOCUSABLE = 0x00000001;
659
660 /**
661 * Mask for use with setFlags indicating bits used for focus.
662 */
663 private static final int FOCUSABLE_MASK = 0x00000001;
664
665 /**
666 * This view will adjust its padding to fit sytem windows (e.g. status bar)
667 */
668 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
669
670 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700671 * This view is visible. Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 */
673 public static final int VISIBLE = 0x00000000;
674
675 /**
676 * This view is invisible, but it still takes up space for layout purposes.
Romain Guy5c22a8c2011-05-13 11:48:45 -0700677 * Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 */
679 public static final int INVISIBLE = 0x00000004;
680
681 /**
682 * This view is invisible, and it doesn't take any space for layout
Romain Guy5c22a8c2011-05-13 11:48:45 -0700683 * purposes. Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 */
685 public static final int GONE = 0x00000008;
686
687 /**
688 * Mask for use with setFlags indicating bits used for visibility.
689 * {@hide}
690 */
691 static final int VISIBILITY_MASK = 0x0000000C;
692
693 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
694
695 /**
696 * This view is enabled. Intrepretation varies by subclass.
697 * Use with ENABLED_MASK when calling setFlags.
698 * {@hide}
699 */
700 static final int ENABLED = 0x00000000;
701
702 /**
703 * This view is disabled. Intrepretation varies by subclass.
704 * Use with ENABLED_MASK when calling setFlags.
705 * {@hide}
706 */
707 static final int DISABLED = 0x00000020;
708
709 /**
710 * Mask for use with setFlags indicating bits used for indicating whether
711 * this view is enabled
712 * {@hide}
713 */
714 static final int ENABLED_MASK = 0x00000020;
715
716 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700717 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
718 * called and further optimizations will be performed. It is okay to have
719 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 * {@hide}
721 */
722 static final int WILL_NOT_DRAW = 0x00000080;
723
724 /**
725 * Mask for use with setFlags indicating bits used for indicating whether
726 * this view is will draw
727 * {@hide}
728 */
729 static final int DRAW_MASK = 0x00000080;
730
731 /**
732 * <p>This view doesn't show scrollbars.</p>
733 * {@hide}
734 */
735 static final int SCROLLBARS_NONE = 0x00000000;
736
737 /**
738 * <p>This view shows horizontal scrollbars.</p>
739 * {@hide}
740 */
741 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
742
743 /**
744 * <p>This view shows vertical scrollbars.</p>
745 * {@hide}
746 */
747 static final int SCROLLBARS_VERTICAL = 0x00000200;
748
749 /**
750 * <p>Mask for use with setFlags indicating bits used for indicating which
751 * scrollbars are enabled.</p>
752 * {@hide}
753 */
754 static final int SCROLLBARS_MASK = 0x00000300;
755
Jeff Brown85a31762010-09-01 17:01:00 -0700756 /**
757 * Indicates that the view should filter touches when its window is obscured.
758 * Refer to the class comments for more information about this security feature.
759 * {@hide}
760 */
761 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
762
763 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764
765 /**
766 * <p>This view doesn't show fading edges.</p>
767 * {@hide}
768 */
769 static final int FADING_EDGE_NONE = 0x00000000;
770
771 /**
772 * <p>This view shows horizontal fading edges.</p>
773 * {@hide}
774 */
775 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
776
777 /**
778 * <p>This view shows vertical fading edges.</p>
779 * {@hide}
780 */
781 static final int FADING_EDGE_VERTICAL = 0x00002000;
782
783 /**
784 * <p>Mask for use with setFlags indicating bits used for indicating which
785 * fading edges are enabled.</p>
786 * {@hide}
787 */
788 static final int FADING_EDGE_MASK = 0x00003000;
789
790 /**
791 * <p>Indicates this view can be clicked. When clickable, a View reacts
792 * to clicks by notifying the OnClickListener.<p>
793 * {@hide}
794 */
795 static final int CLICKABLE = 0x00004000;
796
797 /**
798 * <p>Indicates this view is caching its drawing into a bitmap.</p>
799 * {@hide}
800 */
801 static final int DRAWING_CACHE_ENABLED = 0x00008000;
802
803 /**
804 * <p>Indicates that no icicle should be saved for this view.<p>
805 * {@hide}
806 */
807 static final int SAVE_DISABLED = 0x000010000;
808
809 /**
810 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
811 * property.</p>
812 * {@hide}
813 */
814 static final int SAVE_DISABLED_MASK = 0x000010000;
815
816 /**
817 * <p>Indicates that no drawing cache should ever be created for this view.<p>
818 * {@hide}
819 */
820 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
821
822 /**
823 * <p>Indicates this view can take / keep focus when int touch mode.</p>
824 * {@hide}
825 */
826 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
827
828 /**
829 * <p>Enables low quality mode for the drawing cache.</p>
830 */
831 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
832
833 /**
834 * <p>Enables high quality mode for the drawing cache.</p>
835 */
836 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
837
838 /**
839 * <p>Enables automatic quality mode for the drawing cache.</p>
840 */
841 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
842
843 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
844 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
845 };
846
847 /**
848 * <p>Mask for use with setFlags indicating bits used for the cache
849 * quality property.</p>
850 * {@hide}
851 */
852 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
853
854 /**
855 * <p>
856 * Indicates this view can be long clicked. When long clickable, a View
857 * reacts to long clicks by notifying the OnLongClickListener or showing a
858 * context menu.
859 * </p>
860 * {@hide}
861 */
862 static final int LONG_CLICKABLE = 0x00200000;
863
864 /**
865 * <p>Indicates that this view gets its drawable states from its direct parent
866 * and ignores its original internal states.</p>
867 *
868 * @hide
869 */
870 static final int DUPLICATE_PARENT_STATE = 0x00400000;
871
872 /**
873 * The scrollbar style to display the scrollbars inside the content area,
874 * without increasing the padding. The scrollbars will be overlaid with
875 * translucency on the view's content.
876 */
877 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
878
879 /**
880 * The scrollbar style to display the scrollbars inside the padded area,
881 * increasing the padding of the view. The scrollbars will not overlap the
882 * content area of the view.
883 */
884 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
885
886 /**
887 * The scrollbar style to display the scrollbars at the edge of the view,
888 * without increasing the padding. The scrollbars will be overlaid with
889 * translucency.
890 */
891 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
892
893 /**
894 * The scrollbar style to display the scrollbars at the edge of the view,
895 * increasing the padding of the view. The scrollbars will only overlap the
896 * background, if any.
897 */
898 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
899
900 /**
901 * Mask to check if the scrollbar style is overlay or inset.
902 * {@hide}
903 */
904 static final int SCROLLBARS_INSET_MASK = 0x01000000;
905
906 /**
907 * Mask to check if the scrollbar style is inside or outside.
908 * {@hide}
909 */
910 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
911
912 /**
913 * Mask for scrollbar style.
914 * {@hide}
915 */
916 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
917
918 /**
919 * View flag indicating that the screen should remain on while the
920 * window containing this view is visible to the user. This effectively
921 * takes care of automatically setting the WindowManager's
922 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
923 */
924 public static final int KEEP_SCREEN_ON = 0x04000000;
925
926 /**
927 * View flag indicating whether this view should have sound effects enabled
928 * for events such as clicking and touching.
929 */
930 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
931
932 /**
933 * View flag indicating whether this view should have haptic feedback
934 * enabled for events such as long presses.
935 */
936 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
937
938 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700939 * <p>Indicates that the view hierarchy should stop saving state when
940 * it reaches this view. If state saving is initiated immediately at
941 * the view, it will be allowed.
942 * {@hide}
943 */
944 static final int PARENT_SAVE_DISABLED = 0x20000000;
945
946 /**
947 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
948 * {@hide}
949 */
950 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
951
952 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700953 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
954 * should add all focusable Views regardless if they are focusable in touch mode.
955 */
956 public static final int FOCUSABLES_ALL = 0x00000000;
957
958 /**
959 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
960 * should add only Views focusable in touch mode.
961 */
962 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
963
964 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700965 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 * item.
967 */
968 public static final int FOCUS_BACKWARD = 0x00000001;
969
970 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700971 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 * item.
973 */
974 public static final int FOCUS_FORWARD = 0x00000002;
975
976 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700977 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 */
979 public static final int FOCUS_LEFT = 0x00000011;
980
981 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700982 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 */
984 public static final int FOCUS_UP = 0x00000021;
985
986 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700987 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 */
989 public static final int FOCUS_RIGHT = 0x00000042;
990
991 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700992 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 */
994 public static final int FOCUS_DOWN = 0x00000082;
995
996 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -0800997 * Bits of {@link #getMeasuredWidthAndState()} and
998 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
999 */
1000 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1001
1002 /**
1003 * Bits of {@link #getMeasuredWidthAndState()} and
1004 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1005 */
1006 public static final int MEASURED_STATE_MASK = 0xff000000;
1007
1008 /**
1009 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1010 * for functions that combine both width and height into a single int,
1011 * such as {@link #getMeasuredState()} and the childState argument of
1012 * {@link #resolveSizeAndState(int, int, int)}.
1013 */
1014 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1015
1016 /**
1017 * Bit of {@link #getMeasuredWidthAndState()} and
1018 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1019 * is smaller that the space the view would like to have.
1020 */
1021 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1022
1023 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 * Base View state sets
1025 */
1026 // Singles
1027 /**
1028 * Indicates the view has no states set. States are used with
1029 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1030 * view depending on its state.
1031 *
1032 * @see android.graphics.drawable.Drawable
1033 * @see #getDrawableState()
1034 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001035 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 /**
1037 * Indicates the view is enabled. States are used with
1038 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1039 * view depending on its state.
1040 *
1041 * @see android.graphics.drawable.Drawable
1042 * @see #getDrawableState()
1043 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001044 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 /**
1046 * Indicates the view is focused. States are used with
1047 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1048 * view depending on its state.
1049 *
1050 * @see android.graphics.drawable.Drawable
1051 * @see #getDrawableState()
1052 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001053 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 /**
1055 * Indicates the view is selected. States are used with
1056 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1057 * view depending on its state.
1058 *
1059 * @see android.graphics.drawable.Drawable
1060 * @see #getDrawableState()
1061 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001062 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 /**
1064 * Indicates the view is pressed. States are used with
1065 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1066 * view depending on its state.
1067 *
1068 * @see android.graphics.drawable.Drawable
1069 * @see #getDrawableState()
1070 * @hide
1071 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001072 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 /**
1074 * Indicates the view's window has focus. States are used with
1075 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1076 * view depending on its state.
1077 *
1078 * @see android.graphics.drawable.Drawable
1079 * @see #getDrawableState()
1080 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001081 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 // Doubles
1083 /**
1084 * Indicates the view is enabled and has the focus.
1085 *
1086 * @see #ENABLED_STATE_SET
1087 * @see #FOCUSED_STATE_SET
1088 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001089 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 /**
1091 * Indicates the view is enabled and selected.
1092 *
1093 * @see #ENABLED_STATE_SET
1094 * @see #SELECTED_STATE_SET
1095 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001096 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 /**
1098 * Indicates the view is enabled and that its window has focus.
1099 *
1100 * @see #ENABLED_STATE_SET
1101 * @see #WINDOW_FOCUSED_STATE_SET
1102 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001103 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 /**
1105 * Indicates the view is focused and selected.
1106 *
1107 * @see #FOCUSED_STATE_SET
1108 * @see #SELECTED_STATE_SET
1109 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001110 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 /**
1112 * Indicates the view has the focus and that its window has the focus.
1113 *
1114 * @see #FOCUSED_STATE_SET
1115 * @see #WINDOW_FOCUSED_STATE_SET
1116 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001117 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 /**
1119 * Indicates the view is selected and that its window has the focus.
1120 *
1121 * @see #SELECTED_STATE_SET
1122 * @see #WINDOW_FOCUSED_STATE_SET
1123 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001124 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 // Triples
1126 /**
1127 * Indicates the view is enabled, focused and selected.
1128 *
1129 * @see #ENABLED_STATE_SET
1130 * @see #FOCUSED_STATE_SET
1131 * @see #SELECTED_STATE_SET
1132 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001133 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 /**
1135 * Indicates the view is enabled, focused and its window has the focus.
1136 *
1137 * @see #ENABLED_STATE_SET
1138 * @see #FOCUSED_STATE_SET
1139 * @see #WINDOW_FOCUSED_STATE_SET
1140 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001141 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 /**
1143 * Indicates the view is enabled, selected and its window has the focus.
1144 *
1145 * @see #ENABLED_STATE_SET
1146 * @see #SELECTED_STATE_SET
1147 * @see #WINDOW_FOCUSED_STATE_SET
1148 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001149 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 /**
1151 * Indicates the view is focused, selected and its window has the focus.
1152 *
1153 * @see #FOCUSED_STATE_SET
1154 * @see #SELECTED_STATE_SET
1155 * @see #WINDOW_FOCUSED_STATE_SET
1156 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001157 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 /**
1159 * Indicates the view is enabled, focused, selected and its window
1160 * has the focus.
1161 *
1162 * @see #ENABLED_STATE_SET
1163 * @see #FOCUSED_STATE_SET
1164 * @see #SELECTED_STATE_SET
1165 * @see #WINDOW_FOCUSED_STATE_SET
1166 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001167 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 /**
1169 * Indicates the view is pressed and its window has the focus.
1170 *
1171 * @see #PRESSED_STATE_SET
1172 * @see #WINDOW_FOCUSED_STATE_SET
1173 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001174 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 /**
1176 * Indicates the view is pressed and selected.
1177 *
1178 * @see #PRESSED_STATE_SET
1179 * @see #SELECTED_STATE_SET
1180 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001181 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 /**
1183 * Indicates the view is pressed, selected and its window has the focus.
1184 *
1185 * @see #PRESSED_STATE_SET
1186 * @see #SELECTED_STATE_SET
1187 * @see #WINDOW_FOCUSED_STATE_SET
1188 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001189 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 /**
1191 * Indicates the view is pressed and focused.
1192 *
1193 * @see #PRESSED_STATE_SET
1194 * @see #FOCUSED_STATE_SET
1195 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001196 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 /**
1198 * Indicates the view is pressed, focused and its window has the focus.
1199 *
1200 * @see #PRESSED_STATE_SET
1201 * @see #FOCUSED_STATE_SET
1202 * @see #WINDOW_FOCUSED_STATE_SET
1203 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001204 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 /**
1206 * Indicates the view is pressed, focused and selected.
1207 *
1208 * @see #PRESSED_STATE_SET
1209 * @see #SELECTED_STATE_SET
1210 * @see #FOCUSED_STATE_SET
1211 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001212 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 /**
1214 * Indicates the view is pressed, focused, selected and its window has the focus.
1215 *
1216 * @see #PRESSED_STATE_SET
1217 * @see #FOCUSED_STATE_SET
1218 * @see #SELECTED_STATE_SET
1219 * @see #WINDOW_FOCUSED_STATE_SET
1220 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001221 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 /**
1223 * Indicates the view is pressed and enabled.
1224 *
1225 * @see #PRESSED_STATE_SET
1226 * @see #ENABLED_STATE_SET
1227 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001228 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 /**
1230 * Indicates the view is pressed, enabled and its window has the focus.
1231 *
1232 * @see #PRESSED_STATE_SET
1233 * @see #ENABLED_STATE_SET
1234 * @see #WINDOW_FOCUSED_STATE_SET
1235 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001236 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 /**
1238 * Indicates the view is pressed, enabled and selected.
1239 *
1240 * @see #PRESSED_STATE_SET
1241 * @see #ENABLED_STATE_SET
1242 * @see #SELECTED_STATE_SET
1243 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001244 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 /**
1246 * Indicates the view is pressed, enabled, selected and its window has the
1247 * focus.
1248 *
1249 * @see #PRESSED_STATE_SET
1250 * @see #ENABLED_STATE_SET
1251 * @see #SELECTED_STATE_SET
1252 * @see #WINDOW_FOCUSED_STATE_SET
1253 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001254 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 /**
1256 * Indicates the view is pressed, enabled and focused.
1257 *
1258 * @see #PRESSED_STATE_SET
1259 * @see #ENABLED_STATE_SET
1260 * @see #FOCUSED_STATE_SET
1261 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001262 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 /**
1264 * Indicates the view is pressed, enabled, focused and its window has the
1265 * focus.
1266 *
1267 * @see #PRESSED_STATE_SET
1268 * @see #ENABLED_STATE_SET
1269 * @see #FOCUSED_STATE_SET
1270 * @see #WINDOW_FOCUSED_STATE_SET
1271 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001272 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 /**
1274 * Indicates the view is pressed, enabled, focused and selected.
1275 *
1276 * @see #PRESSED_STATE_SET
1277 * @see #ENABLED_STATE_SET
1278 * @see #SELECTED_STATE_SET
1279 * @see #FOCUSED_STATE_SET
1280 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001281 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 /**
1283 * Indicates the view is pressed, enabled, focused, selected and its window
1284 * has the focus.
1285 *
1286 * @see #PRESSED_STATE_SET
1287 * @see #ENABLED_STATE_SET
1288 * @see #SELECTED_STATE_SET
1289 * @see #FOCUSED_STATE_SET
1290 * @see #WINDOW_FOCUSED_STATE_SET
1291 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001292 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293
1294 /**
1295 * The order here is very important to {@link #getDrawableState()}
1296 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001297 private static final int[][] VIEW_STATE_SETS;
1298
Romain Guyb051e892010-09-28 19:09:36 -07001299 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1300 static final int VIEW_STATE_SELECTED = 1 << 1;
1301 static final int VIEW_STATE_FOCUSED = 1 << 2;
1302 static final int VIEW_STATE_ENABLED = 1 << 3;
1303 static final int VIEW_STATE_PRESSED = 1 << 4;
1304 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001305 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001306 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001307 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1308 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001309
1310 static final int[] VIEW_STATE_IDS = new int[] {
1311 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1312 R.attr.state_selected, VIEW_STATE_SELECTED,
1313 R.attr.state_focused, VIEW_STATE_FOCUSED,
1314 R.attr.state_enabled, VIEW_STATE_ENABLED,
1315 R.attr.state_pressed, VIEW_STATE_PRESSED,
1316 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001317 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001318 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001319 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1320 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 };
1322
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001323 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001324 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1325 throw new IllegalStateException(
1326 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1327 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001328 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001329 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001330 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001331 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001332 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001333 orderedIds[i * 2] = viewState;
1334 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001335 }
1336 }
1337 }
Romain Guyb051e892010-09-28 19:09:36 -07001338 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1339 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1340 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 int numBits = Integer.bitCount(i);
1342 int[] set = new int[numBits];
1343 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001344 for (int j = 0; j < orderedIds.length; j += 2) {
1345 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001346 set[pos++] = orderedIds[j];
1347 }
1348 }
1349 VIEW_STATE_SETS[i] = set;
1350 }
1351
1352 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1353 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1354 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1355 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1356 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1357 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1358 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1359 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1360 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1361 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1362 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1363 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1364 | VIEW_STATE_FOCUSED];
1365 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1366 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1367 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1368 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1369 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1370 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1371 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1372 | VIEW_STATE_ENABLED];
1373 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1374 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1375 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1376 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1377 | VIEW_STATE_ENABLED];
1378 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1379 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1380 | VIEW_STATE_ENABLED];
1381 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1382 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1383 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1384
1385 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1386 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1387 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1388 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1389 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1390 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1391 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1392 | VIEW_STATE_PRESSED];
1393 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1394 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1395 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1396 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1397 | VIEW_STATE_PRESSED];
1398 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1399 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1400 | VIEW_STATE_PRESSED];
1401 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1402 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1403 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1404 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1405 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1406 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1407 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1408 | VIEW_STATE_PRESSED];
1409 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1410 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1411 | VIEW_STATE_PRESSED];
1412 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1413 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1414 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1415 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1416 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1417 | VIEW_STATE_PRESSED];
1418 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1419 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1420 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1421 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1422 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1423 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1424 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1425 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1426 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1427 | VIEW_STATE_PRESSED];
1428 }
1429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 * Temporary Rect currently for use in setBackground(). This will probably
1432 * be extended in the future to hold our own class with more than just
1433 * a Rect. :)
1434 */
1435 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001436
1437 /**
1438 * Map used to store views' tags.
1439 */
1440 private static WeakHashMap<View, SparseArray<Object>> sTags;
1441
1442 /**
1443 * Lock used to access sTags.
1444 */
1445 private static final Object sTagsLock = new Object();
1446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 /**
1448 * The animation currently associated with this view.
1449 * @hide
1450 */
1451 protected Animation mCurrentAnimation = null;
1452
1453 /**
1454 * Width as measured during measure pass.
1455 * {@hide}
1456 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001457 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001458 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459
1460 /**
1461 * Height as measured during measure pass.
1462 * {@hide}
1463 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001464 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001465 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466
1467 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001468 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1469 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1470 * its display list. This flag, used only when hw accelerated, allows us to clear the
1471 * flag while retaining this information until it's needed (at getDisplayList() time and
1472 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1473 *
1474 * {@hide}
1475 */
1476 boolean mRecreateDisplayList = false;
1477
1478 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 * The view's identifier.
1480 * {@hide}
1481 *
1482 * @see #setId(int)
1483 * @see #getId()
1484 */
1485 @ViewDebug.ExportedProperty(resolveId = true)
1486 int mID = NO_ID;
1487
1488 /**
1489 * The view's tag.
1490 * {@hide}
1491 *
1492 * @see #setTag(Object)
1493 * @see #getTag()
1494 */
1495 protected Object mTag;
1496
1497 // for mPrivateFlags:
1498 /** {@hide} */
1499 static final int WANTS_FOCUS = 0x00000001;
1500 /** {@hide} */
1501 static final int FOCUSED = 0x00000002;
1502 /** {@hide} */
1503 static final int SELECTED = 0x00000004;
1504 /** {@hide} */
1505 static final int IS_ROOT_NAMESPACE = 0x00000008;
1506 /** {@hide} */
1507 static final int HAS_BOUNDS = 0x00000010;
1508 /** {@hide} */
1509 static final int DRAWN = 0x00000020;
1510 /**
1511 * When this flag is set, this view is running an animation on behalf of its
1512 * children and should therefore not cancel invalidate requests, even if they
1513 * lie outside of this view's bounds.
1514 *
1515 * {@hide}
1516 */
1517 static final int DRAW_ANIMATION = 0x00000040;
1518 /** {@hide} */
1519 static final int SKIP_DRAW = 0x00000080;
1520 /** {@hide} */
1521 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1522 /** {@hide} */
1523 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1524 /** {@hide} */
1525 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1526 /** {@hide} */
1527 static final int MEASURED_DIMENSION_SET = 0x00000800;
1528 /** {@hide} */
1529 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001530 /** {@hide} */
1531 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532
1533 private static final int PRESSED = 0x00004000;
1534
1535 /** {@hide} */
1536 static final int DRAWING_CACHE_VALID = 0x00008000;
1537 /**
1538 * Flag used to indicate that this view should be drawn once more (and only once
1539 * more) after its animation has completed.
1540 * {@hide}
1541 */
1542 static final int ANIMATION_STARTED = 0x00010000;
1543
1544 private static final int SAVE_STATE_CALLED = 0x00020000;
1545
1546 /**
1547 * Indicates that the View returned true when onSetAlpha() was called and that
1548 * the alpha must be restored.
1549 * {@hide}
1550 */
1551 static final int ALPHA_SET = 0x00040000;
1552
1553 /**
1554 * Set by {@link #setScrollContainer(boolean)}.
1555 */
1556 static final int SCROLL_CONTAINER = 0x00080000;
1557
1558 /**
1559 * Set by {@link #setScrollContainer(boolean)}.
1560 */
1561 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1562
1563 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001564 * View flag indicating whether this view was invalidated (fully or partially.)
1565 *
1566 * @hide
1567 */
1568 static final int DIRTY = 0x00200000;
1569
1570 /**
1571 * View flag indicating whether this view was invalidated by an opaque
1572 * invalidate request.
1573 *
1574 * @hide
1575 */
1576 static final int DIRTY_OPAQUE = 0x00400000;
1577
1578 /**
1579 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1580 *
1581 * @hide
1582 */
1583 static final int DIRTY_MASK = 0x00600000;
1584
1585 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001586 * Indicates whether the background is opaque.
1587 *
1588 * @hide
1589 */
1590 static final int OPAQUE_BACKGROUND = 0x00800000;
1591
1592 /**
1593 * Indicates whether the scrollbars are opaque.
1594 *
1595 * @hide
1596 */
1597 static final int OPAQUE_SCROLLBARS = 0x01000000;
1598
1599 /**
1600 * Indicates whether the view is opaque.
1601 *
1602 * @hide
1603 */
1604 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001605
Adam Powelle14579b2009-12-16 18:39:52 -08001606 /**
1607 * Indicates a prepressed state;
1608 * the short time between ACTION_DOWN and recognizing
1609 * a 'real' press. Prepressed is used to recognize quick taps
1610 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001611 *
Adam Powelle14579b2009-12-16 18:39:52 -08001612 * @hide
1613 */
1614 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001615
Adam Powellc9fbaab2010-02-16 17:16:19 -08001616 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001617 * Indicates whether the view is temporarily detached.
1618 *
1619 * @hide
1620 */
1621 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001622
Adam Powell8568c3a2010-04-19 14:26:11 -07001623 /**
1624 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001625 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001626 * @hide
1627 */
1628 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001629
1630 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001631 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1632 * @hide
1633 */
1634 private static final int HOVERED = 0x10000000;
1635
1636 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001637 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1638 * for transform operations
1639 *
1640 * @hide
1641 */
Adam Powellf37df072010-09-17 16:22:49 -07001642 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001643
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001644 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001645 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001646
Chet Haasefd2b0022010-08-06 13:08:56 -07001647 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001648 * Indicates that this view was specifically invalidated, not just dirtied because some
1649 * child view was invalidated. The flag is used to determine when we need to recreate
1650 * a view's display list (as opposed to just returning a reference to its existing
1651 * display list).
1652 *
1653 * @hide
1654 */
1655 static final int INVALIDATED = 0x80000000;
1656
Christopher Tate3d4bf172011-03-28 16:16:46 -07001657 /* Masks for mPrivateFlags2 */
1658
1659 /**
1660 * Indicates that this view has reported that it can accept the current drag's content.
1661 * Cleared when the drag operation concludes.
1662 * @hide
1663 */
1664 static final int DRAG_CAN_ACCEPT = 0x00000001;
1665
1666 /**
1667 * Indicates that this view is currently directly under the drag location in a
1668 * drag-and-drop operation involving content that it can accept. Cleared when
1669 * the drag exits the view, or when the drag operation concludes.
1670 * @hide
1671 */
1672 static final int DRAG_HOVERED = 0x00000002;
1673
1674 /* End of masks for mPrivateFlags2 */
1675
1676 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1677
Chet Haasedaf98e92011-01-10 14:10:36 -08001678 /**
Adam Powell637d3372010-08-25 14:37:03 -07001679 * Always allow a user to over-scroll this view, provided it is a
1680 * view that can scroll.
1681 *
1682 * @see #getOverScrollMode()
1683 * @see #setOverScrollMode(int)
1684 */
1685 public static final int OVER_SCROLL_ALWAYS = 0;
1686
1687 /**
1688 * Allow a user to over-scroll this view only if the content is large
1689 * enough to meaningfully scroll, provided it is a view that can scroll.
1690 *
1691 * @see #getOverScrollMode()
1692 * @see #setOverScrollMode(int)
1693 */
1694 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1695
1696 /**
1697 * Never allow a user to over-scroll this view.
1698 *
1699 * @see #getOverScrollMode()
1700 * @see #setOverScrollMode(int)
1701 */
1702 public static final int OVER_SCROLL_NEVER = 2;
1703
1704 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001705 * View has requested the status bar to be visible (the default).
1706 *
Joe Malin32736f02011-01-19 16:14:20 -08001707 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001708 */
1709 public static final int STATUS_BAR_VISIBLE = 0;
1710
1711 /**
Jeff Brown05dc66a2011-03-02 14:41:58 -08001712 * View has requested the status bar to be hidden.
Joe Onorato664644d2011-01-23 17:53:23 -08001713 *
Joe Malin32736f02011-01-19 16:14:20 -08001714 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001715 */
1716 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1717
1718 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001719 * @hide
1720 *
1721 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1722 * out of the public fields to keep the undefined bits out of the developer's way.
1723 *
1724 * Flag to make the status bar not expandable. Unless you also
1725 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1726 */
1727 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1728
1729 /**
1730 * @hide
1731 *
1732 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1733 * out of the public fields to keep the undefined bits out of the developer's way.
1734 *
1735 * Flag to hide notification icons and scrolling ticker text.
1736 */
1737 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1738
1739 /**
1740 * @hide
1741 *
1742 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1743 * out of the public fields to keep the undefined bits out of the developer's way.
1744 *
1745 * Flag to disable incoming notification alerts. This will not block
1746 * icons, but it will block sound, vibrating and other visual or aural notifications.
1747 */
1748 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1749
1750 /**
1751 * @hide
1752 *
1753 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1754 * out of the public fields to keep the undefined bits out of the developer's way.
1755 *
1756 * Flag to hide only the scrolling ticker. Note that
1757 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1758 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1759 */
1760 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1761
1762 /**
1763 * @hide
1764 *
1765 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1766 * out of the public fields to keep the undefined bits out of the developer's way.
1767 *
1768 * Flag to hide the center system info area.
1769 */
1770 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1771
1772 /**
1773 * @hide
1774 *
1775 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1776 * out of the public fields to keep the undefined bits out of the developer's way.
1777 *
1778 * Flag to hide only the navigation buttons. Don't use this
1779 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001780 *
1781 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001782 */
1783 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1784
1785 /**
1786 * @hide
1787 *
1788 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1789 * out of the public fields to keep the undefined bits out of the developer's way.
1790 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001791 * Flag to hide only the back button. Don't use this
1792 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1793 */
1794 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1795
1796 /**
1797 * @hide
1798 *
1799 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1800 * out of the public fields to keep the undefined bits out of the developer's way.
1801 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001802 * Flag to hide only the clock. You might use this if your activity has
1803 * its own clock making the status bar's clock redundant.
1804 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001805 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1806
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001807 /**
1808 * @hide
1809 */
1810 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001811
1812 /**
Adam Powell637d3372010-08-25 14:37:03 -07001813 * Controls the over-scroll mode for this view.
1814 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1815 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1816 * and {@link #OVER_SCROLL_NEVER}.
1817 */
1818 private int mOverScrollMode;
1819
1820 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 * The parent this view is attached to.
1822 * {@hide}
1823 *
1824 * @see #getParent()
1825 */
1826 protected ViewParent mParent;
1827
1828 /**
1829 * {@hide}
1830 */
1831 AttachInfo mAttachInfo;
1832
1833 /**
1834 * {@hide}
1835 */
Romain Guy809a7f62009-05-14 15:44:42 -07001836 @ViewDebug.ExportedProperty(flagMapping = {
1837 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1838 name = "FORCE_LAYOUT"),
1839 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1840 name = "LAYOUT_REQUIRED"),
1841 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001842 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001843 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1844 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1845 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1846 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1847 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001849 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850
1851 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001852 * This view's request for the visibility of the status bar.
1853 * @hide
1854 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001855 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001856 int mSystemUiVisibility;
1857
1858 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 * Count of how many windows this view has been attached to.
1860 */
1861 int mWindowAttachCount;
1862
1863 /**
1864 * The layout parameters associated with this view and used by the parent
1865 * {@link android.view.ViewGroup} to determine how this view should be
1866 * laid out.
1867 * {@hide}
1868 */
1869 protected ViewGroup.LayoutParams mLayoutParams;
1870
1871 /**
1872 * The view flags hold various views states.
1873 * {@hide}
1874 */
1875 @ViewDebug.ExportedProperty
1876 int mViewFlags;
1877
1878 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001879 * The transform matrix for the View. This transform is calculated internally
1880 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1881 * is used by default. Do *not* use this variable directly; instead call
1882 * getMatrix(), which will automatically recalculate the matrix if necessary
1883 * to get the correct matrix based on the latest rotation and scale properties.
1884 */
1885 private final Matrix mMatrix = new Matrix();
1886
1887 /**
1888 * The transform matrix for the View. This transform is calculated internally
1889 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1890 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001891 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001892 * to get the correct matrix based on the latest rotation and scale properties.
1893 */
1894 private Matrix mInverseMatrix;
1895
1896 /**
1897 * An internal variable that tracks whether we need to recalculate the
1898 * transform matrix, based on whether the rotation or scaleX/Y properties
1899 * have changed since the matrix was last calculated.
1900 */
Chet Haasea00f3862011-02-22 06:34:40 -08001901 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07001902
1903 /**
1904 * An internal variable that tracks whether we need to recalculate the
1905 * transform matrix, based on whether the rotation or scaleX/Y properties
1906 * have changed since the matrix was last calculated.
1907 */
1908 private boolean mInverseMatrixDirty = true;
1909
1910 /**
1911 * A variable that tracks whether we need to recalculate the
1912 * transform matrix, based on whether the rotation or scaleX/Y properties
1913 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001914 * is only valid after a call to updateMatrix() or to a function that
1915 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001916 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001917 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001918
1919 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001920 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
1921 */
1922 private Camera mCamera = null;
1923
1924 /**
1925 * This matrix is used when computing the matrix for 3D rotations.
1926 */
1927 private Matrix matrix3D = null;
1928
1929 /**
1930 * These prev values are used to recalculate a centered pivot point when necessary. The
1931 * pivot point is only used in matrix operations (when rotation, scale, or translation are
1932 * set), so thes values are only used then as well.
1933 */
1934 private int mPrevWidth = -1;
1935 private int mPrevHeight = -1;
1936
Joe Malin32736f02011-01-19 16:14:20 -08001937 private boolean mLastIsOpaque;
1938
Chet Haasefd2b0022010-08-06 13:08:56 -07001939 /**
1940 * Convenience value to check for float values that are close enough to zero to be considered
1941 * zero.
1942 */
Romain Guy2542d192010-08-18 11:47:12 -07001943 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001944
1945 /**
1946 * The degrees rotation around the vertical axis through the pivot point.
1947 */
1948 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001949 float mRotationY = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001950
1951 /**
1952 * The degrees rotation around the horizontal axis through the pivot point.
1953 */
1954 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001955 float mRotationX = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001956
1957 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001958 * The degrees rotation around the pivot point.
1959 */
1960 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001961 float mRotation = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001962
1963 /**
Chet Haasedf030d22010-07-30 17:22:38 -07001964 * The amount of translation of the object away from its left property (post-layout).
1965 */
1966 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001967 float mTranslationX = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07001968
1969 /**
1970 * The amount of translation of the object away from its top property (post-layout).
1971 */
1972 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001973 float mTranslationY = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07001974
1975 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001976 * The amount of scale in the x direction around the pivot point. A
1977 * value of 1 means no scaling is applied.
1978 */
1979 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001980 float mScaleX = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001981
1982 /**
1983 * The amount of scale in the y direction around the pivot point. A
1984 * value of 1 means no scaling is applied.
1985 */
1986 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001987 float mScaleY = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001988
1989 /**
1990 * The amount of scale in the x direction around the pivot point. A
1991 * value of 1 means no scaling is applied.
1992 */
1993 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001994 float mPivotX = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001995
1996 /**
1997 * The amount of scale in the y direction around the pivot point. A
1998 * value of 1 means no scaling is applied.
1999 */
2000 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002001 float mPivotY = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002002
2003 /**
2004 * The opacity of the View. This is a value from 0 to 1, where 0 means
2005 * completely transparent and 1 means completely opaque.
2006 */
2007 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002008 float mAlpha = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002009
2010 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 * The distance in pixels from the left edge of this view's parent
2012 * to the left edge of this view.
2013 * {@hide}
2014 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002015 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 protected int mLeft;
2017 /**
2018 * The distance in pixels from the left edge of this view's parent
2019 * to the right edge of this view.
2020 * {@hide}
2021 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002022 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 protected int mRight;
2024 /**
2025 * The distance in pixels from the top edge of this view's parent
2026 * to the top edge of this view.
2027 * {@hide}
2028 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002029 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 protected int mTop;
2031 /**
2032 * The distance in pixels from the top edge of this view's parent
2033 * to the bottom edge of this view.
2034 * {@hide}
2035 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002036 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 protected int mBottom;
2038
2039 /**
2040 * The offset, in pixels, by which the content of this view is scrolled
2041 * horizontally.
2042 * {@hide}
2043 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002044 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 protected int mScrollX;
2046 /**
2047 * The offset, in pixels, by which the content of this view is scrolled
2048 * vertically.
2049 * {@hide}
2050 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002051 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 protected int mScrollY;
2053
2054 /**
2055 * The left padding in pixels, that is the distance in pixels between the
2056 * left edge of this view and the left edge of its content.
2057 * {@hide}
2058 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002059 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 protected int mPaddingLeft;
2061 /**
2062 * The right padding in pixels, that is the distance in pixels between the
2063 * right edge of this view and the right edge of its content.
2064 * {@hide}
2065 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002066 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 protected int mPaddingRight;
2068 /**
2069 * The top padding in pixels, that is the distance in pixels between the
2070 * top edge of this view and the top edge of its content.
2071 * {@hide}
2072 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002073 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 protected int mPaddingTop;
2075 /**
2076 * The bottom padding in pixels, that is the distance in pixels between the
2077 * bottom edge of this view and the bottom edge of its content.
2078 * {@hide}
2079 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002080 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 protected int mPaddingBottom;
2082
2083 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002084 * Briefly describes the view and is primarily used for accessibility support.
2085 */
2086 private CharSequence mContentDescription;
2087
2088 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 * Cache the paddingRight set by the user to append to the scrollbar's size.
2090 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002091 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 int mUserPaddingRight;
2093
2094 /**
2095 * Cache the paddingBottom set by the user to append to the scrollbar's size.
2096 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002097 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 int mUserPaddingBottom;
2099
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002100 /**
Adam Powell20232d02010-12-08 21:08:53 -08002101 * Cache the paddingLeft set by the user to append to the scrollbar's size.
2102 */
2103 @ViewDebug.ExportedProperty(category = "padding")
2104 int mUserPaddingLeft;
2105
2106 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002107 * @hide
2108 */
2109 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2110 /**
2111 * @hide
2112 */
2113 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114
2115 private Resources mResources = null;
2116
2117 private Drawable mBGDrawable;
2118
2119 private int mBackgroundResource;
2120 private boolean mBackgroundSizeChanged;
2121
2122 /**
2123 * Listener used to dispatch focus change events.
2124 * This field should be made private, so it is hidden from the SDK.
2125 * {@hide}
2126 */
2127 protected OnFocusChangeListener mOnFocusChangeListener;
2128
2129 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002130 * Listeners for layout change events.
2131 */
2132 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2133
2134 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002135 * Listeners for attach events.
2136 */
2137 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2138
2139 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 * Listener used to dispatch click events.
2141 * This field should be made private, so it is hidden from the SDK.
2142 * {@hide}
2143 */
2144 protected OnClickListener mOnClickListener;
2145
2146 /**
2147 * Listener used to dispatch long click events.
2148 * This field should be made private, so it is hidden from the SDK.
2149 * {@hide}
2150 */
2151 protected OnLongClickListener mOnLongClickListener;
2152
2153 /**
2154 * Listener used to build the context menu.
2155 * This field should be made private, so it is hidden from the SDK.
2156 * {@hide}
2157 */
2158 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2159
2160 private OnKeyListener mOnKeyListener;
2161
2162 private OnTouchListener mOnTouchListener;
2163
Jeff Brown33bbfd22011-02-24 20:55:35 -08002164 private OnGenericMotionListener mOnGenericMotionListener;
2165
Chris Tate32affef2010-10-18 15:29:21 -07002166 private OnDragListener mOnDragListener;
2167
Joe Onorato664644d2011-01-23 17:53:23 -08002168 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 /**
2171 * The application environment this view lives in.
2172 * This field should be made private, so it is hidden from the SDK.
2173 * {@hide}
2174 */
2175 protected Context mContext;
2176
2177 private ScrollabilityCache mScrollCache;
2178
2179 private int[] mDrawableState = null;
2180
Romain Guy0211a0a2011-02-14 16:34:59 -08002181 /**
2182 * Set to true when drawing cache is enabled and cannot be created.
2183 *
2184 * @hide
2185 */
2186 public boolean mCachingFailed;
2187
Romain Guy02890fd2010-08-06 17:58:44 -07002188 private Bitmap mDrawingCache;
2189 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002190 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002191 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192
2193 /**
2194 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2195 * the user may specify which view to go to next.
2196 */
2197 private int mNextFocusLeftId = View.NO_ID;
2198
2199 /**
2200 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2201 * the user may specify which view to go to next.
2202 */
2203 private int mNextFocusRightId = View.NO_ID;
2204
2205 /**
2206 * When this view has focus and the next focus is {@link #FOCUS_UP},
2207 * the user may specify which view to go to next.
2208 */
2209 private int mNextFocusUpId = View.NO_ID;
2210
2211 /**
2212 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2213 * the user may specify which view to go to next.
2214 */
2215 private int mNextFocusDownId = View.NO_ID;
2216
Jeff Brown4e6319b2010-12-13 10:36:51 -08002217 /**
2218 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2219 * the user may specify which view to go to next.
2220 */
2221 int mNextFocusForwardId = View.NO_ID;
2222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002224 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002225 private PerformClick mPerformClick;
Joe Malin32736f02011-01-19 16:14:20 -08002226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 private UnsetPressedState mUnsetPressedState;
2228
2229 /**
2230 * Whether the long press's action has been invoked. The tap's action is invoked on the
2231 * up event while a long press is invoked as soon as the long press duration is reached, so
2232 * a long press could be performed before the tap is checked, in which case the tap's action
2233 * should not be invoked.
2234 */
2235 private boolean mHasPerformedLongPress;
2236
2237 /**
2238 * The minimum height of the view. We'll try our best to have the height
2239 * of this view to at least this amount.
2240 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002241 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 private int mMinHeight;
2243
2244 /**
2245 * The minimum width of the view. We'll try our best to have the width
2246 * of this view to at least this amount.
2247 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002248 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 private int mMinWidth;
2250
2251 /**
2252 * The delegate to handle touch events that are physically in this view
2253 * but should be handled by another view.
2254 */
2255 private TouchDelegate mTouchDelegate = null;
2256
2257 /**
2258 * Solid color to use as a background when creating the drawing cache. Enables
2259 * the cache to use 16 bit bitmaps instead of 32 bit.
2260 */
2261 private int mDrawingCacheBackgroundColor = 0;
2262
2263 /**
2264 * Special tree observer used when mAttachInfo is null.
2265 */
2266 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002267
Adam Powelle14579b2009-12-16 18:39:52 -08002268 /**
2269 * Cache the touch slop from the context that created the view.
2270 */
2271 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002274 * Object that handles automatic animation of view properties.
2275 */
2276 private ViewPropertyAnimator mAnimator = null;
2277
2278 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002279 * Flag indicating that a drag can cross window boundaries. When
2280 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2281 * with this flag set, all visible applications will be able to participate
2282 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002283 *
2284 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002285 */
2286 public static final int DRAG_FLAG_GLOBAL = 1;
2287
2288 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002289 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2290 */
2291 private float mVerticalScrollFactor;
2292
2293 /**
Adam Powell20232d02010-12-08 21:08:53 -08002294 * Position of the vertical scroll bar.
2295 */
2296 private int mVerticalScrollbarPosition;
2297
2298 /**
2299 * Position the scroll bar at the default position as determined by the system.
2300 */
2301 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2302
2303 /**
2304 * Position the scroll bar along the left edge.
2305 */
2306 public static final int SCROLLBAR_POSITION_LEFT = 1;
2307
2308 /**
2309 * Position the scroll bar along the right edge.
2310 */
2311 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2312
2313 /**
Romain Guy171c5922011-01-06 10:04:23 -08002314 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002315 *
2316 * @see #getLayerType()
2317 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002318 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002319 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002320 */
2321 public static final int LAYER_TYPE_NONE = 0;
2322
2323 /**
2324 * <p>Indicates that the view has a software layer. A software layer is backed
2325 * by a bitmap and causes the view to be rendered using Android's software
2326 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002327 *
Romain Guy171c5922011-01-06 10:04:23 -08002328 * <p>Software layers have various usages:</p>
2329 * <p>When the application is not using hardware acceleration, a software layer
2330 * is useful to apply a specific color filter and/or blending mode and/or
2331 * translucency to a view and all its children.</p>
2332 * <p>When the application is using hardware acceleration, a software layer
2333 * is useful to render drawing primitives not supported by the hardware
2334 * accelerated pipeline. It can also be used to cache a complex view tree
2335 * into a texture and reduce the complexity of drawing operations. For instance,
2336 * when animating a complex view tree with a translation, a software layer can
2337 * be used to render the view tree only once.</p>
2338 * <p>Software layers should be avoided when the affected view tree updates
2339 * often. Every update will require to re-render the software layer, which can
2340 * potentially be slow (particularly when hardware acceleration is turned on
2341 * since the layer will have to be uploaded into a hardware texture after every
2342 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002343 *
2344 * @see #getLayerType()
2345 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002346 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002347 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002348 */
2349 public static final int LAYER_TYPE_SOFTWARE = 1;
2350
2351 /**
2352 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2353 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2354 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2355 * rendering pipeline, but only if hardware acceleration is turned on for the
2356 * view hierarchy. When hardware acceleration is turned off, hardware layers
2357 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002358 *
Romain Guy171c5922011-01-06 10:04:23 -08002359 * <p>A hardware layer is useful to apply a specific color filter and/or
2360 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002361 * <p>A hardware layer can be used to cache a complex view tree into a
2362 * texture and reduce the complexity of drawing operations. For instance,
2363 * when animating a complex view tree with a translation, a hardware layer can
2364 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002365 * <p>A hardware layer can also be used to increase the rendering quality when
2366 * rotation transformations are applied on a view. It can also be used to
2367 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002368 *
2369 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002370 * @see #setLayerType(int, android.graphics.Paint)
2371 * @see #LAYER_TYPE_NONE
2372 * @see #LAYER_TYPE_SOFTWARE
2373 */
2374 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002375
Romain Guy3aaff3a2011-01-12 14:18:47 -08002376 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2377 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2378 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2379 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2380 })
Romain Guy171c5922011-01-06 10:04:23 -08002381 int mLayerType = LAYER_TYPE_NONE;
2382 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002383 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002384
2385 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002386 * Consistency verifier for debugging purposes.
2387 * @hide
2388 */
2389 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2390 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2391 new InputEventConsistencyVerifier(this, 0) : null;
2392
2393 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 * Simple constructor to use when creating a view from code.
2395 *
2396 * @param context The Context the view is running in, through which it can
2397 * access the current theme, resources, etc.
2398 */
2399 public View(Context context) {
2400 mContext = context;
2401 mResources = context != null ? context.getResources() : null;
Romain Guy8f1344f52009-05-15 16:03:59 -07002402 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Adam Powelle14579b2009-12-16 18:39:52 -08002403 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002404 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 }
2406
2407 /**
2408 * Constructor that is called when inflating a view from XML. This is called
2409 * when a view is being constructed from an XML file, supplying attributes
2410 * that were specified in the XML file. This version uses a default style of
2411 * 0, so the only attribute values applied are those in the Context's Theme
2412 * and the given AttributeSet.
2413 *
2414 * <p>
2415 * The method onFinishInflate() will be called after all children have been
2416 * added.
2417 *
2418 * @param context The Context the view is running in, through which it can
2419 * access the current theme, resources, etc.
2420 * @param attrs The attributes of the XML tag that is inflating the view.
2421 * @see #View(Context, AttributeSet, int)
2422 */
2423 public View(Context context, AttributeSet attrs) {
2424 this(context, attrs, 0);
2425 }
2426
2427 /**
2428 * Perform inflation from XML and apply a class-specific base style. This
2429 * constructor of View allows subclasses to use their own base style when
2430 * they are inflating. For example, a Button class's constructor would call
2431 * this version of the super class constructor and supply
2432 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2433 * the theme's button style to modify all of the base view attributes (in
2434 * particular its background) as well as the Button class's attributes.
2435 *
2436 * @param context The Context the view is running in, through which it can
2437 * access the current theme, resources, etc.
2438 * @param attrs The attributes of the XML tag that is inflating the view.
2439 * @param defStyle The default style to apply to this view. If 0, no style
2440 * will be applied (beyond what is included in the theme). This may
2441 * either be an attribute resource, whose value will be retrieved
2442 * from the current theme, or an explicit style resource.
2443 * @see #View(Context, AttributeSet)
2444 */
2445 public View(Context context, AttributeSet attrs, int defStyle) {
2446 this(context);
2447
2448 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2449 defStyle, 0);
2450
2451 Drawable background = null;
2452
2453 int leftPadding = -1;
2454 int topPadding = -1;
2455 int rightPadding = -1;
2456 int bottomPadding = -1;
2457
2458 int padding = -1;
2459
2460 int viewFlagValues = 0;
2461 int viewFlagMasks = 0;
2462
2463 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002465 int x = 0;
2466 int y = 0;
2467
Chet Haase73066682010-11-29 15:55:32 -08002468 float tx = 0;
2469 float ty = 0;
2470 float rotation = 0;
2471 float rotationX = 0;
2472 float rotationY = 0;
2473 float sx = 1f;
2474 float sy = 1f;
2475 boolean transformSet = false;
2476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2478
Adam Powell637d3372010-08-25 14:37:03 -07002479 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 final int N = a.getIndexCount();
2481 for (int i = 0; i < N; i++) {
2482 int attr = a.getIndex(i);
2483 switch (attr) {
2484 case com.android.internal.R.styleable.View_background:
2485 background = a.getDrawable(attr);
2486 break;
2487 case com.android.internal.R.styleable.View_padding:
2488 padding = a.getDimensionPixelSize(attr, -1);
2489 break;
2490 case com.android.internal.R.styleable.View_paddingLeft:
2491 leftPadding = a.getDimensionPixelSize(attr, -1);
2492 break;
2493 case com.android.internal.R.styleable.View_paddingTop:
2494 topPadding = a.getDimensionPixelSize(attr, -1);
2495 break;
2496 case com.android.internal.R.styleable.View_paddingRight:
2497 rightPadding = a.getDimensionPixelSize(attr, -1);
2498 break;
2499 case com.android.internal.R.styleable.View_paddingBottom:
2500 bottomPadding = a.getDimensionPixelSize(attr, -1);
2501 break;
2502 case com.android.internal.R.styleable.View_scrollX:
2503 x = a.getDimensionPixelOffset(attr, 0);
2504 break;
2505 case com.android.internal.R.styleable.View_scrollY:
2506 y = a.getDimensionPixelOffset(attr, 0);
2507 break;
Chet Haase73066682010-11-29 15:55:32 -08002508 case com.android.internal.R.styleable.View_alpha:
2509 setAlpha(a.getFloat(attr, 1f));
2510 break;
2511 case com.android.internal.R.styleable.View_transformPivotX:
2512 setPivotX(a.getDimensionPixelOffset(attr, 0));
2513 break;
2514 case com.android.internal.R.styleable.View_transformPivotY:
2515 setPivotY(a.getDimensionPixelOffset(attr, 0));
2516 break;
2517 case com.android.internal.R.styleable.View_translationX:
2518 tx = a.getDimensionPixelOffset(attr, 0);
2519 transformSet = true;
2520 break;
2521 case com.android.internal.R.styleable.View_translationY:
2522 ty = a.getDimensionPixelOffset(attr, 0);
2523 transformSet = true;
2524 break;
2525 case com.android.internal.R.styleable.View_rotation:
2526 rotation = a.getFloat(attr, 0);
2527 transformSet = true;
2528 break;
2529 case com.android.internal.R.styleable.View_rotationX:
2530 rotationX = a.getFloat(attr, 0);
2531 transformSet = true;
2532 break;
2533 case com.android.internal.R.styleable.View_rotationY:
2534 rotationY = a.getFloat(attr, 0);
2535 transformSet = true;
2536 break;
2537 case com.android.internal.R.styleable.View_scaleX:
2538 sx = a.getFloat(attr, 1f);
2539 transformSet = true;
2540 break;
2541 case com.android.internal.R.styleable.View_scaleY:
2542 sy = a.getFloat(attr, 1f);
2543 transformSet = true;
2544 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 case com.android.internal.R.styleable.View_id:
2546 mID = a.getResourceId(attr, NO_ID);
2547 break;
2548 case com.android.internal.R.styleable.View_tag:
2549 mTag = a.getText(attr);
2550 break;
2551 case com.android.internal.R.styleable.View_fitsSystemWindows:
2552 if (a.getBoolean(attr, false)) {
2553 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2554 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2555 }
2556 break;
2557 case com.android.internal.R.styleable.View_focusable:
2558 if (a.getBoolean(attr, false)) {
2559 viewFlagValues |= FOCUSABLE;
2560 viewFlagMasks |= FOCUSABLE_MASK;
2561 }
2562 break;
2563 case com.android.internal.R.styleable.View_focusableInTouchMode:
2564 if (a.getBoolean(attr, false)) {
2565 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2566 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2567 }
2568 break;
2569 case com.android.internal.R.styleable.View_clickable:
2570 if (a.getBoolean(attr, false)) {
2571 viewFlagValues |= CLICKABLE;
2572 viewFlagMasks |= CLICKABLE;
2573 }
2574 break;
2575 case com.android.internal.R.styleable.View_longClickable:
2576 if (a.getBoolean(attr, false)) {
2577 viewFlagValues |= LONG_CLICKABLE;
2578 viewFlagMasks |= LONG_CLICKABLE;
2579 }
2580 break;
2581 case com.android.internal.R.styleable.View_saveEnabled:
2582 if (!a.getBoolean(attr, true)) {
2583 viewFlagValues |= SAVE_DISABLED;
2584 viewFlagMasks |= SAVE_DISABLED_MASK;
2585 }
2586 break;
2587 case com.android.internal.R.styleable.View_duplicateParentState:
2588 if (a.getBoolean(attr, false)) {
2589 viewFlagValues |= DUPLICATE_PARENT_STATE;
2590 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2591 }
2592 break;
2593 case com.android.internal.R.styleable.View_visibility:
2594 final int visibility = a.getInt(attr, 0);
2595 if (visibility != 0) {
2596 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2597 viewFlagMasks |= VISIBILITY_MASK;
2598 }
2599 break;
2600 case com.android.internal.R.styleable.View_drawingCacheQuality:
2601 final int cacheQuality = a.getInt(attr, 0);
2602 if (cacheQuality != 0) {
2603 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2604 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2605 }
2606 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002607 case com.android.internal.R.styleable.View_contentDescription:
2608 mContentDescription = a.getString(attr);
2609 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2611 if (!a.getBoolean(attr, true)) {
2612 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2613 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2614 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002615 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2617 if (!a.getBoolean(attr, true)) {
2618 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2619 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2620 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002621 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 case R.styleable.View_scrollbars:
2623 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2624 if (scrollbars != SCROLLBARS_NONE) {
2625 viewFlagValues |= scrollbars;
2626 viewFlagMasks |= SCROLLBARS_MASK;
2627 initializeScrollbars(a);
2628 }
2629 break;
2630 case R.styleable.View_fadingEdge:
2631 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2632 if (fadingEdge != FADING_EDGE_NONE) {
2633 viewFlagValues |= fadingEdge;
2634 viewFlagMasks |= FADING_EDGE_MASK;
2635 initializeFadingEdge(a);
2636 }
2637 break;
2638 case R.styleable.View_scrollbarStyle:
2639 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2640 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2641 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2642 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2643 }
2644 break;
2645 case R.styleable.View_isScrollContainer:
2646 setScrollContainer = true;
2647 if (a.getBoolean(attr, false)) {
2648 setScrollContainer(true);
2649 }
2650 break;
2651 case com.android.internal.R.styleable.View_keepScreenOn:
2652 if (a.getBoolean(attr, false)) {
2653 viewFlagValues |= KEEP_SCREEN_ON;
2654 viewFlagMasks |= KEEP_SCREEN_ON;
2655 }
2656 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002657 case R.styleable.View_filterTouchesWhenObscured:
2658 if (a.getBoolean(attr, false)) {
2659 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2660 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2661 }
2662 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663 case R.styleable.View_nextFocusLeft:
2664 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2665 break;
2666 case R.styleable.View_nextFocusRight:
2667 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2668 break;
2669 case R.styleable.View_nextFocusUp:
2670 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2671 break;
2672 case R.styleable.View_nextFocusDown:
2673 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2674 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002675 case R.styleable.View_nextFocusForward:
2676 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2677 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678 case R.styleable.View_minWidth:
2679 mMinWidth = a.getDimensionPixelSize(attr, 0);
2680 break;
2681 case R.styleable.View_minHeight:
2682 mMinHeight = a.getDimensionPixelSize(attr, 0);
2683 break;
Romain Guy9a817362009-05-01 10:57:14 -07002684 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002685 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002686 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002687 + "be used within a restricted context");
2688 }
2689
Romain Guy9a817362009-05-01 10:57:14 -07002690 final String handlerName = a.getString(attr);
2691 if (handlerName != null) {
2692 setOnClickListener(new OnClickListener() {
2693 private Method mHandler;
2694
2695 public void onClick(View v) {
2696 if (mHandler == null) {
2697 try {
2698 mHandler = getContext().getClass().getMethod(handlerName,
2699 View.class);
2700 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002701 int id = getId();
2702 String idText = id == NO_ID ? "" : " with id '"
2703 + getContext().getResources().getResourceEntryName(
2704 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002705 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002706 handlerName + "(View) in the activity "
2707 + getContext().getClass() + " for onClick handler"
2708 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002709 }
2710 }
2711
2712 try {
2713 mHandler.invoke(getContext(), View.this);
2714 } catch (IllegalAccessException e) {
2715 throw new IllegalStateException("Could not execute non "
2716 + "public method of the activity", e);
2717 } catch (InvocationTargetException e) {
2718 throw new IllegalStateException("Could not execute "
2719 + "method of the activity", e);
2720 }
2721 }
2722 });
2723 }
2724 break;
Adam Powell637d3372010-08-25 14:37:03 -07002725 case R.styleable.View_overScrollMode:
2726 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2727 break;
Adam Powell20232d02010-12-08 21:08:53 -08002728 case R.styleable.View_verticalScrollbarPosition:
2729 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2730 break;
Romain Guy171c5922011-01-06 10:04:23 -08002731 case R.styleable.View_layerType:
2732 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2733 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 }
2735 }
2736
Adam Powell637d3372010-08-25 14:37:03 -07002737 setOverScrollMode(overScrollMode);
2738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 if (background != null) {
2740 setBackgroundDrawable(background);
2741 }
2742
2743 if (padding >= 0) {
2744 leftPadding = padding;
2745 topPadding = padding;
2746 rightPadding = padding;
2747 bottomPadding = padding;
2748 }
2749
2750 // If the user specified the padding (either with android:padding or
2751 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2752 // use the default padding or the padding from the background drawable
2753 // (stored at this point in mPadding*)
2754 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2755 topPadding >= 0 ? topPadding : mPaddingTop,
2756 rightPadding >= 0 ? rightPadding : mPaddingRight,
2757 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2758
2759 if (viewFlagMasks != 0) {
2760 setFlags(viewFlagValues, viewFlagMasks);
2761 }
2762
2763 // Needs to be called after mViewFlags is set
2764 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2765 recomputePadding();
2766 }
2767
2768 if (x != 0 || y != 0) {
2769 scrollTo(x, y);
2770 }
2771
Chet Haase73066682010-11-29 15:55:32 -08002772 if (transformSet) {
2773 setTranslationX(tx);
2774 setTranslationY(ty);
2775 setRotation(rotation);
2776 setRotationX(rotationX);
2777 setRotationY(rotationY);
2778 setScaleX(sx);
2779 setScaleY(sy);
2780 }
2781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2783 setScrollContainer(true);
2784 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002785
2786 computeOpaqueFlags();
2787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 a.recycle();
2789 }
2790
2791 /**
2792 * Non-public constructor for use in testing
2793 */
2794 View() {
2795 }
2796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 /**
2798 * <p>
2799 * Initializes the fading edges from a given set of styled attributes. This
2800 * method should be called by subclasses that need fading edges and when an
2801 * instance of these subclasses is created programmatically rather than
2802 * being inflated from XML. This method is automatically called when the XML
2803 * is inflated.
2804 * </p>
2805 *
2806 * @param a the styled attributes set to initialize the fading edges from
2807 */
2808 protected void initializeFadingEdge(TypedArray a) {
2809 initScrollCache();
2810
2811 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2812 R.styleable.View_fadingEdgeLength,
2813 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2814 }
2815
2816 /**
2817 * Returns the size of the vertical faded edges used to indicate that more
2818 * content in this view is visible.
2819 *
2820 * @return The size in pixels of the vertical faded edge or 0 if vertical
2821 * faded edges are not enabled for this view.
2822 * @attr ref android.R.styleable#View_fadingEdgeLength
2823 */
2824 public int getVerticalFadingEdgeLength() {
2825 if (isVerticalFadingEdgeEnabled()) {
2826 ScrollabilityCache cache = mScrollCache;
2827 if (cache != null) {
2828 return cache.fadingEdgeLength;
2829 }
2830 }
2831 return 0;
2832 }
2833
2834 /**
2835 * Set the size of the faded edge used to indicate that more content in this
2836 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07002837 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
2838 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
2839 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 *
2841 * @param length The size in pixels of the faded edge used to indicate that more
2842 * content in this view is visible.
2843 */
2844 public void setFadingEdgeLength(int length) {
2845 initScrollCache();
2846 mScrollCache.fadingEdgeLength = length;
2847 }
2848
2849 /**
2850 * Returns the size of the horizontal faded edges used to indicate that more
2851 * content in this view is visible.
2852 *
2853 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2854 * faded edges are not enabled for this view.
2855 * @attr ref android.R.styleable#View_fadingEdgeLength
2856 */
2857 public int getHorizontalFadingEdgeLength() {
2858 if (isHorizontalFadingEdgeEnabled()) {
2859 ScrollabilityCache cache = mScrollCache;
2860 if (cache != null) {
2861 return cache.fadingEdgeLength;
2862 }
2863 }
2864 return 0;
2865 }
2866
2867 /**
2868 * Returns the width of the vertical scrollbar.
2869 *
2870 * @return The width in pixels of the vertical scrollbar or 0 if there
2871 * is no vertical scrollbar.
2872 */
2873 public int getVerticalScrollbarWidth() {
2874 ScrollabilityCache cache = mScrollCache;
2875 if (cache != null) {
2876 ScrollBarDrawable scrollBar = cache.scrollBar;
2877 if (scrollBar != null) {
2878 int size = scrollBar.getSize(true);
2879 if (size <= 0) {
2880 size = cache.scrollBarSize;
2881 }
2882 return size;
2883 }
2884 return 0;
2885 }
2886 return 0;
2887 }
2888
2889 /**
2890 * Returns the height of the horizontal scrollbar.
2891 *
2892 * @return The height in pixels of the horizontal scrollbar or 0 if
2893 * there is no horizontal scrollbar.
2894 */
2895 protected int getHorizontalScrollbarHeight() {
2896 ScrollabilityCache cache = mScrollCache;
2897 if (cache != null) {
2898 ScrollBarDrawable scrollBar = cache.scrollBar;
2899 if (scrollBar != null) {
2900 int size = scrollBar.getSize(false);
2901 if (size <= 0) {
2902 size = cache.scrollBarSize;
2903 }
2904 return size;
2905 }
2906 return 0;
2907 }
2908 return 0;
2909 }
2910
2911 /**
2912 * <p>
2913 * Initializes the scrollbars from a given set of styled attributes. This
2914 * method should be called by subclasses that need scrollbars and when an
2915 * instance of these subclasses is created programmatically rather than
2916 * being inflated from XML. This method is automatically called when the XML
2917 * is inflated.
2918 * </p>
2919 *
2920 * @param a the styled attributes set to initialize the scrollbars from
2921 */
2922 protected void initializeScrollbars(TypedArray a) {
2923 initScrollCache();
2924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08002926
Mike Cleronf116bf82009-09-27 19:14:12 -07002927 if (scrollabilityCache.scrollBar == null) {
2928 scrollabilityCache.scrollBar = new ScrollBarDrawable();
2929 }
Joe Malin32736f02011-01-19 16:14:20 -08002930
Romain Guy8bda2482010-03-02 11:42:11 -08002931 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932
Mike Cleronf116bf82009-09-27 19:14:12 -07002933 if (!fadeScrollbars) {
2934 scrollabilityCache.state = ScrollabilityCache.ON;
2935 }
2936 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08002937
2938
Mike Cleronf116bf82009-09-27 19:14:12 -07002939 scrollabilityCache.scrollBarFadeDuration = a.getInt(
2940 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
2941 .getScrollBarFadeDuration());
2942 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
2943 R.styleable.View_scrollbarDefaultDelayBeforeFade,
2944 ViewConfiguration.getScrollDefaultDelay());
2945
Joe Malin32736f02011-01-19 16:14:20 -08002946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
2948 com.android.internal.R.styleable.View_scrollbarSize,
2949 ViewConfiguration.get(mContext).getScaledScrollBarSize());
2950
2951 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
2952 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
2953
2954 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
2955 if (thumb != null) {
2956 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
2957 }
2958
2959 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
2960 false);
2961 if (alwaysDraw) {
2962 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
2963 }
2964
2965 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
2966 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
2967
2968 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
2969 if (thumb != null) {
2970 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
2971 }
2972
2973 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
2974 false);
2975 if (alwaysDraw) {
2976 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
2977 }
2978
2979 // Re-apply user/background padding so that scrollbar(s) get added
2980 recomputePadding();
2981 }
2982
2983 /**
2984 * <p>
2985 * Initalizes the scrollability cache if necessary.
2986 * </p>
2987 */
2988 private void initScrollCache() {
2989 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07002990 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 }
2992 }
2993
2994 /**
Adam Powell20232d02010-12-08 21:08:53 -08002995 * Set the position of the vertical scroll bar. Should be one of
2996 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
2997 * {@link #SCROLLBAR_POSITION_RIGHT}.
2998 *
2999 * @param position Where the vertical scroll bar should be positioned.
3000 */
3001 public void setVerticalScrollbarPosition(int position) {
3002 if (mVerticalScrollbarPosition != position) {
3003 mVerticalScrollbarPosition = position;
3004 computeOpaqueFlags();
3005 recomputePadding();
3006 }
3007 }
3008
3009 /**
3010 * @return The position where the vertical scroll bar will show, if applicable.
3011 * @see #setVerticalScrollbarPosition(int)
3012 */
3013 public int getVerticalScrollbarPosition() {
3014 return mVerticalScrollbarPosition;
3015 }
3016
3017 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 * Register a callback to be invoked when focus of this view changed.
3019 *
3020 * @param l The callback that will run.
3021 */
3022 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3023 mOnFocusChangeListener = l;
3024 }
3025
3026 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003027 * Add a listener that will be called when the bounds of the view change due to
3028 * layout processing.
3029 *
3030 * @param listener The listener that will be called when layout bounds change.
3031 */
3032 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3033 if (mOnLayoutChangeListeners == null) {
3034 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3035 }
3036 mOnLayoutChangeListeners.add(listener);
3037 }
3038
3039 /**
3040 * Remove a listener for layout changes.
3041 *
3042 * @param listener The listener for layout bounds change.
3043 */
3044 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3045 if (mOnLayoutChangeListeners == null) {
3046 return;
3047 }
3048 mOnLayoutChangeListeners.remove(listener);
3049 }
3050
3051 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003052 * Add a listener for attach state changes.
3053 *
3054 * This listener will be called whenever this view is attached or detached
3055 * from a window. Remove the listener using
3056 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3057 *
3058 * @param listener Listener to attach
3059 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3060 */
3061 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3062 if (mOnAttachStateChangeListeners == null) {
3063 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3064 }
3065 mOnAttachStateChangeListeners.add(listener);
3066 }
3067
3068 /**
3069 * Remove a listener for attach state changes. The listener will receive no further
3070 * notification of window attach/detach events.
3071 *
3072 * @param listener Listener to remove
3073 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3074 */
3075 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3076 if (mOnAttachStateChangeListeners == null) {
3077 return;
3078 }
3079 mOnAttachStateChangeListeners.remove(listener);
3080 }
3081
3082 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 * Returns the focus-change callback registered for this view.
3084 *
3085 * @return The callback, or null if one is not registered.
3086 */
3087 public OnFocusChangeListener getOnFocusChangeListener() {
3088 return mOnFocusChangeListener;
3089 }
3090
3091 /**
3092 * Register a callback to be invoked when this view is clicked. If this view is not
3093 * clickable, it becomes clickable.
3094 *
3095 * @param l The callback that will run
3096 *
3097 * @see #setClickable(boolean)
3098 */
3099 public void setOnClickListener(OnClickListener l) {
3100 if (!isClickable()) {
3101 setClickable(true);
3102 }
3103 mOnClickListener = l;
3104 }
3105
3106 /**
3107 * Register a callback to be invoked when this view is clicked and held. If this view is not
3108 * long clickable, it becomes long clickable.
3109 *
3110 * @param l The callback that will run
3111 *
3112 * @see #setLongClickable(boolean)
3113 */
3114 public void setOnLongClickListener(OnLongClickListener l) {
3115 if (!isLongClickable()) {
3116 setLongClickable(true);
3117 }
3118 mOnLongClickListener = l;
3119 }
3120
3121 /**
3122 * Register a callback to be invoked when the context menu for this view is
3123 * being built. If this view is not long clickable, it becomes long clickable.
3124 *
3125 * @param l The callback that will run
3126 *
3127 */
3128 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3129 if (!isLongClickable()) {
3130 setLongClickable(true);
3131 }
3132 mOnCreateContextMenuListener = l;
3133 }
3134
3135 /**
3136 * Call this view's OnClickListener, if it is defined.
3137 *
3138 * @return True there was an assigned OnClickListener that was called, false
3139 * otherwise is returned.
3140 */
3141 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003142 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 if (mOnClickListener != null) {
3145 playSoundEffect(SoundEffectConstants.CLICK);
3146 mOnClickListener.onClick(this);
3147 return true;
3148 }
3149
3150 return false;
3151 }
3152
3153 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003154 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3155 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003156 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003157 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 */
3159 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003160 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 boolean handled = false;
3163 if (mOnLongClickListener != null) {
3164 handled = mOnLongClickListener.onLongClick(View.this);
3165 }
3166 if (!handled) {
3167 handled = showContextMenu();
3168 }
3169 if (handled) {
3170 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3171 }
3172 return handled;
3173 }
3174
3175 /**
3176 * Bring up the context menu for this view.
3177 *
3178 * @return Whether a context menu was displayed.
3179 */
3180 public boolean showContextMenu() {
3181 return getParent().showContextMenuForChild(this);
3182 }
3183
3184 /**
Adam Powell6e346362010-07-23 10:18:23 -07003185 * Start an action mode.
3186 *
3187 * @param callback Callback that will control the lifecycle of the action mode
3188 * @return The new action mode if it is started, null otherwise
3189 *
3190 * @see ActionMode
3191 */
3192 public ActionMode startActionMode(ActionMode.Callback callback) {
3193 return getParent().startActionModeForChild(this, callback);
3194 }
3195
3196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 * Register a callback to be invoked when a key is pressed in this view.
3198 * @param l the key listener to attach to this view
3199 */
3200 public void setOnKeyListener(OnKeyListener l) {
3201 mOnKeyListener = l;
3202 }
3203
3204 /**
3205 * Register a callback to be invoked when a touch event is sent to this view.
3206 * @param l the touch listener to attach to this view
3207 */
3208 public void setOnTouchListener(OnTouchListener l) {
3209 mOnTouchListener = l;
3210 }
3211
3212 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003213 * Register a callback to be invoked when a generic motion event is sent to this view.
3214 * @param l the generic motion listener to attach to this view
3215 */
3216 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3217 mOnGenericMotionListener = l;
3218 }
3219
3220 /**
Joe Malin32736f02011-01-19 16:14:20 -08003221 * Register a drag event listener callback object for this View. The parameter is
3222 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3223 * View, the system calls the
3224 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3225 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003226 */
3227 public void setOnDragListener(OnDragListener l) {
3228 mOnDragListener = l;
3229 }
3230
3231 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003232 * Give this view focus. This will cause
3233 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 *
3235 * Note: this does not check whether this {@link View} should get focus, it just
3236 * gives it focus no matter what. It should only be called internally by framework
3237 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3238 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003239 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3240 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003241 * focus moved when requestFocus() is called. It may not always
3242 * apply, in which case use the default View.FOCUS_DOWN.
3243 * @param previouslyFocusedRect The rectangle of the view that had focus
3244 * prior in this View's coordinate system.
3245 */
3246 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3247 if (DBG) {
3248 System.out.println(this + " requestFocus()");
3249 }
3250
3251 if ((mPrivateFlags & FOCUSED) == 0) {
3252 mPrivateFlags |= FOCUSED;
3253
3254 if (mParent != null) {
3255 mParent.requestChildFocus(this, this);
3256 }
3257
3258 onFocusChanged(true, direction, previouslyFocusedRect);
3259 refreshDrawableState();
3260 }
3261 }
3262
3263 /**
3264 * Request that a rectangle of this view be visible on the screen,
3265 * scrolling if necessary just enough.
3266 *
3267 * <p>A View should call this if it maintains some notion of which part
3268 * of its content is interesting. For example, a text editing view
3269 * should call this when its cursor moves.
3270 *
3271 * @param rectangle The rectangle.
3272 * @return Whether any parent scrolled.
3273 */
3274 public boolean requestRectangleOnScreen(Rect rectangle) {
3275 return requestRectangleOnScreen(rectangle, false);
3276 }
3277
3278 /**
3279 * Request that a rectangle of this view be visible on the screen,
3280 * scrolling if necessary just enough.
3281 *
3282 * <p>A View should call this if it maintains some notion of which part
3283 * of its content is interesting. For example, a text editing view
3284 * should call this when its cursor moves.
3285 *
3286 * <p>When <code>immediate</code> is set to true, scrolling will not be
3287 * animated.
3288 *
3289 * @param rectangle The rectangle.
3290 * @param immediate True to forbid animated scrolling, false otherwise
3291 * @return Whether any parent scrolled.
3292 */
3293 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3294 View child = this;
3295 ViewParent parent = mParent;
3296 boolean scrolled = false;
3297 while (parent != null) {
3298 scrolled |= parent.requestChildRectangleOnScreen(child,
3299 rectangle, immediate);
3300
3301 // offset rect so next call has the rectangle in the
3302 // coordinate system of its direct child.
3303 rectangle.offset(child.getLeft(), child.getTop());
3304 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3305
3306 if (!(parent instanceof View)) {
3307 break;
3308 }
Romain Guy8506ab42009-06-11 17:35:47 -07003309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 child = (View) parent;
3311 parent = child.getParent();
3312 }
3313 return scrolled;
3314 }
3315
3316 /**
3317 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003318 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003319 */
3320 public void clearFocus() {
3321 if (DBG) {
3322 System.out.println(this + " clearFocus()");
3323 }
3324
3325 if ((mPrivateFlags & FOCUSED) != 0) {
3326 mPrivateFlags &= ~FOCUSED;
3327
3328 if (mParent != null) {
3329 mParent.clearChildFocus(this);
3330 }
3331
3332 onFocusChanged(false, 0, null);
3333 refreshDrawableState();
3334 }
3335 }
3336
3337 /**
3338 * Called to clear the focus of a view that is about to be removed.
3339 * Doesn't call clearChildFocus, which prevents this view from taking
3340 * focus again before it has been removed from the parent
3341 */
3342 void clearFocusForRemoval() {
3343 if ((mPrivateFlags & FOCUSED) != 0) {
3344 mPrivateFlags &= ~FOCUSED;
3345
3346 onFocusChanged(false, 0, null);
3347 refreshDrawableState();
3348 }
3349 }
3350
3351 /**
3352 * Called internally by the view system when a new view is getting focus.
3353 * This is what clears the old focus.
3354 */
3355 void unFocus() {
3356 if (DBG) {
3357 System.out.println(this + " unFocus()");
3358 }
3359
3360 if ((mPrivateFlags & FOCUSED) != 0) {
3361 mPrivateFlags &= ~FOCUSED;
3362
3363 onFocusChanged(false, 0, null);
3364 refreshDrawableState();
3365 }
3366 }
3367
3368 /**
3369 * Returns true if this view has focus iteself, or is the ancestor of the
3370 * view that has focus.
3371 *
3372 * @return True if this view has or contains focus, false otherwise.
3373 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003374 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003375 public boolean hasFocus() {
3376 return (mPrivateFlags & FOCUSED) != 0;
3377 }
3378
3379 /**
3380 * Returns true if this view is focusable or if it contains a reachable View
3381 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3382 * is a View whose parents do not block descendants focus.
3383 *
3384 * Only {@link #VISIBLE} views are considered focusable.
3385 *
3386 * @return True if the view is focusable or if the view contains a focusable
3387 * View, false otherwise.
3388 *
3389 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3390 */
3391 public boolean hasFocusable() {
3392 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3393 }
3394
3395 /**
3396 * Called by the view system when the focus state of this view changes.
3397 * When the focus change event is caused by directional navigation, direction
3398 * and previouslyFocusedRect provide insight into where the focus is coming from.
3399 * When overriding, be sure to call up through to the super class so that
3400 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003401 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003402 * @param gainFocus True if the View has focus; false otherwise.
3403 * @param direction The direction focus has moved when requestFocus()
3404 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003405 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3406 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3407 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003408 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3409 * system, of the previously focused view. If applicable, this will be
3410 * passed in as finer grained information about where the focus is coming
3411 * from (in addition to direction). Will be <code>null</code> otherwise.
3412 */
3413 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003414 if (gainFocus) {
3415 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3416 }
3417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 InputMethodManager imm = InputMethodManager.peekInstance();
3419 if (!gainFocus) {
3420 if (isPressed()) {
3421 setPressed(false);
3422 }
3423 if (imm != null && mAttachInfo != null
3424 && mAttachInfo.mHasWindowFocus) {
3425 imm.focusOut(this);
3426 }
Romain Guya2431d02009-04-30 16:30:00 -07003427 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 } else if (imm != null && mAttachInfo != null
3429 && mAttachInfo.mHasWindowFocus) {
3430 imm.focusIn(this);
3431 }
Romain Guy8506ab42009-06-11 17:35:47 -07003432
Romain Guy0fd89bf2011-01-26 15:41:30 -08003433 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 if (mOnFocusChangeListener != null) {
3435 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3436 }
Joe Malin32736f02011-01-19 16:14:20 -08003437
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003438 if (mAttachInfo != null) {
3439 mAttachInfo.mKeyDispatchState.reset(this);
3440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003441 }
3442
3443 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003444 * {@inheritDoc}
3445 */
3446 public void sendAccessibilityEvent(int eventType) {
3447 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3448 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3449 }
3450 }
3451
3452 /**
3453 * {@inheritDoc}
3454 */
3455 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003456 if (!isShown()) {
3457 return;
3458 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003459
3460 // Populate these here since they are related to the View that
3461 // sends the event and should not be modified while dispatching
3462 // to descendants.
svetoslavganov75986cf2009-05-14 22:28:01 -07003463 event.setClassName(getClass().getName());
3464 event.setPackageName(getContext().getPackageName());
3465 event.setEnabled(isEnabled());
3466 event.setContentDescription(mContentDescription);
3467
3468 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
3469 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3470 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
3471 event.setItemCount(focusablesTempList.size());
3472 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3473 focusablesTempList.clear();
3474 }
3475
3476 dispatchPopulateAccessibilityEvent(event);
3477
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003478 // In the beginning we called #isShown(), so we know that getParent() is not null.
3479 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003480 }
3481
3482 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003483 * Dispatches an {@link AccessibilityEvent} to the {@link View} children to be populated.
3484 * This method first calls {@link #onPopulateAccessibilityEvent(AccessibilityEvent)}
3485 * on this view allowing it to populate information about itself and also decide
3486 * whether to intercept the population i.e. to prevent its children from populating
3487 * the event.
svetoslavganov75986cf2009-05-14 22:28:01 -07003488 *
3489 * @param event The event.
3490 *
3491 * @return True if the event population was completed.
3492 */
3493 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003494 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003495 return false;
3496 }
3497
3498 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003499 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3500 * giving a chance to this View to populate the accessibility evnet with
3501 * information about itself.
3502 *
3503 * @param event The accessibility event which to populate.
3504 */
3505 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3506
3507 }
3508
3509 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003510 * Gets the {@link View} description. It briefly describes the view and is
3511 * primarily used for accessibility support. Set this property to enable
3512 * better accessibility support for your application. This is especially
3513 * true for views that do not have textual representation (For example,
3514 * ImageButton).
3515 *
3516 * @return The content descriptiopn.
3517 *
3518 * @attr ref android.R.styleable#View_contentDescription
3519 */
3520 public CharSequence getContentDescription() {
3521 return mContentDescription;
3522 }
3523
3524 /**
3525 * Sets the {@link View} description. It briefly describes the view and is
3526 * primarily used for accessibility support. Set this property to enable
3527 * better accessibility support for your application. This is especially
3528 * true for views that do not have textual representation (For example,
3529 * ImageButton).
3530 *
3531 * @param contentDescription The content description.
3532 *
3533 * @attr ref android.R.styleable#View_contentDescription
3534 */
3535 public void setContentDescription(CharSequence contentDescription) {
3536 mContentDescription = contentDescription;
3537 }
3538
3539 /**
Romain Guya2431d02009-04-30 16:30:00 -07003540 * Invoked whenever this view loses focus, either by losing window focus or by losing
3541 * focus within its window. This method can be used to clear any state tied to the
3542 * focus. For instance, if a button is held pressed with the trackball and the window
3543 * loses focus, this method can be used to cancel the press.
3544 *
3545 * Subclasses of View overriding this method should always call super.onFocusLost().
3546 *
3547 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003548 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003549 *
3550 * @hide pending API council approval
3551 */
3552 protected void onFocusLost() {
3553 resetPressedState();
3554 }
3555
3556 private void resetPressedState() {
3557 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3558 return;
3559 }
3560
3561 if (isPressed()) {
3562 setPressed(false);
3563
3564 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05003565 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003566 }
3567 }
3568 }
3569
3570 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 * Returns true if this view has focus
3572 *
3573 * @return True if this view has focus, false otherwise.
3574 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003575 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003576 public boolean isFocused() {
3577 return (mPrivateFlags & FOCUSED) != 0;
3578 }
3579
3580 /**
3581 * Find the view in the hierarchy rooted at this view that currently has
3582 * focus.
3583 *
3584 * @return The view that currently has focus, or null if no focused view can
3585 * be found.
3586 */
3587 public View findFocus() {
3588 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
3589 }
3590
3591 /**
3592 * Change whether this view is one of the set of scrollable containers in
3593 * its window. This will be used to determine whether the window can
3594 * resize or must pan when a soft input area is open -- scrollable
3595 * containers allow the window to use resize mode since the container
3596 * will appropriately shrink.
3597 */
3598 public void setScrollContainer(boolean isScrollContainer) {
3599 if (isScrollContainer) {
3600 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
3601 mAttachInfo.mScrollContainers.add(this);
3602 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
3603 }
3604 mPrivateFlags |= SCROLL_CONTAINER;
3605 } else {
3606 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
3607 mAttachInfo.mScrollContainers.remove(this);
3608 }
3609 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
3610 }
3611 }
3612
3613 /**
3614 * Returns the quality of the drawing cache.
3615 *
3616 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3617 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3618 *
3619 * @see #setDrawingCacheQuality(int)
3620 * @see #setDrawingCacheEnabled(boolean)
3621 * @see #isDrawingCacheEnabled()
3622 *
3623 * @attr ref android.R.styleable#View_drawingCacheQuality
3624 */
3625 public int getDrawingCacheQuality() {
3626 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
3627 }
3628
3629 /**
3630 * Set the drawing cache quality of this view. This value is used only when the
3631 * drawing cache is enabled
3632 *
3633 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3634 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3635 *
3636 * @see #getDrawingCacheQuality()
3637 * @see #setDrawingCacheEnabled(boolean)
3638 * @see #isDrawingCacheEnabled()
3639 *
3640 * @attr ref android.R.styleable#View_drawingCacheQuality
3641 */
3642 public void setDrawingCacheQuality(int quality) {
3643 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
3644 }
3645
3646 /**
3647 * Returns whether the screen should remain on, corresponding to the current
3648 * value of {@link #KEEP_SCREEN_ON}.
3649 *
3650 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
3651 *
3652 * @see #setKeepScreenOn(boolean)
3653 *
3654 * @attr ref android.R.styleable#View_keepScreenOn
3655 */
3656 public boolean getKeepScreenOn() {
3657 return (mViewFlags & KEEP_SCREEN_ON) != 0;
3658 }
3659
3660 /**
3661 * Controls whether the screen should remain on, modifying the
3662 * value of {@link #KEEP_SCREEN_ON}.
3663 *
3664 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
3665 *
3666 * @see #getKeepScreenOn()
3667 *
3668 * @attr ref android.R.styleable#View_keepScreenOn
3669 */
3670 public void setKeepScreenOn(boolean keepScreenOn) {
3671 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
3672 }
3673
3674 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003675 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3676 * @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 -08003677 *
3678 * @attr ref android.R.styleable#View_nextFocusLeft
3679 */
3680 public int getNextFocusLeftId() {
3681 return mNextFocusLeftId;
3682 }
3683
3684 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003685 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3686 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
3687 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 *
3689 * @attr ref android.R.styleable#View_nextFocusLeft
3690 */
3691 public void setNextFocusLeftId(int nextFocusLeftId) {
3692 mNextFocusLeftId = nextFocusLeftId;
3693 }
3694
3695 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003696 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3697 * @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 -08003698 *
3699 * @attr ref android.R.styleable#View_nextFocusRight
3700 */
3701 public int getNextFocusRightId() {
3702 return mNextFocusRightId;
3703 }
3704
3705 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003706 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3707 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
3708 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709 *
3710 * @attr ref android.R.styleable#View_nextFocusRight
3711 */
3712 public void setNextFocusRightId(int nextFocusRightId) {
3713 mNextFocusRightId = nextFocusRightId;
3714 }
3715
3716 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003717 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3718 * @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 -08003719 *
3720 * @attr ref android.R.styleable#View_nextFocusUp
3721 */
3722 public int getNextFocusUpId() {
3723 return mNextFocusUpId;
3724 }
3725
3726 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003727 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3728 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
3729 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003730 *
3731 * @attr ref android.R.styleable#View_nextFocusUp
3732 */
3733 public void setNextFocusUpId(int nextFocusUpId) {
3734 mNextFocusUpId = nextFocusUpId;
3735 }
3736
3737 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003738 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3739 * @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 -08003740 *
3741 * @attr ref android.R.styleable#View_nextFocusDown
3742 */
3743 public int getNextFocusDownId() {
3744 return mNextFocusDownId;
3745 }
3746
3747 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003748 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3749 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
3750 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003751 *
3752 * @attr ref android.R.styleable#View_nextFocusDown
3753 */
3754 public void setNextFocusDownId(int nextFocusDownId) {
3755 mNextFocusDownId = nextFocusDownId;
3756 }
3757
3758 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003759 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3760 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
3761 *
3762 * @attr ref android.R.styleable#View_nextFocusForward
3763 */
3764 public int getNextFocusForwardId() {
3765 return mNextFocusForwardId;
3766 }
3767
3768 /**
3769 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3770 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
3771 * decide automatically.
3772 *
3773 * @attr ref android.R.styleable#View_nextFocusForward
3774 */
3775 public void setNextFocusForwardId(int nextFocusForwardId) {
3776 mNextFocusForwardId = nextFocusForwardId;
3777 }
3778
3779 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003780 * Returns the visibility of this view and all of its ancestors
3781 *
3782 * @return True if this view and all of its ancestors are {@link #VISIBLE}
3783 */
3784 public boolean isShown() {
3785 View current = this;
3786 //noinspection ConstantConditions
3787 do {
3788 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
3789 return false;
3790 }
3791 ViewParent parent = current.mParent;
3792 if (parent == null) {
3793 return false; // We are not attached to the view root
3794 }
3795 if (!(parent instanceof View)) {
3796 return true;
3797 }
3798 current = (View) parent;
3799 } while (current != null);
3800
3801 return false;
3802 }
3803
3804 /**
3805 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
3806 * is set
3807 *
3808 * @param insets Insets for system windows
3809 *
3810 * @return True if this view applied the insets, false otherwise
3811 */
3812 protected boolean fitSystemWindows(Rect insets) {
3813 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
3814 mPaddingLeft = insets.left;
3815 mPaddingTop = insets.top;
3816 mPaddingRight = insets.right;
3817 mPaddingBottom = insets.bottom;
3818 requestLayout();
3819 return true;
3820 }
3821 return false;
3822 }
3823
3824 /**
3825 * Returns the visibility status for this view.
3826 *
3827 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3828 * @attr ref android.R.styleable#View_visibility
3829 */
3830 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003831 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
3832 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
3833 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003834 })
3835 public int getVisibility() {
3836 return mViewFlags & VISIBILITY_MASK;
3837 }
3838
3839 /**
3840 * Set the enabled state of this view.
3841 *
3842 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3843 * @attr ref android.R.styleable#View_visibility
3844 */
3845 @RemotableViewMethod
3846 public void setVisibility(int visibility) {
3847 setFlags(visibility, VISIBILITY_MASK);
3848 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
3849 }
3850
3851 /**
3852 * Returns the enabled status for this view. The interpretation of the
3853 * enabled state varies by subclass.
3854 *
3855 * @return True if this view is enabled, false otherwise.
3856 */
3857 @ViewDebug.ExportedProperty
3858 public boolean isEnabled() {
3859 return (mViewFlags & ENABLED_MASK) == ENABLED;
3860 }
3861
3862 /**
3863 * Set the enabled state of this view. The interpretation of the enabled
3864 * state varies by subclass.
3865 *
3866 * @param enabled True if this view is enabled, false otherwise.
3867 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08003868 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003869 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07003870 if (enabled == isEnabled()) return;
3871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003872 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
3873
3874 /*
3875 * The View most likely has to change its appearance, so refresh
3876 * the drawable state.
3877 */
3878 refreshDrawableState();
3879
3880 // Invalidate too, since the default behavior for views is to be
3881 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08003882 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003883 }
3884
3885 /**
3886 * Set whether this view can receive the focus.
3887 *
3888 * Setting this to false will also ensure that this view is not focusable
3889 * in touch mode.
3890 *
3891 * @param focusable If true, this view can receive the focus.
3892 *
3893 * @see #setFocusableInTouchMode(boolean)
3894 * @attr ref android.R.styleable#View_focusable
3895 */
3896 public void setFocusable(boolean focusable) {
3897 if (!focusable) {
3898 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
3899 }
3900 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
3901 }
3902
3903 /**
3904 * Set whether this view can receive focus while in touch mode.
3905 *
3906 * Setting this to true will also ensure that this view is focusable.
3907 *
3908 * @param focusableInTouchMode If true, this view can receive the focus while
3909 * in touch mode.
3910 *
3911 * @see #setFocusable(boolean)
3912 * @attr ref android.R.styleable#View_focusableInTouchMode
3913 */
3914 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
3915 // Focusable in touch mode should always be set before the focusable flag
3916 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
3917 // which, in touch mode, will not successfully request focus on this view
3918 // because the focusable in touch mode flag is not set
3919 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
3920 if (focusableInTouchMode) {
3921 setFlags(FOCUSABLE, FOCUSABLE_MASK);
3922 }
3923 }
3924
3925 /**
3926 * Set whether this view should have sound effects enabled for events such as
3927 * clicking and touching.
3928 *
3929 * <p>You may wish to disable sound effects for a view if you already play sounds,
3930 * for instance, a dial key that plays dtmf tones.
3931 *
3932 * @param soundEffectsEnabled whether sound effects are enabled for this view.
3933 * @see #isSoundEffectsEnabled()
3934 * @see #playSoundEffect(int)
3935 * @attr ref android.R.styleable#View_soundEffectsEnabled
3936 */
3937 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
3938 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
3939 }
3940
3941 /**
3942 * @return whether this view should have sound effects enabled for events such as
3943 * clicking and touching.
3944 *
3945 * @see #setSoundEffectsEnabled(boolean)
3946 * @see #playSoundEffect(int)
3947 * @attr ref android.R.styleable#View_soundEffectsEnabled
3948 */
3949 @ViewDebug.ExportedProperty
3950 public boolean isSoundEffectsEnabled() {
3951 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
3952 }
3953
3954 /**
3955 * Set whether this view should have haptic feedback for events such as
3956 * long presses.
3957 *
3958 * <p>You may wish to disable haptic feedback if your view already controls
3959 * its own haptic feedback.
3960 *
3961 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
3962 * @see #isHapticFeedbackEnabled()
3963 * @see #performHapticFeedback(int)
3964 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3965 */
3966 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
3967 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
3968 }
3969
3970 /**
3971 * @return whether this view should have haptic feedback enabled for events
3972 * long presses.
3973 *
3974 * @see #setHapticFeedbackEnabled(boolean)
3975 * @see #performHapticFeedback(int)
3976 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3977 */
3978 @ViewDebug.ExportedProperty
3979 public boolean isHapticFeedbackEnabled() {
3980 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
3981 }
3982
3983 /**
3984 * If this view doesn't do any drawing on its own, set this flag to
3985 * allow further optimizations. By default, this flag is not set on
3986 * View, but could be set on some View subclasses such as ViewGroup.
3987 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07003988 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
3989 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003990 *
3991 * @param willNotDraw whether or not this View draw on its own
3992 */
3993 public void setWillNotDraw(boolean willNotDraw) {
3994 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
3995 }
3996
3997 /**
3998 * Returns whether or not this View draws on its own.
3999 *
4000 * @return true if this view has nothing to draw, false otherwise
4001 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004002 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004003 public boolean willNotDraw() {
4004 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4005 }
4006
4007 /**
4008 * When a View's drawing cache is enabled, drawing is redirected to an
4009 * offscreen bitmap. Some views, like an ImageView, must be able to
4010 * bypass this mechanism if they already draw a single bitmap, to avoid
4011 * unnecessary usage of the memory.
4012 *
4013 * @param willNotCacheDrawing true if this view does not cache its
4014 * drawing, false otherwise
4015 */
4016 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4017 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4018 }
4019
4020 /**
4021 * Returns whether or not this View can cache its drawing or not.
4022 *
4023 * @return true if this view does not cache its drawing, false otherwise
4024 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004025 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004026 public boolean willNotCacheDrawing() {
4027 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4028 }
4029
4030 /**
4031 * Indicates whether this view reacts to click events or not.
4032 *
4033 * @return true if the view is clickable, false otherwise
4034 *
4035 * @see #setClickable(boolean)
4036 * @attr ref android.R.styleable#View_clickable
4037 */
4038 @ViewDebug.ExportedProperty
4039 public boolean isClickable() {
4040 return (mViewFlags & CLICKABLE) == CLICKABLE;
4041 }
4042
4043 /**
4044 * Enables or disables click events for this view. When a view
4045 * is clickable it will change its state to "pressed" on every click.
4046 * Subclasses should set the view clickable to visually react to
4047 * user's clicks.
4048 *
4049 * @param clickable true to make the view clickable, false otherwise
4050 *
4051 * @see #isClickable()
4052 * @attr ref android.R.styleable#View_clickable
4053 */
4054 public void setClickable(boolean clickable) {
4055 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4056 }
4057
4058 /**
4059 * Indicates whether this view reacts to long click events or not.
4060 *
4061 * @return true if the view is long clickable, false otherwise
4062 *
4063 * @see #setLongClickable(boolean)
4064 * @attr ref android.R.styleable#View_longClickable
4065 */
4066 public boolean isLongClickable() {
4067 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4068 }
4069
4070 /**
4071 * Enables or disables long click events for this view. When a view is long
4072 * clickable it reacts to the user holding down the button for a longer
4073 * duration than a tap. This event can either launch the listener or a
4074 * context menu.
4075 *
4076 * @param longClickable true to make the view long clickable, false otherwise
4077 * @see #isLongClickable()
4078 * @attr ref android.R.styleable#View_longClickable
4079 */
4080 public void setLongClickable(boolean longClickable) {
4081 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4082 }
4083
4084 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004085 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004086 *
4087 * @see #isClickable()
4088 * @see #setClickable(boolean)
4089 *
4090 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4091 * the View's internal state from a previously set "pressed" state.
4092 */
4093 public void setPressed(boolean pressed) {
4094 if (pressed) {
4095 mPrivateFlags |= PRESSED;
4096 } else {
4097 mPrivateFlags &= ~PRESSED;
4098 }
4099 refreshDrawableState();
4100 dispatchSetPressed(pressed);
4101 }
4102
4103 /**
4104 * Dispatch setPressed to all of this View's children.
4105 *
4106 * @see #setPressed(boolean)
4107 *
4108 * @param pressed The new pressed state
4109 */
4110 protected void dispatchSetPressed(boolean pressed) {
4111 }
4112
4113 /**
4114 * Indicates whether the view is currently in pressed state. Unless
4115 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4116 * the pressed state.
4117 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004118 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004119 * @see #isClickable()
4120 * @see #setClickable(boolean)
4121 *
4122 * @return true if the view is currently pressed, false otherwise
4123 */
4124 public boolean isPressed() {
4125 return (mPrivateFlags & PRESSED) == PRESSED;
4126 }
4127
4128 /**
4129 * Indicates whether this view will save its state (that is,
4130 * whether its {@link #onSaveInstanceState} method will be called).
4131 *
4132 * @return Returns true if the view state saving is enabled, else false.
4133 *
4134 * @see #setSaveEnabled(boolean)
4135 * @attr ref android.R.styleable#View_saveEnabled
4136 */
4137 public boolean isSaveEnabled() {
4138 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4139 }
4140
4141 /**
4142 * Controls whether the saving of this view's state is
4143 * enabled (that is, whether its {@link #onSaveInstanceState} method
4144 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004145 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 * for its state to be saved. This flag can only disable the
4147 * saving of this view; any child views may still have their state saved.
4148 *
4149 * @param enabled Set to false to <em>disable</em> state saving, or true
4150 * (the default) to allow it.
4151 *
4152 * @see #isSaveEnabled()
4153 * @see #setId(int)
4154 * @see #onSaveInstanceState()
4155 * @attr ref android.R.styleable#View_saveEnabled
4156 */
4157 public void setSaveEnabled(boolean enabled) {
4158 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4159 }
4160
Jeff Brown85a31762010-09-01 17:01:00 -07004161 /**
4162 * Gets whether the framework should discard touches when the view's
4163 * window is obscured by another visible window.
4164 * Refer to the {@link View} security documentation for more details.
4165 *
4166 * @return True if touch filtering is enabled.
4167 *
4168 * @see #setFilterTouchesWhenObscured(boolean)
4169 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4170 */
4171 @ViewDebug.ExportedProperty
4172 public boolean getFilterTouchesWhenObscured() {
4173 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4174 }
4175
4176 /**
4177 * Sets whether the framework should discard touches when the view's
4178 * window is obscured by another visible window.
4179 * Refer to the {@link View} security documentation for more details.
4180 *
4181 * @param enabled True if touch filtering should be enabled.
4182 *
4183 * @see #getFilterTouchesWhenObscured
4184 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4185 */
4186 public void setFilterTouchesWhenObscured(boolean enabled) {
4187 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4188 FILTER_TOUCHES_WHEN_OBSCURED);
4189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004190
4191 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004192 * Indicates whether the entire hierarchy under this view will save its
4193 * state when a state saving traversal occurs from its parent. The default
4194 * is true; if false, these views will not be saved unless
4195 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4196 *
4197 * @return Returns true if the view state saving from parent is enabled, else false.
4198 *
4199 * @see #setSaveFromParentEnabled(boolean)
4200 */
4201 public boolean isSaveFromParentEnabled() {
4202 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4203 }
4204
4205 /**
4206 * Controls whether the entire hierarchy under this view will save its
4207 * state when a state saving traversal occurs from its parent. The default
4208 * is true; if false, these views will not be saved unless
4209 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4210 *
4211 * @param enabled Set to false to <em>disable</em> state saving, or true
4212 * (the default) to allow it.
4213 *
4214 * @see #isSaveFromParentEnabled()
4215 * @see #setId(int)
4216 * @see #onSaveInstanceState()
4217 */
4218 public void setSaveFromParentEnabled(boolean enabled) {
4219 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4220 }
4221
4222
4223 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004224 * Returns whether this View is able to take focus.
4225 *
4226 * @return True if this view can take focus, or false otherwise.
4227 * @attr ref android.R.styleable#View_focusable
4228 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004229 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004230 public final boolean isFocusable() {
4231 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4232 }
4233
4234 /**
4235 * When a view is focusable, it may not want to take focus when in touch mode.
4236 * For example, a button would like focus when the user is navigating via a D-pad
4237 * so that the user can click on it, but once the user starts touching the screen,
4238 * the button shouldn't take focus
4239 * @return Whether the view is focusable in touch mode.
4240 * @attr ref android.R.styleable#View_focusableInTouchMode
4241 */
4242 @ViewDebug.ExportedProperty
4243 public final boolean isFocusableInTouchMode() {
4244 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4245 }
4246
4247 /**
4248 * Find the nearest view in the specified direction that can take focus.
4249 * This does not actually give focus to that view.
4250 *
4251 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4252 *
4253 * @return The nearest focusable in the specified direction, or null if none
4254 * can be found.
4255 */
4256 public View focusSearch(int direction) {
4257 if (mParent != null) {
4258 return mParent.focusSearch(this, direction);
4259 } else {
4260 return null;
4261 }
4262 }
4263
4264 /**
4265 * This method is the last chance for the focused view and its ancestors to
4266 * respond to an arrow key. This is called when the focused view did not
4267 * consume the key internally, nor could the view system find a new view in
4268 * the requested direction to give focus to.
4269 *
4270 * @param focused The currently focused view.
4271 * @param direction The direction focus wants to move. One of FOCUS_UP,
4272 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4273 * @return True if the this view consumed this unhandled move.
4274 */
4275 public boolean dispatchUnhandledMove(View focused, int direction) {
4276 return false;
4277 }
4278
4279 /**
4280 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004281 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004282 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004283 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4284 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 * @return The user specified next view, or null if there is none.
4286 */
4287 View findUserSetNextFocus(View root, int direction) {
4288 switch (direction) {
4289 case FOCUS_LEFT:
4290 if (mNextFocusLeftId == View.NO_ID) return null;
4291 return findViewShouldExist(root, mNextFocusLeftId);
4292 case FOCUS_RIGHT:
4293 if (mNextFocusRightId == View.NO_ID) return null;
4294 return findViewShouldExist(root, mNextFocusRightId);
4295 case FOCUS_UP:
4296 if (mNextFocusUpId == View.NO_ID) return null;
4297 return findViewShouldExist(root, mNextFocusUpId);
4298 case FOCUS_DOWN:
4299 if (mNextFocusDownId == View.NO_ID) return null;
4300 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004301 case FOCUS_FORWARD:
4302 if (mNextFocusForwardId == View.NO_ID) return null;
4303 return findViewShouldExist(root, mNextFocusForwardId);
4304 case FOCUS_BACKWARD: {
4305 final int id = mID;
4306 return root.findViewByPredicate(new Predicate<View>() {
4307 @Override
4308 public boolean apply(View t) {
4309 return t.mNextFocusForwardId == id;
4310 }
4311 });
4312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004313 }
4314 return null;
4315 }
4316
4317 private static View findViewShouldExist(View root, int childViewId) {
4318 View result = root.findViewById(childViewId);
4319 if (result == null) {
4320 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4321 + "by user for id " + childViewId);
4322 }
4323 return result;
4324 }
4325
4326 /**
4327 * Find and return all focusable views that are descendants of this view,
4328 * possibly including this view if it is focusable itself.
4329 *
4330 * @param direction The direction of the focus
4331 * @return A list of focusable views
4332 */
4333 public ArrayList<View> getFocusables(int direction) {
4334 ArrayList<View> result = new ArrayList<View>(24);
4335 addFocusables(result, direction);
4336 return result;
4337 }
4338
4339 /**
4340 * Add any focusable views that are descendants of this view (possibly
4341 * including this view if it is focusable itself) to views. If we are in touch mode,
4342 * only add views that are also focusable in touch mode.
4343 *
4344 * @param views Focusable views found so far
4345 * @param direction The direction of the focus
4346 */
4347 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004348 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4349 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004350
svetoslavganov75986cf2009-05-14 22:28:01 -07004351 /**
4352 * Adds any focusable views that are descendants of this view (possibly
4353 * including this view if it is focusable itself) to views. This method
4354 * adds all focusable views regardless if we are in touch mode or
4355 * only views focusable in touch mode if we are in touch mode depending on
4356 * the focusable mode paramater.
4357 *
4358 * @param views Focusable views found so far or null if all we are interested is
4359 * the number of focusables.
4360 * @param direction The direction of the focus.
4361 * @param focusableMode The type of focusables to be added.
4362 *
4363 * @see #FOCUSABLES_ALL
4364 * @see #FOCUSABLES_TOUCH_MODE
4365 */
4366 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4367 if (!isFocusable()) {
4368 return;
4369 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004370
svetoslavganov75986cf2009-05-14 22:28:01 -07004371 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4372 isInTouchMode() && !isFocusableInTouchMode()) {
4373 return;
4374 }
4375
4376 if (views != null) {
4377 views.add(this);
4378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379 }
4380
4381 /**
4382 * Find and return all touchable views that are descendants of this view,
4383 * possibly including this view if it is touchable itself.
4384 *
4385 * @return A list of touchable views
4386 */
4387 public ArrayList<View> getTouchables() {
4388 ArrayList<View> result = new ArrayList<View>();
4389 addTouchables(result);
4390 return result;
4391 }
4392
4393 /**
4394 * Add any touchable views that are descendants of this view (possibly
4395 * including this view if it is touchable itself) to views.
4396 *
4397 * @param views Touchable views found so far
4398 */
4399 public void addTouchables(ArrayList<View> views) {
4400 final int viewFlags = mViewFlags;
4401
4402 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4403 && (viewFlags & ENABLED_MASK) == ENABLED) {
4404 views.add(this);
4405 }
4406 }
4407
4408 /**
4409 * Call this to try to give focus to a specific view or to one of its
4410 * descendants.
4411 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004412 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4413 * false), or if it is focusable and it is not focusable in touch mode
4414 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004415 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004416 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004417 * have focus, and you want your parent to look for the next one.
4418 *
4419 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4420 * {@link #FOCUS_DOWN} and <code>null</code>.
4421 *
4422 * @return Whether this view or one of its descendants actually took focus.
4423 */
4424 public final boolean requestFocus() {
4425 return requestFocus(View.FOCUS_DOWN);
4426 }
4427
4428
4429 /**
4430 * Call this to try to give focus to a specific view or to one of its
4431 * descendants and give it a hint about what direction focus is heading.
4432 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004433 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4434 * false), or if it is focusable and it is not focusable in touch mode
4435 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004436 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004437 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 * have focus, and you want your parent to look for the next one.
4439 *
4440 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4441 * <code>null</code> set for the previously focused rectangle.
4442 *
4443 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4444 * @return Whether this view or one of its descendants actually took focus.
4445 */
4446 public final boolean requestFocus(int direction) {
4447 return requestFocus(direction, null);
4448 }
4449
4450 /**
4451 * Call this to try to give focus to a specific view or to one of its descendants
4452 * and give it hints about the direction and a specific rectangle that the focus
4453 * is coming from. The rectangle can help give larger views a finer grained hint
4454 * about where focus is coming from, and therefore, where to show selection, or
4455 * forward focus change internally.
4456 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004457 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4458 * false), or if it is focusable and it is not focusable in touch mode
4459 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004460 *
4461 * A View will not take focus if it is not visible.
4462 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004463 * A View will not take focus if one of its parents has
4464 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
4465 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004466 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004467 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004468 * have focus, and you want your parent to look for the next one.
4469 *
4470 * You may wish to override this method if your custom {@link View} has an internal
4471 * {@link View} that it wishes to forward the request to.
4472 *
4473 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4474 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
4475 * to give a finer grained hint about where focus is coming from. May be null
4476 * if there is no hint.
4477 * @return Whether this view or one of its descendants actually took focus.
4478 */
4479 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
4480 // need to be focusable
4481 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
4482 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4483 return false;
4484 }
4485
4486 // need to be focusable in touch mode if in touch mode
4487 if (isInTouchMode() &&
4488 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
4489 return false;
4490 }
4491
4492 // need to not have any parents blocking us
4493 if (hasAncestorThatBlocksDescendantFocus()) {
4494 return false;
4495 }
4496
4497 handleFocusGainInternal(direction, previouslyFocusedRect);
4498 return true;
4499 }
4500
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004501 /** Gets the ViewAncestor, or null if not attached. */
4502 /*package*/ ViewAncestor getViewAncestor() {
Christopher Tate2c095f32010-10-04 14:13:40 -07004503 View root = getRootView();
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004504 return root != null ? (ViewAncestor)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07004505 }
4506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004507 /**
4508 * Call this to try to give focus to a specific view or to one of its descendants. This is a
4509 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
4510 * touch mode to request focus when they are touched.
4511 *
4512 * @return Whether this view or one of its descendants actually took focus.
4513 *
4514 * @see #isInTouchMode()
4515 *
4516 */
4517 public final boolean requestFocusFromTouch() {
4518 // Leave touch mode if we need to
4519 if (isInTouchMode()) {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004520 ViewAncestor viewRoot = getViewAncestor();
Christopher Tate2c095f32010-10-04 14:13:40 -07004521 if (viewRoot != null) {
4522 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004523 }
4524 }
4525 return requestFocus(View.FOCUS_DOWN);
4526 }
4527
4528 /**
4529 * @return Whether any ancestor of this view blocks descendant focus.
4530 */
4531 private boolean hasAncestorThatBlocksDescendantFocus() {
4532 ViewParent ancestor = mParent;
4533 while (ancestor instanceof ViewGroup) {
4534 final ViewGroup vgAncestor = (ViewGroup) ancestor;
4535 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
4536 return true;
4537 } else {
4538 ancestor = vgAncestor.getParent();
4539 }
4540 }
4541 return false;
4542 }
4543
4544 /**
Romain Guya440b002010-02-24 15:57:54 -08004545 * @hide
4546 */
4547 public void dispatchStartTemporaryDetach() {
4548 onStartTemporaryDetach();
4549 }
4550
4551 /**
4552 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004553 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
4554 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08004555 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004556 */
4557 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08004558 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08004559 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08004560 }
4561
4562 /**
4563 * @hide
4564 */
4565 public void dispatchFinishTemporaryDetach() {
4566 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004567 }
Romain Guy8506ab42009-06-11 17:35:47 -07004568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004569 /**
4570 * Called after {@link #onStartTemporaryDetach} when the container is done
4571 * changing the view.
4572 */
4573 public void onFinishTemporaryDetach() {
4574 }
Romain Guy8506ab42009-06-11 17:35:47 -07004575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004576 /**
4577 * capture information of this view for later analysis: developement only
4578 * check dynamic switch to make sure we only dump view
4579 * when ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW) is set
4580 */
4581 private static void captureViewInfo(String subTag, View v) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004582 if (v == null || SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW, 0) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004583 return;
4584 }
4585 ViewDebug.dumpCapturedView(subTag, v);
4586 }
4587
4588 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004589 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
4590 * for this view's window. Returns null if the view is not currently attached
4591 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07004592 * just use the standard high-level event callbacks like
4593 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004594 */
4595 public KeyEvent.DispatcherState getKeyDispatcherState() {
4596 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
4597 }
Joe Malin32736f02011-01-19 16:14:20 -08004598
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004599 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004600 * Dispatch a key event before it is processed by any input method
4601 * associated with the view hierarchy. This can be used to intercept
4602 * key events in special situations before the IME consumes them; a
4603 * typical example would be handling the BACK key to update the application's
4604 * UI instead of allowing the IME to see it and close itself.
4605 *
4606 * @param event The key event to be dispatched.
4607 * @return True if the event was handled, false otherwise.
4608 */
4609 public boolean dispatchKeyEventPreIme(KeyEvent event) {
4610 return onKeyPreIme(event.getKeyCode(), event);
4611 }
4612
4613 /**
4614 * Dispatch a key event to the next view on the focus path. This path runs
4615 * from the top of the view tree down to the currently focused view. If this
4616 * view has focus, it will dispatch to itself. Otherwise it will dispatch
4617 * the next node down the focus path. This method also fires any key
4618 * listeners.
4619 *
4620 * @param event The key event to be dispatched.
4621 * @return True if the event was handled, false otherwise.
4622 */
4623 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004624 if (mInputEventConsistencyVerifier != null) {
4625 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
4626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004627
Jeff Brown21bc5c92011-02-28 18:27:14 -08004628 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07004629 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004630 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4631 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
4632 return true;
4633 }
4634
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004635 if (event.dispatch(this, mAttachInfo != null
4636 ? mAttachInfo.mKeyDispatchState : null, this)) {
4637 return true;
4638 }
4639
4640 if (mInputEventConsistencyVerifier != null) {
4641 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4642 }
4643 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004644 }
4645
4646 /**
4647 * Dispatches a key shortcut event.
4648 *
4649 * @param event The key event to be dispatched.
4650 * @return True if the event was handled by the view, false otherwise.
4651 */
4652 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
4653 return onKeyShortcut(event.getKeyCode(), event);
4654 }
4655
4656 /**
4657 * Pass the touch screen motion event down to the target view, or this
4658 * view if it is the target.
4659 *
4660 * @param event The motion event to be dispatched.
4661 * @return True if the event was handled by the view, false otherwise.
4662 */
4663 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004664 if (mInputEventConsistencyVerifier != null) {
4665 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
4666 }
4667
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004668 if (onFilterTouchEventForSecurity(event)) {
4669 //noinspection SimplifiableIfStatement
4670 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
4671 mOnTouchListener.onTouch(this, event)) {
4672 return true;
4673 }
4674
4675 if (onTouchEvent(event)) {
4676 return true;
4677 }
Jeff Brown85a31762010-09-01 17:01:00 -07004678 }
4679
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004680 if (mInputEventConsistencyVerifier != null) {
4681 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004682 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004683 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004684 }
4685
4686 /**
Jeff Brown85a31762010-09-01 17:01:00 -07004687 * Filter the touch event to apply security policies.
4688 *
4689 * @param event The motion event to be filtered.
4690 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08004691 *
Jeff Brown85a31762010-09-01 17:01:00 -07004692 * @see #getFilterTouchesWhenObscured
4693 */
4694 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07004695 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07004696 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
4697 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
4698 // Window is obscured, drop this touch.
4699 return false;
4700 }
4701 return true;
4702 }
4703
4704 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004705 * Pass a trackball motion event down to the focused view.
4706 *
4707 * @param event The motion event to be dispatched.
4708 * @return True if the event was handled by the view, false otherwise.
4709 */
4710 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004711 if (mInputEventConsistencyVerifier != null) {
4712 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
4713 }
4714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004715 //Log.i("view", "view=" + this + ", " + event.toString());
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004716 if (onTrackballEvent(event)) {
4717 return true;
4718 }
4719
4720 if (mInputEventConsistencyVerifier != null) {
4721 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4722 }
4723 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004724 }
4725
4726 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004727 * Dispatch a generic motion event.
4728 * <p>
4729 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
4730 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08004731 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07004732 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08004733 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08004734 *
4735 * @param event The motion event to be dispatched.
4736 * @return True if the event was handled by the view, false otherwise.
4737 */
4738 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004739 if (mInputEventConsistencyVerifier != null) {
4740 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
4741 }
4742
Jeff Browna032cc02011-03-07 16:56:21 -08004743 final int source = event.getSource();
4744 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
4745 final int action = event.getAction();
4746 if (action == MotionEvent.ACTION_HOVER_ENTER
4747 || action == MotionEvent.ACTION_HOVER_MOVE
4748 || action == MotionEvent.ACTION_HOVER_EXIT) {
4749 if (dispatchHoverEvent(event)) {
4750 return true;
4751 }
4752 } else if (dispatchGenericPointerEvent(event)) {
4753 return true;
4754 }
4755 } else if (dispatchGenericFocusedEvent(event)) {
4756 return true;
4757 }
4758
Romain Guy7b5b6ab2011-03-14 18:05:08 -07004759 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08004760 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4761 && mOnGenericMotionListener.onGenericMotion(this, event)) {
4762 return true;
4763 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004764
4765 if (onGenericMotionEvent(event)) {
4766 return true;
4767 }
4768
4769 if (mInputEventConsistencyVerifier != null) {
4770 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4771 }
4772 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08004773 }
4774
4775 /**
Jeff Browna032cc02011-03-07 16:56:21 -08004776 * Dispatch a hover event.
4777 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07004778 * Do not call this method directly.
4779 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08004780 * </p>
4781 *
4782 * @param event The motion event to be dispatched.
4783 * @return True if the event was handled by the view, false otherwise.
4784 * @hide
4785 */
4786 protected boolean dispatchHoverEvent(MotionEvent event) {
4787 return onHoverEvent(event);
4788 }
4789
4790 /**
4791 * Dispatch a generic motion event to the view under the first pointer.
4792 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07004793 * Do not call this method directly.
4794 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08004795 * </p>
4796 *
4797 * @param event The motion event to be dispatched.
4798 * @return True if the event was handled by the view, false otherwise.
4799 * @hide
4800 */
4801 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
4802 return false;
4803 }
4804
4805 /**
4806 * Dispatch a generic motion event to the currently focused view.
4807 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07004808 * Do not call this method directly.
4809 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08004810 * </p>
4811 *
4812 * @param event The motion event to be dispatched.
4813 * @return True if the event was handled by the view, false otherwise.
4814 * @hide
4815 */
4816 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
4817 return false;
4818 }
4819
4820 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004821 * Dispatch a pointer event.
4822 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07004823 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
4824 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
4825 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08004826 * and should not be expected to handle other pointing device features.
4827 * </p>
4828 *
4829 * @param event The motion event to be dispatched.
4830 * @return True if the event was handled by the view, false otherwise.
4831 * @hide
4832 */
4833 public final boolean dispatchPointerEvent(MotionEvent event) {
4834 if (event.isTouchEvent()) {
4835 return dispatchTouchEvent(event);
4836 } else {
4837 return dispatchGenericMotionEvent(event);
4838 }
4839 }
4840
4841 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004842 * Called when the window containing this view gains or loses window focus.
4843 * ViewGroups should override to route to their children.
4844 *
4845 * @param hasFocus True if the window containing this view now has focus,
4846 * false otherwise.
4847 */
4848 public void dispatchWindowFocusChanged(boolean hasFocus) {
4849 onWindowFocusChanged(hasFocus);
4850 }
4851
4852 /**
4853 * Called when the window containing this view gains or loses focus. Note
4854 * that this is separate from view focus: to receive key events, both
4855 * your view and its window must have focus. If a window is displayed
4856 * on top of yours that takes input focus, then your own window will lose
4857 * focus but the view focus will remain unchanged.
4858 *
4859 * @param hasWindowFocus True if the window containing this view now has
4860 * focus, false otherwise.
4861 */
4862 public void onWindowFocusChanged(boolean hasWindowFocus) {
4863 InputMethodManager imm = InputMethodManager.peekInstance();
4864 if (!hasWindowFocus) {
4865 if (isPressed()) {
4866 setPressed(false);
4867 }
4868 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4869 imm.focusOut(this);
4870 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05004871 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08004872 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004873 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004874 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4875 imm.focusIn(this);
4876 }
4877 refreshDrawableState();
4878 }
4879
4880 /**
4881 * Returns true if this view is in a window that currently has window focus.
4882 * Note that this is not the same as the view itself having focus.
4883 *
4884 * @return True if this view is in a window that currently has window focus.
4885 */
4886 public boolean hasWindowFocus() {
4887 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
4888 }
4889
4890 /**
Adam Powell326d8082009-12-09 15:10:07 -08004891 * Dispatch a view visibility change down the view hierarchy.
4892 * ViewGroups should override to route to their children.
4893 * @param changedView The view whose visibility changed. Could be 'this' or
4894 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004895 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4896 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004897 */
4898 protected void dispatchVisibilityChanged(View changedView, int visibility) {
4899 onVisibilityChanged(changedView, visibility);
4900 }
4901
4902 /**
4903 * Called when the visibility of the view or an ancestor of the view is changed.
4904 * @param changedView The view whose visibility changed. Could be 'this' or
4905 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004906 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4907 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004908 */
4909 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004910 if (visibility == VISIBLE) {
4911 if (mAttachInfo != null) {
4912 initialAwakenScrollBars();
4913 } else {
4914 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
4915 }
4916 }
Adam Powell326d8082009-12-09 15:10:07 -08004917 }
4918
4919 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08004920 * Dispatch a hint about whether this view is displayed. For instance, when
4921 * a View moves out of the screen, it might receives a display hint indicating
4922 * the view is not displayed. Applications should not <em>rely</em> on this hint
4923 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004924 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004925 * @param hint A hint about whether or not this view is displayed:
4926 * {@link #VISIBLE} or {@link #INVISIBLE}.
4927 */
4928 public void dispatchDisplayHint(int hint) {
4929 onDisplayHint(hint);
4930 }
4931
4932 /**
4933 * Gives this view a hint about whether is displayed or not. For instance, when
4934 * a View moves out of the screen, it might receives a display hint indicating
4935 * the view is not displayed. Applications should not <em>rely</em> on this hint
4936 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004937 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004938 * @param hint A hint about whether or not this view is displayed:
4939 * {@link #VISIBLE} or {@link #INVISIBLE}.
4940 */
4941 protected void onDisplayHint(int hint) {
4942 }
4943
4944 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004945 * Dispatch a window visibility change down the view hierarchy.
4946 * ViewGroups should override to route to their children.
4947 *
4948 * @param visibility The new visibility of the window.
4949 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004950 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004951 */
4952 public void dispatchWindowVisibilityChanged(int visibility) {
4953 onWindowVisibilityChanged(visibility);
4954 }
4955
4956 /**
4957 * Called when the window containing has change its visibility
4958 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
4959 * that this tells you whether or not your window is being made visible
4960 * to the window manager; this does <em>not</em> tell you whether or not
4961 * your window is obscured by other windows on the screen, even if it
4962 * is itself visible.
4963 *
4964 * @param visibility The new visibility of the window.
4965 */
4966 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004967 if (visibility == VISIBLE) {
4968 initialAwakenScrollBars();
4969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004970 }
4971
4972 /**
4973 * Returns the current visibility of the window this view is attached to
4974 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
4975 *
4976 * @return Returns the current visibility of the view's window.
4977 */
4978 public int getWindowVisibility() {
4979 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
4980 }
4981
4982 /**
4983 * Retrieve the overall visible display size in which the window this view is
4984 * attached to has been positioned in. This takes into account screen
4985 * decorations above the window, for both cases where the window itself
4986 * is being position inside of them or the window is being placed under
4987 * then and covered insets are used for the window to position its content
4988 * inside. In effect, this tells you the available area where content can
4989 * be placed and remain visible to users.
4990 *
4991 * <p>This function requires an IPC back to the window manager to retrieve
4992 * the requested information, so should not be used in performance critical
4993 * code like drawing.
4994 *
4995 * @param outRect Filled in with the visible display frame. If the view
4996 * is not attached to a window, this is simply the raw display size.
4997 */
4998 public void getWindowVisibleDisplayFrame(Rect outRect) {
4999 if (mAttachInfo != null) {
5000 try {
5001 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5002 } catch (RemoteException e) {
5003 return;
5004 }
5005 // XXX This is really broken, and probably all needs to be done
5006 // in the window manager, and we need to know more about whether
5007 // we want the area behind or in front of the IME.
5008 final Rect insets = mAttachInfo.mVisibleInsets;
5009 outRect.left += insets.left;
5010 outRect.top += insets.top;
5011 outRect.right -= insets.right;
5012 outRect.bottom -= insets.bottom;
5013 return;
5014 }
5015 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005016 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005017 }
5018
5019 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005020 * Dispatch a notification about a resource configuration change down
5021 * the view hierarchy.
5022 * ViewGroups should override to route to their children.
5023 *
5024 * @param newConfig The new resource configuration.
5025 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005026 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005027 */
5028 public void dispatchConfigurationChanged(Configuration newConfig) {
5029 onConfigurationChanged(newConfig);
5030 }
5031
5032 /**
5033 * Called when the current configuration of the resources being used
5034 * by the application have changed. You can use this to decide when
5035 * to reload resources that can changed based on orientation and other
5036 * configuration characterstics. You only need to use this if you are
5037 * not relying on the normal {@link android.app.Activity} mechanism of
5038 * recreating the activity instance upon a configuration change.
5039 *
5040 * @param newConfig The new resource configuration.
5041 */
5042 protected void onConfigurationChanged(Configuration newConfig) {
5043 }
5044
5045 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005046 * Private function to aggregate all per-view attributes in to the view
5047 * root.
5048 */
5049 void dispatchCollectViewAttributes(int visibility) {
5050 performCollectViewAttributes(visibility);
5051 }
5052
5053 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005054 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005055 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5056 mAttachInfo.mKeepScreenOn = true;
5057 }
5058 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5059 if (mOnSystemUiVisibilityChangeListener != null) {
5060 mAttachInfo.mHasSystemUiListeners = true;
5061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005062 }
5063 }
5064
5065 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005066 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005067 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005068 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5069 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005070 ai.mRecomputeGlobalAttributes = true;
5071 }
5072 }
5073 }
5074
5075 /**
5076 * Returns whether the device is currently in touch mode. Touch mode is entered
5077 * once the user begins interacting with the device by touch, and affects various
5078 * things like whether focus is always visible to the user.
5079 *
5080 * @return Whether the device is in touch mode.
5081 */
5082 @ViewDebug.ExportedProperty
5083 public boolean isInTouchMode() {
5084 if (mAttachInfo != null) {
5085 return mAttachInfo.mInTouchMode;
5086 } else {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005087 return ViewAncestor.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005088 }
5089 }
5090
5091 /**
5092 * Returns the context the view is running in, through which it can
5093 * access the current theme, resources, etc.
5094 *
5095 * @return The view's Context.
5096 */
5097 @ViewDebug.CapturedViewProperty
5098 public final Context getContext() {
5099 return mContext;
5100 }
5101
5102 /**
5103 * Handle a key event before it is processed by any input method
5104 * associated with the view hierarchy. This can be used to intercept
5105 * key events in special situations before the IME consumes them; a
5106 * typical example would be handling the BACK key to update the application's
5107 * UI instead of allowing the IME to see it and close itself.
5108 *
5109 * @param keyCode The value in event.getKeyCode().
5110 * @param event Description of the key event.
5111 * @return If you handled the event, return true. If you want to allow the
5112 * event to be handled by the next receiver, return false.
5113 */
5114 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5115 return false;
5116 }
5117
5118 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005119 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5120 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005121 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5122 * is released, if the view is enabled and clickable.
5123 *
5124 * @param keyCode A key code that represents the button pressed, from
5125 * {@link android.view.KeyEvent}.
5126 * @param event The KeyEvent object that defines the button action.
5127 */
5128 public boolean onKeyDown(int keyCode, KeyEvent event) {
5129 boolean result = false;
5130
5131 switch (keyCode) {
5132 case KeyEvent.KEYCODE_DPAD_CENTER:
5133 case KeyEvent.KEYCODE_ENTER: {
5134 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5135 return true;
5136 }
5137 // Long clickable items don't necessarily have to be clickable
5138 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5139 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5140 (event.getRepeatCount() == 0)) {
5141 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005142 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005143 return true;
5144 }
5145 break;
5146 }
5147 }
5148 return result;
5149 }
5150
5151 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005152 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5153 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5154 * the event).
5155 */
5156 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5157 return false;
5158 }
5159
5160 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005161 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5162 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005163 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5164 * {@link KeyEvent#KEYCODE_ENTER} is released.
5165 *
5166 * @param keyCode A key code that represents the button pressed, from
5167 * {@link android.view.KeyEvent}.
5168 * @param event The KeyEvent object that defines the button action.
5169 */
5170 public boolean onKeyUp(int keyCode, KeyEvent event) {
5171 boolean result = false;
5172
5173 switch (keyCode) {
5174 case KeyEvent.KEYCODE_DPAD_CENTER:
5175 case KeyEvent.KEYCODE_ENTER: {
5176 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5177 return true;
5178 }
5179 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5180 setPressed(false);
5181
5182 if (!mHasPerformedLongPress) {
5183 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005184 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005185
5186 result = performClick();
5187 }
5188 }
5189 break;
5190 }
5191 }
5192 return result;
5193 }
5194
5195 /**
5196 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5197 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5198 * the event).
5199 *
5200 * @param keyCode A key code that represents the button pressed, from
5201 * {@link android.view.KeyEvent}.
5202 * @param repeatCount The number of times the action was made.
5203 * @param event The KeyEvent object that defines the button action.
5204 */
5205 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5206 return false;
5207 }
5208
5209 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005210 * Called on the focused view when a key shortcut event is not handled.
5211 * Override this method to implement local key shortcuts for the View.
5212 * Key shortcuts can also be implemented by setting the
5213 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005214 *
5215 * @param keyCode The value in event.getKeyCode().
5216 * @param event Description of the key event.
5217 * @return If you handled the event, return true. If you want to allow the
5218 * event to be handled by the next receiver, return false.
5219 */
5220 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5221 return false;
5222 }
5223
5224 /**
5225 * Check whether the called view is a text editor, in which case it
5226 * would make sense to automatically display a soft input window for
5227 * it. Subclasses should override this if they implement
5228 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005229 * a call on that method would return a non-null InputConnection, and
5230 * they are really a first-class editor that the user would normally
5231 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005232 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005233 * <p>The default implementation always returns false. This does
5234 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5235 * will not be called or the user can not otherwise perform edits on your
5236 * view; it is just a hint to the system that this is not the primary
5237 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005238 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005239 * @return Returns true if this view is a text editor, else false.
5240 */
5241 public boolean onCheckIsTextEditor() {
5242 return false;
5243 }
Romain Guy8506ab42009-06-11 17:35:47 -07005244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005245 /**
5246 * Create a new InputConnection for an InputMethod to interact
5247 * with the view. The default implementation returns null, since it doesn't
5248 * support input methods. You can override this to implement such support.
5249 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005250 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005251 * <p>When implementing this, you probably also want to implement
5252 * {@link #onCheckIsTextEditor()} to indicate you will return a
5253 * non-null InputConnection.
5254 *
5255 * @param outAttrs Fill in with attribute information about the connection.
5256 */
5257 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5258 return null;
5259 }
5260
5261 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005262 * Called by the {@link android.view.inputmethod.InputMethodManager}
5263 * when a view who is not the current
5264 * input connection target is trying to make a call on the manager. The
5265 * default implementation returns false; you can override this to return
5266 * true for certain views if you are performing InputConnection proxying
5267 * to them.
5268 * @param view The View that is making the InputMethodManager call.
5269 * @return Return true to allow the call, false to reject.
5270 */
5271 public boolean checkInputConnectionProxy(View view) {
5272 return false;
5273 }
Romain Guy8506ab42009-06-11 17:35:47 -07005274
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005275 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005276 * Show the context menu for this view. It is not safe to hold on to the
5277 * menu after returning from this method.
5278 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005279 * You should normally not overload this method. Overload
5280 * {@link #onCreateContextMenu(ContextMenu)} or define an
5281 * {@link OnCreateContextMenuListener} to add items to the context menu.
5282 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005283 * @param menu The context menu to populate
5284 */
5285 public void createContextMenu(ContextMenu menu) {
5286 ContextMenuInfo menuInfo = getContextMenuInfo();
5287
5288 // Sets the current menu info so all items added to menu will have
5289 // my extra info set.
5290 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5291
5292 onCreateContextMenu(menu);
5293 if (mOnCreateContextMenuListener != null) {
5294 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5295 }
5296
5297 // Clear the extra information so subsequent items that aren't mine don't
5298 // have my extra info.
5299 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5300
5301 if (mParent != null) {
5302 mParent.createContextMenu(menu);
5303 }
5304 }
5305
5306 /**
5307 * Views should implement this if they have extra information to associate
5308 * with the context menu. The return result is supplied as a parameter to
5309 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5310 * callback.
5311 *
5312 * @return Extra information about the item for which the context menu
5313 * should be shown. This information will vary across different
5314 * subclasses of View.
5315 */
5316 protected ContextMenuInfo getContextMenuInfo() {
5317 return null;
5318 }
5319
5320 /**
5321 * Views should implement this if the view itself is going to add items to
5322 * the context menu.
5323 *
5324 * @param menu the context menu to populate
5325 */
5326 protected void onCreateContextMenu(ContextMenu menu) {
5327 }
5328
5329 /**
5330 * Implement this method to handle trackball motion events. The
5331 * <em>relative</em> movement of the trackball since the last event
5332 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5333 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5334 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5335 * they will often be fractional values, representing the more fine-grained
5336 * movement information available from a trackball).
5337 *
5338 * @param event The motion event.
5339 * @return True if the event was handled, false otherwise.
5340 */
5341 public boolean onTrackballEvent(MotionEvent event) {
5342 return false;
5343 }
5344
5345 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005346 * Implement this method to handle generic motion events.
5347 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005348 * Generic motion events describe joystick movements, mouse hovers, track pad
5349 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005350 * {@link MotionEvent#getSource() source} of the motion event specifies
5351 * the class of input that was received. Implementations of this method
5352 * must examine the bits in the source before processing the event.
5353 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005354 * </p><p>
5355 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5356 * are delivered to the view under the pointer. All other generic motion events are
5357 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005358 * </p>
5359 * <code>
5360 * public boolean onGenericMotionEvent(MotionEvent event) {
5361 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005362 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5363 * // process the joystick movement...
5364 * return true;
5365 * }
5366 * }
5367 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5368 * switch (event.getAction()) {
5369 * case MotionEvent.ACTION_HOVER_MOVE:
5370 * // process the mouse hover movement...
5371 * return true;
5372 * case MotionEvent.ACTION_SCROLL:
5373 * // process the scroll wheel movement...
5374 * return true;
5375 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005376 * }
5377 * return super.onGenericMotionEvent(event);
5378 * }
5379 * </code>
5380 *
5381 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08005382 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08005383 */
5384 public boolean onGenericMotionEvent(MotionEvent event) {
5385 return false;
5386 }
5387
5388 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005389 * Implement this method to handle hover events.
5390 * <p>
5391 * Hover events are pointer events with action {@link MotionEvent#ACTION_HOVER_ENTER},
5392 * {@link MotionEvent#ACTION_HOVER_MOVE}, or {@link MotionEvent#ACTION_HOVER_EXIT}.
5393 * </p><p>
5394 * The view receives hover enter as the pointer enters the bounds of the view and hover
5395 * exit as the pointer exits the bound of the view or just before the pointer goes down
Romain Guy5c22a8c2011-05-13 11:48:45 -07005396 * (which implies that {@link #onTouchEvent(MotionEvent)} will be called soon).
Jeff Browna032cc02011-03-07 16:56:21 -08005397 * </p><p>
5398 * If the view would like to handle the hover event itself and prevent its children
5399 * from receiving hover, it should return true from this method. If this method returns
5400 * true and a child has already received a hover enter event, the child will
5401 * automatically receive a hover exit event.
5402 * </p><p>
5403 * The default implementation sets the hovered state of the view if the view is
5404 * clickable.
5405 * </p>
5406 *
5407 * @param event The motion event that describes the hover.
5408 * @return True if this view handled the hover event and does not want its children
5409 * to receive the hover event.
5410 */
5411 public boolean onHoverEvent(MotionEvent event) {
Jeff Browna032cc02011-03-07 16:56:21 -08005412 switch (event.getAction()) {
5413 case MotionEvent.ACTION_HOVER_ENTER:
5414 setHovered(true);
5415 break;
5416
5417 case MotionEvent.ACTION_HOVER_EXIT:
5418 setHovered(false);
5419 break;
5420 }
5421
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005422 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08005423 }
5424
5425 /**
5426 * Returns true if the view is currently hovered.
5427 *
5428 * @return True if the view is currently hovered.
5429 */
5430 public boolean isHovered() {
5431 return (mPrivateFlags & HOVERED) != 0;
5432 }
5433
5434 /**
5435 * Sets whether the view is currently hovered.
5436 *
5437 * @param hovered True if the view is hovered.
5438 */
5439 public void setHovered(boolean hovered) {
5440 if (hovered) {
5441 if ((mPrivateFlags & HOVERED) == 0) {
5442 mPrivateFlags |= HOVERED;
5443 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005444 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Jeff Browna032cc02011-03-07 16:56:21 -08005445 }
5446 } else {
5447 if ((mPrivateFlags & HOVERED) != 0) {
5448 mPrivateFlags &= ~HOVERED;
5449 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005450 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Jeff Browna032cc02011-03-07 16:56:21 -08005451 }
5452 }
5453 }
5454
5455 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005456 * Implement this method to handle touch screen motion events.
5457 *
5458 * @param event The motion event.
5459 * @return True if the event was handled, false otherwise.
5460 */
5461 public boolean onTouchEvent(MotionEvent event) {
5462 final int viewFlags = mViewFlags;
5463
5464 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07005465 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
5466 mPrivateFlags &= ~PRESSED;
5467 refreshDrawableState();
5468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005469 // A disabled view that is clickable still consumes the touch
5470 // events, it just doesn't respond to them.
5471 return (((viewFlags & CLICKABLE) == CLICKABLE ||
5472 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
5473 }
5474
5475 if (mTouchDelegate != null) {
5476 if (mTouchDelegate.onTouchEvent(event)) {
5477 return true;
5478 }
5479 }
5480
5481 if (((viewFlags & CLICKABLE) == CLICKABLE ||
5482 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
5483 switch (event.getAction()) {
5484 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08005485 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
5486 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005487 // take focus if we don't have it already and we should in
5488 // touch mode.
5489 boolean focusTaken = false;
5490 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
5491 focusTaken = requestFocus();
5492 }
5493
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08005494 if (prepressed) {
5495 // The button is being released before we actually
5496 // showed it as pressed. Make it show the pressed
5497 // state now (before scheduling the click) to ensure
5498 // the user sees it.
5499 mPrivateFlags |= PRESSED;
5500 refreshDrawableState();
5501 }
Joe Malin32736f02011-01-19 16:14:20 -08005502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005503 if (!mHasPerformedLongPress) {
5504 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005505 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005506
5507 // Only perform take click actions if we were in the pressed state
5508 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08005509 // Use a Runnable and post this rather than calling
5510 // performClick directly. This lets other visual state
5511 // of the view update before click actions start.
5512 if (mPerformClick == null) {
5513 mPerformClick = new PerformClick();
5514 }
5515 if (!post(mPerformClick)) {
5516 performClick();
5517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005518 }
5519 }
5520
5521 if (mUnsetPressedState == null) {
5522 mUnsetPressedState = new UnsetPressedState();
5523 }
5524
Adam Powelle14579b2009-12-16 18:39:52 -08005525 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08005526 postDelayed(mUnsetPressedState,
5527 ViewConfiguration.getPressedStateDuration());
5528 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005529 // If the post failed, unpress right now
5530 mUnsetPressedState.run();
5531 }
Adam Powelle14579b2009-12-16 18:39:52 -08005532 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005533 }
5534 break;
5535
5536 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08005537 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005538
5539 // Walk up the hierarchy to determine if we're inside a scrolling container.
5540 boolean isInScrollingContainer = false;
5541 ViewParent p = getParent();
5542 while (p != null && p instanceof ViewGroup) {
5543 if (((ViewGroup) p).shouldDelayChildPressedState()) {
5544 isInScrollingContainer = true;
5545 break;
5546 }
5547 p = p.getParent();
5548 }
5549
5550 // For views inside a scrolling container, delay the pressed feedback for
5551 // a short period in case this is a scroll.
5552 if (isInScrollingContainer) {
5553 mPrivateFlags |= PREPRESSED;
5554 if (mPendingCheckForTap == null) {
5555 mPendingCheckForTap = new CheckForTap();
5556 }
5557 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
5558 } else {
5559 // Not inside a scrolling container, so show the feedback right away
5560 mPrivateFlags |= PRESSED;
5561 refreshDrawableState();
5562 checkForLongClick(0);
5563 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005564 break;
5565
5566 case MotionEvent.ACTION_CANCEL:
5567 mPrivateFlags &= ~PRESSED;
5568 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08005569 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005570 break;
5571
5572 case MotionEvent.ACTION_MOVE:
5573 final int x = (int) event.getX();
5574 final int y = (int) event.getY();
5575
5576 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07005577 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005578 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08005579 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005580 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08005581 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05005582 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005583
5584 // Need to switch from pressed to not pressed
5585 mPrivateFlags &= ~PRESSED;
5586 refreshDrawableState();
5587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005588 }
5589 break;
5590 }
5591 return true;
5592 }
5593
5594 return false;
5595 }
5596
5597 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05005598 * Remove the longpress detection timer.
5599 */
5600 private void removeLongPressCallback() {
5601 if (mPendingCheckForLongPress != null) {
5602 removeCallbacks(mPendingCheckForLongPress);
5603 }
5604 }
Adam Powell3cb8b632011-01-21 15:34:14 -08005605
5606 /**
5607 * Remove the pending click action
5608 */
5609 private void removePerformClickCallback() {
5610 if (mPerformClick != null) {
5611 removeCallbacks(mPerformClick);
5612 }
5613 }
5614
Adam Powelle14579b2009-12-16 18:39:52 -08005615 /**
Romain Guya440b002010-02-24 15:57:54 -08005616 * Remove the prepress detection timer.
5617 */
5618 private void removeUnsetPressCallback() {
5619 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
5620 setPressed(false);
5621 removeCallbacks(mUnsetPressedState);
5622 }
5623 }
5624
5625 /**
Adam Powelle14579b2009-12-16 18:39:52 -08005626 * Remove the tap detection timer.
5627 */
5628 private void removeTapCallback() {
5629 if (mPendingCheckForTap != null) {
5630 mPrivateFlags &= ~PREPRESSED;
5631 removeCallbacks(mPendingCheckForTap);
5632 }
5633 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005634
5635 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005636 * Cancels a pending long press. Your subclass can use this if you
5637 * want the context menu to come up if the user presses and holds
5638 * at the same place, but you don't want it to come up if they press
5639 * and then move around enough to cause scrolling.
5640 */
5641 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005642 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08005643
5644 /*
5645 * The prepressed state handled by the tap callback is a display
5646 * construct, but the tap callback will post a long press callback
5647 * less its own timeout. Remove it here.
5648 */
5649 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005650 }
5651
5652 /**
5653 * Sets the TouchDelegate for this View.
5654 */
5655 public void setTouchDelegate(TouchDelegate delegate) {
5656 mTouchDelegate = delegate;
5657 }
5658
5659 /**
5660 * Gets the TouchDelegate for this View.
5661 */
5662 public TouchDelegate getTouchDelegate() {
5663 return mTouchDelegate;
5664 }
5665
5666 /**
5667 * Set flags controlling behavior of this view.
5668 *
5669 * @param flags Constant indicating the value which should be set
5670 * @param mask Constant indicating the bit range that should be changed
5671 */
5672 void setFlags(int flags, int mask) {
5673 int old = mViewFlags;
5674 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
5675
5676 int changed = mViewFlags ^ old;
5677 if (changed == 0) {
5678 return;
5679 }
5680 int privateFlags = mPrivateFlags;
5681
5682 /* Check if the FOCUSABLE bit has changed */
5683 if (((changed & FOCUSABLE_MASK) != 0) &&
5684 ((privateFlags & HAS_BOUNDS) !=0)) {
5685 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
5686 && ((privateFlags & FOCUSED) != 0)) {
5687 /* Give up focus if we are no longer focusable */
5688 clearFocus();
5689 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
5690 && ((privateFlags & FOCUSED) == 0)) {
5691 /*
5692 * Tell the view system that we are now available to take focus
5693 * if no one else already has it.
5694 */
5695 if (mParent != null) mParent.focusableViewAvailable(this);
5696 }
5697 }
5698
5699 if ((flags & VISIBILITY_MASK) == VISIBLE) {
5700 if ((changed & VISIBILITY_MASK) != 0) {
5701 /*
5702 * If this view is becoming visible, set the DRAWN flag so that
5703 * the next invalidate() will not be skipped.
5704 */
5705 mPrivateFlags |= DRAWN;
5706
5707 needGlobalAttributesUpdate(true);
5708
5709 // a view becoming visible is worth notifying the parent
5710 // about in case nothing has focus. even if this specific view
5711 // isn't focusable, it may contain something that is, so let
5712 // the root view try to give this focus if nothing else does.
5713 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
5714 mParent.focusableViewAvailable(this);
5715 }
5716 }
5717 }
5718
5719 /* Check if the GONE bit has changed */
5720 if ((changed & GONE) != 0) {
5721 needGlobalAttributesUpdate(false);
5722 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005723 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005724
Romain Guyecd80ee2009-12-03 17:13:02 -08005725 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
5726 if (hasFocus()) clearFocus();
5727 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005728 }
5729 if (mAttachInfo != null) {
5730 mAttachInfo.mViewVisibilityChanged = true;
5731 }
5732 }
5733
5734 /* Check if the VISIBLE bit has changed */
5735 if ((changed & INVISIBLE) != 0) {
5736 needGlobalAttributesUpdate(false);
Romain Guy0fd89bf2011-01-26 15:41:30 -08005737 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005738
5739 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
5740 // root view becoming invisible shouldn't clear focus
5741 if (getRootView() != this) {
5742 clearFocus();
5743 }
5744 }
5745 if (mAttachInfo != null) {
5746 mAttachInfo.mViewVisibilityChanged = true;
5747 }
5748 }
5749
Adam Powell326d8082009-12-09 15:10:07 -08005750 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07005751 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005752 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
5753 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07005754 }
Adam Powell326d8082009-12-09 15:10:07 -08005755 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
5756 }
5757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005758 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
5759 destroyDrawingCache();
5760 }
5761
5762 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
5763 destroyDrawingCache();
5764 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08005765 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005766 }
5767
5768 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
5769 destroyDrawingCache();
5770 mPrivateFlags &= ~DRAWING_CACHE_VALID;
5771 }
5772
5773 if ((changed & DRAW_MASK) != 0) {
5774 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
5775 if (mBGDrawable != null) {
5776 mPrivateFlags &= ~SKIP_DRAW;
5777 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
5778 } else {
5779 mPrivateFlags |= SKIP_DRAW;
5780 }
5781 } else {
5782 mPrivateFlags &= ~SKIP_DRAW;
5783 }
5784 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005785 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005786 }
5787
5788 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08005789 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005790 mParent.recomputeViewAttributes(this);
5791 }
5792 }
5793 }
5794
5795 /**
5796 * Change the view's z order in the tree, so it's on top of other sibling
5797 * views
5798 */
5799 public void bringToFront() {
5800 if (mParent != null) {
5801 mParent.bringChildToFront(this);
5802 }
5803 }
5804
5805 /**
5806 * This is called in response to an internal scroll in this view (i.e., the
5807 * view scrolled its own contents). This is typically as a result of
5808 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
5809 * called.
5810 *
5811 * @param l Current horizontal scroll origin.
5812 * @param t Current vertical scroll origin.
5813 * @param oldl Previous horizontal scroll origin.
5814 * @param oldt Previous vertical scroll origin.
5815 */
5816 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
5817 mBackgroundSizeChanged = true;
5818
5819 final AttachInfo ai = mAttachInfo;
5820 if (ai != null) {
5821 ai.mViewScrollChanged = true;
5822 }
5823 }
5824
5825 /**
Chet Haase21cd1382010-09-01 17:42:29 -07005826 * Interface definition for a callback to be invoked when the layout bounds of a view
5827 * changes due to layout processing.
5828 */
5829 public interface OnLayoutChangeListener {
5830 /**
5831 * Called when the focus state of a view has changed.
5832 *
5833 * @param v The view whose state has changed.
5834 * @param left The new value of the view's left property.
5835 * @param top The new value of the view's top property.
5836 * @param right The new value of the view's right property.
5837 * @param bottom The new value of the view's bottom property.
5838 * @param oldLeft The previous value of the view's left property.
5839 * @param oldTop The previous value of the view's top property.
5840 * @param oldRight The previous value of the view's right property.
5841 * @param oldBottom The previous value of the view's bottom property.
5842 */
5843 void onLayoutChange(View v, int left, int top, int right, int bottom,
5844 int oldLeft, int oldTop, int oldRight, int oldBottom);
5845 }
5846
5847 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005848 * This is called during layout when the size of this view has changed. If
5849 * you were just added to the view hierarchy, you're called with the old
5850 * values of 0.
5851 *
5852 * @param w Current width of this view.
5853 * @param h Current height of this view.
5854 * @param oldw Old width of this view.
5855 * @param oldh Old height of this view.
5856 */
5857 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
5858 }
5859
5860 /**
5861 * Called by draw to draw the child views. This may be overridden
5862 * by derived classes to gain control just before its children are drawn
5863 * (but after its own view has been drawn).
5864 * @param canvas the canvas on which to draw the view
5865 */
5866 protected void dispatchDraw(Canvas canvas) {
5867 }
5868
5869 /**
5870 * Gets the parent of this view. Note that the parent is a
5871 * ViewParent and not necessarily a View.
5872 *
5873 * @return Parent of this view.
5874 */
5875 public final ViewParent getParent() {
5876 return mParent;
5877 }
5878
5879 /**
5880 * Return the scrolled left position of this view. This is the left edge of
5881 * the displayed part of your view. You do not need to draw any pixels
5882 * farther left, since those are outside of the frame of your view on
5883 * screen.
5884 *
5885 * @return The left edge of the displayed part of your view, in pixels.
5886 */
5887 public final int getScrollX() {
5888 return mScrollX;
5889 }
5890
5891 /**
5892 * Return the scrolled top position of this view. This is the top edge of
5893 * the displayed part of your view. You do not need to draw any pixels above
5894 * it, since those are outside of the frame of your view on screen.
5895 *
5896 * @return The top edge of the displayed part of your view, in pixels.
5897 */
5898 public final int getScrollY() {
5899 return mScrollY;
5900 }
5901
5902 /**
5903 * Return the width of the your view.
5904 *
5905 * @return The width of your view, in pixels.
5906 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005907 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005908 public final int getWidth() {
5909 return mRight - mLeft;
5910 }
5911
5912 /**
5913 * Return the height of your view.
5914 *
5915 * @return The height of your view, in pixels.
5916 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005917 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005918 public final int getHeight() {
5919 return mBottom - mTop;
5920 }
5921
5922 /**
5923 * Return the visible drawing bounds of your view. Fills in the output
5924 * rectangle with the values from getScrollX(), getScrollY(),
5925 * getWidth(), and getHeight().
5926 *
5927 * @param outRect The (scrolled) drawing bounds of the view.
5928 */
5929 public void getDrawingRect(Rect outRect) {
5930 outRect.left = mScrollX;
5931 outRect.top = mScrollY;
5932 outRect.right = mScrollX + (mRight - mLeft);
5933 outRect.bottom = mScrollY + (mBottom - mTop);
5934 }
5935
5936 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005937 * Like {@link #getMeasuredWidthAndState()}, but only returns the
5938 * raw width component (that is the result is masked by
5939 * {@link #MEASURED_SIZE_MASK}).
5940 *
5941 * @return The raw measured width of this view.
5942 */
5943 public final int getMeasuredWidth() {
5944 return mMeasuredWidth & MEASURED_SIZE_MASK;
5945 }
5946
5947 /**
5948 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07005949 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08005950 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005951 * This should be used during measurement and layout calculations only. Use
5952 * {@link #getWidth()} to see how wide a view is after layout.
5953 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005954 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005955 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08005956 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005957 return mMeasuredWidth;
5958 }
5959
5960 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005961 * Like {@link #getMeasuredHeightAndState()}, but only returns the
5962 * raw width component (that is the result is masked by
5963 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005964 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005965 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005966 */
5967 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08005968 return mMeasuredHeight & MEASURED_SIZE_MASK;
5969 }
5970
5971 /**
5972 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07005973 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08005974 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
5975 * This should be used during measurement and layout calculations only. Use
5976 * {@link #getHeight()} to see how wide a view is after layout.
5977 *
5978 * @return The measured width of this view as a bit mask.
5979 */
5980 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005981 return mMeasuredHeight;
5982 }
5983
5984 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005985 * Return only the state bits of {@link #getMeasuredWidthAndState()}
5986 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
5987 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
5988 * and the height component is at the shifted bits
5989 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
5990 */
5991 public final int getMeasuredState() {
5992 return (mMeasuredWidth&MEASURED_STATE_MASK)
5993 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
5994 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
5995 }
5996
5997 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005998 * The transform matrix of this view, which is calculated based on the current
5999 * roation, scale, and pivot properties.
6000 *
6001 * @see #getRotation()
6002 * @see #getScaleX()
6003 * @see #getScaleY()
6004 * @see #getPivotX()
6005 * @see #getPivotY()
6006 * @return The current transform matrix for the view
6007 */
6008 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07006009 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07006010 return mMatrix;
6011 }
6012
6013 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006014 * Utility function to determine if the value is far enough away from zero to be
6015 * considered non-zero.
6016 * @param value A floating point value to check for zero-ness
6017 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6018 */
6019 private static boolean nonzero(float value) {
6020 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6021 }
6022
6023 /**
Jeff Brown86671742010-09-30 20:00:15 -07006024 * Returns true if the transform matrix is the identity matrix.
6025 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006026 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006027 * @return True if the transform matrix is the identity matrix, false otherwise.
6028 */
Jeff Brown86671742010-09-30 20:00:15 -07006029 final boolean hasIdentityMatrix() {
6030 updateMatrix();
6031 return mMatrixIsIdentity;
6032 }
6033
6034 /**
6035 * Recomputes the transform matrix if necessary.
6036 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006037 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07006038 if (mMatrixDirty) {
6039 // transform-related properties have changed since the last time someone
6040 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006041
6042 // Figure out if we need to update the pivot point
6043 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006044 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006045 mPrevWidth = mRight - mLeft;
6046 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08006047 mPivotX = mPrevWidth / 2f;
6048 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07006049 }
6050 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006051 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07006052 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
6053 mMatrix.setTranslate(mTranslationX, mTranslationY);
6054 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
6055 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
6056 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07006057 if (mCamera == null) {
6058 mCamera = new Camera();
6059 matrix3D = new Matrix();
6060 }
6061 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07006062 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08006063 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07006064 mCamera.getMatrix(matrix3D);
6065 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006066 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07006067 mMatrix.postConcat(matrix3D);
6068 mCamera.restore();
6069 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006070 mMatrixDirty = false;
6071 mMatrixIsIdentity = mMatrix.isIdentity();
6072 mInverseMatrixDirty = true;
6073 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006074 }
6075
6076 /**
6077 * Utility method to retrieve the inverse of the current mMatrix property.
6078 * We cache the matrix to avoid recalculating it when transform properties
6079 * have not changed.
6080 *
6081 * @return The inverse of the current matrix of this view.
6082 */
Jeff Brown86671742010-09-30 20:00:15 -07006083 final Matrix getInverseMatrix() {
6084 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07006085 if (mInverseMatrixDirty) {
6086 if (mInverseMatrix == null) {
6087 mInverseMatrix = new Matrix();
6088 }
6089 mMatrix.invert(mInverseMatrix);
6090 mInverseMatrixDirty = false;
6091 }
6092 return mInverseMatrix;
6093 }
6094
6095 /**
Romain Guya5364ee2011-02-24 14:46:04 -08006096 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
6097 * views are drawn) from the camera to this view. The camera's distance
6098 * affects 3D transformations, for instance rotations around the X and Y
6099 * axis. If the rotationX or rotationY properties are changed and this view is
6100 * large (more than half the size of the screen), it is recommended to always
6101 * use a camera distance that's greater than the height (X axis rotation) or
6102 * the width (Y axis rotation) of this view.</p>
6103 *
6104 * <p>The distance of the camera from the view plane can have an affect on the
6105 * perspective distortion of the view when it is rotated around the x or y axis.
6106 * For example, a large distance will result in a large viewing angle, and there
6107 * will not be much perspective distortion of the view as it rotates. A short
6108 * distance may cause much more perspective distortion upon rotation, and can
6109 * also result in some drawing artifacts if the rotated view ends up partially
6110 * behind the camera (which is why the recommendation is to use a distance at
6111 * least as far as the size of the view, if the view is to be rotated.)</p>
6112 *
6113 * <p>The distance is expressed in "depth pixels." The default distance depends
6114 * on the screen density. For instance, on a medium density display, the
6115 * default distance is 1280. On a high density display, the default distance
6116 * is 1920.</p>
6117 *
6118 * <p>If you want to specify a distance that leads to visually consistent
6119 * results across various densities, use the following formula:</p>
6120 * <pre>
6121 * float scale = context.getResources().getDisplayMetrics().density;
6122 * view.setCameraDistance(distance * scale);
6123 * </pre>
6124 *
6125 * <p>The density scale factor of a high density display is 1.5,
6126 * and 1920 = 1280 * 1.5.</p>
6127 *
6128 * @param distance The distance in "depth pixels", if negative the opposite
6129 * value is used
6130 *
6131 * @see #setRotationX(float)
6132 * @see #setRotationY(float)
6133 */
6134 public void setCameraDistance(float distance) {
6135 invalidateParentCaches();
6136 invalidate(false);
6137
6138 final float dpi = mResources.getDisplayMetrics().densityDpi;
6139 if (mCamera == null) {
6140 mCamera = new Camera();
6141 matrix3D = new Matrix();
6142 }
6143
6144 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6145 mMatrixDirty = true;
6146
6147 invalidate(false);
6148 }
6149
6150 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006151 * The degrees that the view is rotated around the pivot point.
6152 *
Romain Guya5364ee2011-02-24 14:46:04 -08006153 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006154 * @see #getPivotX()
6155 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006156 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006157 * @return The degrees of rotation.
6158 */
6159 public float getRotation() {
6160 return mRotation;
6161 }
6162
6163 /**
Chet Haase897247b2010-09-09 14:54:47 -07006164 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6165 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006166 *
6167 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006168 *
6169 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006170 * @see #getPivotX()
6171 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006172 * @see #setRotationX(float)
6173 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006174 *
6175 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006176 */
6177 public void setRotation(float rotation) {
6178 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006179 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006180 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006181 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006182 mRotation = rotation;
6183 mMatrixDirty = true;
6184 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006185 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006186 }
6187 }
6188
6189 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006190 * The degrees that the view is rotated around the vertical axis through the pivot point.
6191 *
6192 * @see #getPivotX()
6193 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006194 * @see #setRotationY(float)
6195 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006196 * @return The degrees of Y rotation.
6197 */
6198 public float getRotationY() {
6199 return mRotationY;
6200 }
6201
6202 /**
Chet Haase897247b2010-09-09 14:54:47 -07006203 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6204 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6205 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006206 *
6207 * When rotating large views, it is recommended to adjust the camera distance
6208 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006209 *
6210 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006211 *
6212 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006213 * @see #getPivotX()
6214 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006215 * @see #setRotation(float)
6216 * @see #setRotationX(float)
6217 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006218 *
6219 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006220 */
6221 public void setRotationY(float rotationY) {
6222 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006223 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006224 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006225 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006226 mRotationY = rotationY;
6227 mMatrixDirty = true;
6228 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006229 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006230 }
6231 }
6232
6233 /**
6234 * The degrees that the view is rotated around the horizontal axis through the pivot point.
6235 *
6236 * @see #getPivotX()
6237 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006238 * @see #setRotationX(float)
6239 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006240 * @return The degrees of X rotation.
6241 */
6242 public float getRotationX() {
6243 return mRotationX;
6244 }
6245
6246 /**
Chet Haase897247b2010-09-09 14:54:47 -07006247 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6248 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6249 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006250 *
6251 * When rotating large views, it is recommended to adjust the camera distance
6252 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006253 *
6254 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006255 *
6256 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006257 * @see #getPivotX()
6258 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006259 * @see #setRotation(float)
6260 * @see #setRotationY(float)
6261 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006262 *
6263 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006264 */
6265 public void setRotationX(float rotationX) {
6266 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006267 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006268 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006269 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006270 mRotationX = rotationX;
6271 mMatrixDirty = true;
6272 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006273 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006274 }
6275 }
6276
6277 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006278 * The amount that the view is scaled in x around the pivot point, as a proportion of
6279 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6280 *
Joe Onorato93162322010-09-16 15:42:01 -04006281 * <p>By default, this is 1.0f.
6282 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006283 * @see #getPivotX()
6284 * @see #getPivotY()
6285 * @return The scaling factor.
6286 */
6287 public float getScaleX() {
6288 return mScaleX;
6289 }
6290
6291 /**
6292 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6293 * the view's unscaled width. A value of 1 means that no scaling is applied.
6294 *
6295 * @param scaleX The scaling factor.
6296 * @see #getPivotX()
6297 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006298 *
6299 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006300 */
6301 public void setScaleX(float scaleX) {
6302 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006303 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006304 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006305 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006306 mScaleX = scaleX;
6307 mMatrixDirty = true;
6308 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006309 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006310 }
6311 }
6312
6313 /**
6314 * The amount that the view is scaled in y around the pivot point, as a proportion of
6315 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6316 *
Joe Onorato93162322010-09-16 15:42:01 -04006317 * <p>By default, this is 1.0f.
6318 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006319 * @see #getPivotX()
6320 * @see #getPivotY()
6321 * @return The scaling factor.
6322 */
6323 public float getScaleY() {
6324 return mScaleY;
6325 }
6326
6327 /**
6328 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
6329 * the view's unscaled width. A value of 1 means that no scaling is applied.
6330 *
6331 * @param scaleY The scaling factor.
6332 * @see #getPivotX()
6333 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006334 *
6335 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07006336 */
6337 public void setScaleY(float scaleY) {
6338 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006339 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006340 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006341 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006342 mScaleY = scaleY;
6343 mMatrixDirty = true;
6344 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006345 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006346 }
6347 }
6348
6349 /**
6350 * The x location of the point around which the view is {@link #setRotation(float) rotated}
6351 * and {@link #setScaleX(float) scaled}.
6352 *
6353 * @see #getRotation()
6354 * @see #getScaleX()
6355 * @see #getScaleY()
6356 * @see #getPivotY()
6357 * @return The x location of the pivot point.
6358 */
6359 public float getPivotX() {
6360 return mPivotX;
6361 }
6362
6363 /**
6364 * Sets the x location of the point around which the view is
6365 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07006366 * By default, the pivot point is centered on the object.
6367 * Setting this property disables this behavior and causes the view to use only the
6368 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006369 *
6370 * @param pivotX The x location of the pivot point.
6371 * @see #getRotation()
6372 * @see #getScaleX()
6373 * @see #getScaleY()
6374 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006375 *
6376 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07006377 */
6378 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006379 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006380 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006381 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006382 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006383 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006384 mPivotX = pivotX;
6385 mMatrixDirty = true;
6386 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006387 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006388 }
6389 }
6390
6391 /**
6392 * The y location of the point around which the view is {@link #setRotation(float) rotated}
6393 * and {@link #setScaleY(float) scaled}.
6394 *
6395 * @see #getRotation()
6396 * @see #getScaleX()
6397 * @see #getScaleY()
6398 * @see #getPivotY()
6399 * @return The y location of the pivot point.
6400 */
6401 public float getPivotY() {
6402 return mPivotY;
6403 }
6404
6405 /**
6406 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07006407 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
6408 * Setting this property disables this behavior and causes the view to use only the
6409 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006410 *
6411 * @param pivotY The y location of the pivot point.
6412 * @see #getRotation()
6413 * @see #getScaleX()
6414 * @see #getScaleY()
6415 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006416 *
6417 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07006418 */
6419 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006420 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006421 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006422 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006423 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006424 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006425 mPivotY = pivotY;
6426 mMatrixDirty = true;
6427 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006428 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006429 }
6430 }
6431
6432 /**
6433 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
6434 * completely transparent and 1 means the view is completely opaque.
6435 *
Joe Onorato93162322010-09-16 15:42:01 -04006436 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07006437 * @return The opacity of the view.
6438 */
6439 public float getAlpha() {
6440 return mAlpha;
6441 }
6442
6443 /**
Romain Guy171c5922011-01-06 10:04:23 -08006444 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
6445 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08006446 *
Romain Guy171c5922011-01-06 10:04:23 -08006447 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
6448 * responsible for applying the opacity itself. Otherwise, calling this method is
6449 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08006450 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07006451 *
6452 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08006453 *
Joe Malin32736f02011-01-19 16:14:20 -08006454 * @see #setLayerType(int, android.graphics.Paint)
6455 *
Chet Haase73066682010-11-29 15:55:32 -08006456 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07006457 */
6458 public void setAlpha(float alpha) {
6459 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006460 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07006461 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07006462 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006463 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006464 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07006465 } else {
Romain Guya3496a92010-10-12 11:53:24 -07006466 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006467 invalidate(false);
6468 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006469 }
6470
6471 /**
Chet Haasea00f3862011-02-22 06:34:40 -08006472 * Faster version of setAlpha() which performs the same steps except there are
6473 * no calls to invalidate(). The caller of this function should perform proper invalidation
6474 * on the parent and this object. The return value indicates whether the subclass handles
6475 * alpha (the return value for onSetAlpha()).
6476 *
6477 * @param alpha The new value for the alpha property
6478 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
6479 */
6480 boolean setAlphaNoInvalidation(float alpha) {
6481 mAlpha = alpha;
6482 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
6483 if (subclassHandlesAlpha) {
6484 mPrivateFlags |= ALPHA_SET;
6485 } else {
6486 mPrivateFlags &= ~ALPHA_SET;
6487 }
6488 return subclassHandlesAlpha;
6489 }
6490
6491 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006492 * Top position of this view relative to its parent.
6493 *
6494 * @return The top of this view, in pixels.
6495 */
6496 @ViewDebug.CapturedViewProperty
6497 public final int getTop() {
6498 return mTop;
6499 }
6500
6501 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006502 * Sets the top position of this view relative to its parent. This method is meant to be called
6503 * by the layout system and should not generally be called otherwise, because the property
6504 * may be changed at any time by the layout.
6505 *
6506 * @param top The top of this view, in pixels.
6507 */
6508 public final void setTop(int top) {
6509 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07006510 updateMatrix();
6511 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006512 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006513 int minTop;
6514 int yLoc;
6515 if (top < mTop) {
6516 minTop = top;
6517 yLoc = top - mTop;
6518 } else {
6519 minTop = mTop;
6520 yLoc = 0;
6521 }
Chet Haasee9140a72011-02-16 16:23:29 -08006522 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006523 }
6524 } else {
6525 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006526 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006527 }
6528
Chet Haaseed032702010-10-01 14:05:54 -07006529 int width = mRight - mLeft;
6530 int oldHeight = mBottom - mTop;
6531
Chet Haase21cd1382010-09-01 17:42:29 -07006532 mTop = top;
6533
Chet Haaseed032702010-10-01 14:05:54 -07006534 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6535
Chet Haase21cd1382010-09-01 17:42:29 -07006536 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006537 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6538 // A change in dimension means an auto-centered pivot point changes, too
6539 mMatrixDirty = true;
6540 }
Chet Haase21cd1382010-09-01 17:42:29 -07006541 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006542 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006543 }
Chet Haase55dbb652010-12-21 20:15:08 -08006544 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006545 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006546 }
6547 }
6548
6549 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006550 * Bottom position of this view relative to its parent.
6551 *
6552 * @return The bottom of this view, in pixels.
6553 */
6554 @ViewDebug.CapturedViewProperty
6555 public final int getBottom() {
6556 return mBottom;
6557 }
6558
6559 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08006560 * True if this view has changed since the last time being drawn.
6561 *
6562 * @return The dirty state of this view.
6563 */
6564 public boolean isDirty() {
6565 return (mPrivateFlags & DIRTY_MASK) != 0;
6566 }
6567
6568 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006569 * Sets the bottom position of this view relative to its parent. This method is meant to be
6570 * called by the layout system and should not generally be called otherwise, because the
6571 * property may be changed at any time by the layout.
6572 *
6573 * @param bottom The bottom of this view, in pixels.
6574 */
6575 public final void setBottom(int bottom) {
6576 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07006577 updateMatrix();
6578 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006579 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006580 int maxBottom;
6581 if (bottom < mBottom) {
6582 maxBottom = mBottom;
6583 } else {
6584 maxBottom = bottom;
6585 }
Chet Haasee9140a72011-02-16 16:23:29 -08006586 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006587 }
6588 } else {
6589 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006590 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006591 }
6592
Chet Haaseed032702010-10-01 14:05:54 -07006593 int width = mRight - mLeft;
6594 int oldHeight = mBottom - mTop;
6595
Chet Haase21cd1382010-09-01 17:42:29 -07006596 mBottom = bottom;
6597
Chet Haaseed032702010-10-01 14:05:54 -07006598 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6599
Chet Haase21cd1382010-09-01 17:42:29 -07006600 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006601 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6602 // A change in dimension means an auto-centered pivot point changes, too
6603 mMatrixDirty = true;
6604 }
Chet Haase21cd1382010-09-01 17:42:29 -07006605 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006606 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006607 }
Chet Haase55dbb652010-12-21 20:15:08 -08006608 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006609 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006610 }
6611 }
6612
6613 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006614 * Left position of this view relative to its parent.
6615 *
6616 * @return The left edge of this view, in pixels.
6617 */
6618 @ViewDebug.CapturedViewProperty
6619 public final int getLeft() {
6620 return mLeft;
6621 }
6622
6623 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006624 * Sets the left position of this view relative to its parent. This method is meant to be called
6625 * by the layout system and should not generally be called otherwise, because the property
6626 * may be changed at any time by the layout.
6627 *
6628 * @param left The bottom of this view, in pixels.
6629 */
6630 public final void setLeft(int left) {
6631 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07006632 updateMatrix();
6633 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006634 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006635 int minLeft;
6636 int xLoc;
6637 if (left < mLeft) {
6638 minLeft = left;
6639 xLoc = left - mLeft;
6640 } else {
6641 minLeft = mLeft;
6642 xLoc = 0;
6643 }
Chet Haasee9140a72011-02-16 16:23:29 -08006644 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006645 }
6646 } else {
6647 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006648 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006649 }
6650
Chet Haaseed032702010-10-01 14:05:54 -07006651 int oldWidth = mRight - mLeft;
6652 int height = mBottom - mTop;
6653
Chet Haase21cd1382010-09-01 17:42:29 -07006654 mLeft = left;
6655
Chet Haaseed032702010-10-01 14:05:54 -07006656 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6657
Chet Haase21cd1382010-09-01 17:42:29 -07006658 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006659 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6660 // A change in dimension means an auto-centered pivot point changes, too
6661 mMatrixDirty = true;
6662 }
Chet Haase21cd1382010-09-01 17:42:29 -07006663 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006664 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006665 }
Chet Haase55dbb652010-12-21 20:15:08 -08006666 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006667 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006668 }
6669 }
6670
6671 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006672 * Right position of this view relative to its parent.
6673 *
6674 * @return The right edge of this view, in pixels.
6675 */
6676 @ViewDebug.CapturedViewProperty
6677 public final int getRight() {
6678 return mRight;
6679 }
6680
6681 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006682 * Sets the right position of this view relative to its parent. This method is meant to be called
6683 * by the layout system and should not generally be called otherwise, because the property
6684 * may be changed at any time by the layout.
6685 *
6686 * @param right The bottom of this view, in pixels.
6687 */
6688 public final void setRight(int right) {
6689 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07006690 updateMatrix();
6691 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006692 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006693 int maxRight;
6694 if (right < mRight) {
6695 maxRight = mRight;
6696 } else {
6697 maxRight = right;
6698 }
Chet Haasee9140a72011-02-16 16:23:29 -08006699 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006700 }
6701 } else {
6702 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006703 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006704 }
6705
Chet Haaseed032702010-10-01 14:05:54 -07006706 int oldWidth = mRight - mLeft;
6707 int height = mBottom - mTop;
6708
Chet Haase21cd1382010-09-01 17:42:29 -07006709 mRight = right;
6710
Chet Haaseed032702010-10-01 14:05:54 -07006711 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6712
Chet Haase21cd1382010-09-01 17:42:29 -07006713 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006714 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6715 // A change in dimension means an auto-centered pivot point changes, too
6716 mMatrixDirty = true;
6717 }
Chet Haase21cd1382010-09-01 17:42:29 -07006718 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006719 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006720 }
Chet Haase55dbb652010-12-21 20:15:08 -08006721 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006722 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006723 }
6724 }
6725
6726 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006727 * The visual x position of this view, in pixels. This is equivalent to the
6728 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08006729 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07006730 *
Chet Haasedf030d22010-07-30 17:22:38 -07006731 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006732 */
Chet Haasedf030d22010-07-30 17:22:38 -07006733 public float getX() {
6734 return mLeft + mTranslationX;
6735 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006736
Chet Haasedf030d22010-07-30 17:22:38 -07006737 /**
6738 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
6739 * {@link #setTranslationX(float) translationX} property to be the difference between
6740 * the x value passed in and the current {@link #getLeft() left} property.
6741 *
6742 * @param x The visual x position of this view, in pixels.
6743 */
6744 public void setX(float x) {
6745 setTranslationX(x - mLeft);
6746 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006747
Chet Haasedf030d22010-07-30 17:22:38 -07006748 /**
6749 * The visual y position of this view, in pixels. This is equivalent to the
6750 * {@link #setTranslationY(float) translationY} property plus the current
6751 * {@link #getTop() top} property.
6752 *
6753 * @return The visual y position of this view, in pixels.
6754 */
6755 public float getY() {
6756 return mTop + mTranslationY;
6757 }
6758
6759 /**
6760 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
6761 * {@link #setTranslationY(float) translationY} property to be the difference between
6762 * the y value passed in and the current {@link #getTop() top} property.
6763 *
6764 * @param y The visual y position of this view, in pixels.
6765 */
6766 public void setY(float y) {
6767 setTranslationY(y - mTop);
6768 }
6769
6770
6771 /**
6772 * The horizontal location of this view relative to its {@link #getLeft() left} position.
6773 * This position is post-layout, in addition to wherever the object's
6774 * layout placed it.
6775 *
6776 * @return The horizontal position of this view relative to its left position, in pixels.
6777 */
6778 public float getTranslationX() {
6779 return mTranslationX;
6780 }
6781
6782 /**
6783 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
6784 * This effectively positions the object post-layout, in addition to wherever the object's
6785 * layout placed it.
6786 *
6787 * @param translationX The horizontal position of this view relative to its left position,
6788 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006789 *
6790 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07006791 */
6792 public void setTranslationX(float translationX) {
6793 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006794 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006795 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006796 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006797 mTranslationX = translationX;
6798 mMatrixDirty = true;
6799 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006800 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006801 }
6802 }
6803
6804 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006805 * The horizontal location of this view relative to its {@link #getTop() top} position.
6806 * This position is post-layout, in addition to wherever the object's
6807 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006808 *
Chet Haasedf030d22010-07-30 17:22:38 -07006809 * @return The vertical position of this view relative to its top position,
6810 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006811 */
Chet Haasedf030d22010-07-30 17:22:38 -07006812 public float getTranslationY() {
6813 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07006814 }
6815
6816 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006817 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
6818 * This effectively positions the object post-layout, in addition to wherever the object's
6819 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006820 *
Chet Haasedf030d22010-07-30 17:22:38 -07006821 * @param translationY The vertical position of this view relative to its top position,
6822 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006823 *
6824 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07006825 */
Chet Haasedf030d22010-07-30 17:22:38 -07006826 public void setTranslationY(float translationY) {
6827 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006828 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006829 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006830 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006831 mTranslationY = translationY;
6832 mMatrixDirty = true;
6833 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006834 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006835 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006836 }
6837
6838 /**
Romain Guyda489792011-02-03 01:05:15 -08006839 * @hide
6840 */
Michael Jurkadece29f2011-02-03 01:41:49 -08006841 public void setFastTranslationX(float x) {
6842 mTranslationX = x;
6843 mMatrixDirty = true;
6844 }
6845
6846 /**
6847 * @hide
6848 */
6849 public void setFastTranslationY(float y) {
6850 mTranslationY = y;
6851 mMatrixDirty = true;
6852 }
6853
6854 /**
6855 * @hide
6856 */
Romain Guyda489792011-02-03 01:05:15 -08006857 public void setFastX(float x) {
6858 mTranslationX = x - mLeft;
6859 mMatrixDirty = true;
6860 }
6861
6862 /**
6863 * @hide
6864 */
6865 public void setFastY(float y) {
6866 mTranslationY = y - mTop;
6867 mMatrixDirty = true;
6868 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006869
Romain Guyda489792011-02-03 01:05:15 -08006870 /**
6871 * @hide
6872 */
6873 public void setFastScaleX(float x) {
6874 mScaleX = x;
6875 mMatrixDirty = true;
6876 }
6877
6878 /**
6879 * @hide
6880 */
6881 public void setFastScaleY(float y) {
6882 mScaleY = y;
6883 mMatrixDirty = true;
6884 }
6885
6886 /**
6887 * @hide
6888 */
6889 public void setFastAlpha(float alpha) {
6890 mAlpha = alpha;
6891 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006892
Romain Guyda489792011-02-03 01:05:15 -08006893 /**
6894 * @hide
6895 */
6896 public void setFastRotationY(float y) {
6897 mRotationY = y;
6898 mMatrixDirty = true;
6899 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006900
Romain Guyda489792011-02-03 01:05:15 -08006901 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006902 * Hit rectangle in parent's coordinates
6903 *
6904 * @param outRect The hit rectangle of the view.
6905 */
6906 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07006907 updateMatrix();
6908 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006909 outRect.set(mLeft, mTop, mRight, mBottom);
6910 } else {
6911 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07006912 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07006913 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07006914 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
6915 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07006916 }
6917 }
6918
6919 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07006920 * Determines whether the given point, in local coordinates is inside the view.
6921 */
6922 /*package*/ final boolean pointInView(float localX, float localY) {
6923 return localX >= 0 && localX < (mRight - mLeft)
6924 && localY >= 0 && localY < (mBottom - mTop);
6925 }
6926
6927 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006928 * Utility method to determine whether the given point, in local coordinates,
6929 * is inside the view, where the area of the view is expanded by the slop factor.
6930 * This method is called while processing touch-move events to determine if the event
6931 * is still within the view.
6932 */
6933 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07006934 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07006935 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006936 }
6937
6938 /**
6939 * When a view has focus and the user navigates away from it, the next view is searched for
6940 * starting from the rectangle filled in by this method.
6941 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006942 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
6943 * of the view. However, if your view maintains some idea of internal selection,
6944 * such as a cursor, or a selected row or column, you should override this method and
6945 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006946 *
6947 * @param r The rectangle to fill in, in this view's coordinates.
6948 */
6949 public void getFocusedRect(Rect r) {
6950 getDrawingRect(r);
6951 }
6952
6953 /**
6954 * If some part of this view is not clipped by any of its parents, then
6955 * return that area in r in global (root) coordinates. To convert r to local
6956 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
6957 * -globalOffset.y)) If the view is completely clipped or translated out,
6958 * return false.
6959 *
6960 * @param r If true is returned, r holds the global coordinates of the
6961 * visible portion of this view.
6962 * @param globalOffset If true is returned, globalOffset holds the dx,dy
6963 * between this view and its root. globalOffet may be null.
6964 * @return true if r is non-empty (i.e. part of the view is visible at the
6965 * root level.
6966 */
6967 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
6968 int width = mRight - mLeft;
6969 int height = mBottom - mTop;
6970 if (width > 0 && height > 0) {
6971 r.set(0, 0, width, height);
6972 if (globalOffset != null) {
6973 globalOffset.set(-mScrollX, -mScrollY);
6974 }
6975 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
6976 }
6977 return false;
6978 }
6979
6980 public final boolean getGlobalVisibleRect(Rect r) {
6981 return getGlobalVisibleRect(r, null);
6982 }
6983
6984 public final boolean getLocalVisibleRect(Rect r) {
6985 Point offset = new Point();
6986 if (getGlobalVisibleRect(r, offset)) {
6987 r.offset(-offset.x, -offset.y); // make r local
6988 return true;
6989 }
6990 return false;
6991 }
6992
6993 /**
6994 * Offset this view's vertical location by the specified number of pixels.
6995 *
6996 * @param offset the number of pixels to offset the view by
6997 */
6998 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006999 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007000 updateMatrix();
7001 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007002 final ViewParent p = mParent;
7003 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007004 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007005 int minTop;
7006 int maxBottom;
7007 int yLoc;
7008 if (offset < 0) {
7009 minTop = mTop + offset;
7010 maxBottom = mBottom;
7011 yLoc = offset;
7012 } else {
7013 minTop = mTop;
7014 maxBottom = mBottom + offset;
7015 yLoc = 0;
7016 }
7017 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
7018 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007019 }
7020 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007021 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007022 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007023
Chet Haasec3aa3612010-06-17 08:50:37 -07007024 mTop += offset;
7025 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007026
Chet Haasec3aa3612010-06-17 08:50:37 -07007027 if (!mMatrixIsIdentity) {
7028 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007029 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007030 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007031 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007032 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007033 }
7034
7035 /**
7036 * Offset this view's horizontal location by the specified amount of pixels.
7037 *
7038 * @param offset the numer of pixels to offset the view by
7039 */
7040 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007041 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007042 updateMatrix();
7043 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007044 final ViewParent p = mParent;
7045 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007046 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007047 int minLeft;
7048 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007049 if (offset < 0) {
7050 minLeft = mLeft + offset;
7051 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007052 } else {
7053 minLeft = mLeft;
7054 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007055 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007056 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07007057 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007058 }
7059 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007060 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007061 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007062
Chet Haasec3aa3612010-06-17 08:50:37 -07007063 mLeft += offset;
7064 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007065
Chet Haasec3aa3612010-06-17 08:50:37 -07007066 if (!mMatrixIsIdentity) {
7067 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007068 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007069 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007070 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007071 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007072 }
7073
7074 /**
7075 * Get the LayoutParams associated with this view. All views should have
7076 * layout parameters. These supply parameters to the <i>parent</i> of this
7077 * view specifying how it should be arranged. There are many subclasses of
7078 * ViewGroup.LayoutParams, and these correspond to the different subclasses
7079 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08007080 *
7081 * This method may return null if this View is not attached to a parent
7082 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
7083 * was not invoked successfully. When a View is attached to a parent
7084 * ViewGroup, this method must not return null.
7085 *
7086 * @return The LayoutParams associated with this view, or null if no
7087 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007088 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07007089 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007090 public ViewGroup.LayoutParams getLayoutParams() {
7091 return mLayoutParams;
7092 }
7093
7094 /**
7095 * Set the layout parameters associated with this view. These supply
7096 * parameters to the <i>parent</i> of this view specifying how it should be
7097 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
7098 * correspond to the different subclasses of ViewGroup that are responsible
7099 * for arranging their children.
7100 *
Romain Guy01c174b2011-02-22 11:51:06 -08007101 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007102 */
7103 public void setLayoutParams(ViewGroup.LayoutParams params) {
7104 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007105 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007106 }
7107 mLayoutParams = params;
7108 requestLayout();
7109 }
7110
7111 /**
7112 * Set the scrolled position of your view. This will cause a call to
7113 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7114 * invalidated.
7115 * @param x the x position to scroll to
7116 * @param y the y position to scroll to
7117 */
7118 public void scrollTo(int x, int y) {
7119 if (mScrollX != x || mScrollY != y) {
7120 int oldX = mScrollX;
7121 int oldY = mScrollY;
7122 mScrollX = x;
7123 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007124 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007125 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007126 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007127 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007129 }
7130 }
7131
7132 /**
7133 * Move the scrolled position of your view. This will cause a call to
7134 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7135 * invalidated.
7136 * @param x the amount of pixels to scroll by horizontally
7137 * @param y the amount of pixels to scroll by vertically
7138 */
7139 public void scrollBy(int x, int y) {
7140 scrollTo(mScrollX + x, mScrollY + y);
7141 }
7142
7143 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007144 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7145 * animation to fade the scrollbars out after a default delay. If a subclass
7146 * provides animated scrolling, the start delay should equal the duration
7147 * of the scrolling animation.</p>
7148 *
7149 * <p>The animation starts only if at least one of the scrollbars is
7150 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7151 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7152 * this method returns true, and false otherwise. If the animation is
7153 * started, this method calls {@link #invalidate()}; in that case the
7154 * caller should not call {@link #invalidate()}.</p>
7155 *
7156 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007157 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007158 *
7159 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7160 * and {@link #scrollTo(int, int)}.</p>
7161 *
7162 * @return true if the animation is played, false otherwise
7163 *
7164 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007165 * @see #scrollBy(int, int)
7166 * @see #scrollTo(int, int)
7167 * @see #isHorizontalScrollBarEnabled()
7168 * @see #isVerticalScrollBarEnabled()
7169 * @see #setHorizontalScrollBarEnabled(boolean)
7170 * @see #setVerticalScrollBarEnabled(boolean)
7171 */
7172 protected boolean awakenScrollBars() {
7173 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007174 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007175 }
7176
7177 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007178 * Trigger the scrollbars to draw.
7179 * This method differs from awakenScrollBars() only in its default duration.
7180 * initialAwakenScrollBars() will show the scroll bars for longer than
7181 * usual to give the user more of a chance to notice them.
7182 *
7183 * @return true if the animation is played, false otherwise.
7184 */
7185 private boolean initialAwakenScrollBars() {
7186 return mScrollCache != null &&
7187 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7188 }
7189
7190 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007191 * <p>
7192 * Trigger the scrollbars to draw. When invoked this method starts an
7193 * animation to fade the scrollbars out after a fixed delay. If a subclass
7194 * provides animated scrolling, the start delay should equal the duration of
7195 * the scrolling animation.
7196 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007197 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007198 * <p>
7199 * The animation starts only if at least one of the scrollbars is enabled,
7200 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7201 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7202 * this method returns true, and false otherwise. If the animation is
7203 * started, this method calls {@link #invalidate()}; in that case the caller
7204 * should not call {@link #invalidate()}.
7205 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007206 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007207 * <p>
7208 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007209 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007210 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007211 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007212 * @param startDelay the delay, in milliseconds, after which the animation
7213 * should start; when the delay is 0, the animation starts
7214 * immediately
7215 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007216 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007217 * @see #scrollBy(int, int)
7218 * @see #scrollTo(int, int)
7219 * @see #isHorizontalScrollBarEnabled()
7220 * @see #isVerticalScrollBarEnabled()
7221 * @see #setHorizontalScrollBarEnabled(boolean)
7222 * @see #setVerticalScrollBarEnabled(boolean)
7223 */
7224 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007225 return awakenScrollBars(startDelay, true);
7226 }
Joe Malin32736f02011-01-19 16:14:20 -08007227
Mike Cleron290947b2009-09-29 18:34:32 -07007228 /**
7229 * <p>
7230 * Trigger the scrollbars to draw. When invoked this method starts an
7231 * animation to fade the scrollbars out after a fixed delay. If a subclass
7232 * provides animated scrolling, the start delay should equal the duration of
7233 * the scrolling animation.
7234 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007235 *
Mike Cleron290947b2009-09-29 18:34:32 -07007236 * <p>
7237 * The animation starts only if at least one of the scrollbars is enabled,
7238 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7239 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7240 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08007241 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07007242 * is set to true; in that case the caller
7243 * should not call {@link #invalidate()}.
7244 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007245 *
Mike Cleron290947b2009-09-29 18:34:32 -07007246 * <p>
7247 * This method should be invoked everytime a subclass directly updates the
7248 * scroll parameters.
7249 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007250 *
Mike Cleron290947b2009-09-29 18:34:32 -07007251 * @param startDelay the delay, in milliseconds, after which the animation
7252 * should start; when the delay is 0, the animation starts
7253 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007254 *
Mike Cleron290947b2009-09-29 18:34:32 -07007255 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007256 *
Mike Cleron290947b2009-09-29 18:34:32 -07007257 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007258 *
Mike Cleron290947b2009-09-29 18:34:32 -07007259 * @see #scrollBy(int, int)
7260 * @see #scrollTo(int, int)
7261 * @see #isHorizontalScrollBarEnabled()
7262 * @see #isVerticalScrollBarEnabled()
7263 * @see #setHorizontalScrollBarEnabled(boolean)
7264 * @see #setVerticalScrollBarEnabled(boolean)
7265 */
7266 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007267 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007268
Mike Cleronf116bf82009-09-27 19:14:12 -07007269 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7270 return false;
7271 }
7272
7273 if (scrollCache.scrollBar == null) {
7274 scrollCache.scrollBar = new ScrollBarDrawable();
7275 }
7276
7277 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7278
Mike Cleron290947b2009-09-29 18:34:32 -07007279 if (invalidate) {
7280 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007281 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007282 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007283
7284 if (scrollCache.state == ScrollabilityCache.OFF) {
7285 // FIXME: this is copied from WindowManagerService.
7286 // We should get this value from the system when it
7287 // is possible to do so.
7288 final int KEY_REPEAT_FIRST_DELAY = 750;
7289 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7290 }
7291
7292 // Tell mScrollCache when we should start fading. This may
7293 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007294 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007295 scrollCache.fadeStartTime = fadeStartTime;
7296 scrollCache.state = ScrollabilityCache.ON;
7297
7298 // Schedule our fader to run, unscheduling any old ones first
7299 if (mAttachInfo != null) {
7300 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7301 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7302 }
7303
7304 return true;
7305 }
7306
7307 return false;
7308 }
7309
7310 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007311 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07007312 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
7313 * in the future. This must be called from a UI thread. To call from a non-UI
7314 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007315 *
7316 * WARNING: This method is destructive to dirty.
7317 * @param dirty the rectangle representing the bounds of the dirty region
7318 */
7319 public void invalidate(Rect dirty) {
7320 if (ViewDebug.TRACE_HIERARCHY) {
7321 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7322 }
7323
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007324 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007325 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7326 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007327 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007328 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007329 final ViewParent p = mParent;
7330 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007331 //noinspection PointlessBooleanExpression,ConstantConditions
7332 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7333 if (p != null && ai != null && ai.mHardwareAccelerated) {
7334 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007335 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007336 p.invalidateChild(this, null);
7337 return;
7338 }
Romain Guyaf636eb2010-12-09 17:47:21 -08007339 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007340 if (p != null && ai != null) {
7341 final int scrollX = mScrollX;
7342 final int scrollY = mScrollY;
7343 final Rect r = ai.mTmpInvalRect;
7344 r.set(dirty.left - scrollX, dirty.top - scrollY,
7345 dirty.right - scrollX, dirty.bottom - scrollY);
7346 mParent.invalidateChild(this, r);
7347 }
7348 }
7349 }
7350
7351 /**
7352 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
7353 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007354 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
7355 * will be called at some point in the future. This must be called from
7356 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007357 * @param l the left position of the dirty region
7358 * @param t the top position of the dirty region
7359 * @param r the right position of the dirty region
7360 * @param b the bottom position of the dirty region
7361 */
7362 public void invalidate(int l, int t, int r, int b) {
7363 if (ViewDebug.TRACE_HIERARCHY) {
7364 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7365 }
7366
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007367 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007368 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7369 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007370 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007371 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007372 final ViewParent p = mParent;
7373 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007374 //noinspection PointlessBooleanExpression,ConstantConditions
7375 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7376 if (p != null && ai != null && ai.mHardwareAccelerated) {
7377 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007378 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007379 p.invalidateChild(this, null);
7380 return;
7381 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007383 if (p != null && ai != null && l < r && t < b) {
7384 final int scrollX = mScrollX;
7385 final int scrollY = mScrollY;
7386 final Rect tmpr = ai.mTmpInvalRect;
7387 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
7388 p.invalidateChild(this, tmpr);
7389 }
7390 }
7391 }
7392
7393 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07007394 * Invalidate the whole view. If the view is visible,
7395 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
7396 * the future. This must be called from a UI thread. To call from a non-UI thread,
7397 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007398 */
7399 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07007400 invalidate(true);
7401 }
Joe Malin32736f02011-01-19 16:14:20 -08007402
Chet Haaseed032702010-10-01 14:05:54 -07007403 /**
7404 * This is where the invalidate() work actually happens. A full invalidate()
7405 * causes the drawing cache to be invalidated, but this function can be called with
7406 * invalidateCache set to false to skip that invalidation step for cases that do not
7407 * need it (for example, a component that remains at the same dimensions with the same
7408 * content).
7409 *
7410 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
7411 * well. This is usually true for a full invalidate, but may be set to false if the
7412 * View's contents or dimensions have not changed.
7413 */
Romain Guy849d0a32011-02-01 17:20:48 -08007414 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007415 if (ViewDebug.TRACE_HIERARCHY) {
7416 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7417 }
7418
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007419 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08007420 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08007421 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
7422 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07007423 mPrivateFlags &= ~DRAWN;
7424 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08007425 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07007426 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007428 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07007429 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08007430 //noinspection PointlessBooleanExpression,ConstantConditions
7431 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7432 if (p != null && ai != null && ai.mHardwareAccelerated) {
7433 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007434 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007435 p.invalidateChild(this, null);
7436 return;
7437 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007438 }
Michael Jurkaebefea42010-11-15 16:04:01 -08007439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007440 if (p != null && ai != null) {
7441 final Rect r = ai.mTmpInvalRect;
7442 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7443 // Don't call invalidate -- we don't want to internally scroll
7444 // our own bounds
7445 p.invalidateChild(this, r);
7446 }
7447 }
7448 }
7449
7450 /**
Romain Guyda489792011-02-03 01:05:15 -08007451 * @hide
7452 */
7453 public void fastInvalidate() {
7454 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
7455 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7456 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
7457 if (mParent instanceof View) {
7458 ((View) mParent).mPrivateFlags |= INVALIDATED;
7459 }
7460 mPrivateFlags &= ~DRAWN;
7461 mPrivateFlags |= INVALIDATED;
7462 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07007463 if (mParent != null && mAttachInfo != null) {
7464 if (mAttachInfo.mHardwareAccelerated) {
7465 mParent.invalidateChild(this, null);
7466 } else {
7467 final Rect r = mAttachInfo.mTmpInvalRect;
7468 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7469 // Don't call invalidate -- we don't want to internally scroll
7470 // our own bounds
7471 mParent.invalidateChild(this, r);
7472 }
Romain Guyda489792011-02-03 01:05:15 -08007473 }
7474 }
7475 }
7476
7477 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08007478 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08007479 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7480 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08007481 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
7482 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08007483 *
7484 * @hide
7485 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08007486 protected void invalidateParentCaches() {
7487 if (mParent instanceof View) {
7488 ((View) mParent).mPrivateFlags |= INVALIDATED;
7489 }
7490 }
Joe Malin32736f02011-01-19 16:14:20 -08007491
Romain Guy0fd89bf2011-01-26 15:41:30 -08007492 /**
7493 * Used to indicate that the parent of this view should be invalidated. This functionality
7494 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7495 * which is necessary when various parent-managed properties of the view change, such as
7496 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
7497 * an invalidation event to the parent.
7498 *
7499 * @hide
7500 */
7501 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08007502 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007503 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08007504 }
7505 }
7506
7507 /**
Romain Guy24443ea2009-05-11 11:56:30 -07007508 * Indicates whether this View is opaque. An opaque View guarantees that it will
7509 * draw all the pixels overlapping its bounds using a fully opaque color.
7510 *
7511 * Subclasses of View should override this method whenever possible to indicate
7512 * whether an instance is opaque. Opaque Views are treated in a special way by
7513 * the View hierarchy, possibly allowing it to perform optimizations during
7514 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07007515 *
Romain Guy24443ea2009-05-11 11:56:30 -07007516 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07007517 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007518 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07007519 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07007520 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
7521 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07007522 }
7523
Adam Powell20232d02010-12-08 21:08:53 -08007524 /**
7525 * @hide
7526 */
7527 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07007528 // Opaque if:
7529 // - Has a background
7530 // - Background is opaque
7531 // - Doesn't have scrollbars or scrollbars are inside overlay
7532
7533 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
7534 mPrivateFlags |= OPAQUE_BACKGROUND;
7535 } else {
7536 mPrivateFlags &= ~OPAQUE_BACKGROUND;
7537 }
7538
7539 final int flags = mViewFlags;
7540 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
7541 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
7542 mPrivateFlags |= OPAQUE_SCROLLBARS;
7543 } else {
7544 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
7545 }
7546 }
7547
7548 /**
7549 * @hide
7550 */
7551 protected boolean hasOpaqueScrollbars() {
7552 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07007553 }
7554
7555 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007556 * @return A handler associated with the thread running the View. This
7557 * handler can be used to pump events in the UI events queue.
7558 */
7559 public Handler getHandler() {
7560 if (mAttachInfo != null) {
7561 return mAttachInfo.mHandler;
7562 }
7563 return null;
7564 }
7565
7566 /**
7567 * Causes the Runnable to be added to the message queue.
7568 * The runnable will be run on the user interface thread.
7569 *
7570 * @param action The Runnable that will be executed.
7571 *
7572 * @return Returns true if the Runnable was successfully placed in to the
7573 * message queue. Returns false on failure, usually because the
7574 * looper processing the message queue is exiting.
7575 */
7576 public boolean post(Runnable action) {
7577 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007578 AttachInfo attachInfo = mAttachInfo;
7579 if (attachInfo != null) {
7580 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007581 } else {
7582 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007583 ViewAncestor.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007584 return true;
7585 }
7586
7587 return handler.post(action);
7588 }
7589
7590 /**
7591 * Causes the Runnable to be added to the message queue, to be run
7592 * after the specified amount of time elapses.
7593 * The runnable will be run on the user interface thread.
7594 *
7595 * @param action The Runnable that will be executed.
7596 * @param delayMillis The delay (in milliseconds) until the Runnable
7597 * will be executed.
7598 *
7599 * @return true if the Runnable was successfully placed in to the
7600 * message queue. Returns false on failure, usually because the
7601 * looper processing the message queue is exiting. Note that a
7602 * result of true does not mean the Runnable will be processed --
7603 * if the looper is quit before the delivery time of the message
7604 * occurs then the message will be dropped.
7605 */
7606 public boolean postDelayed(Runnable action, long delayMillis) {
7607 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007608 AttachInfo attachInfo = mAttachInfo;
7609 if (attachInfo != null) {
7610 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007611 } else {
7612 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007613 ViewAncestor.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007614 return true;
7615 }
7616
7617 return handler.postDelayed(action, delayMillis);
7618 }
7619
7620 /**
7621 * Removes the specified Runnable from the message queue.
7622 *
7623 * @param action The Runnable to remove from the message handling queue
7624 *
7625 * @return true if this view could ask the Handler to remove the Runnable,
7626 * false otherwise. When the returned value is true, the Runnable
7627 * may or may not have been actually removed from the message queue
7628 * (for instance, if the Runnable was not in the queue already.)
7629 */
7630 public boolean removeCallbacks(Runnable action) {
7631 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007632 AttachInfo attachInfo = mAttachInfo;
7633 if (attachInfo != null) {
7634 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007635 } else {
7636 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007637 ViewAncestor.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007638 return true;
7639 }
7640
7641 handler.removeCallbacks(action);
7642 return true;
7643 }
7644
7645 /**
7646 * Cause an invalidate to happen on a subsequent cycle through the event loop.
7647 * Use this to invalidate the View from a non-UI thread.
7648 *
7649 * @see #invalidate()
7650 */
7651 public void postInvalidate() {
7652 postInvalidateDelayed(0);
7653 }
7654
7655 /**
7656 * Cause an invalidate of the specified area to happen on a subsequent cycle
7657 * through the event loop. Use this to invalidate the View from a non-UI thread.
7658 *
7659 * @param left The left coordinate of the rectangle to invalidate.
7660 * @param top The top coordinate of the rectangle to invalidate.
7661 * @param right The right coordinate of the rectangle to invalidate.
7662 * @param bottom The bottom coordinate of the rectangle to invalidate.
7663 *
7664 * @see #invalidate(int, int, int, int)
7665 * @see #invalidate(Rect)
7666 */
7667 public void postInvalidate(int left, int top, int right, int bottom) {
7668 postInvalidateDelayed(0, left, top, right, bottom);
7669 }
7670
7671 /**
7672 * Cause an invalidate to happen on a subsequent cycle through the event
7673 * loop. Waits for the specified amount of time.
7674 *
7675 * @param delayMilliseconds the duration in milliseconds to delay the
7676 * invalidation by
7677 */
7678 public void postInvalidateDelayed(long delayMilliseconds) {
7679 // We try only with the AttachInfo because there's no point in invalidating
7680 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07007681 AttachInfo attachInfo = mAttachInfo;
7682 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007683 Message msg = Message.obtain();
7684 msg.what = AttachInfo.INVALIDATE_MSG;
7685 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07007686 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007687 }
7688 }
7689
7690 /**
7691 * Cause an invalidate of the specified area to happen on a subsequent cycle
7692 * through the event loop. Waits for the specified amount of time.
7693 *
7694 * @param delayMilliseconds the duration in milliseconds to delay the
7695 * invalidation by
7696 * @param left The left coordinate of the rectangle to invalidate.
7697 * @param top The top coordinate of the rectangle to invalidate.
7698 * @param right The right coordinate of the rectangle to invalidate.
7699 * @param bottom The bottom coordinate of the rectangle to invalidate.
7700 */
7701 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
7702 int right, int bottom) {
7703
7704 // We try only with the AttachInfo because there's no point in invalidating
7705 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07007706 AttachInfo attachInfo = mAttachInfo;
7707 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007708 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
7709 info.target = this;
7710 info.left = left;
7711 info.top = top;
7712 info.right = right;
7713 info.bottom = bottom;
7714
7715 final Message msg = Message.obtain();
7716 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
7717 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07007718 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007719 }
7720 }
7721
7722 /**
7723 * Called by a parent to request that a child update its values for mScrollX
7724 * and mScrollY if necessary. This will typically be done if the child is
7725 * animating a scroll using a {@link android.widget.Scroller Scroller}
7726 * object.
7727 */
7728 public void computeScroll() {
7729 }
7730
7731 /**
7732 * <p>Indicate whether the horizontal edges are faded when the view is
7733 * scrolled horizontally.</p>
7734 *
7735 * @return true if the horizontal edges should are faded on scroll, false
7736 * otherwise
7737 *
7738 * @see #setHorizontalFadingEdgeEnabled(boolean)
7739 * @attr ref android.R.styleable#View_fadingEdge
7740 */
7741 public boolean isHorizontalFadingEdgeEnabled() {
7742 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
7743 }
7744
7745 /**
7746 * <p>Define whether the horizontal edges should be faded when this view
7747 * is scrolled horizontally.</p>
7748 *
7749 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
7750 * be faded when the view is scrolled
7751 * horizontally
7752 *
7753 * @see #isHorizontalFadingEdgeEnabled()
7754 * @attr ref android.R.styleable#View_fadingEdge
7755 */
7756 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
7757 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
7758 if (horizontalFadingEdgeEnabled) {
7759 initScrollCache();
7760 }
7761
7762 mViewFlags ^= FADING_EDGE_HORIZONTAL;
7763 }
7764 }
7765
7766 /**
7767 * <p>Indicate whether the vertical edges are faded when the view is
7768 * scrolled horizontally.</p>
7769 *
7770 * @return true if the vertical edges should are faded on scroll, false
7771 * otherwise
7772 *
7773 * @see #setVerticalFadingEdgeEnabled(boolean)
7774 * @attr ref android.R.styleable#View_fadingEdge
7775 */
7776 public boolean isVerticalFadingEdgeEnabled() {
7777 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
7778 }
7779
7780 /**
7781 * <p>Define whether the vertical edges should be faded when this view
7782 * is scrolled vertically.</p>
7783 *
7784 * @param verticalFadingEdgeEnabled true if the vertical edges should
7785 * be faded when the view is scrolled
7786 * vertically
7787 *
7788 * @see #isVerticalFadingEdgeEnabled()
7789 * @attr ref android.R.styleable#View_fadingEdge
7790 */
7791 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
7792 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
7793 if (verticalFadingEdgeEnabled) {
7794 initScrollCache();
7795 }
7796
7797 mViewFlags ^= FADING_EDGE_VERTICAL;
7798 }
7799 }
7800
7801 /**
7802 * Returns the strength, or intensity, of the top faded edge. The strength is
7803 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7804 * returns 0.0 or 1.0 but no value in between.
7805 *
7806 * Subclasses should override this method to provide a smoother fade transition
7807 * when scrolling occurs.
7808 *
7809 * @return the intensity of the top fade as a float between 0.0f and 1.0f
7810 */
7811 protected float getTopFadingEdgeStrength() {
7812 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
7813 }
7814
7815 /**
7816 * Returns the strength, or intensity, of the bottom faded edge. The strength is
7817 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7818 * returns 0.0 or 1.0 but no value in between.
7819 *
7820 * Subclasses should override this method to provide a smoother fade transition
7821 * when scrolling occurs.
7822 *
7823 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
7824 */
7825 protected float getBottomFadingEdgeStrength() {
7826 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
7827 computeVerticalScrollRange() ? 1.0f : 0.0f;
7828 }
7829
7830 /**
7831 * Returns the strength, or intensity, of the left faded edge. The strength is
7832 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7833 * returns 0.0 or 1.0 but no value in between.
7834 *
7835 * Subclasses should override this method to provide a smoother fade transition
7836 * when scrolling occurs.
7837 *
7838 * @return the intensity of the left fade as a float between 0.0f and 1.0f
7839 */
7840 protected float getLeftFadingEdgeStrength() {
7841 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
7842 }
7843
7844 /**
7845 * Returns the strength, or intensity, of the right faded edge. The strength is
7846 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7847 * returns 0.0 or 1.0 but no value in between.
7848 *
7849 * Subclasses should override this method to provide a smoother fade transition
7850 * when scrolling occurs.
7851 *
7852 * @return the intensity of the right fade as a float between 0.0f and 1.0f
7853 */
7854 protected float getRightFadingEdgeStrength() {
7855 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
7856 computeHorizontalScrollRange() ? 1.0f : 0.0f;
7857 }
7858
7859 /**
7860 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
7861 * scrollbar is not drawn by default.</p>
7862 *
7863 * @return true if the horizontal scrollbar should be painted, false
7864 * otherwise
7865 *
7866 * @see #setHorizontalScrollBarEnabled(boolean)
7867 */
7868 public boolean isHorizontalScrollBarEnabled() {
7869 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
7870 }
7871
7872 /**
7873 * <p>Define whether the horizontal scrollbar should be drawn or not. The
7874 * scrollbar is not drawn by default.</p>
7875 *
7876 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
7877 * be painted
7878 *
7879 * @see #isHorizontalScrollBarEnabled()
7880 */
7881 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
7882 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
7883 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007884 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007885 recomputePadding();
7886 }
7887 }
7888
7889 /**
7890 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
7891 * scrollbar is not drawn by default.</p>
7892 *
7893 * @return true if the vertical scrollbar should be painted, false
7894 * otherwise
7895 *
7896 * @see #setVerticalScrollBarEnabled(boolean)
7897 */
7898 public boolean isVerticalScrollBarEnabled() {
7899 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
7900 }
7901
7902 /**
7903 * <p>Define whether the vertical scrollbar should be drawn or not. The
7904 * scrollbar is not drawn by default.</p>
7905 *
7906 * @param verticalScrollBarEnabled true if the vertical scrollbar should
7907 * be painted
7908 *
7909 * @see #isVerticalScrollBarEnabled()
7910 */
7911 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
7912 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
7913 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007914 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007915 recomputePadding();
7916 }
7917 }
7918
Adam Powell20232d02010-12-08 21:08:53 -08007919 /**
7920 * @hide
7921 */
7922 protected void recomputePadding() {
7923 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007924 }
Joe Malin32736f02011-01-19 16:14:20 -08007925
Mike Cleronfe81d382009-09-28 14:22:16 -07007926 /**
7927 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08007928 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007929 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08007930 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007931 */
7932 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
7933 initScrollCache();
7934 final ScrollabilityCache scrollabilityCache = mScrollCache;
7935 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007936 if (fadeScrollbars) {
7937 scrollabilityCache.state = ScrollabilityCache.OFF;
7938 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07007939 scrollabilityCache.state = ScrollabilityCache.ON;
7940 }
7941 }
Joe Malin32736f02011-01-19 16:14:20 -08007942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007943 /**
Joe Malin32736f02011-01-19 16:14:20 -08007944 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007945 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08007946 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007947 * @return true if scrollbar fading is enabled
7948 */
7949 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08007950 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007951 }
Joe Malin32736f02011-01-19 16:14:20 -08007952
Mike Cleron52f0a642009-09-28 18:21:37 -07007953 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007954 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
7955 * inset. When inset, they add to the padding of the view. And the scrollbars
7956 * can be drawn inside the padding area or on the edge of the view. For example,
7957 * if a view has a background drawable and you want to draw the scrollbars
7958 * inside the padding specified by the drawable, you can use
7959 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
7960 * appear at the edge of the view, ignoring the padding, then you can use
7961 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
7962 * @param style the style of the scrollbars. Should be one of
7963 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
7964 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
7965 * @see #SCROLLBARS_INSIDE_OVERLAY
7966 * @see #SCROLLBARS_INSIDE_INSET
7967 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7968 * @see #SCROLLBARS_OUTSIDE_INSET
7969 */
7970 public void setScrollBarStyle(int style) {
7971 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
7972 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07007973 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007974 recomputePadding();
7975 }
7976 }
7977
7978 /**
7979 * <p>Returns the current scrollbar style.</p>
7980 * @return the current scrollbar style
7981 * @see #SCROLLBARS_INSIDE_OVERLAY
7982 * @see #SCROLLBARS_INSIDE_INSET
7983 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7984 * @see #SCROLLBARS_OUTSIDE_INSET
7985 */
7986 public int getScrollBarStyle() {
7987 return mViewFlags & SCROLLBARS_STYLE_MASK;
7988 }
7989
7990 /**
7991 * <p>Compute the horizontal range that the horizontal scrollbar
7992 * represents.</p>
7993 *
7994 * <p>The range is expressed in arbitrary units that must be the same as the
7995 * units used by {@link #computeHorizontalScrollExtent()} and
7996 * {@link #computeHorizontalScrollOffset()}.</p>
7997 *
7998 * <p>The default range is the drawing width of this view.</p>
7999 *
8000 * @return the total horizontal range represented by the horizontal
8001 * scrollbar
8002 *
8003 * @see #computeHorizontalScrollExtent()
8004 * @see #computeHorizontalScrollOffset()
8005 * @see android.widget.ScrollBarDrawable
8006 */
8007 protected int computeHorizontalScrollRange() {
8008 return getWidth();
8009 }
8010
8011 /**
8012 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
8013 * within the horizontal range. This value is used to compute the position
8014 * of the thumb within the scrollbar's track.</p>
8015 *
8016 * <p>The range is expressed in arbitrary units that must be the same as the
8017 * units used by {@link #computeHorizontalScrollRange()} and
8018 * {@link #computeHorizontalScrollExtent()}.</p>
8019 *
8020 * <p>The default offset is the scroll offset of this view.</p>
8021 *
8022 * @return the horizontal offset of the scrollbar's thumb
8023 *
8024 * @see #computeHorizontalScrollRange()
8025 * @see #computeHorizontalScrollExtent()
8026 * @see android.widget.ScrollBarDrawable
8027 */
8028 protected int computeHorizontalScrollOffset() {
8029 return mScrollX;
8030 }
8031
8032 /**
8033 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
8034 * within the horizontal range. This value is used to compute the length
8035 * of the thumb within the scrollbar's track.</p>
8036 *
8037 * <p>The range is expressed in arbitrary units that must be the same as the
8038 * units used by {@link #computeHorizontalScrollRange()} and
8039 * {@link #computeHorizontalScrollOffset()}.</p>
8040 *
8041 * <p>The default extent is the drawing width of this view.</p>
8042 *
8043 * @return the horizontal extent of the scrollbar's thumb
8044 *
8045 * @see #computeHorizontalScrollRange()
8046 * @see #computeHorizontalScrollOffset()
8047 * @see android.widget.ScrollBarDrawable
8048 */
8049 protected int computeHorizontalScrollExtent() {
8050 return getWidth();
8051 }
8052
8053 /**
8054 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
8055 *
8056 * <p>The range is expressed in arbitrary units that must be the same as the
8057 * units used by {@link #computeVerticalScrollExtent()} and
8058 * {@link #computeVerticalScrollOffset()}.</p>
8059 *
8060 * @return the total vertical range represented by the vertical scrollbar
8061 *
8062 * <p>The default range is the drawing height of this view.</p>
8063 *
8064 * @see #computeVerticalScrollExtent()
8065 * @see #computeVerticalScrollOffset()
8066 * @see android.widget.ScrollBarDrawable
8067 */
8068 protected int computeVerticalScrollRange() {
8069 return getHeight();
8070 }
8071
8072 /**
8073 * <p>Compute the vertical offset of the vertical scrollbar's thumb
8074 * within the horizontal range. This value is used to compute the position
8075 * of the thumb within the scrollbar's track.</p>
8076 *
8077 * <p>The range is expressed in arbitrary units that must be the same as the
8078 * units used by {@link #computeVerticalScrollRange()} and
8079 * {@link #computeVerticalScrollExtent()}.</p>
8080 *
8081 * <p>The default offset is the scroll offset of this view.</p>
8082 *
8083 * @return the vertical offset of the scrollbar's thumb
8084 *
8085 * @see #computeVerticalScrollRange()
8086 * @see #computeVerticalScrollExtent()
8087 * @see android.widget.ScrollBarDrawable
8088 */
8089 protected int computeVerticalScrollOffset() {
8090 return mScrollY;
8091 }
8092
8093 /**
8094 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
8095 * within the vertical range. This value is used to compute the length
8096 * of the thumb within the scrollbar's track.</p>
8097 *
8098 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08008099 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008100 * {@link #computeVerticalScrollOffset()}.</p>
8101 *
8102 * <p>The default extent is the drawing height of this view.</p>
8103 *
8104 * @return the vertical extent of the scrollbar's thumb
8105 *
8106 * @see #computeVerticalScrollRange()
8107 * @see #computeVerticalScrollOffset()
8108 * @see android.widget.ScrollBarDrawable
8109 */
8110 protected int computeVerticalScrollExtent() {
8111 return getHeight();
8112 }
8113
8114 /**
8115 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8116 * scrollbars are painted only if they have been awakened first.</p>
8117 *
8118 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008119 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008120 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008121 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008122 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008123 // scrollbars are drawn only when the animation is running
8124 final ScrollabilityCache cache = mScrollCache;
8125 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008126
Mike Cleronf116bf82009-09-27 19:14:12 -07008127 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008128
Mike Cleronf116bf82009-09-27 19:14:12 -07008129 if (state == ScrollabilityCache.OFF) {
8130 return;
8131 }
Joe Malin32736f02011-01-19 16:14:20 -08008132
Mike Cleronf116bf82009-09-27 19:14:12 -07008133 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008134
Mike Cleronf116bf82009-09-27 19:14:12 -07008135 if (state == ScrollabilityCache.FADING) {
8136 // We're fading -- get our fade interpolation
8137 if (cache.interpolatorValues == null) {
8138 cache.interpolatorValues = new float[1];
8139 }
Joe Malin32736f02011-01-19 16:14:20 -08008140
Mike Cleronf116bf82009-09-27 19:14:12 -07008141 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008142
Mike Cleronf116bf82009-09-27 19:14:12 -07008143 // Stops the animation if we're done
8144 if (cache.scrollBarInterpolator.timeToValues(values) ==
8145 Interpolator.Result.FREEZE_END) {
8146 cache.state = ScrollabilityCache.OFF;
8147 } else {
8148 cache.scrollBar.setAlpha(Math.round(values[0]));
8149 }
Joe Malin32736f02011-01-19 16:14:20 -08008150
8151 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008152 // drawing. We only want this when we're fading so that
8153 // we prevent excessive redraws
8154 invalidate = true;
8155 } else {
8156 // We're just on -- but we may have been fading before so
8157 // reset alpha
8158 cache.scrollBar.setAlpha(255);
8159 }
8160
Joe Malin32736f02011-01-19 16:14:20 -08008161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008162 final int viewFlags = mViewFlags;
8163
8164 final boolean drawHorizontalScrollBar =
8165 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8166 final boolean drawVerticalScrollBar =
8167 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8168 && !isVerticalScrollBarHidden();
8169
8170 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8171 final int width = mRight - mLeft;
8172 final int height = mBottom - mTop;
8173
8174 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008175
Mike Reede8853fc2009-09-04 14:01:48 -04008176 final int scrollX = mScrollX;
8177 final int scrollY = mScrollY;
8178 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8179
Mike Cleronf116bf82009-09-27 19:14:12 -07008180 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008182 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008183 int size = scrollBar.getSize(false);
8184 if (size <= 0) {
8185 size = cache.scrollBarSize;
8186 }
8187
Mike Cleronf116bf82009-09-27 19:14:12 -07008188 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008189 computeHorizontalScrollOffset(),
8190 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008191 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008192 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008193 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008194 left = scrollX + (mPaddingLeft & inside);
8195 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8196 bottom = top + size;
8197 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8198 if (invalidate) {
8199 invalidate(left, top, right, bottom);
8200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008201 }
8202
8203 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008204 int size = scrollBar.getSize(true);
8205 if (size <= 0) {
8206 size = cache.scrollBarSize;
8207 }
8208
Mike Reede8853fc2009-09-04 14:01:48 -04008209 scrollBar.setParameters(computeVerticalScrollRange(),
8210 computeVerticalScrollOffset(),
8211 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008212 switch (mVerticalScrollbarPosition) {
8213 default:
8214 case SCROLLBAR_POSITION_DEFAULT:
8215 case SCROLLBAR_POSITION_RIGHT:
8216 left = scrollX + width - size - (mUserPaddingRight & inside);
8217 break;
8218 case SCROLLBAR_POSITION_LEFT:
8219 left = scrollX + (mUserPaddingLeft & inside);
8220 break;
8221 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008222 top = scrollY + (mPaddingTop & inside);
8223 right = left + size;
8224 bottom = scrollY + height - (mUserPaddingBottom & inside);
8225 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8226 if (invalidate) {
8227 invalidate(left, top, right, bottom);
8228 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008229 }
8230 }
8231 }
8232 }
Romain Guy8506ab42009-06-11 17:35:47 -07008233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008234 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008235 * 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 -08008236 * FastScroller is visible.
8237 * @return whether to temporarily hide the vertical scrollbar
8238 * @hide
8239 */
8240 protected boolean isVerticalScrollBarHidden() {
8241 return false;
8242 }
8243
8244 /**
8245 * <p>Draw the horizontal scrollbar if
8246 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
8247 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008248 * @param canvas the canvas on which to draw the scrollbar
8249 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008250 *
8251 * @see #isHorizontalScrollBarEnabled()
8252 * @see #computeHorizontalScrollRange()
8253 * @see #computeHorizontalScrollExtent()
8254 * @see #computeHorizontalScrollOffset()
8255 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008256 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008257 */
Romain Guy8fb95422010-08-17 18:38:51 -07008258 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8259 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008260 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008261 scrollBar.draw(canvas);
8262 }
Mike Reede8853fc2009-09-04 14:01:48 -04008263
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008264 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008265 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8266 * returns true.</p>
8267 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008268 * @param canvas the canvas on which to draw the scrollbar
8269 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008270 *
8271 * @see #isVerticalScrollBarEnabled()
8272 * @see #computeVerticalScrollRange()
8273 * @see #computeVerticalScrollExtent()
8274 * @see #computeVerticalScrollOffset()
8275 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008276 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008277 */
Romain Guy8fb95422010-08-17 18:38:51 -07008278 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
8279 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04008280 scrollBar.setBounds(l, t, r, b);
8281 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008282 }
8283
8284 /**
8285 * Implement this to do your drawing.
8286 *
8287 * @param canvas the canvas on which the background will be drawn
8288 */
8289 protected void onDraw(Canvas canvas) {
8290 }
8291
8292 /*
8293 * Caller is responsible for calling requestLayout if necessary.
8294 * (This allows addViewInLayout to not request a new layout.)
8295 */
8296 void assignParent(ViewParent parent) {
8297 if (mParent == null) {
8298 mParent = parent;
8299 } else if (parent == null) {
8300 mParent = null;
8301 } else {
8302 throw new RuntimeException("view " + this + " being added, but"
8303 + " it already has a parent");
8304 }
8305 }
8306
8307 /**
8308 * This is called when the view is attached to a window. At this point it
8309 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008310 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
8311 * however it may be called any time before the first onDraw -- including
8312 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008313 *
8314 * @see #onDetachedFromWindow()
8315 */
8316 protected void onAttachedToWindow() {
8317 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
8318 mParent.requestTransparentRegion(this);
8319 }
Adam Powell8568c3a2010-04-19 14:26:11 -07008320 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
8321 initialAwakenScrollBars();
8322 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
8323 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08008324 jumpDrawablesToCurrentState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008325 }
8326
8327 /**
8328 * This is called when the view is detached from a window. At this point it
8329 * no longer has a surface for drawing.
8330 *
8331 * @see #onAttachedToWindow()
8332 */
8333 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08008334 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08008335
Romain Guya440b002010-02-24 15:57:54 -08008336 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05008337 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08008338 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08008339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008340 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08008341
8342 if (mHardwareLayer != null) {
8343 mHardwareLayer.destroy();
8344 mHardwareLayer = null;
8345 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008346
Chet Haasedaf98e92011-01-10 14:10:36 -08008347 if (mDisplayList != null) {
8348 mDisplayList.invalidate();
8349 }
8350
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008351 if (mAttachInfo != null) {
8352 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
8353 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
8354 }
8355
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08008356 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008357 }
8358
8359 /**
8360 * @return The number of times this view has been attached to a window
8361 */
8362 protected int getWindowAttachCount() {
8363 return mWindowAttachCount;
8364 }
8365
8366 /**
8367 * Retrieve a unique token identifying the window this view is attached to.
8368 * @return Return the window's token for use in
8369 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
8370 */
8371 public IBinder getWindowToken() {
8372 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
8373 }
8374
8375 /**
8376 * Retrieve a unique token identifying the top-level "real" window of
8377 * the window that this view is attached to. That is, this is like
8378 * {@link #getWindowToken}, except if the window this view in is a panel
8379 * window (attached to another containing window), then the token of
8380 * the containing window is returned instead.
8381 *
8382 * @return Returns the associated window token, either
8383 * {@link #getWindowToken()} or the containing window's token.
8384 */
8385 public IBinder getApplicationWindowToken() {
8386 AttachInfo ai = mAttachInfo;
8387 if (ai != null) {
8388 IBinder appWindowToken = ai.mPanelParentWindowToken;
8389 if (appWindowToken == null) {
8390 appWindowToken = ai.mWindowToken;
8391 }
8392 return appWindowToken;
8393 }
8394 return null;
8395 }
8396
8397 /**
8398 * Retrieve private session object this view hierarchy is using to
8399 * communicate with the window manager.
8400 * @return the session object to communicate with the window manager
8401 */
8402 /*package*/ IWindowSession getWindowSession() {
8403 return mAttachInfo != null ? mAttachInfo.mSession : null;
8404 }
8405
8406 /**
8407 * @param info the {@link android.view.View.AttachInfo} to associated with
8408 * this view
8409 */
8410 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
8411 //System.out.println("Attached! " + this);
8412 mAttachInfo = info;
8413 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008414 // We will need to evaluate the drawable state at least once.
8415 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008416 if (mFloatingTreeObserver != null) {
8417 info.mTreeObserver.merge(mFloatingTreeObserver);
8418 mFloatingTreeObserver = null;
8419 }
8420 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
8421 mAttachInfo.mScrollContainers.add(this);
8422 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
8423 }
8424 performCollectViewAttributes(visibility);
8425 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08008426
8427 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8428 mOnAttachStateChangeListeners;
8429 if (listeners != null && listeners.size() > 0) {
8430 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8431 // perform the dispatching. The iterator is a safe guard against listeners that
8432 // could mutate the list by calling the various add/remove methods. This prevents
8433 // the array from being modified while we iterate it.
8434 for (OnAttachStateChangeListener listener : listeners) {
8435 listener.onViewAttachedToWindow(this);
8436 }
8437 }
8438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008439 int vis = info.mWindowVisibility;
8440 if (vis != GONE) {
8441 onWindowVisibilityChanged(vis);
8442 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008443 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
8444 // If nobody has evaluated the drawable state yet, then do it now.
8445 refreshDrawableState();
8446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008447 }
8448
8449 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008450 AttachInfo info = mAttachInfo;
8451 if (info != null) {
8452 int vis = info.mWindowVisibility;
8453 if (vis != GONE) {
8454 onWindowVisibilityChanged(GONE);
8455 }
8456 }
8457
8458 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08008459
Adam Powell4afd62b2011-02-18 15:02:18 -08008460 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8461 mOnAttachStateChangeListeners;
8462 if (listeners != null && listeners.size() > 0) {
8463 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8464 // perform the dispatching. The iterator is a safe guard against listeners that
8465 // could mutate the list by calling the various add/remove methods. This prevents
8466 // the array from being modified while we iterate it.
8467 for (OnAttachStateChangeListener listener : listeners) {
8468 listener.onViewDetachedFromWindow(this);
8469 }
8470 }
8471
Romain Guy01d5edc2011-01-28 11:28:53 -08008472 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008473 mAttachInfo.mScrollContainers.remove(this);
8474 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
8475 }
Romain Guy01d5edc2011-01-28 11:28:53 -08008476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008477 mAttachInfo = null;
8478 }
8479
8480 /**
8481 * Store this view hierarchy's frozen state into the given container.
8482 *
8483 * @param container The SparseArray in which to save the view's state.
8484 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008485 * @see #restoreHierarchyState(android.util.SparseArray)
8486 * @see #dispatchSaveInstanceState(android.util.SparseArray)
8487 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008488 */
8489 public void saveHierarchyState(SparseArray<Parcelable> container) {
8490 dispatchSaveInstanceState(container);
8491 }
8492
8493 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008494 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
8495 * this view and its children. May be overridden to modify how freezing happens to a
8496 * 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 -08008497 *
8498 * @param container The SparseArray in which to save the view's state.
8499 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008500 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
8501 * @see #saveHierarchyState(android.util.SparseArray)
8502 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008503 */
8504 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
8505 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
8506 mPrivateFlags &= ~SAVE_STATE_CALLED;
8507 Parcelable state = onSaveInstanceState();
8508 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8509 throw new IllegalStateException(
8510 "Derived class did not call super.onSaveInstanceState()");
8511 }
8512 if (state != null) {
8513 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
8514 // + ": " + state);
8515 container.put(mID, state);
8516 }
8517 }
8518 }
8519
8520 /**
8521 * Hook allowing a view to generate a representation of its internal state
8522 * that can later be used to create a new instance with that same state.
8523 * This state should only contain information that is not persistent or can
8524 * not be reconstructed later. For example, you will never store your
8525 * current position on screen because that will be computed again when a
8526 * new instance of the view is placed in its view hierarchy.
8527 * <p>
8528 * Some examples of things you may store here: the current cursor position
8529 * in a text view (but usually not the text itself since that is stored in a
8530 * content provider or other persistent storage), the currently selected
8531 * item in a list view.
8532 *
8533 * @return Returns a Parcelable object containing the view's current dynamic
8534 * state, or null if there is nothing interesting to save. The
8535 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008536 * @see #onRestoreInstanceState(android.os.Parcelable)
8537 * @see #saveHierarchyState(android.util.SparseArray)
8538 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008539 * @see #setSaveEnabled(boolean)
8540 */
8541 protected Parcelable onSaveInstanceState() {
8542 mPrivateFlags |= SAVE_STATE_CALLED;
8543 return BaseSavedState.EMPTY_STATE;
8544 }
8545
8546 /**
8547 * Restore this view hierarchy's frozen state from the given container.
8548 *
8549 * @param container The SparseArray which holds previously frozen states.
8550 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008551 * @see #saveHierarchyState(android.util.SparseArray)
8552 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
8553 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008554 */
8555 public void restoreHierarchyState(SparseArray<Parcelable> container) {
8556 dispatchRestoreInstanceState(container);
8557 }
8558
8559 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008560 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
8561 * state for this view and its children. May be overridden to modify how restoring
8562 * happens to a view's children; for example, some views may want to not store state
8563 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008564 *
8565 * @param container The SparseArray which holds previously saved state.
8566 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008567 * @see #dispatchSaveInstanceState(android.util.SparseArray)
8568 * @see #restoreHierarchyState(android.util.SparseArray)
8569 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008570 */
8571 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
8572 if (mID != NO_ID) {
8573 Parcelable state = container.get(mID);
8574 if (state != null) {
8575 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
8576 // + ": " + state);
8577 mPrivateFlags &= ~SAVE_STATE_CALLED;
8578 onRestoreInstanceState(state);
8579 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8580 throw new IllegalStateException(
8581 "Derived class did not call super.onRestoreInstanceState()");
8582 }
8583 }
8584 }
8585 }
8586
8587 /**
8588 * Hook allowing a view to re-apply a representation of its internal state that had previously
8589 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
8590 * null state.
8591 *
8592 * @param state The frozen state that had previously been returned by
8593 * {@link #onSaveInstanceState}.
8594 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008595 * @see #onSaveInstanceState()
8596 * @see #restoreHierarchyState(android.util.SparseArray)
8597 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008598 */
8599 protected void onRestoreInstanceState(Parcelable state) {
8600 mPrivateFlags |= SAVE_STATE_CALLED;
8601 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08008602 throw new IllegalArgumentException("Wrong state class, expecting View State but "
8603 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08008604 + "when two views of different type have the same id in the same hierarchy. "
8605 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08008606 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008607 }
8608 }
8609
8610 /**
8611 * <p>Return the time at which the drawing of the view hierarchy started.</p>
8612 *
8613 * @return the drawing start time in milliseconds
8614 */
8615 public long getDrawingTime() {
8616 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
8617 }
8618
8619 /**
8620 * <p>Enables or disables the duplication of the parent's state into this view. When
8621 * duplication is enabled, this view gets its drawable state from its parent rather
8622 * than from its own internal properties.</p>
8623 *
8624 * <p>Note: in the current implementation, setting this property to true after the
8625 * view was added to a ViewGroup might have no effect at all. This property should
8626 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
8627 *
8628 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
8629 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008630 *
Gilles Debunnefb817032011-01-13 13:52:49 -08008631 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
8632 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008633 *
8634 * @param enabled True to enable duplication of the parent's drawable state, false
8635 * to disable it.
8636 *
8637 * @see #getDrawableState()
8638 * @see #isDuplicateParentStateEnabled()
8639 */
8640 public void setDuplicateParentStateEnabled(boolean enabled) {
8641 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
8642 }
8643
8644 /**
8645 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
8646 *
8647 * @return True if this view's drawable state is duplicated from the parent,
8648 * false otherwise
8649 *
8650 * @see #getDrawableState()
8651 * @see #setDuplicateParentStateEnabled(boolean)
8652 */
8653 public boolean isDuplicateParentStateEnabled() {
8654 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
8655 }
8656
8657 /**
Romain Guy171c5922011-01-06 10:04:23 -08008658 * <p>Specifies the type of layer backing this view. The layer can be
8659 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
8660 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008661 *
Romain Guy171c5922011-01-06 10:04:23 -08008662 * <p>A layer is associated with an optional {@link android.graphics.Paint}
8663 * instance that controls how the layer is composed on screen. The following
8664 * properties of the paint are taken into account when composing the layer:</p>
8665 * <ul>
8666 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
8667 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
8668 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
8669 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08008670 *
Romain Guy171c5922011-01-06 10:04:23 -08008671 * <p>If this view has an alpha value set to < 1.0 by calling
8672 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
8673 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
8674 * equivalent to setting a hardware layer on this view and providing a paint with
8675 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08008676 *
Romain Guy171c5922011-01-06 10:04:23 -08008677 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
8678 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
8679 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008680 *
Romain Guy171c5922011-01-06 10:04:23 -08008681 * @param layerType The ype of layer to use with this view, must be one of
8682 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8683 * {@link #LAYER_TYPE_HARDWARE}
8684 * @param paint The paint used to compose the layer. This argument is optional
8685 * and can be null. It is ignored when the layer type is
8686 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08008687 *
8688 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08008689 * @see #LAYER_TYPE_NONE
8690 * @see #LAYER_TYPE_SOFTWARE
8691 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08008692 * @see #setAlpha(float)
8693 *
Romain Guy171c5922011-01-06 10:04:23 -08008694 * @attr ref android.R.styleable#View_layerType
8695 */
8696 public void setLayerType(int layerType, Paint paint) {
8697 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08008698 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08008699 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
8700 }
Chet Haasedaf98e92011-01-10 14:10:36 -08008701
Romain Guyd6cd5722011-01-17 14:42:41 -08008702 if (layerType == mLayerType) {
8703 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
8704 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008705 invalidateParentCaches();
8706 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08008707 }
8708 return;
8709 }
Romain Guy171c5922011-01-06 10:04:23 -08008710
8711 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08008712 switch (mLayerType) {
8713 case LAYER_TYPE_SOFTWARE:
8714 if (mDrawingCache != null) {
8715 mDrawingCache.recycle();
8716 mDrawingCache = null;
8717 }
Joe Malin32736f02011-01-19 16:14:20 -08008718
Romain Guy6c319ca2011-01-11 14:29:25 -08008719 if (mUnscaledDrawingCache != null) {
8720 mUnscaledDrawingCache.recycle();
8721 mUnscaledDrawingCache = null;
8722 }
8723 break;
8724 case LAYER_TYPE_HARDWARE:
8725 if (mHardwareLayer != null) {
8726 mHardwareLayer.destroy();
8727 mHardwareLayer = null;
8728 }
8729 break;
8730 default:
8731 break;
Romain Guy171c5922011-01-06 10:04:23 -08008732 }
8733
8734 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08008735 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
8736 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
8737 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08008738
Romain Guy0fd89bf2011-01-26 15:41:30 -08008739 invalidateParentCaches();
8740 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08008741 }
8742
8743 /**
8744 * Indicates what type of layer is currently associated with this view. By default
8745 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
8746 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
8747 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08008748 *
Romain Guy171c5922011-01-06 10:04:23 -08008749 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8750 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08008751 *
8752 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08008753 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08008754 * @see #LAYER_TYPE_NONE
8755 * @see #LAYER_TYPE_SOFTWARE
8756 * @see #LAYER_TYPE_HARDWARE
8757 */
8758 public int getLayerType() {
8759 return mLayerType;
8760 }
Joe Malin32736f02011-01-19 16:14:20 -08008761
Romain Guy6c319ca2011-01-11 14:29:25 -08008762 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08008763 * Forces this view's layer to be created and this view to be rendered
8764 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
8765 * invoking this method will have no effect.
8766 *
8767 * This method can for instance be used to render a view into its layer before
8768 * starting an animation. If this view is complex, rendering into the layer
8769 * before starting the animation will avoid skipping frames.
8770 *
8771 * @throws IllegalStateException If this view is not attached to a window
8772 *
8773 * @see #setLayerType(int, android.graphics.Paint)
8774 */
8775 public void buildLayer() {
8776 if (mLayerType == LAYER_TYPE_NONE) return;
8777
8778 if (mAttachInfo == null) {
8779 throw new IllegalStateException("This view must be attached to a window first");
8780 }
8781
8782 switch (mLayerType) {
8783 case LAYER_TYPE_HARDWARE:
8784 getHardwareLayer();
8785 break;
8786 case LAYER_TYPE_SOFTWARE:
8787 buildDrawingCache(true);
8788 break;
8789 }
8790 }
8791
8792 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08008793 * <p>Returns a hardware layer that can be used to draw this view again
8794 * without executing its draw method.</p>
8795 *
8796 * @return A HardwareLayer ready to render, or null if an error occurred.
8797 */
Romain Guy5e7f7662011-01-24 22:35:56 -08008798 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08008799 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
8800 return null;
8801 }
8802
8803 final int width = mRight - mLeft;
8804 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08008805
Romain Guy6c319ca2011-01-11 14:29:25 -08008806 if (width == 0 || height == 0) {
8807 return null;
8808 }
8809
8810 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
8811 if (mHardwareLayer == null) {
8812 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
8813 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08008814 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008815 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
8816 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08008817 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008818 }
8819
Romain Guy5e7f7662011-01-24 22:35:56 -08008820 Canvas currentCanvas = mAttachInfo.mHardwareCanvas;
8821 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
8822 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008823 try {
8824 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08008825 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08008826 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008827
Chet Haase88172fe2011-03-07 17:36:33 -08008828 final int restoreCount = canvas.save();
8829
Romain Guy6c319ca2011-01-11 14:29:25 -08008830 computeScroll();
8831 canvas.translate(-mScrollX, -mScrollY);
8832
Romain Guy6c319ca2011-01-11 14:29:25 -08008833 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008834
Romain Guy6c319ca2011-01-11 14:29:25 -08008835 // Fast path for layouts with no backgrounds
8836 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8837 mPrivateFlags &= ~DIRTY_MASK;
8838 dispatchDraw(canvas);
8839 } else {
8840 draw(canvas);
8841 }
Joe Malin32736f02011-01-19 16:14:20 -08008842
Chet Haase88172fe2011-03-07 17:36:33 -08008843 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08008844 } finally {
8845 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08008846 mHardwareLayer.end(currentCanvas);
8847 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008848 }
8849 }
8850
8851 return mHardwareLayer;
8852 }
Romain Guy171c5922011-01-06 10:04:23 -08008853
8854 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008855 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
8856 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
8857 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
8858 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
8859 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
8860 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008861 *
Romain Guy171c5922011-01-06 10:04:23 -08008862 * <p>Enabling the drawing cache is similar to
8863 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08008864 * acceleration is turned off. When hardware acceleration is turned on, enabling the
8865 * drawing cache has no effect on rendering because the system uses a different mechanism
8866 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
8867 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
8868 * for information on how to enable software and hardware layers.</p>
8869 *
8870 * <p>This API can be used to manually generate
8871 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
8872 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008873 *
8874 * @param enabled true to enable the drawing cache, false otherwise
8875 *
8876 * @see #isDrawingCacheEnabled()
8877 * @see #getDrawingCache()
8878 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08008879 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008880 */
8881 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08008882 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008883 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
8884 }
8885
8886 /**
8887 * <p>Indicates whether the drawing cache is enabled for this view.</p>
8888 *
8889 * @return true if the drawing cache is enabled
8890 *
8891 * @see #setDrawingCacheEnabled(boolean)
8892 * @see #getDrawingCache()
8893 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008894 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008895 public boolean isDrawingCacheEnabled() {
8896 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
8897 }
8898
8899 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08008900 * Debugging utility which recursively outputs the dirty state of a view and its
8901 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08008902 *
Chet Haasedaf98e92011-01-10 14:10:36 -08008903 * @hide
8904 */
Romain Guy676b1732011-02-14 14:45:33 -08008905 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08008906 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
8907 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
8908 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
8909 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
8910 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
8911 if (clear) {
8912 mPrivateFlags &= clearMask;
8913 }
8914 if (this instanceof ViewGroup) {
8915 ViewGroup parent = (ViewGroup) this;
8916 final int count = parent.getChildCount();
8917 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08008918 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08008919 child.outputDirtyFlags(indent + " ", clear, clearMask);
8920 }
8921 }
8922 }
8923
8924 /**
8925 * This method is used by ViewGroup to cause its children to restore or recreate their
8926 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
8927 * to recreate its own display list, which would happen if it went through the normal
8928 * draw/dispatchDraw mechanisms.
8929 *
8930 * @hide
8931 */
8932 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08008933
8934 /**
8935 * A view that is not attached or hardware accelerated cannot create a display list.
8936 * This method checks these conditions and returns the appropriate result.
8937 *
8938 * @return true if view has the ability to create a display list, false otherwise.
8939 *
8940 * @hide
8941 */
8942 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08008943 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08008944 }
Joe Malin32736f02011-01-19 16:14:20 -08008945
Chet Haasedaf98e92011-01-10 14:10:36 -08008946 /**
Romain Guyb051e892010-09-28 19:09:36 -07008947 * <p>Returns a display list that can be used to draw this view again
8948 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008949 *
Romain Guyb051e892010-09-28 19:09:36 -07008950 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08008951 *
8952 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07008953 */
Chet Haasedaf98e92011-01-10 14:10:36 -08008954 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08008955 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07008956 return null;
8957 }
8958
Chet Haasedaf98e92011-01-10 14:10:36 -08008959 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
8960 mDisplayList == null || !mDisplayList.isValid() ||
8961 mRecreateDisplayList)) {
8962 // Don't need to recreate the display list, just need to tell our
8963 // children to restore/recreate theirs
8964 if (mDisplayList != null && mDisplayList.isValid() &&
8965 !mRecreateDisplayList) {
8966 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8967 mPrivateFlags &= ~DIRTY_MASK;
8968 dispatchGetDisplayList();
8969
8970 return mDisplayList;
8971 }
8972
8973 // If we got here, we're recreating it. Mark it as such to ensure that
8974 // we copy in child display lists into ours in drawChild()
8975 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08008976 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008977 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
8978 // If we're creating a new display list, make sure our parent gets invalidated
8979 // since they will need to recreate their display list to account for this
8980 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08008981 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08008982 }
Romain Guyb051e892010-09-28 19:09:36 -07008983
8984 final HardwareCanvas canvas = mDisplayList.start();
8985 try {
8986 int width = mRight - mLeft;
8987 int height = mBottom - mTop;
8988
8989 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08008990 // The dirty rect should always be null for a display list
8991 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07008992
Chet Haase88172fe2011-03-07 17:36:33 -08008993 final int restoreCount = canvas.save();
8994
Chet Haasedaf98e92011-01-10 14:10:36 -08008995 computeScroll();
8996 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008997 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008998
Romain Guyb051e892010-09-28 19:09:36 -07008999 // Fast path for layouts with no backgrounds
9000 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9001 mPrivateFlags &= ~DIRTY_MASK;
9002 dispatchDraw(canvas);
9003 } else {
9004 draw(canvas);
9005 }
Joe Malin32736f02011-01-19 16:14:20 -08009006
Chet Haase88172fe2011-03-07 17:36:33 -08009007 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07009008 } finally {
9009 canvas.onPostDraw();
9010
9011 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07009012 }
Chet Haase77785f92011-01-25 23:22:09 -08009013 } else {
9014 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9015 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07009016 }
9017
9018 return mDisplayList;
9019 }
9020
9021 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009022 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009023 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009024 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009025 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009026 * @see #getDrawingCache(boolean)
9027 */
9028 public Bitmap getDrawingCache() {
9029 return getDrawingCache(false);
9030 }
9031
9032 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009033 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
9034 * is null when caching is disabled. If caching is enabled and the cache is not ready,
9035 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
9036 * draw from the cache when the cache is enabled. To benefit from the cache, you must
9037 * request the drawing cache by calling this method and draw it on screen if the
9038 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009039 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009040 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9041 * this method will create a bitmap of the same size as this view. Because this bitmap
9042 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9043 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9044 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9045 * size than the view. This implies that your application must be able to handle this
9046 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009047 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009048 * @param autoScale Indicates whether the generated bitmap should be scaled based on
9049 * the current density of the screen when the application is in compatibility
9050 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009051 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009052 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009053 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009054 * @see #setDrawingCacheEnabled(boolean)
9055 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07009056 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009057 * @see #destroyDrawingCache()
9058 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009059 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009060 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
9061 return null;
9062 }
9063 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009064 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009065 }
Romain Guy02890fd2010-08-06 17:58:44 -07009066 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009067 }
9068
9069 /**
9070 * <p>Frees the resources used by the drawing cache. If you call
9071 * {@link #buildDrawingCache()} manually without calling
9072 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9073 * should cleanup the cache with this method afterwards.</p>
9074 *
9075 * @see #setDrawingCacheEnabled(boolean)
9076 * @see #buildDrawingCache()
9077 * @see #getDrawingCache()
9078 */
9079 public void destroyDrawingCache() {
9080 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009081 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009082 mDrawingCache = null;
9083 }
Romain Guyfbd8f692009-06-26 14:51:58 -07009084 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009085 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07009086 mUnscaledDrawingCache = null;
9087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009088 }
9089
9090 /**
9091 * Setting a solid background color for the drawing cache's bitmaps will improve
9092 * perfromance and memory usage. Note, though that this should only be used if this
9093 * view will always be drawn on top of a solid color.
9094 *
9095 * @param color The background color to use for the drawing cache's bitmap
9096 *
9097 * @see #setDrawingCacheEnabled(boolean)
9098 * @see #buildDrawingCache()
9099 * @see #getDrawingCache()
9100 */
9101 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08009102 if (color != mDrawingCacheBackgroundColor) {
9103 mDrawingCacheBackgroundColor = color;
9104 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9105 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009106 }
9107
9108 /**
9109 * @see #setDrawingCacheBackgroundColor(int)
9110 *
9111 * @return The background color to used for the drawing cache's bitmap
9112 */
9113 public int getDrawingCacheBackgroundColor() {
9114 return mDrawingCacheBackgroundColor;
9115 }
9116
9117 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009118 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009119 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009120 * @see #buildDrawingCache(boolean)
9121 */
9122 public void buildDrawingCache() {
9123 buildDrawingCache(false);
9124 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08009125
Romain Guyfbd8f692009-06-26 14:51:58 -07009126 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009127 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
9128 *
9129 * <p>If you call {@link #buildDrawingCache()} manually without calling
9130 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9131 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009132 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009133 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9134 * this method will create a bitmap of the same size as this view. Because this bitmap
9135 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9136 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9137 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9138 * size than the view. This implies that your application must be able to handle this
9139 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009140 *
Romain Guy0d9275e2010-10-26 14:22:30 -07009141 * <p>You should avoid calling this method when hardware acceleration is enabled. If
9142 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08009143 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07009144 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009145 *
9146 * @see #getDrawingCache()
9147 * @see #destroyDrawingCache()
9148 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009149 public void buildDrawingCache(boolean autoScale) {
9150 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07009151 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009152 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009153
9154 if (ViewDebug.TRACE_HIERARCHY) {
9155 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
9156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009157
Romain Guy8506ab42009-06-11 17:35:47 -07009158 int width = mRight - mLeft;
9159 int height = mBottom - mTop;
9160
9161 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07009162 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07009163
Romain Guye1123222009-06-29 14:24:56 -07009164 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009165 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
9166 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07009167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009168
9169 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07009170 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08009171 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009172
9173 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07009174 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08009175 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009176 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
9177 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08009178 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009179 return;
9180 }
9181
9182 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07009183 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009184
9185 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009186 Bitmap.Config quality;
9187 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08009188 // Never pick ARGB_4444 because it looks awful
9189 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009190 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
9191 case DRAWING_CACHE_QUALITY_AUTO:
9192 quality = Bitmap.Config.ARGB_8888;
9193 break;
9194 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08009195 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009196 break;
9197 case DRAWING_CACHE_QUALITY_HIGH:
9198 quality = Bitmap.Config.ARGB_8888;
9199 break;
9200 default:
9201 quality = Bitmap.Config.ARGB_8888;
9202 break;
9203 }
9204 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07009205 // Optimization for translucent windows
9206 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08009207 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009208 }
9209
9210 // Try to cleanup memory
9211 if (bitmap != null) bitmap.recycle();
9212
9213 try {
9214 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07009215 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07009216 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07009217 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009218 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07009219 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009220 }
Adam Powell26153a32010-11-08 15:22:27 -08009221 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009222 } catch (OutOfMemoryError e) {
9223 // If there is not enough memory to create the bitmap cache, just
9224 // ignore the issue as bitmap caches are not required to draw the
9225 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07009226 if (autoScale) {
9227 mDrawingCache = null;
9228 } else {
9229 mUnscaledDrawingCache = null;
9230 }
Romain Guy0211a0a2011-02-14 16:34:59 -08009231 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009232 return;
9233 }
9234
9235 clear = drawingCacheBackgroundColor != 0;
9236 }
9237
9238 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009239 if (attachInfo != null) {
9240 canvas = attachInfo.mCanvas;
9241 if (canvas == null) {
9242 canvas = new Canvas();
9243 }
9244 canvas.setBitmap(bitmap);
9245 // Temporarily clobber the cached Canvas in case one of our children
9246 // is also using a drawing cache. Without this, the children would
9247 // steal the canvas by attaching their own bitmap to it and bad, bad
9248 // thing would happen (invisible views, corrupted drawings, etc.)
9249 attachInfo.mCanvas = null;
9250 } else {
9251 // This case should hopefully never or seldom happen
9252 canvas = new Canvas(bitmap);
9253 }
9254
9255 if (clear) {
9256 bitmap.eraseColor(drawingCacheBackgroundColor);
9257 }
9258
9259 computeScroll();
9260 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08009261
Romain Guye1123222009-06-29 14:24:56 -07009262 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009263 final float scale = attachInfo.mApplicationScale;
9264 canvas.scale(scale, scale);
9265 }
Joe Malin32736f02011-01-19 16:14:20 -08009266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009267 canvas.translate(-mScrollX, -mScrollY);
9268
Romain Guy5bcdff42009-05-14 21:27:18 -07009269 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08009270 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
9271 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07009272 mPrivateFlags |= DRAWING_CACHE_VALID;
9273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009274
9275 // Fast path for layouts with no backgrounds
9276 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9277 if (ViewDebug.TRACE_HIERARCHY) {
9278 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9279 }
Romain Guy5bcdff42009-05-14 21:27:18 -07009280 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009281 dispatchDraw(canvas);
9282 } else {
9283 draw(canvas);
9284 }
9285
9286 canvas.restoreToCount(restoreCount);
9287
9288 if (attachInfo != null) {
9289 // Restore the cached Canvas for our siblings
9290 attachInfo.mCanvas = canvas;
9291 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009292 }
9293 }
9294
9295 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009296 * Create a snapshot of the view into a bitmap. We should probably make
9297 * some form of this public, but should think about the API.
9298 */
Romain Guy223ff5c2010-03-02 17:07:47 -08009299 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009300 int width = mRight - mLeft;
9301 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009302
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009303 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07009304 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009305 width = (int) ((width * scale) + 0.5f);
9306 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08009307
Romain Guy8c11e312009-09-14 15:15:30 -07009308 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009309 if (bitmap == null) {
9310 throw new OutOfMemoryError();
9311 }
9312
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009313 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08009314
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009315 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009316 if (attachInfo != null) {
9317 canvas = attachInfo.mCanvas;
9318 if (canvas == null) {
9319 canvas = new Canvas();
9320 }
9321 canvas.setBitmap(bitmap);
9322 // Temporarily clobber the cached Canvas in case one of our children
9323 // is also using a drawing cache. Without this, the children would
9324 // steal the canvas by attaching their own bitmap to it and bad, bad
9325 // things would happen (invisible views, corrupted drawings, etc.)
9326 attachInfo.mCanvas = null;
9327 } else {
9328 // This case should hopefully never or seldom happen
9329 canvas = new Canvas(bitmap);
9330 }
9331
Romain Guy5bcdff42009-05-14 21:27:18 -07009332 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009333 bitmap.eraseColor(backgroundColor);
9334 }
9335
9336 computeScroll();
9337 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009338 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009339 canvas.translate(-mScrollX, -mScrollY);
9340
Romain Guy5bcdff42009-05-14 21:27:18 -07009341 // Temporarily remove the dirty mask
9342 int flags = mPrivateFlags;
9343 mPrivateFlags &= ~DIRTY_MASK;
9344
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009345 // Fast path for layouts with no backgrounds
9346 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9347 dispatchDraw(canvas);
9348 } else {
9349 draw(canvas);
9350 }
9351
Romain Guy5bcdff42009-05-14 21:27:18 -07009352 mPrivateFlags = flags;
9353
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009354 canvas.restoreToCount(restoreCount);
9355
9356 if (attachInfo != null) {
9357 // Restore the cached Canvas for our siblings
9358 attachInfo.mCanvas = canvas;
9359 }
Romain Guy8506ab42009-06-11 17:35:47 -07009360
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009361 return bitmap;
9362 }
9363
9364 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009365 * Indicates whether this View is currently in edit mode. A View is usually
9366 * in edit mode when displayed within a developer tool. For instance, if
9367 * this View is being drawn by a visual user interface builder, this method
9368 * should return true.
9369 *
9370 * Subclasses should check the return value of this method to provide
9371 * different behaviors if their normal behavior might interfere with the
9372 * host environment. For instance: the class spawns a thread in its
9373 * constructor, the drawing code relies on device-specific features, etc.
9374 *
9375 * This method is usually checked in the drawing code of custom widgets.
9376 *
9377 * @return True if this View is in edit mode, false otherwise.
9378 */
9379 public boolean isInEditMode() {
9380 return false;
9381 }
9382
9383 /**
9384 * If the View draws content inside its padding and enables fading edges,
9385 * it needs to support padding offsets. Padding offsets are added to the
9386 * fading edges to extend the length of the fade so that it covers pixels
9387 * drawn inside the padding.
9388 *
9389 * Subclasses of this class should override this method if they need
9390 * to draw content inside the padding.
9391 *
9392 * @return True if padding offset must be applied, false otherwise.
9393 *
9394 * @see #getLeftPaddingOffset()
9395 * @see #getRightPaddingOffset()
9396 * @see #getTopPaddingOffset()
9397 * @see #getBottomPaddingOffset()
9398 *
9399 * @since CURRENT
9400 */
9401 protected boolean isPaddingOffsetRequired() {
9402 return false;
9403 }
9404
9405 /**
9406 * Amount by which to extend the left fading region. Called only when
9407 * {@link #isPaddingOffsetRequired()} returns true.
9408 *
9409 * @return The left padding offset in pixels.
9410 *
9411 * @see #isPaddingOffsetRequired()
9412 *
9413 * @since CURRENT
9414 */
9415 protected int getLeftPaddingOffset() {
9416 return 0;
9417 }
9418
9419 /**
9420 * Amount by which to extend the right fading region. Called only when
9421 * {@link #isPaddingOffsetRequired()} returns true.
9422 *
9423 * @return The right padding offset in pixels.
9424 *
9425 * @see #isPaddingOffsetRequired()
9426 *
9427 * @since CURRENT
9428 */
9429 protected int getRightPaddingOffset() {
9430 return 0;
9431 }
9432
9433 /**
9434 * Amount by which to extend the top fading region. Called only when
9435 * {@link #isPaddingOffsetRequired()} returns true.
9436 *
9437 * @return The top padding offset in pixels.
9438 *
9439 * @see #isPaddingOffsetRequired()
9440 *
9441 * @since CURRENT
9442 */
9443 protected int getTopPaddingOffset() {
9444 return 0;
9445 }
9446
9447 /**
9448 * Amount by which to extend the bottom fading region. Called only when
9449 * {@link #isPaddingOffsetRequired()} returns true.
9450 *
9451 * @return The bottom padding offset in pixels.
9452 *
9453 * @see #isPaddingOffsetRequired()
9454 *
9455 * @since CURRENT
9456 */
9457 protected int getBottomPaddingOffset() {
9458 return 0;
9459 }
9460
9461 /**
Romain Guy2bffd262010-09-12 17:40:02 -07009462 * <p>Indicates whether this view is attached to an hardware accelerated
9463 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009464 *
Romain Guy2bffd262010-09-12 17:40:02 -07009465 * <p>Even if this method returns true, it does not mean that every call
9466 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
9467 * accelerated {@link android.graphics.Canvas}. For instance, if this view
9468 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
9469 * window is hardware accelerated,
9470 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
9471 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009472 *
Romain Guy2bffd262010-09-12 17:40:02 -07009473 * @return True if the view is attached to a window and the window is
9474 * hardware accelerated; false in any other case.
9475 */
9476 public boolean isHardwareAccelerated() {
9477 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
9478 }
Joe Malin32736f02011-01-19 16:14:20 -08009479
Romain Guy2bffd262010-09-12 17:40:02 -07009480 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009481 * Manually render this view (and all of its children) to the given Canvas.
9482 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009483 * called. When implementing a view, implement
9484 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
9485 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009486 *
9487 * @param canvas The Canvas to which the View is rendered.
9488 */
9489 public void draw(Canvas canvas) {
9490 if (ViewDebug.TRACE_HIERARCHY) {
9491 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9492 }
9493
Romain Guy5bcdff42009-05-14 21:27:18 -07009494 final int privateFlags = mPrivateFlags;
9495 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
9496 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
9497 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07009498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009499 /*
9500 * Draw traversal performs several drawing steps which must be executed
9501 * in the appropriate order:
9502 *
9503 * 1. Draw the background
9504 * 2. If necessary, save the canvas' layers to prepare for fading
9505 * 3. Draw view's content
9506 * 4. Draw children
9507 * 5. If necessary, draw the fading edges and restore layers
9508 * 6. Draw decorations (scrollbars for instance)
9509 */
9510
9511 // Step 1, draw the background, if needed
9512 int saveCount;
9513
Romain Guy24443ea2009-05-11 11:56:30 -07009514 if (!dirtyOpaque) {
9515 final Drawable background = mBGDrawable;
9516 if (background != null) {
9517 final int scrollX = mScrollX;
9518 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009519
Romain Guy24443ea2009-05-11 11:56:30 -07009520 if (mBackgroundSizeChanged) {
9521 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
9522 mBackgroundSizeChanged = false;
9523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009524
Romain Guy24443ea2009-05-11 11:56:30 -07009525 if ((scrollX | scrollY) == 0) {
9526 background.draw(canvas);
9527 } else {
9528 canvas.translate(scrollX, scrollY);
9529 background.draw(canvas);
9530 canvas.translate(-scrollX, -scrollY);
9531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009532 }
9533 }
9534
9535 // skip step 2 & 5 if possible (common case)
9536 final int viewFlags = mViewFlags;
9537 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
9538 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
9539 if (!verticalEdges && !horizontalEdges) {
9540 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009541 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009542
9543 // Step 4, draw the children
9544 dispatchDraw(canvas);
9545
9546 // Step 6, draw decorations (scrollbars)
9547 onDrawScrollBars(canvas);
9548
9549 // we're done...
9550 return;
9551 }
9552
9553 /*
9554 * Here we do the full fledged routine...
9555 * (this is an uncommon case where speed matters less,
9556 * this is why we repeat some of the tests that have been
9557 * done above)
9558 */
9559
9560 boolean drawTop = false;
9561 boolean drawBottom = false;
9562 boolean drawLeft = false;
9563 boolean drawRight = false;
9564
9565 float topFadeStrength = 0.0f;
9566 float bottomFadeStrength = 0.0f;
9567 float leftFadeStrength = 0.0f;
9568 float rightFadeStrength = 0.0f;
9569
9570 // Step 2, save the canvas' layers
9571 int paddingLeft = mPaddingLeft;
9572 int paddingTop = mPaddingTop;
9573
9574 final boolean offsetRequired = isPaddingOffsetRequired();
9575 if (offsetRequired) {
9576 paddingLeft += getLeftPaddingOffset();
9577 paddingTop += getTopPaddingOffset();
9578 }
9579
9580 int left = mScrollX + paddingLeft;
9581 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
9582 int top = mScrollY + paddingTop;
9583 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
9584
9585 if (offsetRequired) {
9586 right += getRightPaddingOffset();
9587 bottom += getBottomPaddingOffset();
9588 }
9589
9590 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009591 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
9592 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009593
9594 // clip the fade length if top and bottom fades overlap
9595 // overlapping fades produce odd-looking artifacts
9596 if (verticalEdges && (top + length > bottom - length)) {
9597 length = (bottom - top) / 2;
9598 }
9599
9600 // also clip horizontal fades if necessary
9601 if (horizontalEdges && (left + length > right - length)) {
9602 length = (right - left) / 2;
9603 }
9604
9605 if (verticalEdges) {
9606 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009607 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009608 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009609 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009610 }
9611
9612 if (horizontalEdges) {
9613 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009614 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009615 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009616 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009617 }
9618
9619 saveCount = canvas.getSaveCount();
9620
9621 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -07009622 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009623 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
9624
9625 if (drawTop) {
9626 canvas.saveLayer(left, top, right, top + length, null, flags);
9627 }
9628
9629 if (drawBottom) {
9630 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
9631 }
9632
9633 if (drawLeft) {
9634 canvas.saveLayer(left, top, left + length, bottom, null, flags);
9635 }
9636
9637 if (drawRight) {
9638 canvas.saveLayer(right - length, top, right, bottom, null, flags);
9639 }
9640 } else {
9641 scrollabilityCache.setFadeColor(solidColor);
9642 }
9643
9644 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009645 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009646
9647 // Step 4, draw the children
9648 dispatchDraw(canvas);
9649
9650 // Step 5, draw the fade effect and restore layers
9651 final Paint p = scrollabilityCache.paint;
9652 final Matrix matrix = scrollabilityCache.matrix;
9653 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009654
9655 if (drawTop) {
9656 matrix.setScale(1, fadeHeight * topFadeStrength);
9657 matrix.postTranslate(left, top);
9658 fade.setLocalMatrix(matrix);
9659 canvas.drawRect(left, top, right, top + length, p);
9660 }
9661
9662 if (drawBottom) {
9663 matrix.setScale(1, fadeHeight * bottomFadeStrength);
9664 matrix.postRotate(180);
9665 matrix.postTranslate(left, bottom);
9666 fade.setLocalMatrix(matrix);
9667 canvas.drawRect(left, bottom - length, right, bottom, p);
9668 }
9669
9670 if (drawLeft) {
9671 matrix.setScale(1, fadeHeight * leftFadeStrength);
9672 matrix.postRotate(-90);
9673 matrix.postTranslate(left, top);
9674 fade.setLocalMatrix(matrix);
9675 canvas.drawRect(left, top, left + length, bottom, p);
9676 }
9677
9678 if (drawRight) {
9679 matrix.setScale(1, fadeHeight * rightFadeStrength);
9680 matrix.postRotate(90);
9681 matrix.postTranslate(right, top);
9682 fade.setLocalMatrix(matrix);
9683 canvas.drawRect(right - length, top, right, bottom, p);
9684 }
9685
9686 canvas.restoreToCount(saveCount);
9687
9688 // Step 6, draw decorations (scrollbars)
9689 onDrawScrollBars(canvas);
9690 }
9691
9692 /**
9693 * Override this if your view is known to always be drawn on top of a solid color background,
9694 * and needs to draw fading edges. Returning a non-zero color enables the view system to
9695 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
9696 * should be set to 0xFF.
9697 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009698 * @see #setVerticalFadingEdgeEnabled(boolean)
9699 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009700 *
9701 * @return The known solid color background for this view, or 0 if the color may vary
9702 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009703 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009704 public int getSolidColor() {
9705 return 0;
9706 }
9707
9708 /**
9709 * Build a human readable string representation of the specified view flags.
9710 *
9711 * @param flags the view flags to convert to a string
9712 * @return a String representing the supplied flags
9713 */
9714 private static String printFlags(int flags) {
9715 String output = "";
9716 int numFlags = 0;
9717 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
9718 output += "TAKES_FOCUS";
9719 numFlags++;
9720 }
9721
9722 switch (flags & VISIBILITY_MASK) {
9723 case INVISIBLE:
9724 if (numFlags > 0) {
9725 output += " ";
9726 }
9727 output += "INVISIBLE";
9728 // USELESS HERE numFlags++;
9729 break;
9730 case GONE:
9731 if (numFlags > 0) {
9732 output += " ";
9733 }
9734 output += "GONE";
9735 // USELESS HERE numFlags++;
9736 break;
9737 default:
9738 break;
9739 }
9740 return output;
9741 }
9742
9743 /**
9744 * Build a human readable string representation of the specified private
9745 * view flags.
9746 *
9747 * @param privateFlags the private view flags to convert to a string
9748 * @return a String representing the supplied flags
9749 */
9750 private static String printPrivateFlags(int privateFlags) {
9751 String output = "";
9752 int numFlags = 0;
9753
9754 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
9755 output += "WANTS_FOCUS";
9756 numFlags++;
9757 }
9758
9759 if ((privateFlags & FOCUSED) == FOCUSED) {
9760 if (numFlags > 0) {
9761 output += " ";
9762 }
9763 output += "FOCUSED";
9764 numFlags++;
9765 }
9766
9767 if ((privateFlags & SELECTED) == SELECTED) {
9768 if (numFlags > 0) {
9769 output += " ";
9770 }
9771 output += "SELECTED";
9772 numFlags++;
9773 }
9774
9775 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
9776 if (numFlags > 0) {
9777 output += " ";
9778 }
9779 output += "IS_ROOT_NAMESPACE";
9780 numFlags++;
9781 }
9782
9783 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
9784 if (numFlags > 0) {
9785 output += " ";
9786 }
9787 output += "HAS_BOUNDS";
9788 numFlags++;
9789 }
9790
9791 if ((privateFlags & DRAWN) == DRAWN) {
9792 if (numFlags > 0) {
9793 output += " ";
9794 }
9795 output += "DRAWN";
9796 // USELESS HERE numFlags++;
9797 }
9798 return output;
9799 }
9800
9801 /**
9802 * <p>Indicates whether or not this view's layout will be requested during
9803 * the next hierarchy layout pass.</p>
9804 *
9805 * @return true if the layout will be forced during next layout pass
9806 */
9807 public boolean isLayoutRequested() {
9808 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
9809 }
9810
9811 /**
9812 * Assign a size and position to a view and all of its
9813 * descendants
9814 *
9815 * <p>This is the second phase of the layout mechanism.
9816 * (The first is measuring). In this phase, each parent calls
9817 * layout on all of its children to position them.
9818 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -08009819 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009820 *
Chet Haase9c087442011-01-12 16:20:16 -08009821 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009822 * Derived classes with children should override
9823 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -08009824 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009825 *
9826 * @param l Left position, relative to parent
9827 * @param t Top position, relative to parent
9828 * @param r Right position, relative to parent
9829 * @param b Bottom position, relative to parent
9830 */
Romain Guy5429e1d2010-09-07 12:38:00 -07009831 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -08009832 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -07009833 int oldL = mLeft;
9834 int oldT = mTop;
9835 int oldB = mBottom;
9836 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009837 boolean changed = setFrame(l, t, r, b);
9838 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
9839 if (ViewDebug.TRACE_HIERARCHY) {
9840 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
9841 }
9842
9843 onLayout(changed, l, t, r, b);
9844 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -07009845
9846 if (mOnLayoutChangeListeners != null) {
9847 ArrayList<OnLayoutChangeListener> listenersCopy =
9848 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
9849 int numListeners = listenersCopy.size();
9850 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -07009851 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -07009852 }
9853 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009854 }
9855 mPrivateFlags &= ~FORCE_LAYOUT;
9856 }
9857
9858 /**
9859 * Called from layout when this view should
9860 * assign a size and position to each of its children.
9861 *
9862 * Derived classes with children should override
9863 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -07009864 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009865 * @param changed This is a new size or position for this view
9866 * @param left Left position, relative to parent
9867 * @param top Top position, relative to parent
9868 * @param right Right position, relative to parent
9869 * @param bottom Bottom position, relative to parent
9870 */
9871 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
9872 }
9873
9874 /**
9875 * Assign a size and position to this view.
9876 *
9877 * This is called from layout.
9878 *
9879 * @param left Left position, relative to parent
9880 * @param top Top position, relative to parent
9881 * @param right Right position, relative to parent
9882 * @param bottom Bottom position, relative to parent
9883 * @return true if the new size and position are different than the
9884 * previous ones
9885 * {@hide}
9886 */
9887 protected boolean setFrame(int left, int top, int right, int bottom) {
9888 boolean changed = false;
9889
9890 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07009891 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009892 + right + "," + bottom + ")");
9893 }
9894
9895 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
9896 changed = true;
9897
9898 // Remember our drawn bit
9899 int drawn = mPrivateFlags & DRAWN;
9900
9901 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -08009902 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009903
9904
9905 int oldWidth = mRight - mLeft;
9906 int oldHeight = mBottom - mTop;
9907
9908 mLeft = left;
9909 mTop = top;
9910 mRight = right;
9911 mBottom = bottom;
9912
9913 mPrivateFlags |= HAS_BOUNDS;
9914
9915 int newWidth = right - left;
9916 int newHeight = bottom - top;
9917
9918 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009919 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9920 // A change in dimension means an auto-centered pivot point changes, too
9921 mMatrixDirty = true;
9922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009923 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
9924 }
9925
9926 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
9927 // If we are visible, force the DRAWN bit to on so that
9928 // this invalidate will go through (at least to our parent).
9929 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009930 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009931 // the DRAWN bit.
9932 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009933 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -08009934 // parent display list may need to be recreated based on a change in the bounds
9935 // of any child
9936 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009937 }
9938
9939 // Reset drawn bit to original value (invalidate turns it off)
9940 mPrivateFlags |= drawn;
9941
9942 mBackgroundSizeChanged = true;
9943 }
9944 return changed;
9945 }
9946
9947 /**
9948 * Finalize inflating a view from XML. This is called as the last phase
9949 * of inflation, after all child views have been added.
9950 *
9951 * <p>Even if the subclass overrides onFinishInflate, they should always be
9952 * sure to call the super method, so that we get called.
9953 */
9954 protected void onFinishInflate() {
9955 }
9956
9957 /**
9958 * Returns the resources associated with this view.
9959 *
9960 * @return Resources object.
9961 */
9962 public Resources getResources() {
9963 return mResources;
9964 }
9965
9966 /**
9967 * Invalidates the specified Drawable.
9968 *
9969 * @param drawable the drawable to invalidate
9970 */
9971 public void invalidateDrawable(Drawable drawable) {
9972 if (verifyDrawable(drawable)) {
9973 final Rect dirty = drawable.getBounds();
9974 final int scrollX = mScrollX;
9975 final int scrollY = mScrollY;
9976
9977 invalidate(dirty.left + scrollX, dirty.top + scrollY,
9978 dirty.right + scrollX, dirty.bottom + scrollY);
9979 }
9980 }
9981
9982 /**
9983 * Schedules an action on a drawable to occur at a specified time.
9984 *
9985 * @param who the recipient of the action
9986 * @param what the action to run on the drawable
9987 * @param when the time at which the action must occur. Uses the
9988 * {@link SystemClock#uptimeMillis} timebase.
9989 */
9990 public void scheduleDrawable(Drawable who, Runnable what, long when) {
9991 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9992 mAttachInfo.mHandler.postAtTime(what, who, when);
9993 }
9994 }
9995
9996 /**
9997 * Cancels a scheduled action on a drawable.
9998 *
9999 * @param who the recipient of the action
10000 * @param what the action to cancel
10001 */
10002 public void unscheduleDrawable(Drawable who, Runnable what) {
10003 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10004 mAttachInfo.mHandler.removeCallbacks(what, who);
10005 }
10006 }
10007
10008 /**
10009 * Unschedule any events associated with the given Drawable. This can be
10010 * used when selecting a new Drawable into a view, so that the previous
10011 * one is completely unscheduled.
10012 *
10013 * @param who The Drawable to unschedule.
10014 *
10015 * @see #drawableStateChanged
10016 */
10017 public void unscheduleDrawable(Drawable who) {
10018 if (mAttachInfo != null) {
10019 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
10020 }
10021 }
10022
10023 /**
10024 * If your view subclass is displaying its own Drawable objects, it should
10025 * override this function and return true for any Drawable it is
10026 * displaying. This allows animations for those drawables to be
10027 * scheduled.
10028 *
10029 * <p>Be sure to call through to the super class when overriding this
10030 * function.
10031 *
10032 * @param who The Drawable to verify. Return true if it is one you are
10033 * displaying, else return the result of calling through to the
10034 * super class.
10035 *
10036 * @return boolean If true than the Drawable is being displayed in the
10037 * view; else false and it is not allowed to animate.
10038 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010039 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
10040 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010041 */
10042 protected boolean verifyDrawable(Drawable who) {
10043 return who == mBGDrawable;
10044 }
10045
10046 /**
10047 * This function is called whenever the state of the view changes in such
10048 * a way that it impacts the state of drawables being shown.
10049 *
10050 * <p>Be sure to call through to the superclass when overriding this
10051 * function.
10052 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010053 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010054 */
10055 protected void drawableStateChanged() {
10056 Drawable d = mBGDrawable;
10057 if (d != null && d.isStateful()) {
10058 d.setState(getDrawableState());
10059 }
10060 }
10061
10062 /**
10063 * Call this to force a view to update its drawable state. This will cause
10064 * drawableStateChanged to be called on this view. Views that are interested
10065 * in the new state should call getDrawableState.
10066 *
10067 * @see #drawableStateChanged
10068 * @see #getDrawableState
10069 */
10070 public void refreshDrawableState() {
10071 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
10072 drawableStateChanged();
10073
10074 ViewParent parent = mParent;
10075 if (parent != null) {
10076 parent.childDrawableStateChanged(this);
10077 }
10078 }
10079
10080 /**
10081 * Return an array of resource IDs of the drawable states representing the
10082 * current state of the view.
10083 *
10084 * @return The current drawable state
10085 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010086 * @see Drawable#setState(int[])
10087 * @see #drawableStateChanged()
10088 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010089 */
10090 public final int[] getDrawableState() {
10091 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
10092 return mDrawableState;
10093 } else {
10094 mDrawableState = onCreateDrawableState(0);
10095 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
10096 return mDrawableState;
10097 }
10098 }
10099
10100 /**
10101 * Generate the new {@link android.graphics.drawable.Drawable} state for
10102 * this view. This is called by the view
10103 * system when the cached Drawable state is determined to be invalid. To
10104 * retrieve the current state, you should use {@link #getDrawableState}.
10105 *
10106 * @param extraSpace if non-zero, this is the number of extra entries you
10107 * would like in the returned array in which you can place your own
10108 * states.
10109 *
10110 * @return Returns an array holding the current {@link Drawable} state of
10111 * the view.
10112 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010113 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010114 */
10115 protected int[] onCreateDrawableState(int extraSpace) {
10116 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
10117 mParent instanceof View) {
10118 return ((View) mParent).onCreateDrawableState(extraSpace);
10119 }
10120
10121 int[] drawableState;
10122
10123 int privateFlags = mPrivateFlags;
10124
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010125 int viewStateIndex = 0;
10126 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
10127 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
10128 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070010129 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010130 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
10131 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070010132 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
10133 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010134 // This is set if HW acceleration is requested, even if the current
10135 // process doesn't allow it. This is just to allow app preview
10136 // windows to better match their app.
10137 viewStateIndex |= VIEW_STATE_ACCELERATED;
10138 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070010139 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010140
Christopher Tate3d4bf172011-03-28 16:16:46 -070010141 final int privateFlags2 = mPrivateFlags2;
10142 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
10143 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
10144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010145 drawableState = VIEW_STATE_SETS[viewStateIndex];
10146
10147 //noinspection ConstantIfStatement
10148 if (false) {
10149 Log.i("View", "drawableStateIndex=" + viewStateIndex);
10150 Log.i("View", toString()
10151 + " pressed=" + ((privateFlags & PRESSED) != 0)
10152 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
10153 + " fo=" + hasFocus()
10154 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010155 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010156 + ": " + Arrays.toString(drawableState));
10157 }
10158
10159 if (extraSpace == 0) {
10160 return drawableState;
10161 }
10162
10163 final int[] fullState;
10164 if (drawableState != null) {
10165 fullState = new int[drawableState.length + extraSpace];
10166 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
10167 } else {
10168 fullState = new int[extraSpace];
10169 }
10170
10171 return fullState;
10172 }
10173
10174 /**
10175 * Merge your own state values in <var>additionalState</var> into the base
10176 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070010177 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010178 *
10179 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070010180 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010181 * own additional state values.
10182 *
10183 * @param additionalState The additional state values you would like
10184 * added to <var>baseState</var>; this array is not modified.
10185 *
10186 * @return As a convenience, the <var>baseState</var> array you originally
10187 * passed into the function is returned.
10188 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010189 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010190 */
10191 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
10192 final int N = baseState.length;
10193 int i = N - 1;
10194 while (i >= 0 && baseState[i] == 0) {
10195 i--;
10196 }
10197 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
10198 return baseState;
10199 }
10200
10201 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070010202 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
10203 * on all Drawable objects associated with this view.
10204 */
10205 public void jumpDrawablesToCurrentState() {
10206 if (mBGDrawable != null) {
10207 mBGDrawable.jumpToCurrentState();
10208 }
10209 }
10210
10211 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010212 * Sets the background color for this view.
10213 * @param color the color of the background
10214 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010215 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010216 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070010217 if (mBGDrawable instanceof ColorDrawable) {
10218 ((ColorDrawable) mBGDrawable).setColor(color);
10219 } else {
10220 setBackgroundDrawable(new ColorDrawable(color));
10221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010222 }
10223
10224 /**
10225 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070010226 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010227 * @param resid The identifier of the resource.
10228 * @attr ref android.R.styleable#View_background
10229 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010230 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010231 public void setBackgroundResource(int resid) {
10232 if (resid != 0 && resid == mBackgroundResource) {
10233 return;
10234 }
10235
10236 Drawable d= null;
10237 if (resid != 0) {
10238 d = mResources.getDrawable(resid);
10239 }
10240 setBackgroundDrawable(d);
10241
10242 mBackgroundResource = resid;
10243 }
10244
10245 /**
10246 * Set the background to a given Drawable, or remove the background. If the
10247 * background has padding, this View's padding is set to the background's
10248 * padding. However, when a background is removed, this View's padding isn't
10249 * touched. If setting the padding is desired, please use
10250 * {@link #setPadding(int, int, int, int)}.
10251 *
10252 * @param d The Drawable to use as the background, or null to remove the
10253 * background
10254 */
10255 public void setBackgroundDrawable(Drawable d) {
10256 boolean requestLayout = false;
10257
10258 mBackgroundResource = 0;
10259
10260 /*
10261 * Regardless of whether we're setting a new background or not, we want
10262 * to clear the previous drawable.
10263 */
10264 if (mBGDrawable != null) {
10265 mBGDrawable.setCallback(null);
10266 unscheduleDrawable(mBGDrawable);
10267 }
10268
10269 if (d != null) {
10270 Rect padding = sThreadLocal.get();
10271 if (padding == null) {
10272 padding = new Rect();
10273 sThreadLocal.set(padding);
10274 }
10275 if (d.getPadding(padding)) {
10276 setPadding(padding.left, padding.top, padding.right, padding.bottom);
10277 }
10278
10279 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
10280 // if it has a different minimum size, we should layout again
10281 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
10282 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
10283 requestLayout = true;
10284 }
10285
10286 d.setCallback(this);
10287 if (d.isStateful()) {
10288 d.setState(getDrawableState());
10289 }
10290 d.setVisible(getVisibility() == VISIBLE, false);
10291 mBGDrawable = d;
10292
10293 if ((mPrivateFlags & SKIP_DRAW) != 0) {
10294 mPrivateFlags &= ~SKIP_DRAW;
10295 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
10296 requestLayout = true;
10297 }
10298 } else {
10299 /* Remove the background */
10300 mBGDrawable = null;
10301
10302 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
10303 /*
10304 * This view ONLY drew the background before and we're removing
10305 * the background, so now it won't draw anything
10306 * (hence we SKIP_DRAW)
10307 */
10308 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
10309 mPrivateFlags |= SKIP_DRAW;
10310 }
10311
10312 /*
10313 * When the background is set, we try to apply its padding to this
10314 * View. When the background is removed, we don't touch this View's
10315 * padding. This is noted in the Javadocs. Hence, we don't need to
10316 * requestLayout(), the invalidate() below is sufficient.
10317 */
10318
10319 // The old background's minimum size could have affected this
10320 // View's layout, so let's requestLayout
10321 requestLayout = true;
10322 }
10323
Romain Guy8f1344f52009-05-15 16:03:59 -070010324 computeOpaqueFlags();
10325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010326 if (requestLayout) {
10327 requestLayout();
10328 }
10329
10330 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010331 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010332 }
10333
10334 /**
10335 * Gets the background drawable
10336 * @return The drawable used as the background for this view, if any.
10337 */
10338 public Drawable getBackground() {
10339 return mBGDrawable;
10340 }
10341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010342 /**
10343 * Sets the padding. The view may add on the space required to display
10344 * the scrollbars, depending on the style and visibility of the scrollbars.
10345 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
10346 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
10347 * from the values set in this call.
10348 *
10349 * @attr ref android.R.styleable#View_padding
10350 * @attr ref android.R.styleable#View_paddingBottom
10351 * @attr ref android.R.styleable#View_paddingLeft
10352 * @attr ref android.R.styleable#View_paddingRight
10353 * @attr ref android.R.styleable#View_paddingTop
10354 * @param left the left padding in pixels
10355 * @param top the top padding in pixels
10356 * @param right the right padding in pixels
10357 * @param bottom the bottom padding in pixels
10358 */
10359 public void setPadding(int left, int top, int right, int bottom) {
10360 boolean changed = false;
10361
Adam Powell20232d02010-12-08 21:08:53 -080010362 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010363 mUserPaddingRight = right;
10364 mUserPaddingBottom = bottom;
10365
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010366 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070010367
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010368 // Common case is there are no scroll bars.
10369 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010370 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080010371 // TODO Determine what to do with SCROLLBAR_POSITION_DEFAULT based on RTL settings.
10372 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010373 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080010374 switch (mVerticalScrollbarPosition) {
10375 case SCROLLBAR_POSITION_DEFAULT:
10376 case SCROLLBAR_POSITION_RIGHT:
10377 right += offset;
10378 break;
10379 case SCROLLBAR_POSITION_LEFT:
10380 left += offset;
10381 break;
10382 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010383 }
Adam Powell20232d02010-12-08 21:08:53 -080010384 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010385 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
10386 ? 0 : getHorizontalScrollbarHeight();
10387 }
10388 }
Romain Guy8506ab42009-06-11 17:35:47 -070010389
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010390 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010391 changed = true;
10392 mPaddingLeft = left;
10393 }
10394 if (mPaddingTop != top) {
10395 changed = true;
10396 mPaddingTop = top;
10397 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010398 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010399 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010400 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010401 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010402 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010403 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010404 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010405 }
10406
10407 if (changed) {
10408 requestLayout();
10409 }
10410 }
10411
10412 /**
10413 * Returns the top padding of this view.
10414 *
10415 * @return the top padding in pixels
10416 */
10417 public int getPaddingTop() {
10418 return mPaddingTop;
10419 }
10420
10421 /**
10422 * Returns the bottom padding of this view. If there are inset and enabled
10423 * scrollbars, this value may include the space required to display the
10424 * scrollbars as well.
10425 *
10426 * @return the bottom padding in pixels
10427 */
10428 public int getPaddingBottom() {
10429 return mPaddingBottom;
10430 }
10431
10432 /**
10433 * Returns the left padding of this view. If there are inset and enabled
10434 * scrollbars, this value may include the space required to display the
10435 * scrollbars as well.
10436 *
10437 * @return the left padding in pixels
10438 */
10439 public int getPaddingLeft() {
10440 return mPaddingLeft;
10441 }
10442
10443 /**
10444 * Returns the right padding of this view. If there are inset and enabled
10445 * scrollbars, this value may include the space required to display the
10446 * scrollbars as well.
10447 *
10448 * @return the right padding in pixels
10449 */
10450 public int getPaddingRight() {
10451 return mPaddingRight;
10452 }
10453
10454 /**
10455 * Changes the selection state of this view. A view can be selected or not.
10456 * Note that selection is not the same as focus. Views are typically
10457 * selected in the context of an AdapterView like ListView or GridView;
10458 * the selected view is the view that is highlighted.
10459 *
10460 * @param selected true if the view must be selected, false otherwise
10461 */
10462 public void setSelected(boolean selected) {
10463 if (((mPrivateFlags & SELECTED) != 0) != selected) {
10464 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070010465 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080010466 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010467 refreshDrawableState();
10468 dispatchSetSelected(selected);
10469 }
10470 }
10471
10472 /**
10473 * Dispatch setSelected to all of this View's children.
10474 *
10475 * @see #setSelected(boolean)
10476 *
10477 * @param selected The new selected state
10478 */
10479 protected void dispatchSetSelected(boolean selected) {
10480 }
10481
10482 /**
10483 * Indicates the selection state of this view.
10484 *
10485 * @return true if the view is selected, false otherwise
10486 */
10487 @ViewDebug.ExportedProperty
10488 public boolean isSelected() {
10489 return (mPrivateFlags & SELECTED) != 0;
10490 }
10491
10492 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010493 * Changes the activated state of this view. A view can be activated or not.
10494 * Note that activation is not the same as selection. Selection is
10495 * a transient property, representing the view (hierarchy) the user is
10496 * currently interacting with. Activation is a longer-term state that the
10497 * user can move views in and out of. For example, in a list view with
10498 * single or multiple selection enabled, the views in the current selection
10499 * set are activated. (Um, yeah, we are deeply sorry about the terminology
10500 * here.) The activated state is propagated down to children of the view it
10501 * is set on.
10502 *
10503 * @param activated true if the view must be activated, false otherwise
10504 */
10505 public void setActivated(boolean activated) {
10506 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
10507 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010508 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010509 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070010510 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010511 }
10512 }
10513
10514 /**
10515 * Dispatch setActivated to all of this View's children.
10516 *
10517 * @see #setActivated(boolean)
10518 *
10519 * @param activated The new activated state
10520 */
10521 protected void dispatchSetActivated(boolean activated) {
10522 }
10523
10524 /**
10525 * Indicates the activation state of this view.
10526 *
10527 * @return true if the view is activated, false otherwise
10528 */
10529 @ViewDebug.ExportedProperty
10530 public boolean isActivated() {
10531 return (mPrivateFlags & ACTIVATED) != 0;
10532 }
10533
10534 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010535 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
10536 * observer can be used to get notifications when global events, like
10537 * layout, happen.
10538 *
10539 * The returned ViewTreeObserver observer is not guaranteed to remain
10540 * valid for the lifetime of this View. If the caller of this method keeps
10541 * a long-lived reference to ViewTreeObserver, it should always check for
10542 * the return value of {@link ViewTreeObserver#isAlive()}.
10543 *
10544 * @return The ViewTreeObserver for this view's hierarchy.
10545 */
10546 public ViewTreeObserver getViewTreeObserver() {
10547 if (mAttachInfo != null) {
10548 return mAttachInfo.mTreeObserver;
10549 }
10550 if (mFloatingTreeObserver == null) {
10551 mFloatingTreeObserver = new ViewTreeObserver();
10552 }
10553 return mFloatingTreeObserver;
10554 }
10555
10556 /**
10557 * <p>Finds the topmost view in the current view hierarchy.</p>
10558 *
10559 * @return the topmost view containing this view
10560 */
10561 public View getRootView() {
10562 if (mAttachInfo != null) {
10563 final View v = mAttachInfo.mRootView;
10564 if (v != null) {
10565 return v;
10566 }
10567 }
Romain Guy8506ab42009-06-11 17:35:47 -070010568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010569 View parent = this;
10570
10571 while (parent.mParent != null && parent.mParent instanceof View) {
10572 parent = (View) parent.mParent;
10573 }
10574
10575 return parent;
10576 }
10577
10578 /**
10579 * <p>Computes the coordinates of this view on the screen. The argument
10580 * must be an array of two integers. After the method returns, the array
10581 * contains the x and y location in that order.</p>
10582 *
10583 * @param location an array of two integers in which to hold the coordinates
10584 */
10585 public void getLocationOnScreen(int[] location) {
10586 getLocationInWindow(location);
10587
10588 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070010589 if (info != null) {
10590 location[0] += info.mWindowLeft;
10591 location[1] += info.mWindowTop;
10592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010593 }
10594
10595 /**
10596 * <p>Computes the coordinates of this view in its window. The argument
10597 * must be an array of two integers. After the method returns, the array
10598 * contains the x and y location in that order.</p>
10599 *
10600 * @param location an array of two integers in which to hold the coordinates
10601 */
10602 public void getLocationInWindow(int[] location) {
10603 if (location == null || location.length < 2) {
10604 throw new IllegalArgumentException("location must be an array of "
10605 + "two integers");
10606 }
10607
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010608 location[0] = mLeft + (int) (mTranslationX + 0.5f);
10609 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010610
10611 ViewParent viewParent = mParent;
10612 while (viewParent instanceof View) {
10613 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010614 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
10615 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010616 viewParent = view.mParent;
10617 }
Romain Guy8506ab42009-06-11 17:35:47 -070010618
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010619 if (viewParent instanceof ViewAncestor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010620 // *cough*
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010621 final ViewAncestor vr = (ViewAncestor)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010622 location[1] -= vr.mCurScrollY;
10623 }
10624 }
10625
10626 /**
10627 * {@hide}
10628 * @param id the id of the view to be found
10629 * @return the view of the specified id, null if cannot be found
10630 */
10631 protected View findViewTraversal(int id) {
10632 if (id == mID) {
10633 return this;
10634 }
10635 return null;
10636 }
10637
10638 /**
10639 * {@hide}
10640 * @param tag the tag of the view to be found
10641 * @return the view of specified tag, null if cannot be found
10642 */
10643 protected View findViewWithTagTraversal(Object tag) {
10644 if (tag != null && tag.equals(mTag)) {
10645 return this;
10646 }
10647 return null;
10648 }
10649
10650 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010651 * {@hide}
10652 * @param predicate The predicate to evaluate.
10653 * @return The first view that matches the predicate or null.
10654 */
10655 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
10656 if (predicate.apply(this)) {
10657 return this;
10658 }
10659 return null;
10660 }
10661
10662 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010663 * Look for a child view with the given id. If this view has the given
10664 * id, return this view.
10665 *
10666 * @param id The id to search for.
10667 * @return The view that has the given id in the hierarchy or null
10668 */
10669 public final View findViewById(int id) {
10670 if (id < 0) {
10671 return null;
10672 }
10673 return findViewTraversal(id);
10674 }
10675
10676 /**
10677 * Look for a child view with the given tag. If this view has the given
10678 * tag, return this view.
10679 *
10680 * @param tag The tag to search for, using "tag.equals(getTag())".
10681 * @return The View that has the given tag in the hierarchy or null
10682 */
10683 public final View findViewWithTag(Object tag) {
10684 if (tag == null) {
10685 return null;
10686 }
10687 return findViewWithTagTraversal(tag);
10688 }
10689
10690 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010691 * {@hide}
10692 * Look for a child view that matches the specified predicate.
10693 * If this view matches the predicate, return this view.
10694 *
10695 * @param predicate The predicate to evaluate.
10696 * @return The first view that matches the predicate or null.
10697 */
10698 public final View findViewByPredicate(Predicate<View> predicate) {
10699 return findViewByPredicateTraversal(predicate);
10700 }
10701
10702 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010703 * Sets the identifier for this view. The identifier does not have to be
10704 * unique in this view's hierarchy. The identifier should be a positive
10705 * number.
10706 *
10707 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070010708 * @see #getId()
10709 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010710 *
10711 * @param id a number used to identify the view
10712 *
10713 * @attr ref android.R.styleable#View_id
10714 */
10715 public void setId(int id) {
10716 mID = id;
10717 }
10718
10719 /**
10720 * {@hide}
10721 *
10722 * @param isRoot true if the view belongs to the root namespace, false
10723 * otherwise
10724 */
10725 public void setIsRootNamespace(boolean isRoot) {
10726 if (isRoot) {
10727 mPrivateFlags |= IS_ROOT_NAMESPACE;
10728 } else {
10729 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
10730 }
10731 }
10732
10733 /**
10734 * {@hide}
10735 *
10736 * @return true if the view belongs to the root namespace, false otherwise
10737 */
10738 public boolean isRootNamespace() {
10739 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
10740 }
10741
10742 /**
10743 * Returns this view's identifier.
10744 *
10745 * @return a positive integer used to identify the view or {@link #NO_ID}
10746 * if the view has no ID
10747 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010748 * @see #setId(int)
10749 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010750 * @attr ref android.R.styleable#View_id
10751 */
10752 @ViewDebug.CapturedViewProperty
10753 public int getId() {
10754 return mID;
10755 }
10756
10757 /**
10758 * Returns this view's tag.
10759 *
10760 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070010761 *
10762 * @see #setTag(Object)
10763 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010764 */
10765 @ViewDebug.ExportedProperty
10766 public Object getTag() {
10767 return mTag;
10768 }
10769
10770 /**
10771 * Sets the tag associated with this view. A tag can be used to mark
10772 * a view in its hierarchy and does not have to be unique within the
10773 * hierarchy. Tags can also be used to store data within a view without
10774 * resorting to another data structure.
10775 *
10776 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070010777 *
10778 * @see #getTag()
10779 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010780 */
10781 public void setTag(final Object tag) {
10782 mTag = tag;
10783 }
10784
10785 /**
Romain Guyd90a3312009-05-06 14:54:28 -070010786 * Returns the tag associated with this view and the specified key.
10787 *
10788 * @param key The key identifying the tag
10789 *
10790 * @return the Object stored in this view as a tag
10791 *
10792 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070010793 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070010794 */
10795 public Object getTag(int key) {
10796 SparseArray<Object> tags = null;
10797 synchronized (sTagsLock) {
10798 if (sTags != null) {
10799 tags = sTags.get(this);
10800 }
10801 }
10802
10803 if (tags != null) return tags.get(key);
10804 return null;
10805 }
10806
10807 /**
10808 * Sets a tag associated with this view and a key. A tag can be used
10809 * to mark a view in its hierarchy and does not have to be unique within
10810 * the hierarchy. Tags can also be used to store data within a view
10811 * without resorting to another data structure.
10812 *
10813 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070010814 * application to ensure it is unique (see the <a
10815 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
10816 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070010817 * the Android framework or not associated with any package will cause
10818 * an {@link IllegalArgumentException} to be thrown.
10819 *
10820 * @param key The key identifying the tag
10821 * @param tag An Object to tag the view with
10822 *
10823 * @throws IllegalArgumentException If they specified key is not valid
10824 *
10825 * @see #setTag(Object)
10826 * @see #getTag(int)
10827 */
10828 public void setTag(int key, final Object tag) {
10829 // If the package id is 0x00 or 0x01, it's either an undefined package
10830 // or a framework id
10831 if ((key >>> 24) < 2) {
10832 throw new IllegalArgumentException("The key must be an application-specific "
10833 + "resource id.");
10834 }
10835
10836 setTagInternal(this, key, tag);
10837 }
10838
10839 /**
10840 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
10841 * framework id.
10842 *
10843 * @hide
10844 */
10845 public void setTagInternal(int key, Object tag) {
10846 if ((key >>> 24) != 0x1) {
10847 throw new IllegalArgumentException("The key must be a framework-specific "
10848 + "resource id.");
10849 }
10850
Romain Guy8506ab42009-06-11 17:35:47 -070010851 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070010852 }
10853
10854 private static void setTagInternal(View view, int key, Object tag) {
10855 SparseArray<Object> tags = null;
10856 synchronized (sTagsLock) {
10857 if (sTags == null) {
10858 sTags = new WeakHashMap<View, SparseArray<Object>>();
10859 } else {
10860 tags = sTags.get(view);
10861 }
10862 }
10863
10864 if (tags == null) {
10865 tags = new SparseArray<Object>(2);
10866 synchronized (sTagsLock) {
10867 sTags.put(view, tags);
10868 }
10869 }
10870
10871 tags.put(key, tag);
10872 }
10873
10874 /**
Romain Guy13922e02009-05-12 17:56:14 -070010875 * @param consistency The type of consistency. See ViewDebug for more information.
10876 *
10877 * @hide
10878 */
10879 protected boolean dispatchConsistencyCheck(int consistency) {
10880 return onConsistencyCheck(consistency);
10881 }
10882
10883 /**
10884 * Method that subclasses should implement to check their consistency. The type of
10885 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070010886 *
Romain Guy13922e02009-05-12 17:56:14 -070010887 * @param consistency The type of consistency. See ViewDebug for more information.
10888 *
10889 * @throws IllegalStateException if the view is in an inconsistent state.
10890 *
10891 * @hide
10892 */
10893 protected boolean onConsistencyCheck(int consistency) {
10894 boolean result = true;
10895
10896 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
10897 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
10898
10899 if (checkLayout) {
10900 if (getParent() == null) {
10901 result = false;
10902 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10903 "View " + this + " does not have a parent.");
10904 }
10905
10906 if (mAttachInfo == null) {
10907 result = false;
10908 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10909 "View " + this + " is not attached to a window.");
10910 }
10911 }
10912
10913 if (checkDrawing) {
10914 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
10915 // from their draw() method
10916
10917 if ((mPrivateFlags & DRAWN) != DRAWN &&
10918 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
10919 result = false;
10920 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10921 "View " + this + " was invalidated but its drawing cache is valid.");
10922 }
10923 }
10924
10925 return result;
10926 }
10927
10928 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010929 * Prints information about this view in the log output, with the tag
10930 * {@link #VIEW_LOG_TAG}.
10931 *
10932 * @hide
10933 */
10934 public void debug() {
10935 debug(0);
10936 }
10937
10938 /**
10939 * Prints information about this view in the log output, with the tag
10940 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
10941 * indentation defined by the <code>depth</code>.
10942 *
10943 * @param depth the indentation level
10944 *
10945 * @hide
10946 */
10947 protected void debug(int depth) {
10948 String output = debugIndent(depth - 1);
10949
10950 output += "+ " + this;
10951 int id = getId();
10952 if (id != -1) {
10953 output += " (id=" + id + ")";
10954 }
10955 Object tag = getTag();
10956 if (tag != null) {
10957 output += " (tag=" + tag + ")";
10958 }
10959 Log.d(VIEW_LOG_TAG, output);
10960
10961 if ((mPrivateFlags & FOCUSED) != 0) {
10962 output = debugIndent(depth) + " FOCUSED";
10963 Log.d(VIEW_LOG_TAG, output);
10964 }
10965
10966 output = debugIndent(depth);
10967 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
10968 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
10969 + "} ";
10970 Log.d(VIEW_LOG_TAG, output);
10971
10972 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
10973 || mPaddingBottom != 0) {
10974 output = debugIndent(depth);
10975 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
10976 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
10977 Log.d(VIEW_LOG_TAG, output);
10978 }
10979
10980 output = debugIndent(depth);
10981 output += "mMeasureWidth=" + mMeasuredWidth +
10982 " mMeasureHeight=" + mMeasuredHeight;
10983 Log.d(VIEW_LOG_TAG, output);
10984
10985 output = debugIndent(depth);
10986 if (mLayoutParams == null) {
10987 output += "BAD! no layout params";
10988 } else {
10989 output = mLayoutParams.debug(output);
10990 }
10991 Log.d(VIEW_LOG_TAG, output);
10992
10993 output = debugIndent(depth);
10994 output += "flags={";
10995 output += View.printFlags(mViewFlags);
10996 output += "}";
10997 Log.d(VIEW_LOG_TAG, output);
10998
10999 output = debugIndent(depth);
11000 output += "privateFlags={";
11001 output += View.printPrivateFlags(mPrivateFlags);
11002 output += "}";
11003 Log.d(VIEW_LOG_TAG, output);
11004 }
11005
11006 /**
11007 * Creates an string of whitespaces used for indentation.
11008 *
11009 * @param depth the indentation level
11010 * @return a String containing (depth * 2 + 3) * 2 white spaces
11011 *
11012 * @hide
11013 */
11014 protected static String debugIndent(int depth) {
11015 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
11016 for (int i = 0; i < (depth * 2) + 3; i++) {
11017 spaces.append(' ').append(' ');
11018 }
11019 return spaces.toString();
11020 }
11021
11022 /**
11023 * <p>Return the offset of the widget's text baseline from the widget's top
11024 * boundary. If this widget does not support baseline alignment, this
11025 * method returns -1. </p>
11026 *
11027 * @return the offset of the baseline within the widget's bounds or -1
11028 * if baseline alignment is not supported
11029 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011030 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011031 public int getBaseline() {
11032 return -1;
11033 }
11034
11035 /**
11036 * Call this when something has changed which has invalidated the
11037 * layout of this view. This will schedule a layout pass of the view
11038 * tree.
11039 */
11040 public void requestLayout() {
11041 if (ViewDebug.TRACE_HIERARCHY) {
11042 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
11043 }
11044
11045 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011046 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011047
11048 if (mParent != null && !mParent.isLayoutRequested()) {
11049 mParent.requestLayout();
11050 }
11051 }
11052
11053 /**
11054 * Forces this view to be laid out during the next layout pass.
11055 * This method does not call requestLayout() or forceLayout()
11056 * on the parent.
11057 */
11058 public void forceLayout() {
11059 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011060 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011061 }
11062
11063 /**
11064 * <p>
11065 * This is called to find out how big a view should be. The parent
11066 * supplies constraint information in the width and height parameters.
11067 * </p>
11068 *
11069 * <p>
11070 * The actual mesurement work of a view is performed in
11071 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
11072 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
11073 * </p>
11074 *
11075 *
11076 * @param widthMeasureSpec Horizontal space requirements as imposed by the
11077 * parent
11078 * @param heightMeasureSpec Vertical space requirements as imposed by the
11079 * parent
11080 *
11081 * @see #onMeasure(int, int)
11082 */
11083 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
11084 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
11085 widthMeasureSpec != mOldWidthMeasureSpec ||
11086 heightMeasureSpec != mOldHeightMeasureSpec) {
11087
11088 // first clears the measured dimension flag
11089 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
11090
11091 if (ViewDebug.TRACE_HIERARCHY) {
11092 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
11093 }
11094
11095 // measure ourselves, this should set the measured dimension flag back
11096 onMeasure(widthMeasureSpec, heightMeasureSpec);
11097
11098 // flag not set, setMeasuredDimension() was not invoked, we raise
11099 // an exception to warn the developer
11100 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
11101 throw new IllegalStateException("onMeasure() did not set the"
11102 + " measured dimension by calling"
11103 + " setMeasuredDimension()");
11104 }
11105
11106 mPrivateFlags |= LAYOUT_REQUIRED;
11107 }
11108
11109 mOldWidthMeasureSpec = widthMeasureSpec;
11110 mOldHeightMeasureSpec = heightMeasureSpec;
11111 }
11112
11113 /**
11114 * <p>
11115 * Measure the view and its content to determine the measured width and the
11116 * measured height. This method is invoked by {@link #measure(int, int)} and
11117 * should be overriden by subclasses to provide accurate and efficient
11118 * measurement of their contents.
11119 * </p>
11120 *
11121 * <p>
11122 * <strong>CONTRACT:</strong> When overriding this method, you
11123 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
11124 * measured width and height of this view. Failure to do so will trigger an
11125 * <code>IllegalStateException</code>, thrown by
11126 * {@link #measure(int, int)}. Calling the superclass'
11127 * {@link #onMeasure(int, int)} is a valid use.
11128 * </p>
11129 *
11130 * <p>
11131 * The base class implementation of measure defaults to the background size,
11132 * unless a larger size is allowed by the MeasureSpec. Subclasses should
11133 * override {@link #onMeasure(int, int)} to provide better measurements of
11134 * their content.
11135 * </p>
11136 *
11137 * <p>
11138 * If this method is overridden, it is the subclass's responsibility to make
11139 * sure the measured height and width are at least the view's minimum height
11140 * and width ({@link #getSuggestedMinimumHeight()} and
11141 * {@link #getSuggestedMinimumWidth()}).
11142 * </p>
11143 *
11144 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
11145 * The requirements are encoded with
11146 * {@link android.view.View.MeasureSpec}.
11147 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
11148 * The requirements are encoded with
11149 * {@link android.view.View.MeasureSpec}.
11150 *
11151 * @see #getMeasuredWidth()
11152 * @see #getMeasuredHeight()
11153 * @see #setMeasuredDimension(int, int)
11154 * @see #getSuggestedMinimumHeight()
11155 * @see #getSuggestedMinimumWidth()
11156 * @see android.view.View.MeasureSpec#getMode(int)
11157 * @see android.view.View.MeasureSpec#getSize(int)
11158 */
11159 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
11160 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
11161 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
11162 }
11163
11164 /**
11165 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
11166 * measured width and measured height. Failing to do so will trigger an
11167 * exception at measurement time.</p>
11168 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080011169 * @param measuredWidth The measured width of this view. May be a complex
11170 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11171 * {@link #MEASURED_STATE_TOO_SMALL}.
11172 * @param measuredHeight The measured height of this view. May be a complex
11173 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11174 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011175 */
11176 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
11177 mMeasuredWidth = measuredWidth;
11178 mMeasuredHeight = measuredHeight;
11179
11180 mPrivateFlags |= MEASURED_DIMENSION_SET;
11181 }
11182
11183 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080011184 * Merge two states as returned by {@link #getMeasuredState()}.
11185 * @param curState The current state as returned from a view or the result
11186 * of combining multiple views.
11187 * @param newState The new view state to combine.
11188 * @return Returns a new integer reflecting the combination of the two
11189 * states.
11190 */
11191 public static int combineMeasuredStates(int curState, int newState) {
11192 return curState | newState;
11193 }
11194
11195 /**
11196 * Version of {@link #resolveSizeAndState(int, int, int)}
11197 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
11198 */
11199 public static int resolveSize(int size, int measureSpec) {
11200 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
11201 }
11202
11203 /**
11204 * Utility to reconcile a desired size and state, with constraints imposed
11205 * by a MeasureSpec. Will take the desired size, unless a different size
11206 * is imposed by the constraints. The returned value is a compound integer,
11207 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
11208 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
11209 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011210 *
11211 * @param size How big the view wants to be
11212 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080011213 * @return Size information bit mask as defined by
11214 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011215 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080011216 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011217 int result = size;
11218 int specMode = MeasureSpec.getMode(measureSpec);
11219 int specSize = MeasureSpec.getSize(measureSpec);
11220 switch (specMode) {
11221 case MeasureSpec.UNSPECIFIED:
11222 result = size;
11223 break;
11224 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080011225 if (specSize < size) {
11226 result = specSize | MEASURED_STATE_TOO_SMALL;
11227 } else {
11228 result = size;
11229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011230 break;
11231 case MeasureSpec.EXACTLY:
11232 result = specSize;
11233 break;
11234 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080011235 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011236 }
11237
11238 /**
11239 * Utility to return a default size. Uses the supplied size if the
11240 * MeasureSpec imposed no contraints. Will get larger if allowed
11241 * by the MeasureSpec.
11242 *
11243 * @param size Default size for this view
11244 * @param measureSpec Constraints imposed by the parent
11245 * @return The size this view should be.
11246 */
11247 public static int getDefaultSize(int size, int measureSpec) {
11248 int result = size;
11249 int specMode = MeasureSpec.getMode(measureSpec);
11250 int specSize = MeasureSpec.getSize(measureSpec);
11251
11252 switch (specMode) {
11253 case MeasureSpec.UNSPECIFIED:
11254 result = size;
11255 break;
11256 case MeasureSpec.AT_MOST:
11257 case MeasureSpec.EXACTLY:
11258 result = specSize;
11259 break;
11260 }
11261 return result;
11262 }
11263
11264 /**
11265 * Returns the suggested minimum height that the view should use. This
11266 * returns the maximum of the view's minimum height
11267 * and the background's minimum height
11268 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
11269 * <p>
11270 * When being used in {@link #onMeasure(int, int)}, the caller should still
11271 * ensure the returned height is within the requirements of the parent.
11272 *
11273 * @return The suggested minimum height of the view.
11274 */
11275 protected int getSuggestedMinimumHeight() {
11276 int suggestedMinHeight = mMinHeight;
11277
11278 if (mBGDrawable != null) {
11279 final int bgMinHeight = mBGDrawable.getMinimumHeight();
11280 if (suggestedMinHeight < bgMinHeight) {
11281 suggestedMinHeight = bgMinHeight;
11282 }
11283 }
11284
11285 return suggestedMinHeight;
11286 }
11287
11288 /**
11289 * Returns the suggested minimum width that the view should use. This
11290 * returns the maximum of the view's minimum width)
11291 * and the background's minimum width
11292 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
11293 * <p>
11294 * When being used in {@link #onMeasure(int, int)}, the caller should still
11295 * ensure the returned width is within the requirements of the parent.
11296 *
11297 * @return The suggested minimum width of the view.
11298 */
11299 protected int getSuggestedMinimumWidth() {
11300 int suggestedMinWidth = mMinWidth;
11301
11302 if (mBGDrawable != null) {
11303 final int bgMinWidth = mBGDrawable.getMinimumWidth();
11304 if (suggestedMinWidth < bgMinWidth) {
11305 suggestedMinWidth = bgMinWidth;
11306 }
11307 }
11308
11309 return suggestedMinWidth;
11310 }
11311
11312 /**
11313 * Sets the minimum height of the view. It is not guaranteed the view will
11314 * be able to achieve this minimum height (for example, if its parent layout
11315 * constrains it with less available height).
11316 *
11317 * @param minHeight The minimum height the view will try to be.
11318 */
11319 public void setMinimumHeight(int minHeight) {
11320 mMinHeight = minHeight;
11321 }
11322
11323 /**
11324 * Sets the minimum width of the view. It is not guaranteed the view will
11325 * be able to achieve this minimum width (for example, if its parent layout
11326 * constrains it with less available width).
11327 *
11328 * @param minWidth The minimum width the view will try to be.
11329 */
11330 public void setMinimumWidth(int minWidth) {
11331 mMinWidth = minWidth;
11332 }
11333
11334 /**
11335 * Get the animation currently associated with this view.
11336 *
11337 * @return The animation that is currently playing or
11338 * scheduled to play for this view.
11339 */
11340 public Animation getAnimation() {
11341 return mCurrentAnimation;
11342 }
11343
11344 /**
11345 * Start the specified animation now.
11346 *
11347 * @param animation the animation to start now
11348 */
11349 public void startAnimation(Animation animation) {
11350 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
11351 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011352 invalidateParentCaches();
11353 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011354 }
11355
11356 /**
11357 * Cancels any animations for this view.
11358 */
11359 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080011360 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080011361 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080011362 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011363 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011364 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011365 }
11366
11367 /**
11368 * Sets the next animation to play for this view.
11369 * If you want the animation to play immediately, use
11370 * startAnimation. This method provides allows fine-grained
11371 * control over the start time and invalidation, but you
11372 * must make sure that 1) the animation has a start time set, and
11373 * 2) the view will be invalidated when the animation is supposed to
11374 * start.
11375 *
11376 * @param animation The next animation, or null.
11377 */
11378 public void setAnimation(Animation animation) {
11379 mCurrentAnimation = animation;
11380 if (animation != null) {
11381 animation.reset();
11382 }
11383 }
11384
11385 /**
11386 * Invoked by a parent ViewGroup to notify the start of the animation
11387 * currently associated with this view. If you override this method,
11388 * always call super.onAnimationStart();
11389 *
11390 * @see #setAnimation(android.view.animation.Animation)
11391 * @see #getAnimation()
11392 */
11393 protected void onAnimationStart() {
11394 mPrivateFlags |= ANIMATION_STARTED;
11395 }
11396
11397 /**
11398 * Invoked by a parent ViewGroup to notify the end of the animation
11399 * currently associated with this view. If you override this method,
11400 * always call super.onAnimationEnd();
11401 *
11402 * @see #setAnimation(android.view.animation.Animation)
11403 * @see #getAnimation()
11404 */
11405 protected void onAnimationEnd() {
11406 mPrivateFlags &= ~ANIMATION_STARTED;
11407 }
11408
11409 /**
11410 * Invoked if there is a Transform that involves alpha. Subclass that can
11411 * draw themselves with the specified alpha should return true, and then
11412 * respect that alpha when their onDraw() is called. If this returns false
11413 * then the view may be redirected to draw into an offscreen buffer to
11414 * fulfill the request, which will look fine, but may be slower than if the
11415 * subclass handles it internally. The default implementation returns false.
11416 *
11417 * @param alpha The alpha (0..255) to apply to the view's drawing
11418 * @return true if the view can draw with the specified alpha.
11419 */
11420 protected boolean onSetAlpha(int alpha) {
11421 return false;
11422 }
11423
11424 /**
11425 * This is used by the RootView to perform an optimization when
11426 * the view hierarchy contains one or several SurfaceView.
11427 * SurfaceView is always considered transparent, but its children are not,
11428 * therefore all View objects remove themselves from the global transparent
11429 * region (passed as a parameter to this function).
11430 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011431 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011432 *
11433 * @return Returns true if the effective visibility of the view at this
11434 * point is opaque, regardless of the transparent region; returns false
11435 * if it is possible for underlying windows to be seen behind the view.
11436 *
11437 * {@hide}
11438 */
11439 public boolean gatherTransparentRegion(Region region) {
11440 final AttachInfo attachInfo = mAttachInfo;
11441 if (region != null && attachInfo != null) {
11442 final int pflags = mPrivateFlags;
11443 if ((pflags & SKIP_DRAW) == 0) {
11444 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
11445 // remove it from the transparent region.
11446 final int[] location = attachInfo.mTransparentLocation;
11447 getLocationInWindow(location);
11448 region.op(location[0], location[1], location[0] + mRight - mLeft,
11449 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
11450 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
11451 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
11452 // exists, so we remove the background drawable's non-transparent
11453 // parts from this transparent region.
11454 applyDrawableToTransparentRegion(mBGDrawable, region);
11455 }
11456 }
11457 return true;
11458 }
11459
11460 /**
11461 * Play a sound effect for this view.
11462 *
11463 * <p>The framework will play sound effects for some built in actions, such as
11464 * clicking, but you may wish to play these effects in your widget,
11465 * for instance, for internal navigation.
11466 *
11467 * <p>The sound effect will only be played if sound effects are enabled by the user, and
11468 * {@link #isSoundEffectsEnabled()} is true.
11469 *
11470 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
11471 */
11472 public void playSoundEffect(int soundConstant) {
11473 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
11474 return;
11475 }
11476 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
11477 }
11478
11479 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011480 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011481 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011482 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011483 *
11484 * <p>The framework will provide haptic feedback for some built in actions,
11485 * such as long presses, but you may wish to provide feedback for your
11486 * own widget.
11487 *
11488 * <p>The feedback will only be performed if
11489 * {@link #isHapticFeedbackEnabled()} is true.
11490 *
11491 * @param feedbackConstant One of the constants defined in
11492 * {@link HapticFeedbackConstants}
11493 */
11494 public boolean performHapticFeedback(int feedbackConstant) {
11495 return performHapticFeedback(feedbackConstant, 0);
11496 }
11497
11498 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011499 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011500 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011501 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011502 *
11503 * @param feedbackConstant One of the constants defined in
11504 * {@link HapticFeedbackConstants}
11505 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
11506 */
11507 public boolean performHapticFeedback(int feedbackConstant, int flags) {
11508 if (mAttachInfo == null) {
11509 return false;
11510 }
Romain Guyf607bdc2010-09-10 19:20:06 -070011511 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070011512 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011513 && !isHapticFeedbackEnabled()) {
11514 return false;
11515 }
Romain Guy812ccbe2010-06-01 14:07:24 -070011516 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
11517 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011518 }
11519
11520 /**
Joe Onorato664644d2011-01-23 17:53:23 -080011521 * Request that the visibility of the status bar be changed.
11522 */
11523 public void setSystemUiVisibility(int visibility) {
11524 if (visibility != mSystemUiVisibility) {
11525 mSystemUiVisibility = visibility;
11526 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11527 mParent.recomputeViewAttributes(this);
11528 }
11529 }
11530 }
11531
11532 /**
11533 * Returns the status bar visibility that this view has requested.
11534 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080011535 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080011536 return mSystemUiVisibility;
11537 }
11538
11539 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
11540 mOnSystemUiVisibilityChangeListener = l;
11541 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11542 mParent.recomputeViewAttributes(this);
11543 }
11544 }
11545
11546 /**
11547 */
11548 public void dispatchSystemUiVisibilityChanged(int visibility) {
11549 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080011550 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080011551 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080011552 }
11553 }
11554
11555 /**
Joe Malin32736f02011-01-19 16:14:20 -080011556 * Creates an image that the system displays during the drag and drop
11557 * operation. This is called a &quot;drag shadow&quot;. The default implementation
11558 * for a DragShadowBuilder based on a View returns an image that has exactly the same
11559 * appearance as the given View. The default also positions the center of the drag shadow
11560 * directly under the touch point. If no View is provided (the constructor with no parameters
11561 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
11562 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
11563 * default is an invisible drag shadow.
11564 * <p>
11565 * You are not required to use the View you provide to the constructor as the basis of the
11566 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
11567 * anything you want as the drag shadow.
11568 * </p>
11569 * <p>
11570 * You pass a DragShadowBuilder object to the system when you start the drag. The system
11571 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
11572 * size and position of the drag shadow. It uses this data to construct a
11573 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
11574 * so that your application can draw the shadow image in the Canvas.
11575 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011576 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011577 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070011578 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070011579
11580 /**
Joe Malin32736f02011-01-19 16:14:20 -080011581 * Constructs a shadow image builder based on a View. By default, the resulting drag
11582 * shadow will have the same appearance and dimensions as the View, with the touch point
11583 * over the center of the View.
11584 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070011585 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011586 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070011587 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070011588 }
11589
Christopher Tate17ed60c2011-01-18 12:50:26 -080011590 /**
11591 * Construct a shadow builder object with no associated View. This
11592 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
11593 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
11594 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080011595 * reference to any View object. If they are not overridden, then the result is an
11596 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080011597 */
11598 public DragShadowBuilder() {
11599 mView = new WeakReference<View>(null);
11600 }
11601
11602 /**
11603 * Returns the View object that had been passed to the
11604 * {@link #View.DragShadowBuilder(View)}
11605 * constructor. If that View parameter was {@code null} or if the
11606 * {@link #View.DragShadowBuilder()}
11607 * constructor was used to instantiate the builder object, this method will return
11608 * null.
11609 *
11610 * @return The View object associate with this builder object.
11611 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070011612 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070011613 final public View getView() {
11614 return mView.get();
11615 }
11616
Christopher Tate2c095f32010-10-04 14:13:40 -070011617 /**
Joe Malin32736f02011-01-19 16:14:20 -080011618 * Provides the metrics for the shadow image. These include the dimensions of
11619 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070011620 * be centered under the touch location while dragging.
11621 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011622 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080011623 * same as the dimensions of the View itself and centers the shadow under
11624 * the touch point.
11625 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011626 *
Joe Malin32736f02011-01-19 16:14:20 -080011627 * @param shadowSize A {@link android.graphics.Point} containing the width and height
11628 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
11629 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
11630 * image.
11631 *
11632 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
11633 * shadow image that should be underneath the touch point during the drag and drop
11634 * operation. Your application must set {@link android.graphics.Point#x} to the
11635 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070011636 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011637 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070011638 final View view = mView.get();
11639 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011640 shadowSize.set(view.getWidth(), view.getHeight());
11641 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070011642 } else {
11643 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
11644 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011645 }
11646
11647 /**
Joe Malin32736f02011-01-19 16:14:20 -080011648 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
11649 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011650 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070011651 *
Joe Malin32736f02011-01-19 16:14:20 -080011652 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070011653 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011654 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070011655 final View view = mView.get();
11656 if (view != null) {
11657 view.draw(canvas);
11658 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011659 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070011660 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011661 }
11662 }
11663
11664 /**
Joe Malin32736f02011-01-19 16:14:20 -080011665 * Starts a drag and drop operation. When your application calls this method, it passes a
11666 * {@link android.view.View.DragShadowBuilder} object to the system. The
11667 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
11668 * to get metrics for the drag shadow, and then calls the object's
11669 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
11670 * <p>
11671 * Once the system has the drag shadow, it begins the drag and drop operation by sending
11672 * drag events to all the View objects in your application that are currently visible. It does
11673 * this either by calling the View object's drag listener (an implementation of
11674 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
11675 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
11676 * Both are passed a {@link android.view.DragEvent} object that has a
11677 * {@link android.view.DragEvent#getAction()} value of
11678 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
11679 * </p>
11680 * <p>
11681 * Your application can invoke startDrag() on any attached View object. The View object does not
11682 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
11683 * be related to the View the user selected for dragging.
11684 * </p>
11685 * @param data A {@link android.content.ClipData} object pointing to the data to be
11686 * transferred by the drag and drop operation.
11687 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
11688 * drag shadow.
11689 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
11690 * drop operation. This Object is put into every DragEvent object sent by the system during the
11691 * current drag.
11692 * <p>
11693 * myLocalState is a lightweight mechanism for the sending information from the dragged View
11694 * to the target Views. For example, it can contain flags that differentiate between a
11695 * a copy operation and a move operation.
11696 * </p>
11697 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
11698 * so the parameter should be set to 0.
11699 * @return {@code true} if the method completes successfully, or
11700 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
11701 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070011702 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011703 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011704 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070011705 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011706 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070011707 }
11708 boolean okay = false;
11709
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011710 Point shadowSize = new Point();
11711 Point shadowTouchPoint = new Point();
11712 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070011713
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011714 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
11715 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
11716 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070011717 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011718
Chris Tatea32dcf72010-10-14 12:13:50 -070011719 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011720 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
11721 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070011722 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011723 Surface surface = new Surface();
11724 try {
11725 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011726 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070011727 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070011728 + " surface=" + surface);
11729 if (token != null) {
11730 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070011731 try {
Chris Tate6b391282010-10-14 15:48:59 -070011732 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011733 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070011734 } finally {
11735 surface.unlockCanvasAndPost(canvas);
11736 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011737
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011738 final ViewAncestor root = getViewAncestor();
Christopher Tate407b4e92010-11-30 17:14:08 -080011739
11740 // Cache the local state object for delivery with DragEvents
11741 root.setLocalDragState(myLocalState);
11742
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011743 // repurpose 'shadowSize' for the last touch point
11744 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070011745
Christopher Tatea53146c2010-09-07 11:57:52 -070011746 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011747 shadowSize.x, shadowSize.y,
11748 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070011749 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070011750 }
11751 } catch (Exception e) {
11752 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
11753 surface.destroy();
11754 }
11755
11756 return okay;
11757 }
11758
Christopher Tatea53146c2010-09-07 11:57:52 -070011759 /**
Joe Malin32736f02011-01-19 16:14:20 -080011760 * Handles drag events sent by the system following a call to
11761 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
11762 *<p>
11763 * When the system calls this method, it passes a
11764 * {@link android.view.DragEvent} object. A call to
11765 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
11766 * in DragEvent. The method uses these to determine what is happening in the drag and drop
11767 * operation.
11768 * @param event The {@link android.view.DragEvent} sent by the system.
11769 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
11770 * in DragEvent, indicating the type of drag event represented by this object.
11771 * @return {@code true} if the method was successful, otherwise {@code false}.
11772 * <p>
11773 * The method should return {@code true} in response to an action type of
11774 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
11775 * operation.
11776 * </p>
11777 * <p>
11778 * The method should also return {@code true} in response to an action type of
11779 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
11780 * {@code false} if it didn't.
11781 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011782 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070011783 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070011784 return false;
11785 }
11786
11787 /**
Joe Malin32736f02011-01-19 16:14:20 -080011788 * Detects if this View is enabled and has a drag event listener.
11789 * If both are true, then it calls the drag event listener with the
11790 * {@link android.view.DragEvent} it received. If the drag event listener returns
11791 * {@code true}, then dispatchDragEvent() returns {@code true}.
11792 * <p>
11793 * For all other cases, the method calls the
11794 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
11795 * method and returns its result.
11796 * </p>
11797 * <p>
11798 * This ensures that a drag event is always consumed, even if the View does not have a drag
11799 * event listener. However, if the View has a listener and the listener returns true, then
11800 * onDragEvent() is not called.
11801 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011802 */
11803 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080011804 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070011805 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
11806 && mOnDragListener.onDrag(this, event)) {
11807 return true;
11808 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011809 return onDragEvent(event);
11810 }
11811
Christopher Tate3d4bf172011-03-28 16:16:46 -070011812 boolean canAcceptDrag() {
11813 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
11814 }
11815
Christopher Tatea53146c2010-09-07 11:57:52 -070011816 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070011817 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
11818 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070011819 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070011820 */
11821 public void onCloseSystemDialogs(String reason) {
11822 }
Joe Malin32736f02011-01-19 16:14:20 -080011823
Dianne Hackbornffa42482009-09-23 22:20:11 -070011824 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011825 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070011826 * update a Region being computed for
11827 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011828 * that any non-transparent parts of the Drawable are removed from the
11829 * given transparent region.
11830 *
11831 * @param dr The Drawable whose transparency is to be applied to the region.
11832 * @param region A Region holding the current transparency information,
11833 * where any parts of the region that are set are considered to be
11834 * transparent. On return, this region will be modified to have the
11835 * transparency information reduced by the corresponding parts of the
11836 * Drawable that are not transparent.
11837 * {@hide}
11838 */
11839 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
11840 if (DBG) {
11841 Log.i("View", "Getting transparent region for: " + this);
11842 }
11843 final Region r = dr.getTransparentRegion();
11844 final Rect db = dr.getBounds();
11845 final AttachInfo attachInfo = mAttachInfo;
11846 if (r != null && attachInfo != null) {
11847 final int w = getRight()-getLeft();
11848 final int h = getBottom()-getTop();
11849 if (db.left > 0) {
11850 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
11851 r.op(0, 0, db.left, h, Region.Op.UNION);
11852 }
11853 if (db.right < w) {
11854 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
11855 r.op(db.right, 0, w, h, Region.Op.UNION);
11856 }
11857 if (db.top > 0) {
11858 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
11859 r.op(0, 0, w, db.top, Region.Op.UNION);
11860 }
11861 if (db.bottom < h) {
11862 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
11863 r.op(0, db.bottom, w, h, Region.Op.UNION);
11864 }
11865 final int[] location = attachInfo.mTransparentLocation;
11866 getLocationInWindow(location);
11867 r.translate(location[0], location[1]);
11868 region.op(r, Region.Op.INTERSECT);
11869 } else {
11870 region.op(db, Region.Op.DIFFERENCE);
11871 }
11872 }
11873
Patrick Dubroye0a799a2011-05-04 16:19:22 -070011874 private void checkForLongClick(int delayOffset) {
11875 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
11876 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011877
Patrick Dubroye0a799a2011-05-04 16:19:22 -070011878 if (mPendingCheckForLongPress == null) {
11879 mPendingCheckForLongPress = new CheckForLongPress();
11880 }
11881 mPendingCheckForLongPress.rememberWindowAttachCount();
11882 postDelayed(mPendingCheckForLongPress,
11883 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011885 }
11886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011887 /**
11888 * Inflate a view from an XML resource. This convenience method wraps the {@link
11889 * LayoutInflater} class, which provides a full range of options for view inflation.
11890 *
11891 * @param context The Context object for your activity or application.
11892 * @param resource The resource ID to inflate
11893 * @param root A view group that will be the parent. Used to properly inflate the
11894 * layout_* parameters.
11895 * @see LayoutInflater
11896 */
11897 public static View inflate(Context context, int resource, ViewGroup root) {
11898 LayoutInflater factory = LayoutInflater.from(context);
11899 return factory.inflate(resource, root);
11900 }
Romain Guy33e72ae2010-07-17 12:40:29 -070011901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011902 /**
Adam Powell637d3372010-08-25 14:37:03 -070011903 * Scroll the view with standard behavior for scrolling beyond the normal
11904 * content boundaries. Views that call this method should override
11905 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
11906 * results of an over-scroll operation.
11907 *
11908 * Views can use this method to handle any touch or fling-based scrolling.
11909 *
11910 * @param deltaX Change in X in pixels
11911 * @param deltaY Change in Y in pixels
11912 * @param scrollX Current X scroll value in pixels before applying deltaX
11913 * @param scrollY Current Y scroll value in pixels before applying deltaY
11914 * @param scrollRangeX Maximum content scroll range along the X axis
11915 * @param scrollRangeY Maximum content scroll range along the Y axis
11916 * @param maxOverScrollX Number of pixels to overscroll by in either direction
11917 * along the X axis.
11918 * @param maxOverScrollY Number of pixels to overscroll by in either direction
11919 * along the Y axis.
11920 * @param isTouchEvent true if this scroll operation is the result of a touch event.
11921 * @return true if scrolling was clamped to an over-scroll boundary along either
11922 * axis, false otherwise.
11923 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011924 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070011925 protected boolean overScrollBy(int deltaX, int deltaY,
11926 int scrollX, int scrollY,
11927 int scrollRangeX, int scrollRangeY,
11928 int maxOverScrollX, int maxOverScrollY,
11929 boolean isTouchEvent) {
11930 final int overScrollMode = mOverScrollMode;
11931 final boolean canScrollHorizontal =
11932 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
11933 final boolean canScrollVertical =
11934 computeVerticalScrollRange() > computeVerticalScrollExtent();
11935 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
11936 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
11937 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
11938 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
11939
11940 int newScrollX = scrollX + deltaX;
11941 if (!overScrollHorizontal) {
11942 maxOverScrollX = 0;
11943 }
11944
11945 int newScrollY = scrollY + deltaY;
11946 if (!overScrollVertical) {
11947 maxOverScrollY = 0;
11948 }
11949
11950 // Clamp values if at the limits and record
11951 final int left = -maxOverScrollX;
11952 final int right = maxOverScrollX + scrollRangeX;
11953 final int top = -maxOverScrollY;
11954 final int bottom = maxOverScrollY + scrollRangeY;
11955
11956 boolean clampedX = false;
11957 if (newScrollX > right) {
11958 newScrollX = right;
11959 clampedX = true;
11960 } else if (newScrollX < left) {
11961 newScrollX = left;
11962 clampedX = true;
11963 }
11964
11965 boolean clampedY = false;
11966 if (newScrollY > bottom) {
11967 newScrollY = bottom;
11968 clampedY = true;
11969 } else if (newScrollY < top) {
11970 newScrollY = top;
11971 clampedY = true;
11972 }
11973
11974 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
11975
11976 return clampedX || clampedY;
11977 }
11978
11979 /**
11980 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
11981 * respond to the results of an over-scroll operation.
11982 *
11983 * @param scrollX New X scroll value in pixels
11984 * @param scrollY New Y scroll value in pixels
11985 * @param clampedX True if scrollX was clamped to an over-scroll boundary
11986 * @param clampedY True if scrollY was clamped to an over-scroll boundary
11987 */
11988 protected void onOverScrolled(int scrollX, int scrollY,
11989 boolean clampedX, boolean clampedY) {
11990 // Intentionally empty.
11991 }
11992
11993 /**
11994 * Returns the over-scroll mode for this view. The result will be
11995 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11996 * (allow over-scrolling only if the view content is larger than the container),
11997 * or {@link #OVER_SCROLL_NEVER}.
11998 *
11999 * @return This view's over-scroll mode.
12000 */
12001 public int getOverScrollMode() {
12002 return mOverScrollMode;
12003 }
12004
12005 /**
12006 * Set the over-scroll mode for this view. Valid over-scroll modes are
12007 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12008 * (allow over-scrolling only if the view content is larger than the container),
12009 * or {@link #OVER_SCROLL_NEVER}.
12010 *
12011 * Setting the over-scroll mode of a view will have an effect only if the
12012 * view is capable of scrolling.
12013 *
12014 * @param overScrollMode The new over-scroll mode for this view.
12015 */
12016 public void setOverScrollMode(int overScrollMode) {
12017 if (overScrollMode != OVER_SCROLL_ALWAYS &&
12018 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
12019 overScrollMode != OVER_SCROLL_NEVER) {
12020 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
12021 }
12022 mOverScrollMode = overScrollMode;
12023 }
12024
12025 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012026 * Gets a scale factor that determines the distance the view should scroll
12027 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
12028 * @return The vertical scroll scale factor.
12029 * @hide
12030 */
12031 protected float getVerticalScrollFactor() {
12032 if (mVerticalScrollFactor == 0) {
12033 TypedValue outValue = new TypedValue();
12034 if (!mContext.getTheme().resolveAttribute(
12035 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
12036 throw new IllegalStateException(
12037 "Expected theme to define listPreferredItemHeight.");
12038 }
12039 mVerticalScrollFactor = outValue.getDimension(
12040 mContext.getResources().getDisplayMetrics());
12041 }
12042 return mVerticalScrollFactor;
12043 }
12044
12045 /**
12046 * Gets a scale factor that determines the distance the view should scroll
12047 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
12048 * @return The horizontal scroll scale factor.
12049 * @hide
12050 */
12051 protected float getHorizontalScrollFactor() {
12052 // TODO: Should use something else.
12053 return getVerticalScrollFactor();
12054 }
12055
12056 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012057 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
12058 * Each MeasureSpec represents a requirement for either the width or the height.
12059 * A MeasureSpec is comprised of a size and a mode. There are three possible
12060 * modes:
12061 * <dl>
12062 * <dt>UNSPECIFIED</dt>
12063 * <dd>
12064 * The parent has not imposed any constraint on the child. It can be whatever size
12065 * it wants.
12066 * </dd>
12067 *
12068 * <dt>EXACTLY</dt>
12069 * <dd>
12070 * The parent has determined an exact size for the child. The child is going to be
12071 * given those bounds regardless of how big it wants to be.
12072 * </dd>
12073 *
12074 * <dt>AT_MOST</dt>
12075 * <dd>
12076 * The child can be as large as it wants up to the specified size.
12077 * </dd>
12078 * </dl>
12079 *
12080 * MeasureSpecs are implemented as ints to reduce object allocation. This class
12081 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
12082 */
12083 public static class MeasureSpec {
12084 private static final int MODE_SHIFT = 30;
12085 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
12086
12087 /**
12088 * Measure specification mode: The parent has not imposed any constraint
12089 * on the child. It can be whatever size it wants.
12090 */
12091 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
12092
12093 /**
12094 * Measure specification mode: The parent has determined an exact size
12095 * for the child. The child is going to be given those bounds regardless
12096 * of how big it wants to be.
12097 */
12098 public static final int EXACTLY = 1 << MODE_SHIFT;
12099
12100 /**
12101 * Measure specification mode: The child can be as large as it wants up
12102 * to the specified size.
12103 */
12104 public static final int AT_MOST = 2 << MODE_SHIFT;
12105
12106 /**
12107 * Creates a measure specification based on the supplied size and mode.
12108 *
12109 * The mode must always be one of the following:
12110 * <ul>
12111 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
12112 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
12113 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
12114 * </ul>
12115 *
12116 * @param size the size of the measure specification
12117 * @param mode the mode of the measure specification
12118 * @return the measure specification based on size and mode
12119 */
12120 public static int makeMeasureSpec(int size, int mode) {
12121 return size + mode;
12122 }
12123
12124 /**
12125 * Extracts the mode from the supplied measure specification.
12126 *
12127 * @param measureSpec the measure specification to extract the mode from
12128 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
12129 * {@link android.view.View.MeasureSpec#AT_MOST} or
12130 * {@link android.view.View.MeasureSpec#EXACTLY}
12131 */
12132 public static int getMode(int measureSpec) {
12133 return (measureSpec & MODE_MASK);
12134 }
12135
12136 /**
12137 * Extracts the size from the supplied measure specification.
12138 *
12139 * @param measureSpec the measure specification to extract the size from
12140 * @return the size in pixels defined in the supplied measure specification
12141 */
12142 public static int getSize(int measureSpec) {
12143 return (measureSpec & ~MODE_MASK);
12144 }
12145
12146 /**
12147 * Returns a String representation of the specified measure
12148 * specification.
12149 *
12150 * @param measureSpec the measure specification to convert to a String
12151 * @return a String with the following format: "MeasureSpec: MODE SIZE"
12152 */
12153 public static String toString(int measureSpec) {
12154 int mode = getMode(measureSpec);
12155 int size = getSize(measureSpec);
12156
12157 StringBuilder sb = new StringBuilder("MeasureSpec: ");
12158
12159 if (mode == UNSPECIFIED)
12160 sb.append("UNSPECIFIED ");
12161 else if (mode == EXACTLY)
12162 sb.append("EXACTLY ");
12163 else if (mode == AT_MOST)
12164 sb.append("AT_MOST ");
12165 else
12166 sb.append(mode).append(" ");
12167
12168 sb.append(size);
12169 return sb.toString();
12170 }
12171 }
12172
12173 class CheckForLongPress implements Runnable {
12174
12175 private int mOriginalWindowAttachCount;
12176
12177 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070012178 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012179 && mOriginalWindowAttachCount == mWindowAttachCount) {
12180 if (performLongClick()) {
12181 mHasPerformedLongPress = true;
12182 }
12183 }
12184 }
12185
12186 public void rememberWindowAttachCount() {
12187 mOriginalWindowAttachCount = mWindowAttachCount;
12188 }
12189 }
Joe Malin32736f02011-01-19 16:14:20 -080012190
Adam Powelle14579b2009-12-16 18:39:52 -080012191 private final class CheckForTap implements Runnable {
12192 public void run() {
12193 mPrivateFlags &= ~PREPRESSED;
12194 mPrivateFlags |= PRESSED;
12195 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012196 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080012197 }
12198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012199
Adam Powella35d7682010-03-12 14:48:13 -080012200 private final class PerformClick implements Runnable {
12201 public void run() {
12202 performClick();
12203 }
12204 }
12205
Dianne Hackborn63042d62011-01-26 18:56:29 -080012206 /** @hide */
12207 public void hackTurnOffWindowResizeAnim(boolean off) {
12208 mAttachInfo.mTurnOffWindowResizeAnim = off;
12209 }
Joe Malin32736f02011-01-19 16:14:20 -080012210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012211 /**
Chet Haasea00f3862011-02-22 06:34:40 -080012212 * This method returns a ViewPropertyAnimator object, which can be used to animate
12213 * specific properties on this View.
12214 *
12215 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
12216 */
12217 public ViewPropertyAnimator animate() {
12218 if (mAnimator == null) {
12219 mAnimator = new ViewPropertyAnimator(this);
12220 }
12221 return mAnimator;
12222 }
12223
12224 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012225 * Interface definition for a callback to be invoked when a key event is
12226 * dispatched to this view. The callback will be invoked before the key
12227 * event is given to the view.
12228 */
12229 public interface OnKeyListener {
12230 /**
12231 * Called when a key is dispatched to a view. This allows listeners to
12232 * get a chance to respond before the target view.
12233 *
12234 * @param v The view the key has been dispatched to.
12235 * @param keyCode The code for the physical key that was pressed
12236 * @param event The KeyEvent object containing full information about
12237 * the event.
12238 * @return True if the listener has consumed the event, false otherwise.
12239 */
12240 boolean onKey(View v, int keyCode, KeyEvent event);
12241 }
12242
12243 /**
12244 * Interface definition for a callback to be invoked when a touch event is
12245 * dispatched to this view. The callback will be invoked before the touch
12246 * event is given to the view.
12247 */
12248 public interface OnTouchListener {
12249 /**
12250 * Called when a touch event is dispatched to a view. This allows listeners to
12251 * get a chance to respond before the target view.
12252 *
12253 * @param v The view the touch event has been dispatched to.
12254 * @param event The MotionEvent object containing full information about
12255 * the event.
12256 * @return True if the listener has consumed the event, false otherwise.
12257 */
12258 boolean onTouch(View v, MotionEvent event);
12259 }
12260
12261 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012262 * Interface definition for a callback to be invoked when a generic motion event is
12263 * dispatched to this view. The callback will be invoked before the generic motion
12264 * event is given to the view.
12265 */
12266 public interface OnGenericMotionListener {
12267 /**
12268 * Called when a generic motion event is dispatched to a view. This allows listeners to
12269 * get a chance to respond before the target view.
12270 *
12271 * @param v The view the generic motion event has been dispatched to.
12272 * @param event The MotionEvent object containing full information about
12273 * the event.
12274 * @return True if the listener has consumed the event, false otherwise.
12275 */
12276 boolean onGenericMotion(View v, MotionEvent event);
12277 }
12278
12279 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012280 * Interface definition for a callback to be invoked when a view has been clicked and held.
12281 */
12282 public interface OnLongClickListener {
12283 /**
12284 * Called when a view has been clicked and held.
12285 *
12286 * @param v The view that was clicked and held.
12287 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080012288 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012289 */
12290 boolean onLongClick(View v);
12291 }
12292
12293 /**
Chris Tate32affef2010-10-18 15:29:21 -070012294 * Interface definition for a callback to be invoked when a drag is being dispatched
12295 * to this view. The callback will be invoked before the hosting view's own
12296 * onDrag(event) method. If the listener wants to fall back to the hosting view's
12297 * onDrag(event) behavior, it should return 'false' from this callback.
12298 */
12299 public interface OnDragListener {
12300 /**
12301 * Called when a drag event is dispatched to a view. This allows listeners
12302 * to get a chance to override base View behavior.
12303 *
Joe Malin32736f02011-01-19 16:14:20 -080012304 * @param v The View that received the drag event.
12305 * @param event The {@link android.view.DragEvent} object for the drag event.
12306 * @return {@code true} if the drag event was handled successfully, or {@code false}
12307 * if the drag event was not handled. Note that {@code false} will trigger the View
12308 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070012309 */
12310 boolean onDrag(View v, DragEvent event);
12311 }
12312
12313 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012314 * Interface definition for a callback to be invoked when the focus state of
12315 * a view changed.
12316 */
12317 public interface OnFocusChangeListener {
12318 /**
12319 * Called when the focus state of a view has changed.
12320 *
12321 * @param v The view whose state has changed.
12322 * @param hasFocus The new focus state of v.
12323 */
12324 void onFocusChange(View v, boolean hasFocus);
12325 }
12326
12327 /**
12328 * Interface definition for a callback to be invoked when a view is clicked.
12329 */
12330 public interface OnClickListener {
12331 /**
12332 * Called when a view has been clicked.
12333 *
12334 * @param v The view that was clicked.
12335 */
12336 void onClick(View v);
12337 }
12338
12339 /**
12340 * Interface definition for a callback to be invoked when the context menu
12341 * for this view is being built.
12342 */
12343 public interface OnCreateContextMenuListener {
12344 /**
12345 * Called when the context menu for this view is being built. It is not
12346 * safe to hold onto the menu after this method returns.
12347 *
12348 * @param menu The context menu that is being built
12349 * @param v The view for which the context menu is being built
12350 * @param menuInfo Extra information about the item for which the
12351 * context menu should be shown. This information will vary
12352 * depending on the class of v.
12353 */
12354 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
12355 }
12356
Joe Onorato664644d2011-01-23 17:53:23 -080012357 /**
12358 * Interface definition for a callback to be invoked when the status bar changes
12359 * visibility.
12360 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012361 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080012362 */
12363 public interface OnSystemUiVisibilityChangeListener {
12364 /**
12365 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070012366 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080012367 *
12368 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
12369 */
12370 public void onSystemUiVisibilityChange(int visibility);
12371 }
12372
Adam Powell4afd62b2011-02-18 15:02:18 -080012373 /**
12374 * Interface definition for a callback to be invoked when this view is attached
12375 * or detached from its window.
12376 */
12377 public interface OnAttachStateChangeListener {
12378 /**
12379 * Called when the view is attached to a window.
12380 * @param v The view that was attached
12381 */
12382 public void onViewAttachedToWindow(View v);
12383 /**
12384 * Called when the view is detached from a window.
12385 * @param v The view that was detached
12386 */
12387 public void onViewDetachedFromWindow(View v);
12388 }
12389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012390 private final class UnsetPressedState implements Runnable {
12391 public void run() {
12392 setPressed(false);
12393 }
12394 }
12395
12396 /**
12397 * Base class for derived classes that want to save and restore their own
12398 * state in {@link android.view.View#onSaveInstanceState()}.
12399 */
12400 public static class BaseSavedState extends AbsSavedState {
12401 /**
12402 * Constructor used when reading from a parcel. Reads the state of the superclass.
12403 *
12404 * @param source
12405 */
12406 public BaseSavedState(Parcel source) {
12407 super(source);
12408 }
12409
12410 /**
12411 * Constructor called by derived classes when creating their SavedState objects
12412 *
12413 * @param superState The state of the superclass of this view
12414 */
12415 public BaseSavedState(Parcelable superState) {
12416 super(superState);
12417 }
12418
12419 public static final Parcelable.Creator<BaseSavedState> CREATOR =
12420 new Parcelable.Creator<BaseSavedState>() {
12421 public BaseSavedState createFromParcel(Parcel in) {
12422 return new BaseSavedState(in);
12423 }
12424
12425 public BaseSavedState[] newArray(int size) {
12426 return new BaseSavedState[size];
12427 }
12428 };
12429 }
12430
12431 /**
12432 * A set of information given to a view when it is attached to its parent
12433 * window.
12434 */
12435 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012436 interface Callbacks {
12437 void playSoundEffect(int effectId);
12438 boolean performHapticFeedback(int effectId, boolean always);
12439 }
12440
12441 /**
12442 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
12443 * to a Handler. This class contains the target (View) to invalidate and
12444 * the coordinates of the dirty rectangle.
12445 *
12446 * For performance purposes, this class also implements a pool of up to
12447 * POOL_LIMIT objects that get reused. This reduces memory allocations
12448 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012449 */
Romain Guyd928d682009-03-31 17:52:16 -070012450 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012451 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070012452 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
12453 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070012454 public InvalidateInfo newInstance() {
12455 return new InvalidateInfo();
12456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012457
Romain Guyd928d682009-03-31 17:52:16 -070012458 public void onAcquired(InvalidateInfo element) {
12459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012460
Romain Guyd928d682009-03-31 17:52:16 -070012461 public void onReleased(InvalidateInfo element) {
12462 }
12463 }, POOL_LIMIT)
12464 );
12465
12466 private InvalidateInfo mNext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012467
12468 View target;
12469
12470 int left;
12471 int top;
12472 int right;
12473 int bottom;
12474
Romain Guyd928d682009-03-31 17:52:16 -070012475 public void setNextPoolable(InvalidateInfo element) {
12476 mNext = element;
12477 }
12478
12479 public InvalidateInfo getNextPoolable() {
12480 return mNext;
12481 }
12482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012483 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070012484 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012485 }
12486
12487 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070012488 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012489 }
12490 }
12491
12492 final IWindowSession mSession;
12493
12494 final IWindow mWindow;
12495
12496 final IBinder mWindowToken;
12497
12498 final Callbacks mRootCallbacks;
12499
Chet Haasedaf98e92011-01-10 14:10:36 -080012500 Canvas mHardwareCanvas;
12501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012502 /**
12503 * The top view of the hierarchy.
12504 */
12505 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070012506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012507 IBinder mPanelParentWindowToken;
12508 Surface mSurface;
12509
Romain Guyb051e892010-09-28 19:09:36 -070012510 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012511 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070012512 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080012513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012514 /**
Romain Guy8506ab42009-06-11 17:35:47 -070012515 * Scale factor used by the compatibility mode
12516 */
12517 float mApplicationScale;
12518
12519 /**
12520 * Indicates whether the application is in compatibility mode
12521 */
12522 boolean mScalingRequired;
12523
12524 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012525 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080012526 */
12527 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080012528
Dianne Hackborn63042d62011-01-26 18:56:29 -080012529 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012530 * Left position of this view's window
12531 */
12532 int mWindowLeft;
12533
12534 /**
12535 * Top position of this view's window
12536 */
12537 int mWindowTop;
12538
12539 /**
Adam Powell26153a32010-11-08 15:22:27 -080012540 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070012541 */
Adam Powell26153a32010-11-08 15:22:27 -080012542 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070012543
12544 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012545 * For windows that are full-screen but using insets to layout inside
12546 * of the screen decorations, these are the current insets for the
12547 * content of the window.
12548 */
12549 final Rect mContentInsets = new Rect();
12550
12551 /**
12552 * For windows that are full-screen but using insets to layout inside
12553 * of the screen decorations, these are the current insets for the
12554 * actual visible parts of the window.
12555 */
12556 final Rect mVisibleInsets = new Rect();
12557
12558 /**
12559 * The internal insets given by this window. This value is
12560 * supplied by the client (through
12561 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
12562 * be given to the window manager when changed to be used in laying
12563 * out windows behind it.
12564 */
12565 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
12566 = new ViewTreeObserver.InternalInsetsInfo();
12567
12568 /**
12569 * All views in the window's hierarchy that serve as scroll containers,
12570 * used to determine if the window can be resized or must be panned
12571 * to adjust for a soft input area.
12572 */
12573 final ArrayList<View> mScrollContainers = new ArrayList<View>();
12574
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070012575 final KeyEvent.DispatcherState mKeyDispatchState
12576 = new KeyEvent.DispatcherState();
12577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012578 /**
12579 * Indicates whether the view's window currently has the focus.
12580 */
12581 boolean mHasWindowFocus;
12582
12583 /**
12584 * The current visibility of the window.
12585 */
12586 int mWindowVisibility;
12587
12588 /**
12589 * Indicates the time at which drawing started to occur.
12590 */
12591 long mDrawingTime;
12592
12593 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070012594 * Indicates whether or not ignoring the DIRTY_MASK flags.
12595 */
12596 boolean mIgnoreDirtyState;
12597
12598 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012599 * Indicates whether the view's window is currently in touch mode.
12600 */
12601 boolean mInTouchMode;
12602
12603 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012604 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012605 * the next time it performs a traversal
12606 */
12607 boolean mRecomputeGlobalAttributes;
12608
12609 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012610 * Set during a traveral if any views want to keep the screen on.
12611 */
12612 boolean mKeepScreenOn;
12613
12614 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012615 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
12616 */
12617 int mSystemUiVisibility;
12618
12619 /**
12620 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
12621 * attached.
12622 */
12623 boolean mHasSystemUiListeners;
12624
12625 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012626 * Set if the visibility of any views has changed.
12627 */
12628 boolean mViewVisibilityChanged;
12629
12630 /**
12631 * Set to true if a view has been scrolled.
12632 */
12633 boolean mViewScrollChanged;
12634
12635 /**
12636 * Global to the view hierarchy used as a temporary for dealing with
12637 * x/y points in the transparent region computations.
12638 */
12639 final int[] mTransparentLocation = new int[2];
12640
12641 /**
12642 * Global to the view hierarchy used as a temporary for dealing with
12643 * x/y points in the ViewGroup.invalidateChild implementation.
12644 */
12645 final int[] mInvalidateChildLocation = new int[2];
12646
Chet Haasec3aa3612010-06-17 08:50:37 -070012647
12648 /**
12649 * Global to the view hierarchy used as a temporary for dealing with
12650 * x/y location when view is transformed.
12651 */
12652 final float[] mTmpTransformLocation = new float[2];
12653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012654 /**
12655 * The view tree observer used to dispatch global events like
12656 * layout, pre-draw, touch mode change, etc.
12657 */
12658 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
12659
12660 /**
12661 * A Canvas used by the view hierarchy to perform bitmap caching.
12662 */
12663 Canvas mCanvas;
12664
12665 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012666 * A Handler supplied by a view's {@link android.view.ViewAncestor}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012667 * handler can be used to pump events in the UI events queue.
12668 */
12669 final Handler mHandler;
12670
12671 /**
12672 * Identifier for messages requesting the view to be invalidated.
12673 * Such messages should be sent to {@link #mHandler}.
12674 */
12675 static final int INVALIDATE_MSG = 0x1;
12676
12677 /**
12678 * Identifier for messages requesting the view to invalidate a region.
12679 * Such messages should be sent to {@link #mHandler}.
12680 */
12681 static final int INVALIDATE_RECT_MSG = 0x2;
12682
12683 /**
12684 * Temporary for use in computing invalidate rectangles while
12685 * calling up the hierarchy.
12686 */
12687 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070012688
12689 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070012690 * Temporary for use in computing hit areas with transformed views
12691 */
12692 final RectF mTmpTransformRect = new RectF();
12693
12694 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070012695 * Temporary list for use in collecting focusable descendents of a view.
12696 */
12697 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
12698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012699 /**
12700 * Creates a new set of attachment information with the specified
12701 * events handler and thread.
12702 *
12703 * @param handler the events handler the view must use
12704 */
12705 AttachInfo(IWindowSession session, IWindow window,
12706 Handler handler, Callbacks effectPlayer) {
12707 mSession = session;
12708 mWindow = window;
12709 mWindowToken = window.asBinder();
12710 mHandler = handler;
12711 mRootCallbacks = effectPlayer;
12712 }
12713 }
12714
12715 /**
12716 * <p>ScrollabilityCache holds various fields used by a View when scrolling
12717 * is supported. This avoids keeping too many unused fields in most
12718 * instances of View.</p>
12719 */
Mike Cleronf116bf82009-09-27 19:14:12 -070012720 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080012721
Mike Cleronf116bf82009-09-27 19:14:12 -070012722 /**
12723 * Scrollbars are not visible
12724 */
12725 public static final int OFF = 0;
12726
12727 /**
12728 * Scrollbars are visible
12729 */
12730 public static final int ON = 1;
12731
12732 /**
12733 * Scrollbars are fading away
12734 */
12735 public static final int FADING = 2;
12736
12737 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080012738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012739 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070012740 public int scrollBarDefaultDelayBeforeFade;
12741 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012742
12743 public int scrollBarSize;
12744 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070012745 public float[] interpolatorValues;
12746 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012747
12748 public final Paint paint;
12749 public final Matrix matrix;
12750 public Shader shader;
12751
Mike Cleronf116bf82009-09-27 19:14:12 -070012752 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
12753
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012754 private static final float[] OPAQUE = { 255 };
12755 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080012756
Mike Cleronf116bf82009-09-27 19:14:12 -070012757 /**
12758 * When fading should start. This time moves into the future every time
12759 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
12760 */
12761 public long fadeStartTime;
12762
12763
12764 /**
12765 * The current state of the scrollbars: ON, OFF, or FADING
12766 */
12767 public int state = OFF;
12768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012769 private int mLastColor;
12770
Mike Cleronf116bf82009-09-27 19:14:12 -070012771 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012772 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
12773 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070012774 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
12775 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012776
12777 paint = new Paint();
12778 matrix = new Matrix();
12779 // use use a height of 1, and then wack the matrix each time we
12780 // actually use it.
12781 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012783 paint.setShader(shader);
12784 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070012785 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012786 }
Romain Guy8506ab42009-06-11 17:35:47 -070012787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012788 public void setFadeColor(int color) {
12789 if (color != 0 && color != mLastColor) {
12790 mLastColor = color;
12791 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070012792
Romain Guye55e1a72009-08-27 10:42:26 -070012793 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
12794 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012796 paint.setShader(shader);
12797 // Restore the default transfer mode (src_over)
12798 paint.setXfermode(null);
12799 }
12800 }
Joe Malin32736f02011-01-19 16:14:20 -080012801
Mike Cleronf116bf82009-09-27 19:14:12 -070012802 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070012803 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070012804 if (now >= fadeStartTime) {
12805
12806 // the animation fades the scrollbars out by changing
12807 // the opacity (alpha) from fully opaque to fully
12808 // transparent
12809 int nextFrame = (int) now;
12810 int framesCount = 0;
12811
12812 Interpolator interpolator = scrollBarInterpolator;
12813
12814 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012815 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070012816
12817 // End transparent
12818 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012819 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070012820
12821 state = FADING;
12822
12823 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080012824 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070012825 }
12826 }
12827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012828 }
12829}