blob: 98d07c4ca1b08a34e1b881123783a792e2674c75 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Pools;
58import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080059import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070061import android.view.accessibility.AccessibilityEvent;
62import android.view.accessibility.AccessibilityEventSource;
63import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070065import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.view.inputmethod.InputConnection;
68import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.widget.ScrollBarDrawable;
70
Christopher Tatea0374192010-10-05 13:06:41 -070071import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070072import java.lang.reflect.InvocationTargetException;
73import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import java.util.ArrayList;
75import java.util.Arrays;
Romain Guyd90a3312009-05-06 14:54:28 -070076import java.util.WeakHashMap;
Adam Powell4afd62b2011-02-18 15:02:18 -080077import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078
79/**
80 * <p>
81 * This class represents the basic building block for user interface components. A View
82 * occupies a rectangular area on the screen and is responsible for drawing and
83 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070084 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
86 * are invisible containers that hold other Views (or other ViewGroups) and define
87 * their layout properties.
88 * </p>
89 *
90 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070091 * <p>For an introduction to using this class to develop your
92 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -070094 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
96 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
97 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
98 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
99 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
100 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
101 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
102 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
103 * </p>
104 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700105 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 * <a name="Using"></a>
107 * <h3>Using Views</h3>
108 * <p>
109 * All of the views in a window are arranged in a single tree. You can add views
110 * either from code or by specifying a tree of views in one or more XML layout
111 * files. There are many specialized subclasses of views that act as controls or
112 * are capable of displaying text, images, or other content.
113 * </p>
114 * <p>
115 * Once you have created a tree of views, there are typically a few types of
116 * common operations you may wish to perform:
117 * <ul>
118 * <li><strong>Set properties:</strong> for example setting the text of a
119 * {@link android.widget.TextView}. The available properties and the methods
120 * that set them will vary among the different subclasses of views. Note that
121 * properties that are known at build time can be set in the XML layout
122 * files.</li>
123 * <li><strong>Set focus:</strong> The framework will handled moving focus in
124 * response to user input. To force focus to a specific view, call
125 * {@link #requestFocus}.</li>
126 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
127 * that will be notified when something interesting happens to the view. For
128 * example, all views will let you set a listener to be notified when the view
129 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700130 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
131 * Other view subclasses offer more specialized listeners. For example, a Button
132 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700134 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 * </ul>
136 * </p>
137 * <p><em>
138 * Note: The Android framework is responsible for measuring, laying out and
139 * drawing views. You should not call methods that perform these actions on
140 * views yourself unless you are actually implementing a
141 * {@link android.view.ViewGroup}.
142 * </em></p>
143 *
144 * <a name="Lifecycle"></a>
145 * <h3>Implementing a Custom View</h3>
146 *
147 * <p>
148 * To implement a custom view, you will usually begin by providing overrides for
149 * some of the standard methods that the framework calls on all views. You do
150 * not need to override all of these methods. In fact, you can start by just
151 * overriding {@link #onDraw(android.graphics.Canvas)}.
152 * <table border="2" width="85%" align="center" cellpadding="5">
153 * <thead>
154 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
155 * </thead>
156 *
157 * <tbody>
158 * <tr>
159 * <td rowspan="2">Creation</td>
160 * <td>Constructors</td>
161 * <td>There is a form of the constructor that are called when the view
162 * is created from code and a form that is called when the view is
163 * inflated from a layout file. The second form should parse and apply
164 * any attributes defined in the layout file.
165 * </td>
166 * </tr>
167 * <tr>
168 * <td><code>{@link #onFinishInflate()}</code></td>
169 * <td>Called after a view and all of its children has been inflated
170 * from XML.</td>
171 * </tr>
172 *
173 * <tr>
174 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700175 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 * <td>Called to determine the size requirements for this view and all
177 * of its children.
178 * </td>
179 * </tr>
180 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700181 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 * <td>Called when this view should assign a size and position to all
183 * of its children.
184 * </td>
185 * </tr>
186 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700187 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 * <td>Called when the size of this view has changed.
189 * </td>
190 * </tr>
191 *
192 * <tr>
193 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700194 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 * <td>Called when the view should render its content.
196 * </td>
197 * </tr>
198 *
199 * <tr>
200 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700201 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 * <td>Called when a new key event occurs.
203 * </td>
204 * </tr>
205 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700206 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * <td>Called when a key up event occurs.
208 * </td>
209 * </tr>
210 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700211 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 * <td>Called when a trackball motion event occurs.
213 * </td>
214 * </tr>
215 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700216 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 * <td>Called when a touch screen motion event occurs.
218 * </td>
219 * </tr>
220 *
221 * <tr>
222 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700223 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 * <td>Called when the view gains or loses focus.
225 * </td>
226 * </tr>
227 *
228 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700229 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 * <td>Called when the window containing the view gains or loses focus.
231 * </td>
232 * </tr>
233 *
234 * <tr>
235 * <td rowspan="3">Attaching</td>
236 * <td><code>{@link #onAttachedToWindow()}</code></td>
237 * <td>Called when the view is attached to a window.
238 * </td>
239 * </tr>
240 *
241 * <tr>
242 * <td><code>{@link #onDetachedFromWindow}</code></td>
243 * <td>Called when the view is detached from its window.
244 * </td>
245 * </tr>
246 *
247 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700248 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 * <td>Called when the visibility of the window containing the view
250 * has changed.
251 * </td>
252 * </tr>
253 * </tbody>
254 *
255 * </table>
256 * </p>
257 *
258 * <a name="IDs"></a>
259 * <h3>IDs</h3>
260 * Views may have an integer id associated with them. These ids are typically
261 * assigned in the layout XML files, and are used to find specific views within
262 * the view tree. A common pattern is to:
263 * <ul>
264 * <li>Define a Button in the layout file and assign it a unique ID.
265 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700266 * &lt;Button
267 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 * android:layout_width="wrap_content"
269 * android:layout_height="wrap_content"
270 * android:text="@string/my_button_text"/&gt;
271 * </pre></li>
272 * <li>From the onCreate method of an Activity, find the Button
273 * <pre class="prettyprint">
274 * Button myButton = (Button) findViewById(R.id.my_button);
275 * </pre></li>
276 * </ul>
277 * <p>
278 * View IDs need not be unique throughout the tree, but it is good practice to
279 * ensure that they are at least unique within the part of the tree you are
280 * searching.
281 * </p>
282 *
283 * <a name="Position"></a>
284 * <h3>Position</h3>
285 * <p>
286 * The geometry of a view is that of a rectangle. A view has a location,
287 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
288 * two dimensions, expressed as a width and a height. The unit for location
289 * and dimensions is the pixel.
290 * </p>
291 *
292 * <p>
293 * It is possible to retrieve the location of a view by invoking the methods
294 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
295 * coordinate of the rectangle representing the view. The latter returns the
296 * top, or Y, coordinate of the rectangle representing the view. These methods
297 * both return the location of the view relative to its parent. For instance,
298 * when getLeft() returns 20, that means the view is located 20 pixels to the
299 * right of the left edge of its direct parent.
300 * </p>
301 *
302 * <p>
303 * In addition, several convenience methods are offered to avoid unnecessary
304 * computations, namely {@link #getRight()} and {@link #getBottom()}.
305 * These methods return the coordinates of the right and bottom edges of the
306 * rectangle representing the view. For instance, calling {@link #getRight()}
307 * is similar to the following computation: <code>getLeft() + getWidth()</code>
308 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
309 * </p>
310 *
311 * <a name="SizePaddingMargins"></a>
312 * <h3>Size, padding and margins</h3>
313 * <p>
314 * The size of a view is expressed with a width and a height. A view actually
315 * possess two pairs of width and height values.
316 * </p>
317 *
318 * <p>
319 * The first pair is known as <em>measured width</em> and
320 * <em>measured height</em>. These dimensions define how big a view wants to be
321 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
322 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
323 * and {@link #getMeasuredHeight()}.
324 * </p>
325 *
326 * <p>
327 * The second pair is simply known as <em>width</em> and <em>height</em>, or
328 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
329 * dimensions define the actual size of the view on screen, at drawing time and
330 * after layout. These values may, but do not have to, be different from the
331 * measured width and height. The width and height can be obtained by calling
332 * {@link #getWidth()} and {@link #getHeight()}.
333 * </p>
334 *
335 * <p>
336 * To measure its dimensions, a view takes into account its padding. The padding
337 * is expressed in pixels for the left, top, right and bottom parts of the view.
338 * Padding can be used to offset the content of the view by a specific amount of
339 * pixels. For instance, a left padding of 2 will push the view's content by
340 * 2 pixels to the right of the left edge. Padding can be set using the
341 * {@link #setPadding(int, int, int, int)} method and queried by calling
342 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
343 * {@link #getPaddingRight()} and {@link #getPaddingBottom()}.
344 * </p>
345 *
346 * <p>
347 * Even though a view can define a padding, it does not provide any support for
348 * margins. However, view groups provide such a support. Refer to
349 * {@link android.view.ViewGroup} and
350 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
351 * </p>
352 *
353 * <a name="Layout"></a>
354 * <h3>Layout</h3>
355 * <p>
356 * Layout is a two pass process: a measure pass and a layout pass. The measuring
357 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
358 * of the view tree. Each view pushes dimension specifications down the tree
359 * during the recursion. At the end of the measure pass, every view has stored
360 * its measurements. The second pass happens in
361 * {@link #layout(int,int,int,int)} and is also top-down. During
362 * this pass each parent is responsible for positioning all of its children
363 * using the sizes computed in the measure pass.
364 * </p>
365 *
366 * <p>
367 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
368 * {@link #getMeasuredHeight()} values must be set, along with those for all of
369 * that view's descendants. A view's measured width and measured height values
370 * must respect the constraints imposed by the view's parents. This guarantees
371 * that at the end of the measure pass, all parents accept all of their
372 * children's measurements. A parent view may call measure() more than once on
373 * its children. For example, the parent may measure each child once with
374 * unspecified dimensions to find out how big they want to be, then call
375 * measure() on them again with actual numbers if the sum of all the children's
376 * unconstrained sizes is too big or too small.
377 * </p>
378 *
379 * <p>
380 * The measure pass uses two classes to communicate dimensions. The
381 * {@link MeasureSpec} class is used by views to tell their parents how they
382 * want to be measured and positioned. The base LayoutParams class just
383 * describes how big the view wants to be for both width and height. For each
384 * dimension, it can specify one of:
385 * <ul>
386 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800387 * <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 -0800388 * (minus padding)
389 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
390 * enclose its content (plus padding).
391 * </ul>
392 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
393 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
394 * an X and Y value.
395 * </p>
396 *
397 * <p>
398 * MeasureSpecs are used to push requirements down the tree from parent to
399 * child. A MeasureSpec can be in one of three modes:
400 * <ul>
401 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
402 * of a child view. For example, a LinearLayout may call measure() on its child
403 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
404 * tall the child view wants to be given a width of 240 pixels.
405 * <li>EXACTLY: This is used by the parent to impose an exact size on the
406 * child. The child must use this size, and guarantee that all of its
407 * descendants will fit within this size.
408 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
409 * child. The child must gurantee that it and all of its descendants will fit
410 * within this size.
411 * </ul>
412 * </p>
413 *
414 * <p>
415 * To intiate a layout, call {@link #requestLayout}. This method is typically
416 * called by a view on itself when it believes that is can no longer fit within
417 * its current bounds.
418 * </p>
419 *
420 * <a name="Drawing"></a>
421 * <h3>Drawing</h3>
422 * <p>
423 * Drawing is handled by walking the tree and rendering each view that
424 * intersects the the invalid region. Because the tree is traversed in-order,
425 * this means that parents will draw before (i.e., behind) their children, with
426 * siblings drawn in the order they appear in the tree.
427 * If you set a background drawable for a View, then the View will draw it for you
428 * before calling back to its <code>onDraw()</code> method.
429 * </p>
430 *
431 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700432 * 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 -0800433 * </p>
434 *
435 * <p>
436 * To force a view to draw, call {@link #invalidate()}.
437 * </p>
438 *
439 * <a name="EventHandlingThreading"></a>
440 * <h3>Event Handling and Threading</h3>
441 * <p>
442 * The basic cycle of a view is as follows:
443 * <ol>
444 * <li>An event comes in and is dispatched to the appropriate view. The view
445 * handles the event and notifies any listeners.</li>
446 * <li>If in the course of processing the event, the view's bounds may need
447 * to be changed, the view will call {@link #requestLayout()}.</li>
448 * <li>Similarly, if in the course of processing the event the view's appearance
449 * may need to be changed, the view will call {@link #invalidate()}.</li>
450 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
451 * the framework will take care of measuring, laying out, and drawing the tree
452 * as appropriate.</li>
453 * </ol>
454 * </p>
455 *
456 * <p><em>Note: The entire view tree is single threaded. You must always be on
457 * the UI thread when calling any method on any view.</em>
458 * If you are doing work on other threads and want to update the state of a view
459 * from that thread, you should use a {@link Handler}.
460 * </p>
461 *
462 * <a name="FocusHandling"></a>
463 * <h3>Focus Handling</h3>
464 * <p>
465 * The framework will handle routine focus movement in response to user input.
466 * This includes changing the focus as views are removed or hidden, or as new
467 * views become available. Views indicate their willingness to take focus
468 * through the {@link #isFocusable} method. To change whether a view can take
469 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
470 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
471 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
472 * </p>
473 * <p>
474 * Focus movement is based on an algorithm which finds the nearest neighbor in a
475 * given direction. In rare cases, the default algorithm may not match the
476 * intended behavior of the developer. In these situations, you can provide
477 * explicit overrides by using these XML attributes in the layout file:
478 * <pre>
479 * nextFocusDown
480 * nextFocusLeft
481 * nextFocusRight
482 * nextFocusUp
483 * </pre>
484 * </p>
485 *
486 *
487 * <p>
488 * To get a particular view to take focus, call {@link #requestFocus()}.
489 * </p>
490 *
491 * <a name="TouchMode"></a>
492 * <h3>Touch Mode</h3>
493 * <p>
494 * When a user is navigating a user interface via directional keys such as a D-pad, it is
495 * necessary to give focus to actionable items such as buttons so the user can see
496 * what will take input. If the device has touch capabilities, however, and the user
497 * begins interacting with the interface by touching it, it is no longer necessary to
498 * always highlight, or give focus to, a particular view. This motivates a mode
499 * for interaction named 'touch mode'.
500 * </p>
501 * <p>
502 * For a touch capable device, once the user touches the screen, the device
503 * will enter touch mode. From this point onward, only views for which
504 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
505 * Other views that are touchable, like buttons, will not take focus when touched; they will
506 * only fire the on click listeners.
507 * </p>
508 * <p>
509 * Any time a user hits a directional key, such as a D-pad direction, the view device will
510 * exit touch mode, and find a view to take focus, so that the user may resume interacting
511 * with the user interface without touching the screen again.
512 * </p>
513 * <p>
514 * The touch mode state is maintained across {@link android.app.Activity}s. Call
515 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
516 * </p>
517 *
518 * <a name="Scrolling"></a>
519 * <h3>Scrolling</h3>
520 * <p>
521 * The framework provides basic support for views that wish to internally
522 * scroll their content. This includes keeping track of the X and Y scroll
523 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800524 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700525 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 * </p>
527 *
528 * <a name="Tags"></a>
529 * <h3>Tags</h3>
530 * <p>
531 * Unlike IDs, tags are not used to identify views. Tags are essentially an
532 * extra piece of information that can be associated with a view. They are most
533 * often used as a convenience to store data related to views in the views
534 * themselves rather than by putting them in a separate structure.
535 * </p>
536 *
537 * <a name="Animation"></a>
538 * <h3>Animation</h3>
539 * <p>
540 * You can attach an {@link Animation} object to a view using
541 * {@link #setAnimation(Animation)} or
542 * {@link #startAnimation(Animation)}. The animation can alter the scale,
543 * rotation, translation and alpha of a view over time. If the animation is
544 * attached to a view that has children, the animation will affect the entire
545 * subtree rooted by that node. When an animation is started, the framework will
546 * take care of redrawing the appropriate views until the animation completes.
547 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800548 * <p>
549 * Starting with Android 3.0, the preferred way of animating views is to use the
550 * {@link android.animation} package APIs.
551 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 *
Jeff Brown85a31762010-09-01 17:01:00 -0700553 * <a name="Security"></a>
554 * <h3>Security</h3>
555 * <p>
556 * Sometimes it is essential that an application be able to verify that an action
557 * is being performed with the full knowledge and consent of the user, such as
558 * granting a permission request, making a purchase or clicking on an advertisement.
559 * Unfortunately, a malicious application could try to spoof the user into
560 * performing these actions, unaware, by concealing the intended purpose of the view.
561 * As a remedy, the framework offers a touch filtering mechanism that can be used to
562 * improve the security of views that provide access to sensitive functionality.
563 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700564 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800565 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700566 * will discard touches that are received whenever the view's window is obscured by
567 * another visible window. As a result, the view will not receive touches whenever a
568 * toast, dialog or other window appears above the view's window.
569 * </p><p>
570 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700571 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
572 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700573 * </p>
574 *
Romain Guy171c5922011-01-06 10:04:23 -0800575 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700576 * @attr ref android.R.styleable#View_background
577 * @attr ref android.R.styleable#View_clickable
578 * @attr ref android.R.styleable#View_contentDescription
579 * @attr ref android.R.styleable#View_drawingCacheQuality
580 * @attr ref android.R.styleable#View_duplicateParentState
581 * @attr ref android.R.styleable#View_id
582 * @attr ref android.R.styleable#View_fadingEdge
583 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700584 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700586 * @attr ref android.R.styleable#View_isScrollContainer
587 * @attr ref android.R.styleable#View_focusable
588 * @attr ref android.R.styleable#View_focusableInTouchMode
589 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
590 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800591 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700592 * @attr ref android.R.styleable#View_longClickable
593 * @attr ref android.R.styleable#View_minHeight
594 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 * @attr ref android.R.styleable#View_nextFocusDown
596 * @attr ref android.R.styleable#View_nextFocusLeft
597 * @attr ref android.R.styleable#View_nextFocusRight
598 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700599 * @attr ref android.R.styleable#View_onClick
600 * @attr ref android.R.styleable#View_padding
601 * @attr ref android.R.styleable#View_paddingBottom
602 * @attr ref android.R.styleable#View_paddingLeft
603 * @attr ref android.R.styleable#View_paddingRight
604 * @attr ref android.R.styleable#View_paddingTop
605 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800606 * @attr ref android.R.styleable#View_rotation
607 * @attr ref android.R.styleable#View_rotationX
608 * @attr ref android.R.styleable#View_rotationY
609 * @attr ref android.R.styleable#View_scaleX
610 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 * @attr ref android.R.styleable#View_scrollX
612 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700613 * @attr ref android.R.styleable#View_scrollbarSize
614 * @attr ref android.R.styleable#View_scrollbarStyle
615 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700616 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
617 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
619 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 * @attr ref android.R.styleable#View_scrollbarThumbVertical
621 * @attr ref android.R.styleable#View_scrollbarTrackVertical
622 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
623 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700624 * @attr ref android.R.styleable#View_soundEffectsEnabled
625 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800626 * @attr ref android.R.styleable#View_transformPivotX
627 * @attr ref android.R.styleable#View_transformPivotY
628 * @attr ref android.R.styleable#View_translationX
629 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700630 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 *
632 * @see android.view.ViewGroup
633 */
svetoslavganov75986cf2009-05-14 22:28:01 -0700634public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 private static final boolean DBG = false;
636
637 /**
638 * The logging tag used by this class with android.util.Log.
639 */
640 protected static final String VIEW_LOG_TAG = "View";
641
642 /**
643 * Used to mark a View that has no ID.
644 */
645 public static final int NO_ID = -1;
646
647 /**
648 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
649 * calling setFlags.
650 */
651 private static final int NOT_FOCUSABLE = 0x00000000;
652
653 /**
654 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
655 * setFlags.
656 */
657 private static final int FOCUSABLE = 0x00000001;
658
659 /**
660 * Mask for use with setFlags indicating bits used for focus.
661 */
662 private static final int FOCUSABLE_MASK = 0x00000001;
663
664 /**
665 * This view will adjust its padding to fit sytem windows (e.g. status bar)
666 */
667 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
668
669 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700670 * This view is visible. Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 */
672 public static final int VISIBLE = 0x00000000;
673
674 /**
675 * This view is invisible, but it still takes up space for layout purposes.
Romain Guy5c22a8c2011-05-13 11:48:45 -0700676 * Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 */
678 public static final int INVISIBLE = 0x00000004;
679
680 /**
681 * This view is invisible, and it doesn't take any space for layout
Romain Guy5c22a8c2011-05-13 11:48:45 -0700682 * purposes. Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 */
684 public static final int GONE = 0x00000008;
685
686 /**
687 * Mask for use with setFlags indicating bits used for visibility.
688 * {@hide}
689 */
690 static final int VISIBILITY_MASK = 0x0000000C;
691
692 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
693
694 /**
695 * This view is enabled. Intrepretation varies by subclass.
696 * Use with ENABLED_MASK when calling setFlags.
697 * {@hide}
698 */
699 static final int ENABLED = 0x00000000;
700
701 /**
702 * This view is disabled. Intrepretation varies by subclass.
703 * Use with ENABLED_MASK when calling setFlags.
704 * {@hide}
705 */
706 static final int DISABLED = 0x00000020;
707
708 /**
709 * Mask for use with setFlags indicating bits used for indicating whether
710 * this view is enabled
711 * {@hide}
712 */
713 static final int ENABLED_MASK = 0x00000020;
714
715 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700716 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
717 * called and further optimizations will be performed. It is okay to have
718 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 * {@hide}
720 */
721 static final int WILL_NOT_DRAW = 0x00000080;
722
723 /**
724 * Mask for use with setFlags indicating bits used for indicating whether
725 * this view is will draw
726 * {@hide}
727 */
728 static final int DRAW_MASK = 0x00000080;
729
730 /**
731 * <p>This view doesn't show scrollbars.</p>
732 * {@hide}
733 */
734 static final int SCROLLBARS_NONE = 0x00000000;
735
736 /**
737 * <p>This view shows horizontal scrollbars.</p>
738 * {@hide}
739 */
740 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
741
742 /**
743 * <p>This view shows vertical scrollbars.</p>
744 * {@hide}
745 */
746 static final int SCROLLBARS_VERTICAL = 0x00000200;
747
748 /**
749 * <p>Mask for use with setFlags indicating bits used for indicating which
750 * scrollbars are enabled.</p>
751 * {@hide}
752 */
753 static final int SCROLLBARS_MASK = 0x00000300;
754
Jeff Brown85a31762010-09-01 17:01:00 -0700755 /**
756 * Indicates that the view should filter touches when its window is obscured.
757 * Refer to the class comments for more information about this security feature.
758 * {@hide}
759 */
760 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
761
762 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763
764 /**
765 * <p>This view doesn't show fading edges.</p>
766 * {@hide}
767 */
768 static final int FADING_EDGE_NONE = 0x00000000;
769
770 /**
771 * <p>This view shows horizontal fading edges.</p>
772 * {@hide}
773 */
774 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
775
776 /**
777 * <p>This view shows vertical fading edges.</p>
778 * {@hide}
779 */
780 static final int FADING_EDGE_VERTICAL = 0x00002000;
781
782 /**
783 * <p>Mask for use with setFlags indicating bits used for indicating which
784 * fading edges are enabled.</p>
785 * {@hide}
786 */
787 static final int FADING_EDGE_MASK = 0x00003000;
788
789 /**
790 * <p>Indicates this view can be clicked. When clickable, a View reacts
791 * to clicks by notifying the OnClickListener.<p>
792 * {@hide}
793 */
794 static final int CLICKABLE = 0x00004000;
795
796 /**
797 * <p>Indicates this view is caching its drawing into a bitmap.</p>
798 * {@hide}
799 */
800 static final int DRAWING_CACHE_ENABLED = 0x00008000;
801
802 /**
803 * <p>Indicates that no icicle should be saved for this view.<p>
804 * {@hide}
805 */
806 static final int SAVE_DISABLED = 0x000010000;
807
808 /**
809 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
810 * property.</p>
811 * {@hide}
812 */
813 static final int SAVE_DISABLED_MASK = 0x000010000;
814
815 /**
816 * <p>Indicates that no drawing cache should ever be created for this view.<p>
817 * {@hide}
818 */
819 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
820
821 /**
822 * <p>Indicates this view can take / keep focus when int touch mode.</p>
823 * {@hide}
824 */
825 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
826
827 /**
828 * <p>Enables low quality mode for the drawing cache.</p>
829 */
830 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
831
832 /**
833 * <p>Enables high quality mode for the drawing cache.</p>
834 */
835 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
836
837 /**
838 * <p>Enables automatic quality mode for the drawing cache.</p>
839 */
840 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
841
842 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
843 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
844 };
845
846 /**
847 * <p>Mask for use with setFlags indicating bits used for the cache
848 * quality property.</p>
849 * {@hide}
850 */
851 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
852
853 /**
854 * <p>
855 * Indicates this view can be long clicked. When long clickable, a View
856 * reacts to long clicks by notifying the OnLongClickListener or showing a
857 * context menu.
858 * </p>
859 * {@hide}
860 */
861 static final int LONG_CLICKABLE = 0x00200000;
862
863 /**
864 * <p>Indicates that this view gets its drawable states from its direct parent
865 * and ignores its original internal states.</p>
866 *
867 * @hide
868 */
869 static final int DUPLICATE_PARENT_STATE = 0x00400000;
870
871 /**
872 * The scrollbar style to display the scrollbars inside the content area,
873 * without increasing the padding. The scrollbars will be overlaid with
874 * translucency on the view's content.
875 */
876 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
877
878 /**
879 * The scrollbar style to display the scrollbars inside the padded area,
880 * increasing the padding of the view. The scrollbars will not overlap the
881 * content area of the view.
882 */
883 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
884
885 /**
886 * The scrollbar style to display the scrollbars at the edge of the view,
887 * without increasing the padding. The scrollbars will be overlaid with
888 * translucency.
889 */
890 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
891
892 /**
893 * The scrollbar style to display the scrollbars at the edge of the view,
894 * increasing the padding of the view. The scrollbars will only overlap the
895 * background, if any.
896 */
897 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
898
899 /**
900 * Mask to check if the scrollbar style is overlay or inset.
901 * {@hide}
902 */
903 static final int SCROLLBARS_INSET_MASK = 0x01000000;
904
905 /**
906 * Mask to check if the scrollbar style is inside or outside.
907 * {@hide}
908 */
909 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
910
911 /**
912 * Mask for scrollbar style.
913 * {@hide}
914 */
915 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
916
917 /**
918 * View flag indicating that the screen should remain on while the
919 * window containing this view is visible to the user. This effectively
920 * takes care of automatically setting the WindowManager's
921 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
922 */
923 public static final int KEEP_SCREEN_ON = 0x04000000;
924
925 /**
926 * View flag indicating whether this view should have sound effects enabled
927 * for events such as clicking and touching.
928 */
929 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
930
931 /**
932 * View flag indicating whether this view should have haptic feedback
933 * enabled for events such as long presses.
934 */
935 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
936
937 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700938 * <p>Indicates that the view hierarchy should stop saving state when
939 * it reaches this view. If state saving is initiated immediately at
940 * the view, it will be allowed.
941 * {@hide}
942 */
943 static final int PARENT_SAVE_DISABLED = 0x20000000;
944
945 /**
946 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
947 * {@hide}
948 */
949 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
950
951 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800952 * Horizontal direction of this view is from Left to Right.
953 * Use with {@link #setHorizontalDirection}.
954 * {@hide}
955 */
956 public static final int HORIZONTAL_DIRECTION_LTR = 0x00000000;
957
958 /**
959 * Horizontal direction of this view is from Right to Left.
960 * Use with {@link #setHorizontalDirection}.
961 * {@hide}
962 */
963 public static final int HORIZONTAL_DIRECTION_RTL = 0x40000000;
964
965 /**
966 * Horizontal direction of this view is inherited from its parent.
967 * Use with {@link #setHorizontalDirection}.
968 * {@hide}
969 */
970 public static final int HORIZONTAL_DIRECTION_INHERIT = 0x80000000;
971
972 /**
973 * Horizontal direction of this view is from deduced from the default language
974 * script for the locale. Use with {@link #setHorizontalDirection}.
975 * {@hide}
976 */
977 public static final int HORIZONTAL_DIRECTION_LOCALE = 0xC0000000;
978
979 /**
980 * Mask for use with setFlags indicating bits used for horizontalDirection.
981 * {@hide}
982 */
983 static final int HORIZONTAL_DIRECTION_MASK = 0xC0000000;
984
985 private static final int[] HORIZONTAL_DIRECTION_FLAGS = { HORIZONTAL_DIRECTION_LTR,
986 HORIZONTAL_DIRECTION_RTL, HORIZONTAL_DIRECTION_INHERIT, HORIZONTAL_DIRECTION_LOCALE};
987
988 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700989 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
990 * should add all focusable Views regardless if they are focusable in touch mode.
991 */
992 public static final int FOCUSABLES_ALL = 0x00000000;
993
994 /**
995 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
996 * should add only Views focusable in touch mode.
997 */
998 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
999
1000 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001001 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 * item.
1003 */
1004 public static final int FOCUS_BACKWARD = 0x00000001;
1005
1006 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001007 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 * item.
1009 */
1010 public static final int FOCUS_FORWARD = 0x00000002;
1011
1012 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001013 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 */
1015 public static final int FOCUS_LEFT = 0x00000011;
1016
1017 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001018 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 */
1020 public static final int FOCUS_UP = 0x00000021;
1021
1022 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001023 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 */
1025 public static final int FOCUS_RIGHT = 0x00000042;
1026
1027 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001028 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 */
1030 public static final int FOCUS_DOWN = 0x00000082;
1031
1032 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001033 * Bits of {@link #getMeasuredWidthAndState()} and
1034 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1035 */
1036 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1037
1038 /**
1039 * Bits of {@link #getMeasuredWidthAndState()} and
1040 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1041 */
1042 public static final int MEASURED_STATE_MASK = 0xff000000;
1043
1044 /**
1045 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1046 * for functions that combine both width and height into a single int,
1047 * such as {@link #getMeasuredState()} and the childState argument of
1048 * {@link #resolveSizeAndState(int, int, int)}.
1049 */
1050 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1051
1052 /**
1053 * Bit of {@link #getMeasuredWidthAndState()} and
1054 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1055 * is smaller that the space the view would like to have.
1056 */
1057 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1058
1059 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 * Base View state sets
1061 */
1062 // Singles
1063 /**
1064 * Indicates the view has no states set. 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 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001071 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 /**
1073 * Indicates the view is enabled. States are used with
1074 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1075 * view depending on its state.
1076 *
1077 * @see android.graphics.drawable.Drawable
1078 * @see #getDrawableState()
1079 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001080 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 /**
1082 * Indicates the view is focused. States are used with
1083 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1084 * view depending on its state.
1085 *
1086 * @see android.graphics.drawable.Drawable
1087 * @see #getDrawableState()
1088 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001089 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 /**
1091 * Indicates the view is selected. States are used with
1092 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1093 * view depending on its state.
1094 *
1095 * @see android.graphics.drawable.Drawable
1096 * @see #getDrawableState()
1097 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001098 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 /**
1100 * Indicates the view is pressed. States are used with
1101 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1102 * view depending on its state.
1103 *
1104 * @see android.graphics.drawable.Drawable
1105 * @see #getDrawableState()
1106 * @hide
1107 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001108 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 /**
1110 * Indicates the view's window has focus. States are used with
1111 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1112 * view depending on its state.
1113 *
1114 * @see android.graphics.drawable.Drawable
1115 * @see #getDrawableState()
1116 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001117 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 // Doubles
1119 /**
1120 * Indicates the view is enabled and has the focus.
1121 *
1122 * @see #ENABLED_STATE_SET
1123 * @see #FOCUSED_STATE_SET
1124 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001125 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 /**
1127 * Indicates the view is enabled and selected.
1128 *
1129 * @see #ENABLED_STATE_SET
1130 * @see #SELECTED_STATE_SET
1131 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001132 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 /**
1134 * Indicates the view is enabled and that its window has focus.
1135 *
1136 * @see #ENABLED_STATE_SET
1137 * @see #WINDOW_FOCUSED_STATE_SET
1138 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001139 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 /**
1141 * Indicates the view is focused and selected.
1142 *
1143 * @see #FOCUSED_STATE_SET
1144 * @see #SELECTED_STATE_SET
1145 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001146 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 /**
1148 * Indicates the view has the focus and that its window has the focus.
1149 *
1150 * @see #FOCUSED_STATE_SET
1151 * @see #WINDOW_FOCUSED_STATE_SET
1152 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001153 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 /**
1155 * Indicates the view is selected and that its window has the focus.
1156 *
1157 * @see #SELECTED_STATE_SET
1158 * @see #WINDOW_FOCUSED_STATE_SET
1159 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001160 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 // Triples
1162 /**
1163 * Indicates the view is enabled, focused and selected.
1164 *
1165 * @see #ENABLED_STATE_SET
1166 * @see #FOCUSED_STATE_SET
1167 * @see #SELECTED_STATE_SET
1168 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001169 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 /**
1171 * Indicates the view is enabled, focused and its window has the focus.
1172 *
1173 * @see #ENABLED_STATE_SET
1174 * @see #FOCUSED_STATE_SET
1175 * @see #WINDOW_FOCUSED_STATE_SET
1176 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001177 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 /**
1179 * Indicates the view is enabled, selected and its window has the focus.
1180 *
1181 * @see #ENABLED_STATE_SET
1182 * @see #SELECTED_STATE_SET
1183 * @see #WINDOW_FOCUSED_STATE_SET
1184 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001185 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 /**
1187 * Indicates the view is focused, selected and its window has the focus.
1188 *
1189 * @see #FOCUSED_STATE_SET
1190 * @see #SELECTED_STATE_SET
1191 * @see #WINDOW_FOCUSED_STATE_SET
1192 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001193 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 /**
1195 * Indicates the view is enabled, focused, selected and its window
1196 * has the focus.
1197 *
1198 * @see #ENABLED_STATE_SET
1199 * @see #FOCUSED_STATE_SET
1200 * @see #SELECTED_STATE_SET
1201 * @see #WINDOW_FOCUSED_STATE_SET
1202 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001203 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 /**
1205 * Indicates the view is pressed and its window has the focus.
1206 *
1207 * @see #PRESSED_STATE_SET
1208 * @see #WINDOW_FOCUSED_STATE_SET
1209 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001210 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 /**
1212 * Indicates the view is pressed and selected.
1213 *
1214 * @see #PRESSED_STATE_SET
1215 * @see #SELECTED_STATE_SET
1216 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001217 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 /**
1219 * Indicates the view is pressed, selected and its window has the focus.
1220 *
1221 * @see #PRESSED_STATE_SET
1222 * @see #SELECTED_STATE_SET
1223 * @see #WINDOW_FOCUSED_STATE_SET
1224 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001225 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 /**
1227 * Indicates the view is pressed and focused.
1228 *
1229 * @see #PRESSED_STATE_SET
1230 * @see #FOCUSED_STATE_SET
1231 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001232 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 /**
1234 * Indicates the view is pressed, focused and its window has the focus.
1235 *
1236 * @see #PRESSED_STATE_SET
1237 * @see #FOCUSED_STATE_SET
1238 * @see #WINDOW_FOCUSED_STATE_SET
1239 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001240 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 /**
1242 * Indicates the view is pressed, focused and selected.
1243 *
1244 * @see #PRESSED_STATE_SET
1245 * @see #SELECTED_STATE_SET
1246 * @see #FOCUSED_STATE_SET
1247 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001248 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 /**
1250 * Indicates the view is pressed, focused, selected and its window has the focus.
1251 *
1252 * @see #PRESSED_STATE_SET
1253 * @see #FOCUSED_STATE_SET
1254 * @see #SELECTED_STATE_SET
1255 * @see #WINDOW_FOCUSED_STATE_SET
1256 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001257 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 /**
1259 * Indicates the view is pressed and enabled.
1260 *
1261 * @see #PRESSED_STATE_SET
1262 * @see #ENABLED_STATE_SET
1263 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001264 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 /**
1266 * Indicates the view is pressed, enabled and its window has the focus.
1267 *
1268 * @see #PRESSED_STATE_SET
1269 * @see #ENABLED_STATE_SET
1270 * @see #WINDOW_FOCUSED_STATE_SET
1271 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001272 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 /**
1274 * Indicates the view is pressed, enabled and selected.
1275 *
1276 * @see #PRESSED_STATE_SET
1277 * @see #ENABLED_STATE_SET
1278 * @see #SELECTED_STATE_SET
1279 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001280 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 /**
1282 * Indicates the view is pressed, enabled, selected and its window has the
1283 * focus.
1284 *
1285 * @see #PRESSED_STATE_SET
1286 * @see #ENABLED_STATE_SET
1287 * @see #SELECTED_STATE_SET
1288 * @see #WINDOW_FOCUSED_STATE_SET
1289 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001290 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 /**
1292 * Indicates the view is pressed, enabled and focused.
1293 *
1294 * @see #PRESSED_STATE_SET
1295 * @see #ENABLED_STATE_SET
1296 * @see #FOCUSED_STATE_SET
1297 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001298 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 /**
1300 * Indicates the view is pressed, enabled, focused and its window has the
1301 * focus.
1302 *
1303 * @see #PRESSED_STATE_SET
1304 * @see #ENABLED_STATE_SET
1305 * @see #FOCUSED_STATE_SET
1306 * @see #WINDOW_FOCUSED_STATE_SET
1307 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001308 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 /**
1310 * Indicates the view is pressed, enabled, focused and selected.
1311 *
1312 * @see #PRESSED_STATE_SET
1313 * @see #ENABLED_STATE_SET
1314 * @see #SELECTED_STATE_SET
1315 * @see #FOCUSED_STATE_SET
1316 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001317 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 /**
1319 * Indicates the view is pressed, enabled, focused, selected and its window
1320 * has the focus.
1321 *
1322 * @see #PRESSED_STATE_SET
1323 * @see #ENABLED_STATE_SET
1324 * @see #SELECTED_STATE_SET
1325 * @see #FOCUSED_STATE_SET
1326 * @see #WINDOW_FOCUSED_STATE_SET
1327 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001328 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329
1330 /**
1331 * The order here is very important to {@link #getDrawableState()}
1332 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001333 private static final int[][] VIEW_STATE_SETS;
1334
Romain Guyb051e892010-09-28 19:09:36 -07001335 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1336 static final int VIEW_STATE_SELECTED = 1 << 1;
1337 static final int VIEW_STATE_FOCUSED = 1 << 2;
1338 static final int VIEW_STATE_ENABLED = 1 << 3;
1339 static final int VIEW_STATE_PRESSED = 1 << 4;
1340 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001341 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001342 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001343 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1344 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001345
1346 static final int[] VIEW_STATE_IDS = new int[] {
1347 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1348 R.attr.state_selected, VIEW_STATE_SELECTED,
1349 R.attr.state_focused, VIEW_STATE_FOCUSED,
1350 R.attr.state_enabled, VIEW_STATE_ENABLED,
1351 R.attr.state_pressed, VIEW_STATE_PRESSED,
1352 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001353 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001354 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001355 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1356 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 };
1358
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001359 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001360 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1361 throw new IllegalStateException(
1362 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1363 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001364 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001365 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001366 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001367 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001368 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001369 orderedIds[i * 2] = viewState;
1370 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001371 }
1372 }
1373 }
Romain Guyb051e892010-09-28 19:09:36 -07001374 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1375 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1376 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001377 int numBits = Integer.bitCount(i);
1378 int[] set = new int[numBits];
1379 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001380 for (int j = 0; j < orderedIds.length; j += 2) {
1381 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001382 set[pos++] = orderedIds[j];
1383 }
1384 }
1385 VIEW_STATE_SETS[i] = set;
1386 }
1387
1388 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1389 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1390 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1391 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1392 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1393 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1394 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1395 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1396 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1397 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1398 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1399 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1400 | VIEW_STATE_FOCUSED];
1401 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1402 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1403 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1404 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1405 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1406 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1407 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1408 | VIEW_STATE_ENABLED];
1409 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1410 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1411 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1412 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1413 | VIEW_STATE_ENABLED];
1414 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1416 | VIEW_STATE_ENABLED];
1417 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1418 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1419 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1420
1421 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1422 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1424 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1425 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1426 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1427 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1428 | VIEW_STATE_PRESSED];
1429 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1430 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1431 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1432 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1433 | VIEW_STATE_PRESSED];
1434 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1435 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1436 | VIEW_STATE_PRESSED];
1437 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1438 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1439 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1440 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1441 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1442 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1444 | VIEW_STATE_PRESSED];
1445 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1447 | VIEW_STATE_PRESSED];
1448 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1449 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1450 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1451 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1452 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1453 | VIEW_STATE_PRESSED];
1454 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1455 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1456 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1457 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1459 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1460 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1462 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1463 | VIEW_STATE_PRESSED];
1464 }
1465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 * Temporary Rect currently for use in setBackground(). This will probably
1468 * be extended in the future to hold our own class with more than just
1469 * a Rect. :)
1470 */
1471 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001472
1473 /**
1474 * Map used to store views' tags.
1475 */
1476 private static WeakHashMap<View, SparseArray<Object>> sTags;
1477
1478 /**
1479 * Lock used to access sTags.
1480 */
1481 private static final Object sTagsLock = new Object();
1482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 /**
1484 * The animation currently associated with this view.
1485 * @hide
1486 */
1487 protected Animation mCurrentAnimation = null;
1488
1489 /**
1490 * Width as measured during measure pass.
1491 * {@hide}
1492 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001493 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001494 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495
1496 /**
1497 * Height as measured during measure pass.
1498 * {@hide}
1499 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001500 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001501 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502
1503 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001504 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1505 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1506 * its display list. This flag, used only when hw accelerated, allows us to clear the
1507 * flag while retaining this information until it's needed (at getDisplayList() time and
1508 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1509 *
1510 * {@hide}
1511 */
1512 boolean mRecreateDisplayList = false;
1513
1514 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 * The view's identifier.
1516 * {@hide}
1517 *
1518 * @see #setId(int)
1519 * @see #getId()
1520 */
1521 @ViewDebug.ExportedProperty(resolveId = true)
1522 int mID = NO_ID;
1523
1524 /**
1525 * The view's tag.
1526 * {@hide}
1527 *
1528 * @see #setTag(Object)
1529 * @see #getTag()
1530 */
1531 protected Object mTag;
1532
1533 // for mPrivateFlags:
1534 /** {@hide} */
1535 static final int WANTS_FOCUS = 0x00000001;
1536 /** {@hide} */
1537 static final int FOCUSED = 0x00000002;
1538 /** {@hide} */
1539 static final int SELECTED = 0x00000004;
1540 /** {@hide} */
1541 static final int IS_ROOT_NAMESPACE = 0x00000008;
1542 /** {@hide} */
1543 static final int HAS_BOUNDS = 0x00000010;
1544 /** {@hide} */
1545 static final int DRAWN = 0x00000020;
1546 /**
1547 * When this flag is set, this view is running an animation on behalf of its
1548 * children and should therefore not cancel invalidate requests, even if they
1549 * lie outside of this view's bounds.
1550 *
1551 * {@hide}
1552 */
1553 static final int DRAW_ANIMATION = 0x00000040;
1554 /** {@hide} */
1555 static final int SKIP_DRAW = 0x00000080;
1556 /** {@hide} */
1557 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1558 /** {@hide} */
1559 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1560 /** {@hide} */
1561 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1562 /** {@hide} */
1563 static final int MEASURED_DIMENSION_SET = 0x00000800;
1564 /** {@hide} */
1565 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001566 /** {@hide} */
1567 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568
1569 private static final int PRESSED = 0x00004000;
1570
1571 /** {@hide} */
1572 static final int DRAWING_CACHE_VALID = 0x00008000;
1573 /**
1574 * Flag used to indicate that this view should be drawn once more (and only once
1575 * more) after its animation has completed.
1576 * {@hide}
1577 */
1578 static final int ANIMATION_STARTED = 0x00010000;
1579
1580 private static final int SAVE_STATE_CALLED = 0x00020000;
1581
1582 /**
1583 * Indicates that the View returned true when onSetAlpha() was called and that
1584 * the alpha must be restored.
1585 * {@hide}
1586 */
1587 static final int ALPHA_SET = 0x00040000;
1588
1589 /**
1590 * Set by {@link #setScrollContainer(boolean)}.
1591 */
1592 static final int SCROLL_CONTAINER = 0x00080000;
1593
1594 /**
1595 * Set by {@link #setScrollContainer(boolean)}.
1596 */
1597 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1598
1599 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001600 * View flag indicating whether this view was invalidated (fully or partially.)
1601 *
1602 * @hide
1603 */
1604 static final int DIRTY = 0x00200000;
1605
1606 /**
1607 * View flag indicating whether this view was invalidated by an opaque
1608 * invalidate request.
1609 *
1610 * @hide
1611 */
1612 static final int DIRTY_OPAQUE = 0x00400000;
1613
1614 /**
1615 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1616 *
1617 * @hide
1618 */
1619 static final int DIRTY_MASK = 0x00600000;
1620
1621 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001622 * Indicates whether the background is opaque.
1623 *
1624 * @hide
1625 */
1626 static final int OPAQUE_BACKGROUND = 0x00800000;
1627
1628 /**
1629 * Indicates whether the scrollbars are opaque.
1630 *
1631 * @hide
1632 */
1633 static final int OPAQUE_SCROLLBARS = 0x01000000;
1634
1635 /**
1636 * Indicates whether the view is opaque.
1637 *
1638 * @hide
1639 */
1640 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001641
Adam Powelle14579b2009-12-16 18:39:52 -08001642 /**
1643 * Indicates a prepressed state;
1644 * the short time between ACTION_DOWN and recognizing
1645 * a 'real' press. Prepressed is used to recognize quick taps
1646 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001647 *
Adam Powelle14579b2009-12-16 18:39:52 -08001648 * @hide
1649 */
1650 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001651
Adam Powellc9fbaab2010-02-16 17:16:19 -08001652 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001653 * Indicates whether the view is temporarily detached.
1654 *
1655 * @hide
1656 */
1657 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001658
Adam Powell8568c3a2010-04-19 14:26:11 -07001659 /**
1660 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001661 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001662 * @hide
1663 */
1664 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001665
1666 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001667 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1668 * @hide
1669 */
1670 private static final int HOVERED = 0x10000000;
1671
1672 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001673 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1674 * for transform operations
1675 *
1676 * @hide
1677 */
Adam Powellf37df072010-09-17 16:22:49 -07001678 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001679
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001680 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001681 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001682
Chet Haasefd2b0022010-08-06 13:08:56 -07001683 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001684 * Indicates that this view was specifically invalidated, not just dirtied because some
1685 * child view was invalidated. The flag is used to determine when we need to recreate
1686 * a view's display list (as opposed to just returning a reference to its existing
1687 * display list).
1688 *
1689 * @hide
1690 */
1691 static final int INVALIDATED = 0x80000000;
1692
Christopher Tate3d4bf172011-03-28 16:16:46 -07001693 /* Masks for mPrivateFlags2 */
1694
1695 /**
1696 * Indicates that this view has reported that it can accept the current drag's content.
1697 * Cleared when the drag operation concludes.
1698 * @hide
1699 */
1700 static final int DRAG_CAN_ACCEPT = 0x00000001;
1701
1702 /**
1703 * Indicates that this view is currently directly under the drag location in a
1704 * drag-and-drop operation involving content that it can accept. Cleared when
1705 * the drag exits the view, or when the drag operation concludes.
1706 * @hide
1707 */
1708 static final int DRAG_HOVERED = 0x00000002;
1709
Cibu Johny86666632010-02-22 13:01:02 -08001710 /**
1711 * Indicates whether the view is drawn in right-to-left direction.
1712 *
1713 * @hide
1714 */
1715 static final int RESOLVED_LAYOUT_RTL = 0x00000004;
1716
Christopher Tate3d4bf172011-03-28 16:16:46 -07001717 /* End of masks for mPrivateFlags2 */
1718
1719 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1720
Chet Haasedaf98e92011-01-10 14:10:36 -08001721 /**
Adam Powell637d3372010-08-25 14:37:03 -07001722 * Always allow a user to over-scroll this view, provided it is a
1723 * view that can scroll.
1724 *
1725 * @see #getOverScrollMode()
1726 * @see #setOverScrollMode(int)
1727 */
1728 public static final int OVER_SCROLL_ALWAYS = 0;
1729
1730 /**
1731 * Allow a user to over-scroll this view only if the content is large
1732 * enough to meaningfully scroll, provided it is a view that can scroll.
1733 *
1734 * @see #getOverScrollMode()
1735 * @see #setOverScrollMode(int)
1736 */
1737 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1738
1739 /**
1740 * Never allow a user to over-scroll this view.
1741 *
1742 * @see #getOverScrollMode()
1743 * @see #setOverScrollMode(int)
1744 */
1745 public static final int OVER_SCROLL_NEVER = 2;
1746
1747 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001748 * View has requested the status bar to be visible (the default).
1749 *
Joe Malin32736f02011-01-19 16:14:20 -08001750 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001751 */
1752 public static final int STATUS_BAR_VISIBLE = 0;
1753
1754 /**
Jeff Brown05dc66a2011-03-02 14:41:58 -08001755 * View has requested the status bar to be hidden.
Joe Onorato664644d2011-01-23 17:53:23 -08001756 *
Joe Malin32736f02011-01-19 16:14:20 -08001757 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001758 */
1759 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1760
1761 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001762 * @hide
1763 *
1764 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1765 * out of the public fields to keep the undefined bits out of the developer's way.
1766 *
1767 * Flag to make the status bar not expandable. Unless you also
1768 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1769 */
1770 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
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 notification icons and scrolling ticker text.
1779 */
1780 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1781
1782 /**
1783 * @hide
1784 *
1785 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1786 * out of the public fields to keep the undefined bits out of the developer's way.
1787 *
1788 * Flag to disable incoming notification alerts. This will not block
1789 * icons, but it will block sound, vibrating and other visual or aural notifications.
1790 */
1791 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1792
1793 /**
1794 * @hide
1795 *
1796 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1797 * out of the public fields to keep the undefined bits out of the developer's way.
1798 *
1799 * Flag to hide only the scrolling ticker. Note that
1800 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1801 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1802 */
1803 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1804
1805 /**
1806 * @hide
1807 *
1808 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1809 * out of the public fields to keep the undefined bits out of the developer's way.
1810 *
1811 * Flag to hide the center system info area.
1812 */
1813 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1814
1815 /**
1816 * @hide
1817 *
1818 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1819 * out of the public fields to keep the undefined bits out of the developer's way.
1820 *
1821 * Flag to hide only the navigation buttons. Don't use this
1822 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001823 *
1824 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001825 */
1826 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1827
1828 /**
1829 * @hide
1830 *
1831 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1832 * out of the public fields to keep the undefined bits out of the developer's way.
1833 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001834 * Flag to hide only the back button. Don't use this
1835 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1836 */
1837 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1838
1839 /**
1840 * @hide
1841 *
1842 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1843 * out of the public fields to keep the undefined bits out of the developer's way.
1844 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001845 * Flag to hide only the clock. You might use this if your activity has
1846 * its own clock making the status bar's clock redundant.
1847 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001848 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1849
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001850 /**
1851 * @hide
1852 */
1853 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001854
1855 /**
Adam Powell637d3372010-08-25 14:37:03 -07001856 * Controls the over-scroll mode for this view.
1857 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1858 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1859 * and {@link #OVER_SCROLL_NEVER}.
1860 */
1861 private int mOverScrollMode;
1862
1863 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 * The parent this view is attached to.
1865 * {@hide}
1866 *
1867 * @see #getParent()
1868 */
1869 protected ViewParent mParent;
1870
1871 /**
1872 * {@hide}
1873 */
1874 AttachInfo mAttachInfo;
1875
1876 /**
1877 * {@hide}
1878 */
Romain Guy809a7f62009-05-14 15:44:42 -07001879 @ViewDebug.ExportedProperty(flagMapping = {
1880 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1881 name = "FORCE_LAYOUT"),
1882 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1883 name = "LAYOUT_REQUIRED"),
1884 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001885 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001886 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1887 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1888 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1889 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1890 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001892 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893
1894 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001895 * This view's request for the visibility of the status bar.
1896 * @hide
1897 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001898 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001899 int mSystemUiVisibility;
1900
1901 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 * Count of how many windows this view has been attached to.
1903 */
1904 int mWindowAttachCount;
1905
1906 /**
1907 * The layout parameters associated with this view and used by the parent
1908 * {@link android.view.ViewGroup} to determine how this view should be
1909 * laid out.
1910 * {@hide}
1911 */
1912 protected ViewGroup.LayoutParams mLayoutParams;
1913
1914 /**
1915 * The view flags hold various views states.
1916 * {@hide}
1917 */
1918 @ViewDebug.ExportedProperty
1919 int mViewFlags;
1920
1921 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001922 * The transform matrix for the View. This transform is calculated internally
1923 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1924 * is used by default. Do *not* use this variable directly; instead call
1925 * getMatrix(), which will automatically recalculate the matrix if necessary
1926 * to get the correct matrix based on the latest rotation and scale properties.
1927 */
1928 private final Matrix mMatrix = new Matrix();
1929
1930 /**
1931 * The transform matrix for the View. This transform is calculated internally
1932 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1933 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001934 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001935 * to get the correct matrix based on the latest rotation and scale properties.
1936 */
1937 private Matrix mInverseMatrix;
1938
1939 /**
1940 * An internal variable that tracks whether we need to recalculate the
1941 * transform matrix, based on whether the rotation or scaleX/Y properties
1942 * have changed since the matrix was last calculated.
1943 */
Chet Haasea00f3862011-02-22 06:34:40 -08001944 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07001945
1946 /**
1947 * An internal variable that tracks whether we need to recalculate the
1948 * transform matrix, based on whether the rotation or scaleX/Y properties
1949 * have changed since the matrix was last calculated.
1950 */
1951 private boolean mInverseMatrixDirty = true;
1952
1953 /**
1954 * A variable that tracks whether we need to recalculate the
1955 * transform matrix, based on whether the rotation or scaleX/Y properties
1956 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001957 * is only valid after a call to updateMatrix() or to a function that
1958 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001959 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001960 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001961
1962 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001963 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
1964 */
1965 private Camera mCamera = null;
1966
1967 /**
1968 * This matrix is used when computing the matrix for 3D rotations.
1969 */
1970 private Matrix matrix3D = null;
1971
1972 /**
1973 * These prev values are used to recalculate a centered pivot point when necessary. The
1974 * pivot point is only used in matrix operations (when rotation, scale, or translation are
1975 * set), so thes values are only used then as well.
1976 */
1977 private int mPrevWidth = -1;
1978 private int mPrevHeight = -1;
1979
Joe Malin32736f02011-01-19 16:14:20 -08001980 private boolean mLastIsOpaque;
1981
Chet Haasefd2b0022010-08-06 13:08:56 -07001982 /**
1983 * Convenience value to check for float values that are close enough to zero to be considered
1984 * zero.
1985 */
Romain Guy2542d192010-08-18 11:47:12 -07001986 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001987
1988 /**
1989 * The degrees rotation around the vertical axis through the pivot point.
1990 */
1991 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001992 float mRotationY = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001993
1994 /**
1995 * The degrees rotation around the horizontal axis through the pivot point.
1996 */
1997 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001998 float mRotationX = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001999
2000 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002001 * The degrees rotation around the pivot point.
2002 */
2003 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002004 float mRotation = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002005
2006 /**
Chet Haasedf030d22010-07-30 17:22:38 -07002007 * The amount of translation of the object away from its left property (post-layout).
2008 */
2009 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002010 float mTranslationX = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002011
2012 /**
2013 * The amount of translation of the object away from its top property (post-layout).
2014 */
2015 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002016 float mTranslationY = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002017
2018 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002019 * The amount of scale in the x direction around the pivot point. A
2020 * value of 1 means no scaling is applied.
2021 */
2022 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002023 float mScaleX = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002024
2025 /**
2026 * The amount of scale in the y direction around the pivot point. A
2027 * value of 1 means no scaling is applied.
2028 */
2029 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002030 float mScaleY = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002031
2032 /**
2033 * The amount of scale in the x direction around the pivot point. A
2034 * value of 1 means no scaling is applied.
2035 */
2036 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002037 float mPivotX = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002038
2039 /**
2040 * The amount of scale in the y direction around the pivot point. A
2041 * value of 1 means no scaling is applied.
2042 */
2043 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002044 float mPivotY = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002045
2046 /**
2047 * The opacity of the View. This is a value from 0 to 1, where 0 means
2048 * completely transparent and 1 means completely opaque.
2049 */
2050 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002051 float mAlpha = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002052
2053 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 * The distance in pixels from the left edge of this view's parent
2055 * to the left edge of this view.
2056 * {@hide}
2057 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002058 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 protected int mLeft;
2060 /**
2061 * The distance in pixels from the left edge of this view's parent
2062 * to the right edge of this view.
2063 * {@hide}
2064 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002065 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 protected int mRight;
2067 /**
2068 * The distance in pixels from the top edge of this view's parent
2069 * to the top edge of this view.
2070 * {@hide}
2071 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002072 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 protected int mTop;
2074 /**
2075 * The distance in pixels from the top edge of this view's parent
2076 * to the bottom edge of this view.
2077 * {@hide}
2078 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002079 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 protected int mBottom;
2081
2082 /**
2083 * The offset, in pixels, by which the content of this view is scrolled
2084 * horizontally.
2085 * {@hide}
2086 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002087 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 protected int mScrollX;
2089 /**
2090 * The offset, in pixels, by which the content of this view is scrolled
2091 * vertically.
2092 * {@hide}
2093 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002094 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 protected int mScrollY;
2096
2097 /**
2098 * The left padding in pixels, that is the distance in pixels between the
2099 * left edge of this view and the left edge of its content.
2100 * {@hide}
2101 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002102 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 protected int mPaddingLeft;
2104 /**
2105 * The right padding in pixels, that is the distance in pixels between the
2106 * right edge of this view and the right edge of its content.
2107 * {@hide}
2108 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002109 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 protected int mPaddingRight;
2111 /**
2112 * The top padding in pixels, that is the distance in pixels between the
2113 * top edge of this view and the top edge of its content.
2114 * {@hide}
2115 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002116 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 protected int mPaddingTop;
2118 /**
2119 * The bottom padding in pixels, that is the distance in pixels between the
2120 * bottom edge of this view and the bottom edge of its content.
2121 * {@hide}
2122 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002123 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 protected int mPaddingBottom;
2125
2126 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002127 * Briefly describes the view and is primarily used for accessibility support.
2128 */
2129 private CharSequence mContentDescription;
2130
2131 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 * Cache the paddingRight set by the user to append to the scrollbar's size.
2133 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002134 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 int mUserPaddingRight;
2136
2137 /**
2138 * Cache the paddingBottom set by the user to append to the scrollbar's size.
2139 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002140 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 int mUserPaddingBottom;
2142
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002143 /**
Adam Powell20232d02010-12-08 21:08:53 -08002144 * Cache the paddingLeft set by the user to append to the scrollbar's size.
2145 */
2146 @ViewDebug.ExportedProperty(category = "padding")
2147 int mUserPaddingLeft;
2148
2149 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002150 * @hide
2151 */
2152 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2153 /**
2154 * @hide
2155 */
2156 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157
2158 private Resources mResources = null;
2159
2160 private Drawable mBGDrawable;
2161
2162 private int mBackgroundResource;
2163 private boolean mBackgroundSizeChanged;
2164
2165 /**
2166 * Listener used to dispatch focus change events.
2167 * This field should be made private, so it is hidden from the SDK.
2168 * {@hide}
2169 */
2170 protected OnFocusChangeListener mOnFocusChangeListener;
2171
2172 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002173 * Listeners for layout change events.
2174 */
2175 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2176
2177 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002178 * Listeners for attach events.
2179 */
2180 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2181
2182 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 * Listener used to dispatch click events.
2184 * This field should be made private, so it is hidden from the SDK.
2185 * {@hide}
2186 */
2187 protected OnClickListener mOnClickListener;
2188
2189 /**
2190 * Listener used to dispatch long click events.
2191 * This field should be made private, so it is hidden from the SDK.
2192 * {@hide}
2193 */
2194 protected OnLongClickListener mOnLongClickListener;
2195
2196 /**
2197 * Listener used to build the context menu.
2198 * This field should be made private, so it is hidden from the SDK.
2199 * {@hide}
2200 */
2201 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2202
2203 private OnKeyListener mOnKeyListener;
2204
2205 private OnTouchListener mOnTouchListener;
2206
Jeff Brown33bbfd22011-02-24 20:55:35 -08002207 private OnGenericMotionListener mOnGenericMotionListener;
2208
Chris Tate32affef2010-10-18 15:29:21 -07002209 private OnDragListener mOnDragListener;
2210
Joe Onorato664644d2011-01-23 17:53:23 -08002211 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 /**
2214 * The application environment this view lives in.
2215 * This field should be made private, so it is hidden from the SDK.
2216 * {@hide}
2217 */
2218 protected Context mContext;
2219
2220 private ScrollabilityCache mScrollCache;
2221
2222 private int[] mDrawableState = null;
2223
Romain Guy0211a0a2011-02-14 16:34:59 -08002224 /**
2225 * Set to true when drawing cache is enabled and cannot be created.
2226 *
2227 * @hide
2228 */
2229 public boolean mCachingFailed;
2230
Romain Guy02890fd2010-08-06 17:58:44 -07002231 private Bitmap mDrawingCache;
2232 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002233 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002234 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235
2236 /**
2237 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2238 * the user may specify which view to go to next.
2239 */
2240 private int mNextFocusLeftId = View.NO_ID;
2241
2242 /**
2243 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2244 * the user may specify which view to go to next.
2245 */
2246 private int mNextFocusRightId = View.NO_ID;
2247
2248 /**
2249 * When this view has focus and the next focus is {@link #FOCUS_UP},
2250 * the user may specify which view to go to next.
2251 */
2252 private int mNextFocusUpId = View.NO_ID;
2253
2254 /**
2255 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2256 * the user may specify which view to go to next.
2257 */
2258 private int mNextFocusDownId = View.NO_ID;
2259
Jeff Brown4e6319b2010-12-13 10:36:51 -08002260 /**
2261 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2262 * the user may specify which view to go to next.
2263 */
2264 int mNextFocusForwardId = View.NO_ID;
2265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002267 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002268 private PerformClick mPerformClick;
Joe Malin32736f02011-01-19 16:14:20 -08002269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 private UnsetPressedState mUnsetPressedState;
2271
2272 /**
2273 * Whether the long press's action has been invoked. The tap's action is invoked on the
2274 * up event while a long press is invoked as soon as the long press duration is reached, so
2275 * a long press could be performed before the tap is checked, in which case the tap's action
2276 * should not be invoked.
2277 */
2278 private boolean mHasPerformedLongPress;
2279
2280 /**
2281 * The minimum height of the view. We'll try our best to have the height
2282 * of this view to at least this amount.
2283 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002284 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 private int mMinHeight;
2286
2287 /**
2288 * The minimum width of the view. We'll try our best to have the width
2289 * of this view to at least this amount.
2290 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002291 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 private int mMinWidth;
2293
2294 /**
2295 * The delegate to handle touch events that are physically in this view
2296 * but should be handled by another view.
2297 */
2298 private TouchDelegate mTouchDelegate = null;
2299
2300 /**
2301 * Solid color to use as a background when creating the drawing cache. Enables
2302 * the cache to use 16 bit bitmaps instead of 32 bit.
2303 */
2304 private int mDrawingCacheBackgroundColor = 0;
2305
2306 /**
2307 * Special tree observer used when mAttachInfo is null.
2308 */
2309 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002310
Adam Powelle14579b2009-12-16 18:39:52 -08002311 /**
2312 * Cache the touch slop from the context that created the view.
2313 */
2314 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002317 * Object that handles automatic animation of view properties.
2318 */
2319 private ViewPropertyAnimator mAnimator = null;
2320
2321 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002322 * Flag indicating that a drag can cross window boundaries. When
2323 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2324 * with this flag set, all visible applications will be able to participate
2325 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002326 *
2327 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002328 */
2329 public static final int DRAG_FLAG_GLOBAL = 1;
2330
2331 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002332 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2333 */
2334 private float mVerticalScrollFactor;
2335
2336 /**
Adam Powell20232d02010-12-08 21:08:53 -08002337 * Position of the vertical scroll bar.
2338 */
2339 private int mVerticalScrollbarPosition;
2340
2341 /**
2342 * Position the scroll bar at the default position as determined by the system.
2343 */
2344 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2345
2346 /**
2347 * Position the scroll bar along the left edge.
2348 */
2349 public static final int SCROLLBAR_POSITION_LEFT = 1;
2350
2351 /**
2352 * Position the scroll bar along the right edge.
2353 */
2354 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2355
2356 /**
Romain Guy171c5922011-01-06 10:04:23 -08002357 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002358 *
2359 * @see #getLayerType()
2360 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002361 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002362 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002363 */
2364 public static final int LAYER_TYPE_NONE = 0;
2365
2366 /**
2367 * <p>Indicates that the view has a software layer. A software layer is backed
2368 * by a bitmap and causes the view to be rendered using Android's software
2369 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002370 *
Romain Guy171c5922011-01-06 10:04:23 -08002371 * <p>Software layers have various usages:</p>
2372 * <p>When the application is not using hardware acceleration, a software layer
2373 * is useful to apply a specific color filter and/or blending mode and/or
2374 * translucency to a view and all its children.</p>
2375 * <p>When the application is using hardware acceleration, a software layer
2376 * is useful to render drawing primitives not supported by the hardware
2377 * accelerated pipeline. It can also be used to cache a complex view tree
2378 * into a texture and reduce the complexity of drawing operations. For instance,
2379 * when animating a complex view tree with a translation, a software layer can
2380 * be used to render the view tree only once.</p>
2381 * <p>Software layers should be avoided when the affected view tree updates
2382 * often. Every update will require to re-render the software layer, which can
2383 * potentially be slow (particularly when hardware acceleration is turned on
2384 * since the layer will have to be uploaded into a hardware texture after every
2385 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002386 *
2387 * @see #getLayerType()
2388 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002389 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002390 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002391 */
2392 public static final int LAYER_TYPE_SOFTWARE = 1;
2393
2394 /**
2395 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2396 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2397 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2398 * rendering pipeline, but only if hardware acceleration is turned on for the
2399 * view hierarchy. When hardware acceleration is turned off, hardware layers
2400 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002401 *
Romain Guy171c5922011-01-06 10:04:23 -08002402 * <p>A hardware layer is useful to apply a specific color filter and/or
2403 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002404 * <p>A hardware layer can be used to cache a complex view tree into a
2405 * texture and reduce the complexity of drawing operations. For instance,
2406 * when animating a complex view tree with a translation, a hardware layer can
2407 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002408 * <p>A hardware layer can also be used to increase the rendering quality when
2409 * rotation transformations are applied on a view. It can also be used to
2410 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002411 *
2412 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002413 * @see #setLayerType(int, android.graphics.Paint)
2414 * @see #LAYER_TYPE_NONE
2415 * @see #LAYER_TYPE_SOFTWARE
2416 */
2417 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002418
Romain Guy3aaff3a2011-01-12 14:18:47 -08002419 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2420 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2421 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2422 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2423 })
Romain Guy171c5922011-01-06 10:04:23 -08002424 int mLayerType = LAYER_TYPE_NONE;
2425 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002426 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002427
2428 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002429 * Consistency verifier for debugging purposes.
2430 * @hide
2431 */
2432 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2433 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2434 new InputEventConsistencyVerifier(this, 0) : null;
2435
2436 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 * Simple constructor to use when creating a view from code.
2438 *
2439 * @param context The Context the view is running in, through which it can
2440 * access the current theme, resources, etc.
2441 */
2442 public View(Context context) {
2443 mContext = context;
2444 mResources = context != null ? context.getResources() : null;
Romain Guy8f1344f52009-05-15 16:03:59 -07002445 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Adam Powelle14579b2009-12-16 18:39:52 -08002446 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002447 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 }
2449
2450 /**
2451 * Constructor that is called when inflating a view from XML. This is called
2452 * when a view is being constructed from an XML file, supplying attributes
2453 * that were specified in the XML file. This version uses a default style of
2454 * 0, so the only attribute values applied are those in the Context's Theme
2455 * and the given AttributeSet.
2456 *
2457 * <p>
2458 * The method onFinishInflate() will be called after all children have been
2459 * added.
2460 *
2461 * @param context The Context the view is running in, through which it can
2462 * access the current theme, resources, etc.
2463 * @param attrs The attributes of the XML tag that is inflating the view.
2464 * @see #View(Context, AttributeSet, int)
2465 */
2466 public View(Context context, AttributeSet attrs) {
2467 this(context, attrs, 0);
2468 }
2469
2470 /**
2471 * Perform inflation from XML and apply a class-specific base style. This
2472 * constructor of View allows subclasses to use their own base style when
2473 * they are inflating. For example, a Button class's constructor would call
2474 * this version of the super class constructor and supply
2475 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2476 * the theme's button style to modify all of the base view attributes (in
2477 * particular its background) as well as the Button class's attributes.
2478 *
2479 * @param context The Context the view is running in, through which it can
2480 * access the current theme, resources, etc.
2481 * @param attrs The attributes of the XML tag that is inflating the view.
2482 * @param defStyle The default style to apply to this view. If 0, no style
2483 * will be applied (beyond what is included in the theme). This may
2484 * either be an attribute resource, whose value will be retrieved
2485 * from the current theme, or an explicit style resource.
2486 * @see #View(Context, AttributeSet)
2487 */
2488 public View(Context context, AttributeSet attrs, int defStyle) {
2489 this(context);
2490
2491 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2492 defStyle, 0);
2493
2494 Drawable background = null;
2495
2496 int leftPadding = -1;
2497 int topPadding = -1;
2498 int rightPadding = -1;
2499 int bottomPadding = -1;
2500
2501 int padding = -1;
2502
2503 int viewFlagValues = 0;
2504 int viewFlagMasks = 0;
2505
2506 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 int x = 0;
2509 int y = 0;
2510
Chet Haase73066682010-11-29 15:55:32 -08002511 float tx = 0;
2512 float ty = 0;
2513 float rotation = 0;
2514 float rotationX = 0;
2515 float rotationY = 0;
2516 float sx = 1f;
2517 float sy = 1f;
2518 boolean transformSet = false;
2519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2521
Adam Powell637d3372010-08-25 14:37:03 -07002522 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 final int N = a.getIndexCount();
2524 for (int i = 0; i < N; i++) {
2525 int attr = a.getIndex(i);
2526 switch (attr) {
2527 case com.android.internal.R.styleable.View_background:
2528 background = a.getDrawable(attr);
2529 break;
2530 case com.android.internal.R.styleable.View_padding:
2531 padding = a.getDimensionPixelSize(attr, -1);
2532 break;
2533 case com.android.internal.R.styleable.View_paddingLeft:
2534 leftPadding = a.getDimensionPixelSize(attr, -1);
2535 break;
2536 case com.android.internal.R.styleable.View_paddingTop:
2537 topPadding = a.getDimensionPixelSize(attr, -1);
2538 break;
2539 case com.android.internal.R.styleable.View_paddingRight:
2540 rightPadding = a.getDimensionPixelSize(attr, -1);
2541 break;
2542 case com.android.internal.R.styleable.View_paddingBottom:
2543 bottomPadding = a.getDimensionPixelSize(attr, -1);
2544 break;
2545 case com.android.internal.R.styleable.View_scrollX:
2546 x = a.getDimensionPixelOffset(attr, 0);
2547 break;
2548 case com.android.internal.R.styleable.View_scrollY:
2549 y = a.getDimensionPixelOffset(attr, 0);
2550 break;
Chet Haase73066682010-11-29 15:55:32 -08002551 case com.android.internal.R.styleable.View_alpha:
2552 setAlpha(a.getFloat(attr, 1f));
2553 break;
2554 case com.android.internal.R.styleable.View_transformPivotX:
2555 setPivotX(a.getDimensionPixelOffset(attr, 0));
2556 break;
2557 case com.android.internal.R.styleable.View_transformPivotY:
2558 setPivotY(a.getDimensionPixelOffset(attr, 0));
2559 break;
2560 case com.android.internal.R.styleable.View_translationX:
2561 tx = a.getDimensionPixelOffset(attr, 0);
2562 transformSet = true;
2563 break;
2564 case com.android.internal.R.styleable.View_translationY:
2565 ty = a.getDimensionPixelOffset(attr, 0);
2566 transformSet = true;
2567 break;
2568 case com.android.internal.R.styleable.View_rotation:
2569 rotation = a.getFloat(attr, 0);
2570 transformSet = true;
2571 break;
2572 case com.android.internal.R.styleable.View_rotationX:
2573 rotationX = a.getFloat(attr, 0);
2574 transformSet = true;
2575 break;
2576 case com.android.internal.R.styleable.View_rotationY:
2577 rotationY = a.getFloat(attr, 0);
2578 transformSet = true;
2579 break;
2580 case com.android.internal.R.styleable.View_scaleX:
2581 sx = a.getFloat(attr, 1f);
2582 transformSet = true;
2583 break;
2584 case com.android.internal.R.styleable.View_scaleY:
2585 sy = a.getFloat(attr, 1f);
2586 transformSet = true;
2587 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 case com.android.internal.R.styleable.View_id:
2589 mID = a.getResourceId(attr, NO_ID);
2590 break;
2591 case com.android.internal.R.styleable.View_tag:
2592 mTag = a.getText(attr);
2593 break;
2594 case com.android.internal.R.styleable.View_fitsSystemWindows:
2595 if (a.getBoolean(attr, false)) {
2596 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2597 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2598 }
2599 break;
2600 case com.android.internal.R.styleable.View_focusable:
2601 if (a.getBoolean(attr, false)) {
2602 viewFlagValues |= FOCUSABLE;
2603 viewFlagMasks |= FOCUSABLE_MASK;
2604 }
2605 break;
2606 case com.android.internal.R.styleable.View_focusableInTouchMode:
2607 if (a.getBoolean(attr, false)) {
2608 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2609 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2610 }
2611 break;
2612 case com.android.internal.R.styleable.View_clickable:
2613 if (a.getBoolean(attr, false)) {
2614 viewFlagValues |= CLICKABLE;
2615 viewFlagMasks |= CLICKABLE;
2616 }
2617 break;
2618 case com.android.internal.R.styleable.View_longClickable:
2619 if (a.getBoolean(attr, false)) {
2620 viewFlagValues |= LONG_CLICKABLE;
2621 viewFlagMasks |= LONG_CLICKABLE;
2622 }
2623 break;
2624 case com.android.internal.R.styleable.View_saveEnabled:
2625 if (!a.getBoolean(attr, true)) {
2626 viewFlagValues |= SAVE_DISABLED;
2627 viewFlagMasks |= SAVE_DISABLED_MASK;
2628 }
2629 break;
2630 case com.android.internal.R.styleable.View_duplicateParentState:
2631 if (a.getBoolean(attr, false)) {
2632 viewFlagValues |= DUPLICATE_PARENT_STATE;
2633 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2634 }
2635 break;
2636 case com.android.internal.R.styleable.View_visibility:
2637 final int visibility = a.getInt(attr, 0);
2638 if (visibility != 0) {
2639 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2640 viewFlagMasks |= VISIBILITY_MASK;
2641 }
2642 break;
Cibu Johny7632cb92010-02-22 13:01:02 -08002643 case com.android.internal.R.styleable.View_horizontalDirection:
2644 final int layoutDirection = a.getInt(attr, 0);
2645 if (layoutDirection != 0) {
2646 viewFlagValues |= HORIZONTAL_DIRECTION_FLAGS[layoutDirection];
2647 viewFlagMasks |= HORIZONTAL_DIRECTION_MASK;
2648 }
2649 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 case com.android.internal.R.styleable.View_drawingCacheQuality:
2651 final int cacheQuality = a.getInt(attr, 0);
2652 if (cacheQuality != 0) {
2653 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2654 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2655 }
2656 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002657 case com.android.internal.R.styleable.View_contentDescription:
2658 mContentDescription = a.getString(attr);
2659 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2661 if (!a.getBoolean(attr, true)) {
2662 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2663 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2664 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002665 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2667 if (!a.getBoolean(attr, true)) {
2668 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2669 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2670 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002671 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 case R.styleable.View_scrollbars:
2673 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2674 if (scrollbars != SCROLLBARS_NONE) {
2675 viewFlagValues |= scrollbars;
2676 viewFlagMasks |= SCROLLBARS_MASK;
2677 initializeScrollbars(a);
2678 }
2679 break;
2680 case R.styleable.View_fadingEdge:
2681 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2682 if (fadingEdge != FADING_EDGE_NONE) {
2683 viewFlagValues |= fadingEdge;
2684 viewFlagMasks |= FADING_EDGE_MASK;
2685 initializeFadingEdge(a);
2686 }
2687 break;
2688 case R.styleable.View_scrollbarStyle:
2689 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2690 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2691 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2692 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2693 }
2694 break;
2695 case R.styleable.View_isScrollContainer:
2696 setScrollContainer = true;
2697 if (a.getBoolean(attr, false)) {
2698 setScrollContainer(true);
2699 }
2700 break;
2701 case com.android.internal.R.styleable.View_keepScreenOn:
2702 if (a.getBoolean(attr, false)) {
2703 viewFlagValues |= KEEP_SCREEN_ON;
2704 viewFlagMasks |= KEEP_SCREEN_ON;
2705 }
2706 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002707 case R.styleable.View_filterTouchesWhenObscured:
2708 if (a.getBoolean(attr, false)) {
2709 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2710 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2711 }
2712 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 case R.styleable.View_nextFocusLeft:
2714 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2715 break;
2716 case R.styleable.View_nextFocusRight:
2717 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2718 break;
2719 case R.styleable.View_nextFocusUp:
2720 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2721 break;
2722 case R.styleable.View_nextFocusDown:
2723 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2724 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002725 case R.styleable.View_nextFocusForward:
2726 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2727 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 case R.styleable.View_minWidth:
2729 mMinWidth = a.getDimensionPixelSize(attr, 0);
2730 break;
2731 case R.styleable.View_minHeight:
2732 mMinHeight = a.getDimensionPixelSize(attr, 0);
2733 break;
Romain Guy9a817362009-05-01 10:57:14 -07002734 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002735 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002736 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002737 + "be used within a restricted context");
2738 }
2739
Romain Guy9a817362009-05-01 10:57:14 -07002740 final String handlerName = a.getString(attr);
2741 if (handlerName != null) {
2742 setOnClickListener(new OnClickListener() {
2743 private Method mHandler;
2744
2745 public void onClick(View v) {
2746 if (mHandler == null) {
2747 try {
2748 mHandler = getContext().getClass().getMethod(handlerName,
2749 View.class);
2750 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002751 int id = getId();
2752 String idText = id == NO_ID ? "" : " with id '"
2753 + getContext().getResources().getResourceEntryName(
2754 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002755 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002756 handlerName + "(View) in the activity "
2757 + getContext().getClass() + " for onClick handler"
2758 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002759 }
2760 }
2761
2762 try {
2763 mHandler.invoke(getContext(), View.this);
2764 } catch (IllegalAccessException e) {
2765 throw new IllegalStateException("Could not execute non "
2766 + "public method of the activity", e);
2767 } catch (InvocationTargetException e) {
2768 throw new IllegalStateException("Could not execute "
2769 + "method of the activity", e);
2770 }
2771 }
2772 });
2773 }
2774 break;
Adam Powell637d3372010-08-25 14:37:03 -07002775 case R.styleable.View_overScrollMode:
2776 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2777 break;
Adam Powell20232d02010-12-08 21:08:53 -08002778 case R.styleable.View_verticalScrollbarPosition:
2779 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2780 break;
Romain Guy171c5922011-01-06 10:04:23 -08002781 case R.styleable.View_layerType:
2782 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2783 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 }
2785 }
2786
Adam Powell637d3372010-08-25 14:37:03 -07002787 setOverScrollMode(overScrollMode);
2788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 if (background != null) {
2790 setBackgroundDrawable(background);
2791 }
2792
2793 if (padding >= 0) {
2794 leftPadding = padding;
2795 topPadding = padding;
2796 rightPadding = padding;
2797 bottomPadding = padding;
2798 }
2799
2800 // If the user specified the padding (either with android:padding or
2801 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2802 // use the default padding or the padding from the background drawable
2803 // (stored at this point in mPadding*)
2804 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2805 topPadding >= 0 ? topPadding : mPaddingTop,
2806 rightPadding >= 0 ? rightPadding : mPaddingRight,
2807 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2808
2809 if (viewFlagMasks != 0) {
2810 setFlags(viewFlagValues, viewFlagMasks);
2811 }
2812
2813 // Needs to be called after mViewFlags is set
2814 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2815 recomputePadding();
2816 }
2817
2818 if (x != 0 || y != 0) {
2819 scrollTo(x, y);
2820 }
2821
Chet Haase73066682010-11-29 15:55:32 -08002822 if (transformSet) {
2823 setTranslationX(tx);
2824 setTranslationY(ty);
2825 setRotation(rotation);
2826 setRotationX(rotationX);
2827 setRotationY(rotationY);
2828 setScaleX(sx);
2829 setScaleY(sy);
2830 }
2831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2833 setScrollContainer(true);
2834 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002835
2836 computeOpaqueFlags();
2837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 a.recycle();
2839 }
2840
2841 /**
2842 * Non-public constructor for use in testing
2843 */
2844 View() {
2845 }
2846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 /**
2848 * <p>
2849 * Initializes the fading edges from a given set of styled attributes. This
2850 * method should be called by subclasses that need fading edges and when an
2851 * instance of these subclasses is created programmatically rather than
2852 * being inflated from XML. This method is automatically called when the XML
2853 * is inflated.
2854 * </p>
2855 *
2856 * @param a the styled attributes set to initialize the fading edges from
2857 */
2858 protected void initializeFadingEdge(TypedArray a) {
2859 initScrollCache();
2860
2861 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2862 R.styleable.View_fadingEdgeLength,
2863 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2864 }
2865
2866 /**
2867 * Returns the size of the vertical faded edges used to indicate that more
2868 * content in this view is visible.
2869 *
2870 * @return The size in pixels of the vertical faded edge or 0 if vertical
2871 * faded edges are not enabled for this view.
2872 * @attr ref android.R.styleable#View_fadingEdgeLength
2873 */
2874 public int getVerticalFadingEdgeLength() {
2875 if (isVerticalFadingEdgeEnabled()) {
2876 ScrollabilityCache cache = mScrollCache;
2877 if (cache != null) {
2878 return cache.fadingEdgeLength;
2879 }
2880 }
2881 return 0;
2882 }
2883
2884 /**
2885 * Set the size of the faded edge used to indicate that more content in this
2886 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07002887 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
2888 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
2889 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 *
2891 * @param length The size in pixels of the faded edge used to indicate that more
2892 * content in this view is visible.
2893 */
2894 public void setFadingEdgeLength(int length) {
2895 initScrollCache();
2896 mScrollCache.fadingEdgeLength = length;
2897 }
2898
2899 /**
2900 * Returns the size of the horizontal faded edges used to indicate that more
2901 * content in this view is visible.
2902 *
2903 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2904 * faded edges are not enabled for this view.
2905 * @attr ref android.R.styleable#View_fadingEdgeLength
2906 */
2907 public int getHorizontalFadingEdgeLength() {
2908 if (isHorizontalFadingEdgeEnabled()) {
2909 ScrollabilityCache cache = mScrollCache;
2910 if (cache != null) {
2911 return cache.fadingEdgeLength;
2912 }
2913 }
2914 return 0;
2915 }
2916
2917 /**
2918 * Returns the width of the vertical scrollbar.
2919 *
2920 * @return The width in pixels of the vertical scrollbar or 0 if there
2921 * is no vertical scrollbar.
2922 */
2923 public int getVerticalScrollbarWidth() {
2924 ScrollabilityCache cache = mScrollCache;
2925 if (cache != null) {
2926 ScrollBarDrawable scrollBar = cache.scrollBar;
2927 if (scrollBar != null) {
2928 int size = scrollBar.getSize(true);
2929 if (size <= 0) {
2930 size = cache.scrollBarSize;
2931 }
2932 return size;
2933 }
2934 return 0;
2935 }
2936 return 0;
2937 }
2938
2939 /**
2940 * Returns the height of the horizontal scrollbar.
2941 *
2942 * @return The height in pixels of the horizontal scrollbar or 0 if
2943 * there is no horizontal scrollbar.
2944 */
2945 protected int getHorizontalScrollbarHeight() {
2946 ScrollabilityCache cache = mScrollCache;
2947 if (cache != null) {
2948 ScrollBarDrawable scrollBar = cache.scrollBar;
2949 if (scrollBar != null) {
2950 int size = scrollBar.getSize(false);
2951 if (size <= 0) {
2952 size = cache.scrollBarSize;
2953 }
2954 return size;
2955 }
2956 return 0;
2957 }
2958 return 0;
2959 }
2960
2961 /**
2962 * <p>
2963 * Initializes the scrollbars from a given set of styled attributes. This
2964 * method should be called by subclasses that need scrollbars and when an
2965 * instance of these subclasses is created programmatically rather than
2966 * being inflated from XML. This method is automatically called when the XML
2967 * is inflated.
2968 * </p>
2969 *
2970 * @param a the styled attributes set to initialize the scrollbars from
2971 */
2972 protected void initializeScrollbars(TypedArray a) {
2973 initScrollCache();
2974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08002976
Mike Cleronf116bf82009-09-27 19:14:12 -07002977 if (scrollabilityCache.scrollBar == null) {
2978 scrollabilityCache.scrollBar = new ScrollBarDrawable();
2979 }
Joe Malin32736f02011-01-19 16:14:20 -08002980
Romain Guy8bda2482010-03-02 11:42:11 -08002981 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982
Mike Cleronf116bf82009-09-27 19:14:12 -07002983 if (!fadeScrollbars) {
2984 scrollabilityCache.state = ScrollabilityCache.ON;
2985 }
2986 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08002987
2988
Mike Cleronf116bf82009-09-27 19:14:12 -07002989 scrollabilityCache.scrollBarFadeDuration = a.getInt(
2990 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
2991 .getScrollBarFadeDuration());
2992 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
2993 R.styleable.View_scrollbarDefaultDelayBeforeFade,
2994 ViewConfiguration.getScrollDefaultDelay());
2995
Joe Malin32736f02011-01-19 16:14:20 -08002996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
2998 com.android.internal.R.styleable.View_scrollbarSize,
2999 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3000
3001 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3002 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3003
3004 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3005 if (thumb != null) {
3006 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3007 }
3008
3009 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3010 false);
3011 if (alwaysDraw) {
3012 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3013 }
3014
3015 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3016 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3017
3018 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3019 if (thumb != null) {
3020 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3021 }
3022
3023 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3024 false);
3025 if (alwaysDraw) {
3026 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3027 }
3028
3029 // Re-apply user/background padding so that scrollbar(s) get added
3030 recomputePadding();
3031 }
3032
3033 /**
3034 * <p>
3035 * Initalizes the scrollability cache if necessary.
3036 * </p>
3037 */
3038 private void initScrollCache() {
3039 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003040 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003041 }
3042 }
3043
3044 /**
Adam Powell20232d02010-12-08 21:08:53 -08003045 * Set the position of the vertical scroll bar. Should be one of
3046 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3047 * {@link #SCROLLBAR_POSITION_RIGHT}.
3048 *
3049 * @param position Where the vertical scroll bar should be positioned.
3050 */
3051 public void setVerticalScrollbarPosition(int position) {
3052 if (mVerticalScrollbarPosition != position) {
3053 mVerticalScrollbarPosition = position;
3054 computeOpaqueFlags();
3055 recomputePadding();
3056 }
3057 }
3058
3059 /**
3060 * @return The position where the vertical scroll bar will show, if applicable.
3061 * @see #setVerticalScrollbarPosition(int)
3062 */
3063 public int getVerticalScrollbarPosition() {
3064 return mVerticalScrollbarPosition;
3065 }
3066
3067 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003068 * Register a callback to be invoked when focus of this view changed.
3069 *
3070 * @param l The callback that will run.
3071 */
3072 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3073 mOnFocusChangeListener = l;
3074 }
3075
3076 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003077 * Add a listener that will be called when the bounds of the view change due to
3078 * layout processing.
3079 *
3080 * @param listener The listener that will be called when layout bounds change.
3081 */
3082 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3083 if (mOnLayoutChangeListeners == null) {
3084 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3085 }
3086 mOnLayoutChangeListeners.add(listener);
3087 }
3088
3089 /**
3090 * Remove a listener for layout changes.
3091 *
3092 * @param listener The listener for layout bounds change.
3093 */
3094 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3095 if (mOnLayoutChangeListeners == null) {
3096 return;
3097 }
3098 mOnLayoutChangeListeners.remove(listener);
3099 }
3100
3101 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003102 * Add a listener for attach state changes.
3103 *
3104 * This listener will be called whenever this view is attached or detached
3105 * from a window. Remove the listener using
3106 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3107 *
3108 * @param listener Listener to attach
3109 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3110 */
3111 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3112 if (mOnAttachStateChangeListeners == null) {
3113 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3114 }
3115 mOnAttachStateChangeListeners.add(listener);
3116 }
3117
3118 /**
3119 * Remove a listener for attach state changes. The listener will receive no further
3120 * notification of window attach/detach events.
3121 *
3122 * @param listener Listener to remove
3123 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3124 */
3125 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3126 if (mOnAttachStateChangeListeners == null) {
3127 return;
3128 }
3129 mOnAttachStateChangeListeners.remove(listener);
3130 }
3131
3132 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133 * Returns the focus-change callback registered for this view.
3134 *
3135 * @return The callback, or null if one is not registered.
3136 */
3137 public OnFocusChangeListener getOnFocusChangeListener() {
3138 return mOnFocusChangeListener;
3139 }
3140
3141 /**
3142 * Register a callback to be invoked when this view is clicked. If this view is not
3143 * clickable, it becomes clickable.
3144 *
3145 * @param l The callback that will run
3146 *
3147 * @see #setClickable(boolean)
3148 */
3149 public void setOnClickListener(OnClickListener l) {
3150 if (!isClickable()) {
3151 setClickable(true);
3152 }
3153 mOnClickListener = l;
3154 }
3155
3156 /**
3157 * Register a callback to be invoked when this view is clicked and held. If this view is not
3158 * long clickable, it becomes long clickable.
3159 *
3160 * @param l The callback that will run
3161 *
3162 * @see #setLongClickable(boolean)
3163 */
3164 public void setOnLongClickListener(OnLongClickListener l) {
3165 if (!isLongClickable()) {
3166 setLongClickable(true);
3167 }
3168 mOnLongClickListener = l;
3169 }
3170
3171 /**
3172 * Register a callback to be invoked when the context menu for this view is
3173 * being built. If this view is not long clickable, it becomes long clickable.
3174 *
3175 * @param l The callback that will run
3176 *
3177 */
3178 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3179 if (!isLongClickable()) {
3180 setLongClickable(true);
3181 }
3182 mOnCreateContextMenuListener = l;
3183 }
3184
3185 /**
3186 * Call this view's OnClickListener, if it is defined.
3187 *
3188 * @return True there was an assigned OnClickListener that was called, false
3189 * otherwise is returned.
3190 */
3191 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003192 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 if (mOnClickListener != null) {
3195 playSoundEffect(SoundEffectConstants.CLICK);
3196 mOnClickListener.onClick(this);
3197 return true;
3198 }
3199
3200 return false;
3201 }
3202
3203 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003204 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3205 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003207 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 */
3209 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003210 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003212 boolean handled = false;
3213 if (mOnLongClickListener != null) {
3214 handled = mOnLongClickListener.onLongClick(View.this);
3215 }
3216 if (!handled) {
3217 handled = showContextMenu();
3218 }
3219 if (handled) {
3220 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3221 }
3222 return handled;
3223 }
3224
3225 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003226 * Performs button-related actions during a touch down event.
3227 *
3228 * @param event The event.
3229 * @return True if the down was consumed.
3230 *
3231 * @hide
3232 */
3233 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3234 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3235 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3236 return true;
3237 }
3238 }
3239 return false;
3240 }
3241
3242 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 * Bring up the context menu for this view.
3244 *
3245 * @return Whether a context menu was displayed.
3246 */
3247 public boolean showContextMenu() {
3248 return getParent().showContextMenuForChild(this);
3249 }
3250
3251 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003252 * Bring up the context menu for this view, referring to the item under the specified point.
3253 *
3254 * @param x The referenced x coordinate.
3255 * @param y The referenced y coordinate.
3256 * @param metaState The keyboard modifiers that were pressed.
3257 * @return Whether a context menu was displayed.
3258 *
3259 * @hide
3260 */
3261 public boolean showContextMenu(float x, float y, int metaState) {
3262 return showContextMenu();
3263 }
3264
3265 /**
Adam Powell6e346362010-07-23 10:18:23 -07003266 * Start an action mode.
3267 *
3268 * @param callback Callback that will control the lifecycle of the action mode
3269 * @return The new action mode if it is started, null otherwise
3270 *
3271 * @see ActionMode
3272 */
3273 public ActionMode startActionMode(ActionMode.Callback callback) {
3274 return getParent().startActionModeForChild(this, callback);
3275 }
3276
3277 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003278 * Register a callback to be invoked when a key is pressed in this view.
3279 * @param l the key listener to attach to this view
3280 */
3281 public void setOnKeyListener(OnKeyListener l) {
3282 mOnKeyListener = l;
3283 }
3284
3285 /**
3286 * Register a callback to be invoked when a touch event is sent to this view.
3287 * @param l the touch listener to attach to this view
3288 */
3289 public void setOnTouchListener(OnTouchListener l) {
3290 mOnTouchListener = l;
3291 }
3292
3293 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003294 * Register a callback to be invoked when a generic motion event is sent to this view.
3295 * @param l the generic motion listener to attach to this view
3296 */
3297 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3298 mOnGenericMotionListener = l;
3299 }
3300
3301 /**
Joe Malin32736f02011-01-19 16:14:20 -08003302 * Register a drag event listener callback object for this View. The parameter is
3303 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3304 * View, the system calls the
3305 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3306 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003307 */
3308 public void setOnDragListener(OnDragListener l) {
3309 mOnDragListener = l;
3310 }
3311
3312 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003313 * Give this view focus. This will cause
3314 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003315 *
3316 * Note: this does not check whether this {@link View} should get focus, it just
3317 * gives it focus no matter what. It should only be called internally by framework
3318 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3319 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003320 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3321 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 * focus moved when requestFocus() is called. It may not always
3323 * apply, in which case use the default View.FOCUS_DOWN.
3324 * @param previouslyFocusedRect The rectangle of the view that had focus
3325 * prior in this View's coordinate system.
3326 */
3327 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3328 if (DBG) {
3329 System.out.println(this + " requestFocus()");
3330 }
3331
3332 if ((mPrivateFlags & FOCUSED) == 0) {
3333 mPrivateFlags |= FOCUSED;
3334
3335 if (mParent != null) {
3336 mParent.requestChildFocus(this, this);
3337 }
3338
3339 onFocusChanged(true, direction, previouslyFocusedRect);
3340 refreshDrawableState();
3341 }
3342 }
3343
3344 /**
3345 * Request that a rectangle of this view be visible on the screen,
3346 * scrolling if necessary just enough.
3347 *
3348 * <p>A View should call this if it maintains some notion of which part
3349 * of its content is interesting. For example, a text editing view
3350 * should call this when its cursor moves.
3351 *
3352 * @param rectangle The rectangle.
3353 * @return Whether any parent scrolled.
3354 */
3355 public boolean requestRectangleOnScreen(Rect rectangle) {
3356 return requestRectangleOnScreen(rectangle, false);
3357 }
3358
3359 /**
3360 * Request that a rectangle of this view be visible on the screen,
3361 * scrolling if necessary just enough.
3362 *
3363 * <p>A View should call this if it maintains some notion of which part
3364 * of its content is interesting. For example, a text editing view
3365 * should call this when its cursor moves.
3366 *
3367 * <p>When <code>immediate</code> is set to true, scrolling will not be
3368 * animated.
3369 *
3370 * @param rectangle The rectangle.
3371 * @param immediate True to forbid animated scrolling, false otherwise
3372 * @return Whether any parent scrolled.
3373 */
3374 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3375 View child = this;
3376 ViewParent parent = mParent;
3377 boolean scrolled = false;
3378 while (parent != null) {
3379 scrolled |= parent.requestChildRectangleOnScreen(child,
3380 rectangle, immediate);
3381
3382 // offset rect so next call has the rectangle in the
3383 // coordinate system of its direct child.
3384 rectangle.offset(child.getLeft(), child.getTop());
3385 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3386
3387 if (!(parent instanceof View)) {
3388 break;
3389 }
Romain Guy8506ab42009-06-11 17:35:47 -07003390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 child = (View) parent;
3392 parent = child.getParent();
3393 }
3394 return scrolled;
3395 }
3396
3397 /**
3398 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003399 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003400 */
3401 public void clearFocus() {
3402 if (DBG) {
3403 System.out.println(this + " clearFocus()");
3404 }
3405
3406 if ((mPrivateFlags & FOCUSED) != 0) {
3407 mPrivateFlags &= ~FOCUSED;
3408
3409 if (mParent != null) {
3410 mParent.clearChildFocus(this);
3411 }
3412
3413 onFocusChanged(false, 0, null);
3414 refreshDrawableState();
3415 }
3416 }
3417
3418 /**
3419 * Called to clear the focus of a view that is about to be removed.
3420 * Doesn't call clearChildFocus, which prevents this view from taking
3421 * focus again before it has been removed from the parent
3422 */
3423 void clearFocusForRemoval() {
3424 if ((mPrivateFlags & FOCUSED) != 0) {
3425 mPrivateFlags &= ~FOCUSED;
3426
3427 onFocusChanged(false, 0, null);
3428 refreshDrawableState();
3429 }
3430 }
3431
3432 /**
3433 * Called internally by the view system when a new view is getting focus.
3434 * This is what clears the old focus.
3435 */
3436 void unFocus() {
3437 if (DBG) {
3438 System.out.println(this + " unFocus()");
3439 }
3440
3441 if ((mPrivateFlags & FOCUSED) != 0) {
3442 mPrivateFlags &= ~FOCUSED;
3443
3444 onFocusChanged(false, 0, null);
3445 refreshDrawableState();
3446 }
3447 }
3448
3449 /**
3450 * Returns true if this view has focus iteself, or is the ancestor of the
3451 * view that has focus.
3452 *
3453 * @return True if this view has or contains focus, false otherwise.
3454 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003455 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 public boolean hasFocus() {
3457 return (mPrivateFlags & FOCUSED) != 0;
3458 }
3459
3460 /**
3461 * Returns true if this view is focusable or if it contains a reachable View
3462 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3463 * is a View whose parents do not block descendants focus.
3464 *
3465 * Only {@link #VISIBLE} views are considered focusable.
3466 *
3467 * @return True if the view is focusable or if the view contains a focusable
3468 * View, false otherwise.
3469 *
3470 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3471 */
3472 public boolean hasFocusable() {
3473 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3474 }
3475
3476 /**
3477 * Called by the view system when the focus state of this view changes.
3478 * When the focus change event is caused by directional navigation, direction
3479 * and previouslyFocusedRect provide insight into where the focus is coming from.
3480 * When overriding, be sure to call up through to the super class so that
3481 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003482 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003483 * @param gainFocus True if the View has focus; false otherwise.
3484 * @param direction The direction focus has moved when requestFocus()
3485 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003486 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3487 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3488 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003489 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3490 * system, of the previously focused view. If applicable, this will be
3491 * passed in as finer grained information about where the focus is coming
3492 * from (in addition to direction). Will be <code>null</code> otherwise.
3493 */
3494 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003495 if (gainFocus) {
3496 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3497 }
3498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 InputMethodManager imm = InputMethodManager.peekInstance();
3500 if (!gainFocus) {
3501 if (isPressed()) {
3502 setPressed(false);
3503 }
3504 if (imm != null && mAttachInfo != null
3505 && mAttachInfo.mHasWindowFocus) {
3506 imm.focusOut(this);
3507 }
Romain Guya2431d02009-04-30 16:30:00 -07003508 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003509 } else if (imm != null && mAttachInfo != null
3510 && mAttachInfo.mHasWindowFocus) {
3511 imm.focusIn(this);
3512 }
Romain Guy8506ab42009-06-11 17:35:47 -07003513
Romain Guy0fd89bf2011-01-26 15:41:30 -08003514 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 if (mOnFocusChangeListener != null) {
3516 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3517 }
Joe Malin32736f02011-01-19 16:14:20 -08003518
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003519 if (mAttachInfo != null) {
3520 mAttachInfo.mKeyDispatchState.reset(this);
3521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003522 }
3523
3524 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003525 * Sends an accessibility event of the given type. If accessiiblity is
3526 * not enabled this method has no effect. The default implementation calls
3527 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3528 * to populate information about the event source (this View), then calls
3529 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3530 * populate the text content of the event source including its descendants,
3531 * and last calls
3532 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3533 * on its parent to resuest sending of the event to interested parties.
3534 *
3535 * @param eventType The type of the event to send.
3536 *
3537 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3538 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3539 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
svetoslavganov75986cf2009-05-14 22:28:01 -07003540 */
3541 public void sendAccessibilityEvent(int eventType) {
3542 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3543 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3544 }
3545 }
3546
3547 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003548 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3549 * takes as an argument an empty {@link AccessibilityEvent} and does not
3550 * perfrom a check whether accessibility is enabled.
3551 *
3552 * @param event The event to send.
3553 *
3554 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003555 */
3556 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003557 if (!isShown()) {
3558 return;
3559 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003560 onInitializeAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003561 dispatchPopulateAccessibilityEvent(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003562 // In the beginning we called #isShown(), so we know that getParent() is not null.
3563 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003564 }
3565
3566 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003567 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3568 * to its children for adding their text content to the event. Note that the
3569 * event text is populated in a separate dispatch path since we add to the
3570 * event not only the text of the source but also the text of all its descendants.
3571 * </p>
3572 * A typical implementation will call
3573 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3574 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3575 * on each child. Override this method if custom population of the event text
3576 * content is required.
svetoslavganov75986cf2009-05-14 22:28:01 -07003577 *
3578 * @param event The event.
3579 *
3580 * @return True if the event population was completed.
3581 */
3582 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003583 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003584 return false;
3585 }
3586
3587 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003588 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003589 * giving a chance to this View to populate the accessibility event with its
3590 * text content. While the implementation is free to modify other event
3591 * attributes this should be performed in
3592 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
3593 * <p>
3594 * Example: Adding formatted date string to an accessibility event in addition
3595 * to the text added by the super implementation.
3596 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003597 * public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3598 * super.onPopulateAccessibilityEvent(event);
3599 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
3600 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
3601 * mCurrentDate.getTimeInMillis(), flags);
3602 * event.getText().add(selectedDateUtterance);
3603 * }
3604 * </code></pre></p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003605 *
3606 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003607 *
3608 * @see #sendAccessibilityEvent(int)
3609 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003610 */
3611 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3612
3613 }
3614
3615 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003616 * Initializes an {@link AccessibilityEvent} with information about the
3617 * the type of the event and this View which is the event source. In other
3618 * words, the source of an accessibility event is the view whose state
3619 * change triggered firing the event.
3620 * <p>
3621 * Example: Setting the password property of an event in addition
3622 * to properties set by the super implementation.
3623 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003624 * public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3625 * super.onInitializeAccessibilityEvent(event);
3626 * event.setPassword(true);
3627 * }
3628 * </code></pre></p>
3629 * @param event The event to initialeze.
3630 *
3631 * @see #sendAccessibilityEvent(int)
3632 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3633 */
3634 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3635 event.setClassName(getClass().getName());
3636 event.setPackageName(getContext().getPackageName());
3637 event.setEnabled(isEnabled());
3638 event.setContentDescription(mContentDescription);
3639
3640 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
3641 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3642 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
3643 event.setItemCount(focusablesTempList.size());
3644 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3645 focusablesTempList.clear();
3646 }
3647 }
3648
3649 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003650 * Gets the {@link View} description. It briefly describes the view and is
3651 * primarily used for accessibility support. Set this property to enable
3652 * better accessibility support for your application. This is especially
3653 * true for views that do not have textual representation (For example,
3654 * ImageButton).
3655 *
3656 * @return The content descriptiopn.
3657 *
3658 * @attr ref android.R.styleable#View_contentDescription
3659 */
3660 public CharSequence getContentDescription() {
3661 return mContentDescription;
3662 }
3663
3664 /**
3665 * Sets the {@link View} description. It briefly describes the view and is
3666 * primarily used for accessibility support. Set this property to enable
3667 * better accessibility support for your application. This is especially
3668 * true for views that do not have textual representation (For example,
3669 * ImageButton).
3670 *
3671 * @param contentDescription The content description.
3672 *
3673 * @attr ref android.R.styleable#View_contentDescription
3674 */
3675 public void setContentDescription(CharSequence contentDescription) {
3676 mContentDescription = contentDescription;
3677 }
3678
3679 /**
Romain Guya2431d02009-04-30 16:30:00 -07003680 * Invoked whenever this view loses focus, either by losing window focus or by losing
3681 * focus within its window. This method can be used to clear any state tied to the
3682 * focus. For instance, if a button is held pressed with the trackball and the window
3683 * loses focus, this method can be used to cancel the press.
3684 *
3685 * Subclasses of View overriding this method should always call super.onFocusLost().
3686 *
3687 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003688 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003689 *
3690 * @hide pending API council approval
3691 */
3692 protected void onFocusLost() {
3693 resetPressedState();
3694 }
3695
3696 private void resetPressedState() {
3697 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3698 return;
3699 }
3700
3701 if (isPressed()) {
3702 setPressed(false);
3703
3704 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05003705 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003706 }
3707 }
3708 }
3709
3710 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711 * Returns true if this view has focus
3712 *
3713 * @return True if this view has focus, false otherwise.
3714 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003715 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716 public boolean isFocused() {
3717 return (mPrivateFlags & FOCUSED) != 0;
3718 }
3719
3720 /**
3721 * Find the view in the hierarchy rooted at this view that currently has
3722 * focus.
3723 *
3724 * @return The view that currently has focus, or null if no focused view can
3725 * be found.
3726 */
3727 public View findFocus() {
3728 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
3729 }
3730
3731 /**
3732 * Change whether this view is one of the set of scrollable containers in
3733 * its window. This will be used to determine whether the window can
3734 * resize or must pan when a soft input area is open -- scrollable
3735 * containers allow the window to use resize mode since the container
3736 * will appropriately shrink.
3737 */
3738 public void setScrollContainer(boolean isScrollContainer) {
3739 if (isScrollContainer) {
3740 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
3741 mAttachInfo.mScrollContainers.add(this);
3742 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
3743 }
3744 mPrivateFlags |= SCROLL_CONTAINER;
3745 } else {
3746 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
3747 mAttachInfo.mScrollContainers.remove(this);
3748 }
3749 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
3750 }
3751 }
3752
3753 /**
3754 * Returns the quality of the drawing cache.
3755 *
3756 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3757 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3758 *
3759 * @see #setDrawingCacheQuality(int)
3760 * @see #setDrawingCacheEnabled(boolean)
3761 * @see #isDrawingCacheEnabled()
3762 *
3763 * @attr ref android.R.styleable#View_drawingCacheQuality
3764 */
3765 public int getDrawingCacheQuality() {
3766 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
3767 }
3768
3769 /**
3770 * Set the drawing cache quality of this view. This value is used only when the
3771 * drawing cache is enabled
3772 *
3773 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3774 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3775 *
3776 * @see #getDrawingCacheQuality()
3777 * @see #setDrawingCacheEnabled(boolean)
3778 * @see #isDrawingCacheEnabled()
3779 *
3780 * @attr ref android.R.styleable#View_drawingCacheQuality
3781 */
3782 public void setDrawingCacheQuality(int quality) {
3783 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
3784 }
3785
3786 /**
3787 * Returns whether the screen should remain on, corresponding to the current
3788 * value of {@link #KEEP_SCREEN_ON}.
3789 *
3790 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
3791 *
3792 * @see #setKeepScreenOn(boolean)
3793 *
3794 * @attr ref android.R.styleable#View_keepScreenOn
3795 */
3796 public boolean getKeepScreenOn() {
3797 return (mViewFlags & KEEP_SCREEN_ON) != 0;
3798 }
3799
3800 /**
3801 * Controls whether the screen should remain on, modifying the
3802 * value of {@link #KEEP_SCREEN_ON}.
3803 *
3804 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
3805 *
3806 * @see #getKeepScreenOn()
3807 *
3808 * @attr ref android.R.styleable#View_keepScreenOn
3809 */
3810 public void setKeepScreenOn(boolean keepScreenOn) {
3811 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
3812 }
3813
3814 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003815 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3816 * @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 -08003817 *
3818 * @attr ref android.R.styleable#View_nextFocusLeft
3819 */
3820 public int getNextFocusLeftId() {
3821 return mNextFocusLeftId;
3822 }
3823
3824 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003825 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3826 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
3827 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003828 *
3829 * @attr ref android.R.styleable#View_nextFocusLeft
3830 */
3831 public void setNextFocusLeftId(int nextFocusLeftId) {
3832 mNextFocusLeftId = nextFocusLeftId;
3833 }
3834
3835 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003836 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3837 * @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 -08003838 *
3839 * @attr ref android.R.styleable#View_nextFocusRight
3840 */
3841 public int getNextFocusRightId() {
3842 return mNextFocusRightId;
3843 }
3844
3845 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003846 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3847 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
3848 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003849 *
3850 * @attr ref android.R.styleable#View_nextFocusRight
3851 */
3852 public void setNextFocusRightId(int nextFocusRightId) {
3853 mNextFocusRightId = nextFocusRightId;
3854 }
3855
3856 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003857 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3858 * @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 -08003859 *
3860 * @attr ref android.R.styleable#View_nextFocusUp
3861 */
3862 public int getNextFocusUpId() {
3863 return mNextFocusUpId;
3864 }
3865
3866 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003867 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3868 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
3869 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003870 *
3871 * @attr ref android.R.styleable#View_nextFocusUp
3872 */
3873 public void setNextFocusUpId(int nextFocusUpId) {
3874 mNextFocusUpId = nextFocusUpId;
3875 }
3876
3877 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003878 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3879 * @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 -08003880 *
3881 * @attr ref android.R.styleable#View_nextFocusDown
3882 */
3883 public int getNextFocusDownId() {
3884 return mNextFocusDownId;
3885 }
3886
3887 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003888 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3889 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
3890 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003891 *
3892 * @attr ref android.R.styleable#View_nextFocusDown
3893 */
3894 public void setNextFocusDownId(int nextFocusDownId) {
3895 mNextFocusDownId = nextFocusDownId;
3896 }
3897
3898 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003899 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3900 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
3901 *
3902 * @attr ref android.R.styleable#View_nextFocusForward
3903 */
3904 public int getNextFocusForwardId() {
3905 return mNextFocusForwardId;
3906 }
3907
3908 /**
3909 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3910 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
3911 * decide automatically.
3912 *
3913 * @attr ref android.R.styleable#View_nextFocusForward
3914 */
3915 public void setNextFocusForwardId(int nextFocusForwardId) {
3916 mNextFocusForwardId = nextFocusForwardId;
3917 }
3918
3919 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003920 * Returns the visibility of this view and all of its ancestors
3921 *
3922 * @return True if this view and all of its ancestors are {@link #VISIBLE}
3923 */
3924 public boolean isShown() {
3925 View current = this;
3926 //noinspection ConstantConditions
3927 do {
3928 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
3929 return false;
3930 }
3931 ViewParent parent = current.mParent;
3932 if (parent == null) {
3933 return false; // We are not attached to the view root
3934 }
3935 if (!(parent instanceof View)) {
3936 return true;
3937 }
3938 current = (View) parent;
3939 } while (current != null);
3940
3941 return false;
3942 }
3943
3944 /**
3945 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
3946 * is set
3947 *
3948 * @param insets Insets for system windows
3949 *
3950 * @return True if this view applied the insets, false otherwise
3951 */
3952 protected boolean fitSystemWindows(Rect insets) {
3953 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
3954 mPaddingLeft = insets.left;
3955 mPaddingTop = insets.top;
3956 mPaddingRight = insets.right;
3957 mPaddingBottom = insets.bottom;
3958 requestLayout();
3959 return true;
3960 }
3961 return false;
3962 }
3963
3964 /**
3965 * Returns the visibility status for this view.
3966 *
3967 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3968 * @attr ref android.R.styleable#View_visibility
3969 */
3970 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003971 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
3972 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
3973 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003974 })
3975 public int getVisibility() {
3976 return mViewFlags & VISIBILITY_MASK;
3977 }
3978
3979 /**
3980 * Set the enabled state of this view.
3981 *
3982 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3983 * @attr ref android.R.styleable#View_visibility
3984 */
3985 @RemotableViewMethod
3986 public void setVisibility(int visibility) {
3987 setFlags(visibility, VISIBILITY_MASK);
3988 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
3989 }
3990
3991 /**
3992 * Returns the enabled status for this view. The interpretation of the
3993 * enabled state varies by subclass.
3994 *
3995 * @return True if this view is enabled, false otherwise.
3996 */
3997 @ViewDebug.ExportedProperty
3998 public boolean isEnabled() {
3999 return (mViewFlags & ENABLED_MASK) == ENABLED;
4000 }
4001
4002 /**
4003 * Set the enabled state of this view. The interpretation of the enabled
4004 * state varies by subclass.
4005 *
4006 * @param enabled True if this view is enabled, false otherwise.
4007 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004008 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004010 if (enabled == isEnabled()) return;
4011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004012 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4013
4014 /*
4015 * The View most likely has to change its appearance, so refresh
4016 * the drawable state.
4017 */
4018 refreshDrawableState();
4019
4020 // Invalidate too, since the default behavior for views is to be
4021 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004022 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023 }
4024
4025 /**
4026 * Set whether this view can receive the focus.
4027 *
4028 * Setting this to false will also ensure that this view is not focusable
4029 * in touch mode.
4030 *
4031 * @param focusable If true, this view can receive the focus.
4032 *
4033 * @see #setFocusableInTouchMode(boolean)
4034 * @attr ref android.R.styleable#View_focusable
4035 */
4036 public void setFocusable(boolean focusable) {
4037 if (!focusable) {
4038 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4039 }
4040 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4041 }
4042
4043 /**
4044 * Set whether this view can receive focus while in touch mode.
4045 *
4046 * Setting this to true will also ensure that this view is focusable.
4047 *
4048 * @param focusableInTouchMode If true, this view can receive the focus while
4049 * in touch mode.
4050 *
4051 * @see #setFocusable(boolean)
4052 * @attr ref android.R.styleable#View_focusableInTouchMode
4053 */
4054 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4055 // Focusable in touch mode should always be set before the focusable flag
4056 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4057 // which, in touch mode, will not successfully request focus on this view
4058 // because the focusable in touch mode flag is not set
4059 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4060 if (focusableInTouchMode) {
4061 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4062 }
4063 }
4064
4065 /**
4066 * Set whether this view should have sound effects enabled for events such as
4067 * clicking and touching.
4068 *
4069 * <p>You may wish to disable sound effects for a view if you already play sounds,
4070 * for instance, a dial key that plays dtmf tones.
4071 *
4072 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4073 * @see #isSoundEffectsEnabled()
4074 * @see #playSoundEffect(int)
4075 * @attr ref android.R.styleable#View_soundEffectsEnabled
4076 */
4077 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4078 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4079 }
4080
4081 /**
4082 * @return whether this view should have sound effects enabled for events such as
4083 * clicking and touching.
4084 *
4085 * @see #setSoundEffectsEnabled(boolean)
4086 * @see #playSoundEffect(int)
4087 * @attr ref android.R.styleable#View_soundEffectsEnabled
4088 */
4089 @ViewDebug.ExportedProperty
4090 public boolean isSoundEffectsEnabled() {
4091 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4092 }
4093
4094 /**
4095 * Set whether this view should have haptic feedback for events such as
4096 * long presses.
4097 *
4098 * <p>You may wish to disable haptic feedback if your view already controls
4099 * its own haptic feedback.
4100 *
4101 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4102 * @see #isHapticFeedbackEnabled()
4103 * @see #performHapticFeedback(int)
4104 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4105 */
4106 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4107 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4108 }
4109
4110 /**
4111 * @return whether this view should have haptic feedback enabled for events
4112 * long presses.
4113 *
4114 * @see #setHapticFeedbackEnabled(boolean)
4115 * @see #performHapticFeedback(int)
4116 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4117 */
4118 @ViewDebug.ExportedProperty
4119 public boolean isHapticFeedbackEnabled() {
4120 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4121 }
4122
4123 /**
Cibu Johny7632cb92010-02-22 13:01:02 -08004124 * Returns the horizontal direction for this view.
4125 *
4126 * @return One of {@link #HORIZONTAL_DIRECTION_LTR},
4127 * {@link #HORIZONTAL_DIRECTION_RTL},
4128 * {@link #HORIZONTAL_DIRECTION_INHERIT} or
4129 * {@link #HORIZONTAL_DIRECTION_LOCALE}.
4130 * @attr ref android.R.styleable#View_horizontalDirection
4131 * @hide
4132 */
4133 @ViewDebug.ExportedProperty(mapping = {
4134 @ViewDebug.IntToString(from = HORIZONTAL_DIRECTION_LTR, to = "LTR"),
4135 @ViewDebug.IntToString(from = HORIZONTAL_DIRECTION_RTL, to = "RTL"),
4136 @ViewDebug.IntToString(from = HORIZONTAL_DIRECTION_INHERIT, to = "INHERIT"),
4137 @ViewDebug.IntToString(from = HORIZONTAL_DIRECTION_LOCALE, to = "LOCALE")
4138 })
4139 public int getHorizontalDirection() {
4140 return mViewFlags & HORIZONTAL_DIRECTION_MASK;
4141 }
4142
4143 /**
4144 * Set the horizontal direction for this view.
4145 *
4146 * @param horizontalDirection One of {@link #HORIZONTAL_DIRECTION_LTR},
4147 * {@link #HORIZONTAL_DIRECTION_RTL},
4148 * {@link #HORIZONTAL_DIRECTION_INHERIT} or
4149 * {@link #HORIZONTAL_DIRECTION_LOCALE}.
4150 * @attr ref android.R.styleable#View_horizontalDirection
4151 * @hide
4152 */
4153 @RemotableViewMethod
4154 public void setHorizontalDirection(int horizontalDirection) {
4155 setFlags(horizontalDirection, HORIZONTAL_DIRECTION_MASK);
4156 }
4157
4158 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 * If this view doesn't do any drawing on its own, set this flag to
4160 * allow further optimizations. By default, this flag is not set on
4161 * View, but could be set on some View subclasses such as ViewGroup.
4162 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004163 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4164 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004165 *
4166 * @param willNotDraw whether or not this View draw on its own
4167 */
4168 public void setWillNotDraw(boolean willNotDraw) {
4169 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4170 }
4171
4172 /**
4173 * Returns whether or not this View draws on its own.
4174 *
4175 * @return true if this view has nothing to draw, false otherwise
4176 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004177 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 public boolean willNotDraw() {
4179 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4180 }
4181
4182 /**
4183 * When a View's drawing cache is enabled, drawing is redirected to an
4184 * offscreen bitmap. Some views, like an ImageView, must be able to
4185 * bypass this mechanism if they already draw a single bitmap, to avoid
4186 * unnecessary usage of the memory.
4187 *
4188 * @param willNotCacheDrawing true if this view does not cache its
4189 * drawing, false otherwise
4190 */
4191 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4192 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4193 }
4194
4195 /**
4196 * Returns whether or not this View can cache its drawing or not.
4197 *
4198 * @return true if this view does not cache its drawing, false otherwise
4199 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004200 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 public boolean willNotCacheDrawing() {
4202 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4203 }
4204
4205 /**
4206 * Indicates whether this view reacts to click events or not.
4207 *
4208 * @return true if the view is clickable, false otherwise
4209 *
4210 * @see #setClickable(boolean)
4211 * @attr ref android.R.styleable#View_clickable
4212 */
4213 @ViewDebug.ExportedProperty
4214 public boolean isClickable() {
4215 return (mViewFlags & CLICKABLE) == CLICKABLE;
4216 }
4217
4218 /**
4219 * Enables or disables click events for this view. When a view
4220 * is clickable it will change its state to "pressed" on every click.
4221 * Subclasses should set the view clickable to visually react to
4222 * user's clicks.
4223 *
4224 * @param clickable true to make the view clickable, false otherwise
4225 *
4226 * @see #isClickable()
4227 * @attr ref android.R.styleable#View_clickable
4228 */
4229 public void setClickable(boolean clickable) {
4230 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4231 }
4232
4233 /**
4234 * Indicates whether this view reacts to long click events or not.
4235 *
4236 * @return true if the view is long clickable, false otherwise
4237 *
4238 * @see #setLongClickable(boolean)
4239 * @attr ref android.R.styleable#View_longClickable
4240 */
4241 public boolean isLongClickable() {
4242 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4243 }
4244
4245 /**
4246 * Enables or disables long click events for this view. When a view is long
4247 * clickable it reacts to the user holding down the button for a longer
4248 * duration than a tap. This event can either launch the listener or a
4249 * context menu.
4250 *
4251 * @param longClickable true to make the view long clickable, false otherwise
4252 * @see #isLongClickable()
4253 * @attr ref android.R.styleable#View_longClickable
4254 */
4255 public void setLongClickable(boolean longClickable) {
4256 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4257 }
4258
4259 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004260 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004261 *
4262 * @see #isClickable()
4263 * @see #setClickable(boolean)
4264 *
4265 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4266 * the View's internal state from a previously set "pressed" state.
4267 */
4268 public void setPressed(boolean pressed) {
4269 if (pressed) {
4270 mPrivateFlags |= PRESSED;
4271 } else {
4272 mPrivateFlags &= ~PRESSED;
4273 }
4274 refreshDrawableState();
4275 dispatchSetPressed(pressed);
4276 }
4277
4278 /**
4279 * Dispatch setPressed to all of this View's children.
4280 *
4281 * @see #setPressed(boolean)
4282 *
4283 * @param pressed The new pressed state
4284 */
4285 protected void dispatchSetPressed(boolean pressed) {
4286 }
4287
4288 /**
4289 * Indicates whether the view is currently in pressed state. Unless
4290 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4291 * the pressed state.
4292 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004293 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004294 * @see #isClickable()
4295 * @see #setClickable(boolean)
4296 *
4297 * @return true if the view is currently pressed, false otherwise
4298 */
4299 public boolean isPressed() {
4300 return (mPrivateFlags & PRESSED) == PRESSED;
4301 }
4302
4303 /**
4304 * Indicates whether this view will save its state (that is,
4305 * whether its {@link #onSaveInstanceState} method will be called).
4306 *
4307 * @return Returns true if the view state saving is enabled, else false.
4308 *
4309 * @see #setSaveEnabled(boolean)
4310 * @attr ref android.R.styleable#View_saveEnabled
4311 */
4312 public boolean isSaveEnabled() {
4313 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4314 }
4315
4316 /**
4317 * Controls whether the saving of this view's state is
4318 * enabled (that is, whether its {@link #onSaveInstanceState} method
4319 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004320 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 * for its state to be saved. This flag can only disable the
4322 * saving of this view; any child views may still have their state saved.
4323 *
4324 * @param enabled Set to false to <em>disable</em> state saving, or true
4325 * (the default) to allow it.
4326 *
4327 * @see #isSaveEnabled()
4328 * @see #setId(int)
4329 * @see #onSaveInstanceState()
4330 * @attr ref android.R.styleable#View_saveEnabled
4331 */
4332 public void setSaveEnabled(boolean enabled) {
4333 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4334 }
4335
Jeff Brown85a31762010-09-01 17:01:00 -07004336 /**
4337 * Gets whether the framework should discard touches when the view's
4338 * window is obscured by another visible window.
4339 * Refer to the {@link View} security documentation for more details.
4340 *
4341 * @return True if touch filtering is enabled.
4342 *
4343 * @see #setFilterTouchesWhenObscured(boolean)
4344 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4345 */
4346 @ViewDebug.ExportedProperty
4347 public boolean getFilterTouchesWhenObscured() {
4348 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4349 }
4350
4351 /**
4352 * Sets whether the framework should discard touches when the view's
4353 * window is obscured by another visible window.
4354 * Refer to the {@link View} security documentation for more details.
4355 *
4356 * @param enabled True if touch filtering should be enabled.
4357 *
4358 * @see #getFilterTouchesWhenObscured
4359 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4360 */
4361 public void setFilterTouchesWhenObscured(boolean enabled) {
4362 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4363 FILTER_TOUCHES_WHEN_OBSCURED);
4364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004365
4366 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004367 * Indicates whether the entire hierarchy under this view will save its
4368 * state when a state saving traversal occurs from its parent. The default
4369 * is true; if false, these views will not be saved unless
4370 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4371 *
4372 * @return Returns true if the view state saving from parent is enabled, else false.
4373 *
4374 * @see #setSaveFromParentEnabled(boolean)
4375 */
4376 public boolean isSaveFromParentEnabled() {
4377 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4378 }
4379
4380 /**
4381 * Controls whether the entire hierarchy under this view will save its
4382 * state when a state saving traversal occurs from its parent. The default
4383 * is true; if false, these views will not be saved unless
4384 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4385 *
4386 * @param enabled Set to false to <em>disable</em> state saving, or true
4387 * (the default) to allow it.
4388 *
4389 * @see #isSaveFromParentEnabled()
4390 * @see #setId(int)
4391 * @see #onSaveInstanceState()
4392 */
4393 public void setSaveFromParentEnabled(boolean enabled) {
4394 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4395 }
4396
4397
4398 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399 * Returns whether this View is able to take focus.
4400 *
4401 * @return True if this view can take focus, or false otherwise.
4402 * @attr ref android.R.styleable#View_focusable
4403 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004404 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004405 public final boolean isFocusable() {
4406 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4407 }
4408
4409 /**
4410 * When a view is focusable, it may not want to take focus when in touch mode.
4411 * For example, a button would like focus when the user is navigating via a D-pad
4412 * so that the user can click on it, but once the user starts touching the screen,
4413 * the button shouldn't take focus
4414 * @return Whether the view is focusable in touch mode.
4415 * @attr ref android.R.styleable#View_focusableInTouchMode
4416 */
4417 @ViewDebug.ExportedProperty
4418 public final boolean isFocusableInTouchMode() {
4419 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4420 }
4421
4422 /**
4423 * Find the nearest view in the specified direction that can take focus.
4424 * This does not actually give focus to that view.
4425 *
4426 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4427 *
4428 * @return The nearest focusable in the specified direction, or null if none
4429 * can be found.
4430 */
4431 public View focusSearch(int direction) {
4432 if (mParent != null) {
4433 return mParent.focusSearch(this, direction);
4434 } else {
4435 return null;
4436 }
4437 }
4438
4439 /**
4440 * This method is the last chance for the focused view and its ancestors to
4441 * respond to an arrow key. This is called when the focused view did not
4442 * consume the key internally, nor could the view system find a new view in
4443 * the requested direction to give focus to.
4444 *
4445 * @param focused The currently focused view.
4446 * @param direction The direction focus wants to move. One of FOCUS_UP,
4447 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4448 * @return True if the this view consumed this unhandled move.
4449 */
4450 public boolean dispatchUnhandledMove(View focused, int direction) {
4451 return false;
4452 }
4453
4454 /**
4455 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004456 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004457 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004458 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4459 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004460 * @return The user specified next view, or null if there is none.
4461 */
4462 View findUserSetNextFocus(View root, int direction) {
4463 switch (direction) {
4464 case FOCUS_LEFT:
4465 if (mNextFocusLeftId == View.NO_ID) return null;
4466 return findViewShouldExist(root, mNextFocusLeftId);
4467 case FOCUS_RIGHT:
4468 if (mNextFocusRightId == View.NO_ID) return null;
4469 return findViewShouldExist(root, mNextFocusRightId);
4470 case FOCUS_UP:
4471 if (mNextFocusUpId == View.NO_ID) return null;
4472 return findViewShouldExist(root, mNextFocusUpId);
4473 case FOCUS_DOWN:
4474 if (mNextFocusDownId == View.NO_ID) return null;
4475 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004476 case FOCUS_FORWARD:
4477 if (mNextFocusForwardId == View.NO_ID) return null;
4478 return findViewShouldExist(root, mNextFocusForwardId);
4479 case FOCUS_BACKWARD: {
4480 final int id = mID;
4481 return root.findViewByPredicate(new Predicate<View>() {
4482 @Override
4483 public boolean apply(View t) {
4484 return t.mNextFocusForwardId == id;
4485 }
4486 });
4487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004488 }
4489 return null;
4490 }
4491
4492 private static View findViewShouldExist(View root, int childViewId) {
4493 View result = root.findViewById(childViewId);
4494 if (result == null) {
4495 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4496 + "by user for id " + childViewId);
4497 }
4498 return result;
4499 }
4500
4501 /**
4502 * Find and return all focusable views that are descendants of this view,
4503 * possibly including this view if it is focusable itself.
4504 *
4505 * @param direction The direction of the focus
4506 * @return A list of focusable views
4507 */
4508 public ArrayList<View> getFocusables(int direction) {
4509 ArrayList<View> result = new ArrayList<View>(24);
4510 addFocusables(result, direction);
4511 return result;
4512 }
4513
4514 /**
4515 * Add any focusable views that are descendants of this view (possibly
4516 * including this view if it is focusable itself) to views. If we are in touch mode,
4517 * only add views that are also focusable in touch mode.
4518 *
4519 * @param views Focusable views found so far
4520 * @param direction The direction of the focus
4521 */
4522 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004523 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4524 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004525
svetoslavganov75986cf2009-05-14 22:28:01 -07004526 /**
4527 * Adds any focusable views that are descendants of this view (possibly
4528 * including this view if it is focusable itself) to views. This method
4529 * adds all focusable views regardless if we are in touch mode or
4530 * only views focusable in touch mode if we are in touch mode depending on
4531 * the focusable mode paramater.
4532 *
4533 * @param views Focusable views found so far or null if all we are interested is
4534 * the number of focusables.
4535 * @param direction The direction of the focus.
4536 * @param focusableMode The type of focusables to be added.
4537 *
4538 * @see #FOCUSABLES_ALL
4539 * @see #FOCUSABLES_TOUCH_MODE
4540 */
4541 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4542 if (!isFocusable()) {
4543 return;
4544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004545
svetoslavganov75986cf2009-05-14 22:28:01 -07004546 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4547 isInTouchMode() && !isFocusableInTouchMode()) {
4548 return;
4549 }
4550
4551 if (views != null) {
4552 views.add(this);
4553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004554 }
4555
4556 /**
4557 * Find and return all touchable views that are descendants of this view,
4558 * possibly including this view if it is touchable itself.
4559 *
4560 * @return A list of touchable views
4561 */
4562 public ArrayList<View> getTouchables() {
4563 ArrayList<View> result = new ArrayList<View>();
4564 addTouchables(result);
4565 return result;
4566 }
4567
4568 /**
4569 * Add any touchable views that are descendants of this view (possibly
4570 * including this view if it is touchable itself) to views.
4571 *
4572 * @param views Touchable views found so far
4573 */
4574 public void addTouchables(ArrayList<View> views) {
4575 final int viewFlags = mViewFlags;
4576
4577 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4578 && (viewFlags & ENABLED_MASK) == ENABLED) {
4579 views.add(this);
4580 }
4581 }
4582
4583 /**
4584 * Call this to try to give focus to a specific view or to one of its
4585 * descendants.
4586 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004587 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4588 * false), or if it is focusable and it is not focusable in touch mode
4589 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004590 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004591 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004592 * have focus, and you want your parent to look for the next one.
4593 *
4594 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4595 * {@link #FOCUS_DOWN} and <code>null</code>.
4596 *
4597 * @return Whether this view or one of its descendants actually took focus.
4598 */
4599 public final boolean requestFocus() {
4600 return requestFocus(View.FOCUS_DOWN);
4601 }
4602
4603
4604 /**
4605 * Call this to try to give focus to a specific view or to one of its
4606 * descendants and give it a hint about what direction focus is heading.
4607 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004608 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4609 * false), or if it is focusable and it is not focusable in touch mode
4610 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004611 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004612 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004613 * have focus, and you want your parent to look for the next one.
4614 *
4615 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4616 * <code>null</code> set for the previously focused rectangle.
4617 *
4618 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4619 * @return Whether this view or one of its descendants actually took focus.
4620 */
4621 public final boolean requestFocus(int direction) {
4622 return requestFocus(direction, null);
4623 }
4624
4625 /**
4626 * Call this to try to give focus to a specific view or to one of its descendants
4627 * and give it hints about the direction and a specific rectangle that the focus
4628 * is coming from. The rectangle can help give larger views a finer grained hint
4629 * about where focus is coming from, and therefore, where to show selection, or
4630 * forward focus change internally.
4631 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004632 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4633 * false), or if it is focusable and it is not focusable in touch mode
4634 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004635 *
4636 * A View will not take focus if it is not visible.
4637 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004638 * A View will not take focus if one of its parents has
4639 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
4640 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004641 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004642 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004643 * have focus, and you want your parent to look for the next one.
4644 *
4645 * You may wish to override this method if your custom {@link View} has an internal
4646 * {@link View} that it wishes to forward the request to.
4647 *
4648 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4649 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
4650 * to give a finer grained hint about where focus is coming from. May be null
4651 * if there is no hint.
4652 * @return Whether this view or one of its descendants actually took focus.
4653 */
4654 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
4655 // need to be focusable
4656 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
4657 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4658 return false;
4659 }
4660
4661 // need to be focusable in touch mode if in touch mode
4662 if (isInTouchMode() &&
4663 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
4664 return false;
4665 }
4666
4667 // need to not have any parents blocking us
4668 if (hasAncestorThatBlocksDescendantFocus()) {
4669 return false;
4670 }
4671
4672 handleFocusGainInternal(direction, previouslyFocusedRect);
4673 return true;
4674 }
4675
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004676 /** Gets the ViewAncestor, or null if not attached. */
4677 /*package*/ ViewAncestor getViewAncestor() {
Christopher Tate2c095f32010-10-04 14:13:40 -07004678 View root = getRootView();
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004679 return root != null ? (ViewAncestor)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07004680 }
4681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004682 /**
4683 * Call this to try to give focus to a specific view or to one of its descendants. This is a
4684 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
4685 * touch mode to request focus when they are touched.
4686 *
4687 * @return Whether this view or one of its descendants actually took focus.
4688 *
4689 * @see #isInTouchMode()
4690 *
4691 */
4692 public final boolean requestFocusFromTouch() {
4693 // Leave touch mode if we need to
4694 if (isInTouchMode()) {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004695 ViewAncestor viewRoot = getViewAncestor();
Christopher Tate2c095f32010-10-04 14:13:40 -07004696 if (viewRoot != null) {
4697 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004698 }
4699 }
4700 return requestFocus(View.FOCUS_DOWN);
4701 }
4702
4703 /**
4704 * @return Whether any ancestor of this view blocks descendant focus.
4705 */
4706 private boolean hasAncestorThatBlocksDescendantFocus() {
4707 ViewParent ancestor = mParent;
4708 while (ancestor instanceof ViewGroup) {
4709 final ViewGroup vgAncestor = (ViewGroup) ancestor;
4710 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
4711 return true;
4712 } else {
4713 ancestor = vgAncestor.getParent();
4714 }
4715 }
4716 return false;
4717 }
4718
4719 /**
Romain Guya440b002010-02-24 15:57:54 -08004720 * @hide
4721 */
4722 public void dispatchStartTemporaryDetach() {
4723 onStartTemporaryDetach();
4724 }
4725
4726 /**
4727 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004728 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
4729 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08004730 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004731 */
4732 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08004733 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08004734 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08004735 }
4736
4737 /**
4738 * @hide
4739 */
4740 public void dispatchFinishTemporaryDetach() {
4741 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004742 }
Romain Guy8506ab42009-06-11 17:35:47 -07004743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004744 /**
4745 * Called after {@link #onStartTemporaryDetach} when the container is done
4746 * changing the view.
4747 */
4748 public void onFinishTemporaryDetach() {
4749 }
Romain Guy8506ab42009-06-11 17:35:47 -07004750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004751 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004752 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
4753 * for this view's window. Returns null if the view is not currently attached
4754 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07004755 * just use the standard high-level event callbacks like
4756 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004757 */
4758 public KeyEvent.DispatcherState getKeyDispatcherState() {
4759 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
4760 }
Joe Malin32736f02011-01-19 16:14:20 -08004761
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004762 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004763 * Dispatch a key event before it is processed by any input method
4764 * associated with the view hierarchy. This can be used to intercept
4765 * key events in special situations before the IME consumes them; a
4766 * typical example would be handling the BACK key to update the application's
4767 * UI instead of allowing the IME to see it and close itself.
4768 *
4769 * @param event The key event to be dispatched.
4770 * @return True if the event was handled, false otherwise.
4771 */
4772 public boolean dispatchKeyEventPreIme(KeyEvent event) {
4773 return onKeyPreIme(event.getKeyCode(), event);
4774 }
4775
4776 /**
4777 * Dispatch a key event to the next view on the focus path. This path runs
4778 * from the top of the view tree down to the currently focused view. If this
4779 * view has focus, it will dispatch to itself. Otherwise it will dispatch
4780 * the next node down the focus path. This method also fires any key
4781 * listeners.
4782 *
4783 * @param event The key event to be dispatched.
4784 * @return True if the event was handled, false otherwise.
4785 */
4786 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004787 if (mInputEventConsistencyVerifier != null) {
4788 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
4789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004790
Jeff Brown21bc5c92011-02-28 18:27:14 -08004791 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07004792 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004793 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4794 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
4795 return true;
4796 }
4797
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004798 if (event.dispatch(this, mAttachInfo != null
4799 ? mAttachInfo.mKeyDispatchState : null, this)) {
4800 return true;
4801 }
4802
4803 if (mInputEventConsistencyVerifier != null) {
4804 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4805 }
4806 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004807 }
4808
4809 /**
4810 * Dispatches a key shortcut event.
4811 *
4812 * @param event The key event to be dispatched.
4813 * @return True if the event was handled by the view, false otherwise.
4814 */
4815 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
4816 return onKeyShortcut(event.getKeyCode(), event);
4817 }
4818
4819 /**
4820 * Pass the touch screen motion event down to the target view, or this
4821 * view if it is the target.
4822 *
4823 * @param event The motion event to be dispatched.
4824 * @return True if the event was handled by the view, false otherwise.
4825 */
4826 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004827 if (mInputEventConsistencyVerifier != null) {
4828 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
4829 }
4830
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004831 if (onFilterTouchEventForSecurity(event)) {
4832 //noinspection SimplifiableIfStatement
4833 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
4834 mOnTouchListener.onTouch(this, event)) {
4835 return true;
4836 }
4837
4838 if (onTouchEvent(event)) {
4839 return true;
4840 }
Jeff Brown85a31762010-09-01 17:01:00 -07004841 }
4842
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004843 if (mInputEventConsistencyVerifier != null) {
4844 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004845 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004846 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004847 }
4848
4849 /**
Jeff Brown85a31762010-09-01 17:01:00 -07004850 * Filter the touch event to apply security policies.
4851 *
4852 * @param event The motion event to be filtered.
4853 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08004854 *
Jeff Brown85a31762010-09-01 17:01:00 -07004855 * @see #getFilterTouchesWhenObscured
4856 */
4857 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07004858 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07004859 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
4860 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
4861 // Window is obscured, drop this touch.
4862 return false;
4863 }
4864 return true;
4865 }
4866
4867 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004868 * Pass a trackball motion event down to the focused view.
4869 *
4870 * @param event The motion event to be dispatched.
4871 * @return True if the event was handled by the view, false otherwise.
4872 */
4873 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004874 if (mInputEventConsistencyVerifier != null) {
4875 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
4876 }
4877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004878 //Log.i("view", "view=" + this + ", " + event.toString());
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004879 if (onTrackballEvent(event)) {
4880 return true;
4881 }
4882
4883 if (mInputEventConsistencyVerifier != null) {
4884 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4885 }
4886 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004887 }
4888
4889 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004890 * Dispatch a generic motion event.
4891 * <p>
4892 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
4893 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08004894 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07004895 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08004896 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08004897 *
4898 * @param event The motion event to be dispatched.
4899 * @return True if the event was handled by the view, false otherwise.
4900 */
4901 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004902 if (mInputEventConsistencyVerifier != null) {
4903 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
4904 }
4905
Jeff Browna032cc02011-03-07 16:56:21 -08004906 final int source = event.getSource();
4907 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
4908 final int action = event.getAction();
4909 if (action == MotionEvent.ACTION_HOVER_ENTER
4910 || action == MotionEvent.ACTION_HOVER_MOVE
4911 || action == MotionEvent.ACTION_HOVER_EXIT) {
4912 if (dispatchHoverEvent(event)) {
4913 return true;
4914 }
4915 } else if (dispatchGenericPointerEvent(event)) {
4916 return true;
4917 }
4918 } else if (dispatchGenericFocusedEvent(event)) {
4919 return true;
4920 }
4921
Romain Guy7b5b6ab2011-03-14 18:05:08 -07004922 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08004923 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4924 && mOnGenericMotionListener.onGenericMotion(this, event)) {
4925 return true;
4926 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004927
4928 if (onGenericMotionEvent(event)) {
4929 return true;
4930 }
4931
4932 if (mInputEventConsistencyVerifier != null) {
4933 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4934 }
4935 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08004936 }
4937
4938 /**
Jeff Browna032cc02011-03-07 16:56:21 -08004939 * Dispatch a hover event.
4940 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07004941 * Do not call this method directly.
4942 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08004943 * </p>
4944 *
4945 * @param event The motion event to be dispatched.
4946 * @return True if the event was handled by the view, false otherwise.
4947 * @hide
4948 */
4949 protected boolean dispatchHoverEvent(MotionEvent event) {
4950 return onHoverEvent(event);
4951 }
4952
4953 /**
4954 * Dispatch a generic motion event to the view under the first pointer.
4955 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07004956 * Do not call this method directly.
4957 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08004958 * </p>
4959 *
4960 * @param event The motion event to be dispatched.
4961 * @return True if the event was handled by the view, false otherwise.
4962 * @hide
4963 */
4964 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
4965 return false;
4966 }
4967
4968 /**
4969 * Dispatch a generic motion event to the currently focused view.
4970 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07004971 * Do not call this method directly.
4972 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08004973 * </p>
4974 *
4975 * @param event The motion event to be dispatched.
4976 * @return True if the event was handled by the view, false otherwise.
4977 * @hide
4978 */
4979 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
4980 return false;
4981 }
4982
4983 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004984 * Dispatch a pointer event.
4985 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07004986 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
4987 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
4988 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08004989 * and should not be expected to handle other pointing device features.
4990 * </p>
4991 *
4992 * @param event The motion event to be dispatched.
4993 * @return True if the event was handled by the view, false otherwise.
4994 * @hide
4995 */
4996 public final boolean dispatchPointerEvent(MotionEvent event) {
4997 if (event.isTouchEvent()) {
4998 return dispatchTouchEvent(event);
4999 } else {
5000 return dispatchGenericMotionEvent(event);
5001 }
5002 }
5003
5004 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005005 * Called when the window containing this view gains or loses window focus.
5006 * ViewGroups should override to route to their children.
5007 *
5008 * @param hasFocus True if the window containing this view now has focus,
5009 * false otherwise.
5010 */
5011 public void dispatchWindowFocusChanged(boolean hasFocus) {
5012 onWindowFocusChanged(hasFocus);
5013 }
5014
5015 /**
5016 * Called when the window containing this view gains or loses focus. Note
5017 * that this is separate from view focus: to receive key events, both
5018 * your view and its window must have focus. If a window is displayed
5019 * on top of yours that takes input focus, then your own window will lose
5020 * focus but the view focus will remain unchanged.
5021 *
5022 * @param hasWindowFocus True if the window containing this view now has
5023 * focus, false otherwise.
5024 */
5025 public void onWindowFocusChanged(boolean hasWindowFocus) {
5026 InputMethodManager imm = InputMethodManager.peekInstance();
5027 if (!hasWindowFocus) {
5028 if (isPressed()) {
5029 setPressed(false);
5030 }
5031 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5032 imm.focusOut(this);
5033 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005034 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005035 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005036 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005037 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5038 imm.focusIn(this);
5039 }
5040 refreshDrawableState();
5041 }
5042
5043 /**
5044 * Returns true if this view is in a window that currently has window focus.
5045 * Note that this is not the same as the view itself having focus.
5046 *
5047 * @return True if this view is in a window that currently has window focus.
5048 */
5049 public boolean hasWindowFocus() {
5050 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5051 }
5052
5053 /**
Adam Powell326d8082009-12-09 15:10:07 -08005054 * Dispatch a view visibility change down the view hierarchy.
5055 * ViewGroups should override to route to their children.
5056 * @param changedView The view whose visibility changed. Could be 'this' or
5057 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005058 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5059 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005060 */
5061 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5062 onVisibilityChanged(changedView, visibility);
5063 }
5064
5065 /**
5066 * Called when the visibility of the view or an ancestor of the view is changed.
5067 * @param changedView The view whose visibility changed. Could be 'this' or
5068 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005069 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5070 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005071 */
5072 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005073 if (visibility == VISIBLE) {
5074 if (mAttachInfo != null) {
5075 initialAwakenScrollBars();
5076 } else {
5077 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5078 }
5079 }
Adam Powell326d8082009-12-09 15:10:07 -08005080 }
5081
5082 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005083 * Dispatch a hint about whether this view is displayed. For instance, when
5084 * a View moves out of the screen, it might receives a display hint indicating
5085 * the view is not displayed. Applications should not <em>rely</em> on this hint
5086 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005087 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005088 * @param hint A hint about whether or not this view is displayed:
5089 * {@link #VISIBLE} or {@link #INVISIBLE}.
5090 */
5091 public void dispatchDisplayHint(int hint) {
5092 onDisplayHint(hint);
5093 }
5094
5095 /**
5096 * Gives this view a hint about whether is displayed or not. For instance, when
5097 * a View moves out of the screen, it might receives a display hint indicating
5098 * the view is not displayed. Applications should not <em>rely</em> on this hint
5099 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005100 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005101 * @param hint A hint about whether or not this view is displayed:
5102 * {@link #VISIBLE} or {@link #INVISIBLE}.
5103 */
5104 protected void onDisplayHint(int hint) {
5105 }
5106
5107 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005108 * Dispatch a window visibility change down the view hierarchy.
5109 * ViewGroups should override to route to their children.
5110 *
5111 * @param visibility The new visibility of the window.
5112 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005113 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005114 */
5115 public void dispatchWindowVisibilityChanged(int visibility) {
5116 onWindowVisibilityChanged(visibility);
5117 }
5118
5119 /**
5120 * Called when the window containing has change its visibility
5121 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5122 * that this tells you whether or not your window is being made visible
5123 * to the window manager; this does <em>not</em> tell you whether or not
5124 * your window is obscured by other windows on the screen, even if it
5125 * is itself visible.
5126 *
5127 * @param visibility The new visibility of the window.
5128 */
5129 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005130 if (visibility == VISIBLE) {
5131 initialAwakenScrollBars();
5132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005133 }
5134
5135 /**
5136 * Returns the current visibility of the window this view is attached to
5137 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5138 *
5139 * @return Returns the current visibility of the view's window.
5140 */
5141 public int getWindowVisibility() {
5142 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5143 }
5144
5145 /**
5146 * Retrieve the overall visible display size in which the window this view is
5147 * attached to has been positioned in. This takes into account screen
5148 * decorations above the window, for both cases where the window itself
5149 * is being position inside of them or the window is being placed under
5150 * then and covered insets are used for the window to position its content
5151 * inside. In effect, this tells you the available area where content can
5152 * be placed and remain visible to users.
5153 *
5154 * <p>This function requires an IPC back to the window manager to retrieve
5155 * the requested information, so should not be used in performance critical
5156 * code like drawing.
5157 *
5158 * @param outRect Filled in with the visible display frame. If the view
5159 * is not attached to a window, this is simply the raw display size.
5160 */
5161 public void getWindowVisibleDisplayFrame(Rect outRect) {
5162 if (mAttachInfo != null) {
5163 try {
5164 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5165 } catch (RemoteException e) {
5166 return;
5167 }
5168 // XXX This is really broken, and probably all needs to be done
5169 // in the window manager, and we need to know more about whether
5170 // we want the area behind or in front of the IME.
5171 final Rect insets = mAttachInfo.mVisibleInsets;
5172 outRect.left += insets.left;
5173 outRect.top += insets.top;
5174 outRect.right -= insets.right;
5175 outRect.bottom -= insets.bottom;
5176 return;
5177 }
5178 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005179 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005180 }
5181
5182 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005183 * Dispatch a notification about a resource configuration change down
5184 * the view hierarchy.
5185 * ViewGroups should override to route to their children.
5186 *
5187 * @param newConfig The new resource configuration.
5188 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005189 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005190 */
5191 public void dispatchConfigurationChanged(Configuration newConfig) {
5192 onConfigurationChanged(newConfig);
5193 }
5194
5195 /**
5196 * Called when the current configuration of the resources being used
5197 * by the application have changed. You can use this to decide when
5198 * to reload resources that can changed based on orientation and other
5199 * configuration characterstics. You only need to use this if you are
5200 * not relying on the normal {@link android.app.Activity} mechanism of
5201 * recreating the activity instance upon a configuration change.
5202 *
5203 * @param newConfig The new resource configuration.
5204 */
5205 protected void onConfigurationChanged(Configuration newConfig) {
5206 }
5207
5208 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005209 * Private function to aggregate all per-view attributes in to the view
5210 * root.
5211 */
5212 void dispatchCollectViewAttributes(int visibility) {
5213 performCollectViewAttributes(visibility);
5214 }
5215
5216 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005217 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005218 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5219 mAttachInfo.mKeepScreenOn = true;
5220 }
5221 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5222 if (mOnSystemUiVisibilityChangeListener != null) {
5223 mAttachInfo.mHasSystemUiListeners = true;
5224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225 }
5226 }
5227
5228 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005229 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005230 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005231 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5232 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005233 ai.mRecomputeGlobalAttributes = true;
5234 }
5235 }
5236 }
5237
5238 /**
5239 * Returns whether the device is currently in touch mode. Touch mode is entered
5240 * once the user begins interacting with the device by touch, and affects various
5241 * things like whether focus is always visible to the user.
5242 *
5243 * @return Whether the device is in touch mode.
5244 */
5245 @ViewDebug.ExportedProperty
5246 public boolean isInTouchMode() {
5247 if (mAttachInfo != null) {
5248 return mAttachInfo.mInTouchMode;
5249 } else {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005250 return ViewAncestor.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005251 }
5252 }
5253
5254 /**
5255 * Returns the context the view is running in, through which it can
5256 * access the current theme, resources, etc.
5257 *
5258 * @return The view's Context.
5259 */
5260 @ViewDebug.CapturedViewProperty
5261 public final Context getContext() {
5262 return mContext;
5263 }
5264
5265 /**
5266 * Handle a key event before it is processed by any input method
5267 * associated with the view hierarchy. This can be used to intercept
5268 * key events in special situations before the IME consumes them; a
5269 * typical example would be handling the BACK key to update the application's
5270 * UI instead of allowing the IME to see it and close itself.
5271 *
5272 * @param keyCode The value in event.getKeyCode().
5273 * @param event Description of the key event.
5274 * @return If you handled the event, return true. If you want to allow the
5275 * event to be handled by the next receiver, return false.
5276 */
5277 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5278 return false;
5279 }
5280
5281 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005282 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5283 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005284 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5285 * is released, if the view is enabled and clickable.
5286 *
5287 * @param keyCode A key code that represents the button pressed, from
5288 * {@link android.view.KeyEvent}.
5289 * @param event The KeyEvent object that defines the button action.
5290 */
5291 public boolean onKeyDown(int keyCode, KeyEvent event) {
5292 boolean result = false;
5293
5294 switch (keyCode) {
5295 case KeyEvent.KEYCODE_DPAD_CENTER:
5296 case KeyEvent.KEYCODE_ENTER: {
5297 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5298 return true;
5299 }
5300 // Long clickable items don't necessarily have to be clickable
5301 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5302 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5303 (event.getRepeatCount() == 0)) {
5304 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005305 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005306 return true;
5307 }
5308 break;
5309 }
5310 }
5311 return result;
5312 }
5313
5314 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005315 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5316 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5317 * the event).
5318 */
5319 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5320 return false;
5321 }
5322
5323 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005324 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5325 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005326 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5327 * {@link KeyEvent#KEYCODE_ENTER} is released.
5328 *
5329 * @param keyCode A key code that represents the button pressed, from
5330 * {@link android.view.KeyEvent}.
5331 * @param event The KeyEvent object that defines the button action.
5332 */
5333 public boolean onKeyUp(int keyCode, KeyEvent event) {
5334 boolean result = false;
5335
5336 switch (keyCode) {
5337 case KeyEvent.KEYCODE_DPAD_CENTER:
5338 case KeyEvent.KEYCODE_ENTER: {
5339 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5340 return true;
5341 }
5342 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5343 setPressed(false);
5344
5345 if (!mHasPerformedLongPress) {
5346 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005347 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005348
5349 result = performClick();
5350 }
5351 }
5352 break;
5353 }
5354 }
5355 return result;
5356 }
5357
5358 /**
5359 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5360 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5361 * the event).
5362 *
5363 * @param keyCode A key code that represents the button pressed, from
5364 * {@link android.view.KeyEvent}.
5365 * @param repeatCount The number of times the action was made.
5366 * @param event The KeyEvent object that defines the button action.
5367 */
5368 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5369 return false;
5370 }
5371
5372 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005373 * Called on the focused view when a key shortcut event is not handled.
5374 * Override this method to implement local key shortcuts for the View.
5375 * Key shortcuts can also be implemented by setting the
5376 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005377 *
5378 * @param keyCode The value in event.getKeyCode().
5379 * @param event Description of the key event.
5380 * @return If you handled the event, return true. If you want to allow the
5381 * event to be handled by the next receiver, return false.
5382 */
5383 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5384 return false;
5385 }
5386
5387 /**
5388 * Check whether the called view is a text editor, in which case it
5389 * would make sense to automatically display a soft input window for
5390 * it. Subclasses should override this if they implement
5391 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005392 * a call on that method would return a non-null InputConnection, and
5393 * they are really a first-class editor that the user would normally
5394 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005395 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005396 * <p>The default implementation always returns false. This does
5397 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5398 * will not be called or the user can not otherwise perform edits on your
5399 * view; it is just a hint to the system that this is not the primary
5400 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005401 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005402 * @return Returns true if this view is a text editor, else false.
5403 */
5404 public boolean onCheckIsTextEditor() {
5405 return false;
5406 }
Romain Guy8506ab42009-06-11 17:35:47 -07005407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005408 /**
5409 * Create a new InputConnection for an InputMethod to interact
5410 * with the view. The default implementation returns null, since it doesn't
5411 * support input methods. You can override this to implement such support.
5412 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005413 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005414 * <p>When implementing this, you probably also want to implement
5415 * {@link #onCheckIsTextEditor()} to indicate you will return a
5416 * non-null InputConnection.
5417 *
5418 * @param outAttrs Fill in with attribute information about the connection.
5419 */
5420 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5421 return null;
5422 }
5423
5424 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005425 * Called by the {@link android.view.inputmethod.InputMethodManager}
5426 * when a view who is not the current
5427 * input connection target is trying to make a call on the manager. The
5428 * default implementation returns false; you can override this to return
5429 * true for certain views if you are performing InputConnection proxying
5430 * to them.
5431 * @param view The View that is making the InputMethodManager call.
5432 * @return Return true to allow the call, false to reject.
5433 */
5434 public boolean checkInputConnectionProxy(View view) {
5435 return false;
5436 }
Romain Guy8506ab42009-06-11 17:35:47 -07005437
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005438 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005439 * Show the context menu for this view. It is not safe to hold on to the
5440 * menu after returning from this method.
5441 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005442 * You should normally not overload this method. Overload
5443 * {@link #onCreateContextMenu(ContextMenu)} or define an
5444 * {@link OnCreateContextMenuListener} to add items to the context menu.
5445 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005446 * @param menu The context menu to populate
5447 */
5448 public void createContextMenu(ContextMenu menu) {
5449 ContextMenuInfo menuInfo = getContextMenuInfo();
5450
5451 // Sets the current menu info so all items added to menu will have
5452 // my extra info set.
5453 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5454
5455 onCreateContextMenu(menu);
5456 if (mOnCreateContextMenuListener != null) {
5457 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5458 }
5459
5460 // Clear the extra information so subsequent items that aren't mine don't
5461 // have my extra info.
5462 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5463
5464 if (mParent != null) {
5465 mParent.createContextMenu(menu);
5466 }
5467 }
5468
5469 /**
5470 * Views should implement this if they have extra information to associate
5471 * with the context menu. The return result is supplied as a parameter to
5472 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5473 * callback.
5474 *
5475 * @return Extra information about the item for which the context menu
5476 * should be shown. This information will vary across different
5477 * subclasses of View.
5478 */
5479 protected ContextMenuInfo getContextMenuInfo() {
5480 return null;
5481 }
5482
5483 /**
5484 * Views should implement this if the view itself is going to add items to
5485 * the context menu.
5486 *
5487 * @param menu the context menu to populate
5488 */
5489 protected void onCreateContextMenu(ContextMenu menu) {
5490 }
5491
5492 /**
5493 * Implement this method to handle trackball motion events. The
5494 * <em>relative</em> movement of the trackball since the last event
5495 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5496 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5497 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5498 * they will often be fractional values, representing the more fine-grained
5499 * movement information available from a trackball).
5500 *
5501 * @param event The motion event.
5502 * @return True if the event was handled, false otherwise.
5503 */
5504 public boolean onTrackballEvent(MotionEvent event) {
5505 return false;
5506 }
5507
5508 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005509 * Implement this method to handle generic motion events.
5510 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005511 * Generic motion events describe joystick movements, mouse hovers, track pad
5512 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005513 * {@link MotionEvent#getSource() source} of the motion event specifies
5514 * the class of input that was received. Implementations of this method
5515 * must examine the bits in the source before processing the event.
5516 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005517 * </p><p>
5518 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5519 * are delivered to the view under the pointer. All other generic motion events are
5520 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005521 * </p>
5522 * <code>
5523 * public boolean onGenericMotionEvent(MotionEvent event) {
5524 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005525 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5526 * // process the joystick movement...
5527 * return true;
5528 * }
5529 * }
5530 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5531 * switch (event.getAction()) {
5532 * case MotionEvent.ACTION_HOVER_MOVE:
5533 * // process the mouse hover movement...
5534 * return true;
5535 * case MotionEvent.ACTION_SCROLL:
5536 * // process the scroll wheel movement...
5537 * return true;
5538 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005539 * }
5540 * return super.onGenericMotionEvent(event);
5541 * }
5542 * </code>
5543 *
5544 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08005545 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08005546 */
5547 public boolean onGenericMotionEvent(MotionEvent event) {
5548 return false;
5549 }
5550
5551 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005552 * Implement this method to handle hover events.
5553 * <p>
5554 * Hover events are pointer events with action {@link MotionEvent#ACTION_HOVER_ENTER},
5555 * {@link MotionEvent#ACTION_HOVER_MOVE}, or {@link MotionEvent#ACTION_HOVER_EXIT}.
5556 * </p><p>
5557 * The view receives hover enter as the pointer enters the bounds of the view and hover
5558 * exit as the pointer exits the bound of the view or just before the pointer goes down
Romain Guy5c22a8c2011-05-13 11:48:45 -07005559 * (which implies that {@link #onTouchEvent(MotionEvent)} will be called soon).
Jeff Browna032cc02011-03-07 16:56:21 -08005560 * </p><p>
5561 * If the view would like to handle the hover event itself and prevent its children
5562 * from receiving hover, it should return true from this method. If this method returns
5563 * true and a child has already received a hover enter event, the child will
5564 * automatically receive a hover exit event.
5565 * </p><p>
5566 * The default implementation sets the hovered state of the view if the view is
5567 * clickable.
5568 * </p>
5569 *
5570 * @param event The motion event that describes the hover.
5571 * @return True if this view handled the hover event and does not want its children
5572 * to receive the hover event.
5573 */
5574 public boolean onHoverEvent(MotionEvent event) {
Jeff Browna032cc02011-03-07 16:56:21 -08005575 switch (event.getAction()) {
5576 case MotionEvent.ACTION_HOVER_ENTER:
5577 setHovered(true);
5578 break;
5579
5580 case MotionEvent.ACTION_HOVER_EXIT:
5581 setHovered(false);
5582 break;
5583 }
5584
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005585 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08005586 }
5587
5588 /**
5589 * Returns true if the view is currently hovered.
5590 *
5591 * @return True if the view is currently hovered.
5592 */
5593 public boolean isHovered() {
5594 return (mPrivateFlags & HOVERED) != 0;
5595 }
5596
5597 /**
5598 * Sets whether the view is currently hovered.
5599 *
5600 * @param hovered True if the view is hovered.
5601 */
5602 public void setHovered(boolean hovered) {
5603 if (hovered) {
5604 if ((mPrivateFlags & HOVERED) == 0) {
5605 mPrivateFlags |= HOVERED;
5606 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005607 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Jeff Browna032cc02011-03-07 16:56:21 -08005608 }
5609 } else {
5610 if ((mPrivateFlags & HOVERED) != 0) {
5611 mPrivateFlags &= ~HOVERED;
5612 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005613 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Jeff Browna032cc02011-03-07 16:56:21 -08005614 }
5615 }
5616 }
5617
5618 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005619 * Implement this method to handle touch screen motion events.
5620 *
5621 * @param event The motion event.
5622 * @return True if the event was handled, false otherwise.
5623 */
5624 public boolean onTouchEvent(MotionEvent event) {
5625 final int viewFlags = mViewFlags;
5626
5627 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07005628 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
5629 mPrivateFlags &= ~PRESSED;
5630 refreshDrawableState();
5631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005632 // A disabled view that is clickable still consumes the touch
5633 // events, it just doesn't respond to them.
5634 return (((viewFlags & CLICKABLE) == CLICKABLE ||
5635 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
5636 }
5637
5638 if (mTouchDelegate != null) {
5639 if (mTouchDelegate.onTouchEvent(event)) {
5640 return true;
5641 }
5642 }
5643
5644 if (((viewFlags & CLICKABLE) == CLICKABLE ||
5645 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
5646 switch (event.getAction()) {
5647 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08005648 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
5649 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005650 // take focus if we don't have it already and we should in
5651 // touch mode.
5652 boolean focusTaken = false;
5653 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
5654 focusTaken = requestFocus();
5655 }
5656
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08005657 if (prepressed) {
5658 // The button is being released before we actually
5659 // showed it as pressed. Make it show the pressed
5660 // state now (before scheduling the click) to ensure
5661 // the user sees it.
5662 mPrivateFlags |= PRESSED;
5663 refreshDrawableState();
5664 }
Joe Malin32736f02011-01-19 16:14:20 -08005665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005666 if (!mHasPerformedLongPress) {
5667 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005668 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005669
5670 // Only perform take click actions if we were in the pressed state
5671 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08005672 // Use a Runnable and post this rather than calling
5673 // performClick directly. This lets other visual state
5674 // of the view update before click actions start.
5675 if (mPerformClick == null) {
5676 mPerformClick = new PerformClick();
5677 }
5678 if (!post(mPerformClick)) {
5679 performClick();
5680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005681 }
5682 }
5683
5684 if (mUnsetPressedState == null) {
5685 mUnsetPressedState = new UnsetPressedState();
5686 }
5687
Adam Powelle14579b2009-12-16 18:39:52 -08005688 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08005689 postDelayed(mUnsetPressedState,
5690 ViewConfiguration.getPressedStateDuration());
5691 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005692 // If the post failed, unpress right now
5693 mUnsetPressedState.run();
5694 }
Adam Powelle14579b2009-12-16 18:39:52 -08005695 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005696 }
5697 break;
5698
5699 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08005700 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005701
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005702 if (performButtonActionOnTouchDown(event)) {
5703 break;
5704 }
5705
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005706 // Walk up the hierarchy to determine if we're inside a scrolling container.
5707 boolean isInScrollingContainer = false;
5708 ViewParent p = getParent();
5709 while (p != null && p instanceof ViewGroup) {
5710 if (((ViewGroup) p).shouldDelayChildPressedState()) {
5711 isInScrollingContainer = true;
5712 break;
5713 }
5714 p = p.getParent();
5715 }
5716
5717 // For views inside a scrolling container, delay the pressed feedback for
5718 // a short period in case this is a scroll.
5719 if (isInScrollingContainer) {
5720 mPrivateFlags |= PREPRESSED;
5721 if (mPendingCheckForTap == null) {
5722 mPendingCheckForTap = new CheckForTap();
5723 }
5724 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
5725 } else {
5726 // Not inside a scrolling container, so show the feedback right away
5727 mPrivateFlags |= PRESSED;
5728 refreshDrawableState();
5729 checkForLongClick(0);
5730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005731 break;
5732
5733 case MotionEvent.ACTION_CANCEL:
5734 mPrivateFlags &= ~PRESSED;
5735 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08005736 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005737 break;
5738
5739 case MotionEvent.ACTION_MOVE:
5740 final int x = (int) event.getX();
5741 final int y = (int) event.getY();
5742
5743 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07005744 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005745 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08005746 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005747 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08005748 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05005749 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005750
5751 // Need to switch from pressed to not pressed
5752 mPrivateFlags &= ~PRESSED;
5753 refreshDrawableState();
5754 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005755 }
5756 break;
5757 }
5758 return true;
5759 }
5760
5761 return false;
5762 }
5763
5764 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05005765 * Remove the longpress detection timer.
5766 */
5767 private void removeLongPressCallback() {
5768 if (mPendingCheckForLongPress != null) {
5769 removeCallbacks(mPendingCheckForLongPress);
5770 }
5771 }
Adam Powell3cb8b632011-01-21 15:34:14 -08005772
5773 /**
5774 * Remove the pending click action
5775 */
5776 private void removePerformClickCallback() {
5777 if (mPerformClick != null) {
5778 removeCallbacks(mPerformClick);
5779 }
5780 }
5781
Adam Powelle14579b2009-12-16 18:39:52 -08005782 /**
Romain Guya440b002010-02-24 15:57:54 -08005783 * Remove the prepress detection timer.
5784 */
5785 private void removeUnsetPressCallback() {
5786 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
5787 setPressed(false);
5788 removeCallbacks(mUnsetPressedState);
5789 }
5790 }
5791
5792 /**
Adam Powelle14579b2009-12-16 18:39:52 -08005793 * Remove the tap detection timer.
5794 */
5795 private void removeTapCallback() {
5796 if (mPendingCheckForTap != null) {
5797 mPrivateFlags &= ~PREPRESSED;
5798 removeCallbacks(mPendingCheckForTap);
5799 }
5800 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005801
5802 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005803 * Cancels a pending long press. Your subclass can use this if you
5804 * want the context menu to come up if the user presses and holds
5805 * at the same place, but you don't want it to come up if they press
5806 * and then move around enough to cause scrolling.
5807 */
5808 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005809 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08005810
5811 /*
5812 * The prepressed state handled by the tap callback is a display
5813 * construct, but the tap callback will post a long press callback
5814 * less its own timeout. Remove it here.
5815 */
5816 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005817 }
5818
5819 /**
5820 * Sets the TouchDelegate for this View.
5821 */
5822 public void setTouchDelegate(TouchDelegate delegate) {
5823 mTouchDelegate = delegate;
5824 }
5825
5826 /**
5827 * Gets the TouchDelegate for this View.
5828 */
5829 public TouchDelegate getTouchDelegate() {
5830 return mTouchDelegate;
5831 }
5832
5833 /**
5834 * Set flags controlling behavior of this view.
5835 *
5836 * @param flags Constant indicating the value which should be set
5837 * @param mask Constant indicating the bit range that should be changed
5838 */
5839 void setFlags(int flags, int mask) {
5840 int old = mViewFlags;
5841 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
5842
5843 int changed = mViewFlags ^ old;
5844 if (changed == 0) {
5845 return;
5846 }
5847 int privateFlags = mPrivateFlags;
5848
5849 /* Check if the FOCUSABLE bit has changed */
5850 if (((changed & FOCUSABLE_MASK) != 0) &&
5851 ((privateFlags & HAS_BOUNDS) !=0)) {
5852 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
5853 && ((privateFlags & FOCUSED) != 0)) {
5854 /* Give up focus if we are no longer focusable */
5855 clearFocus();
5856 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
5857 && ((privateFlags & FOCUSED) == 0)) {
5858 /*
5859 * Tell the view system that we are now available to take focus
5860 * if no one else already has it.
5861 */
5862 if (mParent != null) mParent.focusableViewAvailable(this);
5863 }
5864 }
5865
5866 if ((flags & VISIBILITY_MASK) == VISIBLE) {
5867 if ((changed & VISIBILITY_MASK) != 0) {
5868 /*
5869 * If this view is becoming visible, set the DRAWN flag so that
5870 * the next invalidate() will not be skipped.
5871 */
5872 mPrivateFlags |= DRAWN;
5873
5874 needGlobalAttributesUpdate(true);
5875
5876 // a view becoming visible is worth notifying the parent
5877 // about in case nothing has focus. even if this specific view
5878 // isn't focusable, it may contain something that is, so let
5879 // the root view try to give this focus if nothing else does.
5880 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
5881 mParent.focusableViewAvailable(this);
5882 }
5883 }
5884 }
5885
5886 /* Check if the GONE bit has changed */
5887 if ((changed & GONE) != 0) {
5888 needGlobalAttributesUpdate(false);
5889 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005890 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005891
Romain Guyecd80ee2009-12-03 17:13:02 -08005892 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
5893 if (hasFocus()) clearFocus();
5894 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005895 }
5896 if (mAttachInfo != null) {
5897 mAttachInfo.mViewVisibilityChanged = true;
5898 }
5899 }
5900
5901 /* Check if the VISIBLE bit has changed */
5902 if ((changed & INVISIBLE) != 0) {
5903 needGlobalAttributesUpdate(false);
Romain Guy0fd89bf2011-01-26 15:41:30 -08005904 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005905
5906 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
5907 // root view becoming invisible shouldn't clear focus
5908 if (getRootView() != this) {
5909 clearFocus();
5910 }
5911 }
5912 if (mAttachInfo != null) {
5913 mAttachInfo.mViewVisibilityChanged = true;
5914 }
5915 }
5916
Adam Powell326d8082009-12-09 15:10:07 -08005917 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07005918 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005919 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
5920 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07005921 }
Adam Powell326d8082009-12-09 15:10:07 -08005922 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
5923 }
5924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005925 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
5926 destroyDrawingCache();
5927 }
5928
5929 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
5930 destroyDrawingCache();
5931 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08005932 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005933 }
5934
5935 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
5936 destroyDrawingCache();
5937 mPrivateFlags &= ~DRAWING_CACHE_VALID;
5938 }
5939
5940 if ((changed & DRAW_MASK) != 0) {
5941 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
5942 if (mBGDrawable != null) {
5943 mPrivateFlags &= ~SKIP_DRAW;
5944 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
5945 } else {
5946 mPrivateFlags |= SKIP_DRAW;
5947 }
5948 } else {
5949 mPrivateFlags &= ~SKIP_DRAW;
5950 }
5951 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005952 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005953 }
5954
5955 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08005956 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005957 mParent.recomputeViewAttributes(this);
5958 }
5959 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005960
5961 if ((changed & HORIZONTAL_DIRECTION_MASK) != 0) {
5962 requestLayout();
5963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005964 }
5965
5966 /**
5967 * Change the view's z order in the tree, so it's on top of other sibling
5968 * views
5969 */
5970 public void bringToFront() {
5971 if (mParent != null) {
5972 mParent.bringChildToFront(this);
5973 }
5974 }
5975
5976 /**
5977 * This is called in response to an internal scroll in this view (i.e., the
5978 * view scrolled its own contents). This is typically as a result of
5979 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
5980 * called.
5981 *
5982 * @param l Current horizontal scroll origin.
5983 * @param t Current vertical scroll origin.
5984 * @param oldl Previous horizontal scroll origin.
5985 * @param oldt Previous vertical scroll origin.
5986 */
5987 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
5988 mBackgroundSizeChanged = true;
5989
5990 final AttachInfo ai = mAttachInfo;
5991 if (ai != null) {
5992 ai.mViewScrollChanged = true;
5993 }
5994 }
5995
5996 /**
Chet Haase21cd1382010-09-01 17:42:29 -07005997 * Interface definition for a callback to be invoked when the layout bounds of a view
5998 * changes due to layout processing.
5999 */
6000 public interface OnLayoutChangeListener {
6001 /**
6002 * Called when the focus state of a view has changed.
6003 *
6004 * @param v The view whose state has changed.
6005 * @param left The new value of the view's left property.
6006 * @param top The new value of the view's top property.
6007 * @param right The new value of the view's right property.
6008 * @param bottom The new value of the view's bottom property.
6009 * @param oldLeft The previous value of the view's left property.
6010 * @param oldTop The previous value of the view's top property.
6011 * @param oldRight The previous value of the view's right property.
6012 * @param oldBottom The previous value of the view's bottom property.
6013 */
6014 void onLayoutChange(View v, int left, int top, int right, int bottom,
6015 int oldLeft, int oldTop, int oldRight, int oldBottom);
6016 }
6017
6018 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006019 * This is called during layout when the size of this view has changed. If
6020 * you were just added to the view hierarchy, you're called with the old
6021 * values of 0.
6022 *
6023 * @param w Current width of this view.
6024 * @param h Current height of this view.
6025 * @param oldw Old width of this view.
6026 * @param oldh Old height of this view.
6027 */
6028 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6029 }
6030
6031 /**
6032 * Called by draw to draw the child views. This may be overridden
6033 * by derived classes to gain control just before its children are drawn
6034 * (but after its own view has been drawn).
6035 * @param canvas the canvas on which to draw the view
6036 */
6037 protected void dispatchDraw(Canvas canvas) {
6038 }
6039
6040 /**
6041 * Gets the parent of this view. Note that the parent is a
6042 * ViewParent and not necessarily a View.
6043 *
6044 * @return Parent of this view.
6045 */
6046 public final ViewParent getParent() {
6047 return mParent;
6048 }
6049
6050 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006051 * Set the horizontal scrolled position of your view. This will cause a call to
6052 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6053 * invalidated.
6054 * @param value the x position to scroll to
6055 */
6056 public void setScrollX(int value) {
6057 scrollTo(value, mScrollY);
6058 }
6059
6060 /**
6061 * Set the vertical scrolled position of your view. This will cause a call to
6062 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6063 * invalidated.
6064 * @param value the y position to scroll to
6065 */
6066 public void setScrollY(int value) {
6067 scrollTo(mScrollX, value);
6068 }
6069
6070 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006071 * Return the scrolled left position of this view. This is the left edge of
6072 * the displayed part of your view. You do not need to draw any pixels
6073 * farther left, since those are outside of the frame of your view on
6074 * screen.
6075 *
6076 * @return The left edge of the displayed part of your view, in pixels.
6077 */
6078 public final int getScrollX() {
6079 return mScrollX;
6080 }
6081
6082 /**
6083 * Return the scrolled top position of this view. This is the top edge of
6084 * the displayed part of your view. You do not need to draw any pixels above
6085 * it, since those are outside of the frame of your view on screen.
6086 *
6087 * @return The top edge of the displayed part of your view, in pixels.
6088 */
6089 public final int getScrollY() {
6090 return mScrollY;
6091 }
6092
6093 /**
6094 * Return the width of the your view.
6095 *
6096 * @return The width of your view, in pixels.
6097 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006098 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006099 public final int getWidth() {
6100 return mRight - mLeft;
6101 }
6102
6103 /**
6104 * Return the height of your view.
6105 *
6106 * @return The height of your view, in pixels.
6107 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006108 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006109 public final int getHeight() {
6110 return mBottom - mTop;
6111 }
6112
6113 /**
6114 * Return the visible drawing bounds of your view. Fills in the output
6115 * rectangle with the values from getScrollX(), getScrollY(),
6116 * getWidth(), and getHeight().
6117 *
6118 * @param outRect The (scrolled) drawing bounds of the view.
6119 */
6120 public void getDrawingRect(Rect outRect) {
6121 outRect.left = mScrollX;
6122 outRect.top = mScrollY;
6123 outRect.right = mScrollX + (mRight - mLeft);
6124 outRect.bottom = mScrollY + (mBottom - mTop);
6125 }
6126
6127 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006128 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6129 * raw width component (that is the result is masked by
6130 * {@link #MEASURED_SIZE_MASK}).
6131 *
6132 * @return The raw measured width of this view.
6133 */
6134 public final int getMeasuredWidth() {
6135 return mMeasuredWidth & MEASURED_SIZE_MASK;
6136 }
6137
6138 /**
6139 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006140 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006141 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006142 * This should be used during measurement and layout calculations only. Use
6143 * {@link #getWidth()} to see how wide a view is after layout.
6144 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006145 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006146 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006147 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006148 return mMeasuredWidth;
6149 }
6150
6151 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006152 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6153 * raw width component (that is the result is masked by
6154 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006155 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006156 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006157 */
6158 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006159 return mMeasuredHeight & MEASURED_SIZE_MASK;
6160 }
6161
6162 /**
6163 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006164 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006165 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6166 * This should be used during measurement and layout calculations only. Use
6167 * {@link #getHeight()} to see how wide a view is after layout.
6168 *
6169 * @return The measured width of this view as a bit mask.
6170 */
6171 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006172 return mMeasuredHeight;
6173 }
6174
6175 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006176 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6177 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6178 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6179 * and the height component is at the shifted bits
6180 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6181 */
6182 public final int getMeasuredState() {
6183 return (mMeasuredWidth&MEASURED_STATE_MASK)
6184 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6185 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6186 }
6187
6188 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006189 * The transform matrix of this view, which is calculated based on the current
6190 * roation, scale, and pivot properties.
6191 *
6192 * @see #getRotation()
6193 * @see #getScaleX()
6194 * @see #getScaleY()
6195 * @see #getPivotX()
6196 * @see #getPivotY()
6197 * @return The current transform matrix for the view
6198 */
6199 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07006200 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07006201 return mMatrix;
6202 }
6203
6204 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006205 * Utility function to determine if the value is far enough away from zero to be
6206 * considered non-zero.
6207 * @param value A floating point value to check for zero-ness
6208 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6209 */
6210 private static boolean nonzero(float value) {
6211 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6212 }
6213
6214 /**
Jeff Brown86671742010-09-30 20:00:15 -07006215 * Returns true if the transform matrix is the identity matrix.
6216 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006217 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006218 * @return True if the transform matrix is the identity matrix, false otherwise.
6219 */
Jeff Brown86671742010-09-30 20:00:15 -07006220 final boolean hasIdentityMatrix() {
6221 updateMatrix();
6222 return mMatrixIsIdentity;
6223 }
6224
6225 /**
6226 * Recomputes the transform matrix if necessary.
6227 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006228 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07006229 if (mMatrixDirty) {
6230 // transform-related properties have changed since the last time someone
6231 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006232
6233 // Figure out if we need to update the pivot point
6234 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006235 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006236 mPrevWidth = mRight - mLeft;
6237 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08006238 mPivotX = mPrevWidth / 2f;
6239 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07006240 }
6241 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006242 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07006243 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
6244 mMatrix.setTranslate(mTranslationX, mTranslationY);
6245 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
6246 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
6247 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07006248 if (mCamera == null) {
6249 mCamera = new Camera();
6250 matrix3D = new Matrix();
6251 }
6252 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07006253 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08006254 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07006255 mCamera.getMatrix(matrix3D);
6256 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006257 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07006258 mMatrix.postConcat(matrix3D);
6259 mCamera.restore();
6260 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006261 mMatrixDirty = false;
6262 mMatrixIsIdentity = mMatrix.isIdentity();
6263 mInverseMatrixDirty = true;
6264 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006265 }
6266
6267 /**
6268 * Utility method to retrieve the inverse of the current mMatrix property.
6269 * We cache the matrix to avoid recalculating it when transform properties
6270 * have not changed.
6271 *
6272 * @return The inverse of the current matrix of this view.
6273 */
Jeff Brown86671742010-09-30 20:00:15 -07006274 final Matrix getInverseMatrix() {
6275 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07006276 if (mInverseMatrixDirty) {
6277 if (mInverseMatrix == null) {
6278 mInverseMatrix = new Matrix();
6279 }
6280 mMatrix.invert(mInverseMatrix);
6281 mInverseMatrixDirty = false;
6282 }
6283 return mInverseMatrix;
6284 }
6285
6286 /**
Romain Guya5364ee2011-02-24 14:46:04 -08006287 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
6288 * views are drawn) from the camera to this view. The camera's distance
6289 * affects 3D transformations, for instance rotations around the X and Y
6290 * axis. If the rotationX or rotationY properties are changed and this view is
6291 * large (more than half the size of the screen), it is recommended to always
6292 * use a camera distance that's greater than the height (X axis rotation) or
6293 * the width (Y axis rotation) of this view.</p>
6294 *
6295 * <p>The distance of the camera from the view plane can have an affect on the
6296 * perspective distortion of the view when it is rotated around the x or y axis.
6297 * For example, a large distance will result in a large viewing angle, and there
6298 * will not be much perspective distortion of the view as it rotates. A short
6299 * distance may cause much more perspective distortion upon rotation, and can
6300 * also result in some drawing artifacts if the rotated view ends up partially
6301 * behind the camera (which is why the recommendation is to use a distance at
6302 * least as far as the size of the view, if the view is to be rotated.)</p>
6303 *
6304 * <p>The distance is expressed in "depth pixels." The default distance depends
6305 * on the screen density. For instance, on a medium density display, the
6306 * default distance is 1280. On a high density display, the default distance
6307 * is 1920.</p>
6308 *
6309 * <p>If you want to specify a distance that leads to visually consistent
6310 * results across various densities, use the following formula:</p>
6311 * <pre>
6312 * float scale = context.getResources().getDisplayMetrics().density;
6313 * view.setCameraDistance(distance * scale);
6314 * </pre>
6315 *
6316 * <p>The density scale factor of a high density display is 1.5,
6317 * and 1920 = 1280 * 1.5.</p>
6318 *
6319 * @param distance The distance in "depth pixels", if negative the opposite
6320 * value is used
6321 *
6322 * @see #setRotationX(float)
6323 * @see #setRotationY(float)
6324 */
6325 public void setCameraDistance(float distance) {
6326 invalidateParentCaches();
6327 invalidate(false);
6328
6329 final float dpi = mResources.getDisplayMetrics().densityDpi;
6330 if (mCamera == null) {
6331 mCamera = new Camera();
6332 matrix3D = new Matrix();
6333 }
6334
6335 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6336 mMatrixDirty = true;
6337
6338 invalidate(false);
6339 }
6340
6341 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006342 * The degrees that the view is rotated around the pivot point.
6343 *
Romain Guya5364ee2011-02-24 14:46:04 -08006344 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006345 * @see #getPivotX()
6346 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006347 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006348 * @return The degrees of rotation.
6349 */
6350 public float getRotation() {
6351 return mRotation;
6352 }
6353
6354 /**
Chet Haase897247b2010-09-09 14:54:47 -07006355 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6356 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006357 *
6358 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006359 *
6360 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006361 * @see #getPivotX()
6362 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006363 * @see #setRotationX(float)
6364 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006365 *
6366 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006367 */
6368 public void setRotation(float rotation) {
6369 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006370 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006371 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006372 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006373 mRotation = rotation;
6374 mMatrixDirty = true;
6375 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006376 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006377 }
6378 }
6379
6380 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006381 * The degrees that the view is rotated around the vertical axis through the pivot point.
6382 *
6383 * @see #getPivotX()
6384 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006385 * @see #setRotationY(float)
6386 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006387 * @return The degrees of Y rotation.
6388 */
6389 public float getRotationY() {
6390 return mRotationY;
6391 }
6392
6393 /**
Chet Haase897247b2010-09-09 14:54:47 -07006394 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6395 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6396 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006397 *
6398 * When rotating large views, it is recommended to adjust the camera distance
6399 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006400 *
6401 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006402 *
6403 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006404 * @see #getPivotX()
6405 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006406 * @see #setRotation(float)
6407 * @see #setRotationX(float)
6408 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006409 *
6410 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006411 */
6412 public void setRotationY(float rotationY) {
6413 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006414 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006415 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006416 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006417 mRotationY = rotationY;
6418 mMatrixDirty = true;
6419 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006420 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006421 }
6422 }
6423
6424 /**
6425 * The degrees that the view is rotated around the horizontal axis through the pivot point.
6426 *
6427 * @see #getPivotX()
6428 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006429 * @see #setRotationX(float)
6430 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006431 * @return The degrees of X rotation.
6432 */
6433 public float getRotationX() {
6434 return mRotationX;
6435 }
6436
6437 /**
Chet Haase897247b2010-09-09 14:54:47 -07006438 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6439 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6440 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006441 *
6442 * When rotating large views, it is recommended to adjust the camera distance
6443 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006444 *
6445 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006446 *
6447 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006448 * @see #getPivotX()
6449 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006450 * @see #setRotation(float)
6451 * @see #setRotationY(float)
6452 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006453 *
6454 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006455 */
6456 public void setRotationX(float rotationX) {
6457 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006458 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006459 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006460 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006461 mRotationX = rotationX;
6462 mMatrixDirty = true;
6463 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006464 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006465 }
6466 }
6467
6468 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006469 * The amount that the view is scaled in x around the pivot point, as a proportion of
6470 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6471 *
Joe Onorato93162322010-09-16 15:42:01 -04006472 * <p>By default, this is 1.0f.
6473 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006474 * @see #getPivotX()
6475 * @see #getPivotY()
6476 * @return The scaling factor.
6477 */
6478 public float getScaleX() {
6479 return mScaleX;
6480 }
6481
6482 /**
6483 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6484 * the view's unscaled width. A value of 1 means that no scaling is applied.
6485 *
6486 * @param scaleX The scaling factor.
6487 * @see #getPivotX()
6488 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006489 *
6490 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006491 */
6492 public void setScaleX(float scaleX) {
6493 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006494 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006495 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006496 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006497 mScaleX = scaleX;
6498 mMatrixDirty = true;
6499 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006500 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006501 }
6502 }
6503
6504 /**
6505 * The amount that the view is scaled in y around the pivot point, as a proportion of
6506 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6507 *
Joe Onorato93162322010-09-16 15:42:01 -04006508 * <p>By default, this is 1.0f.
6509 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006510 * @see #getPivotX()
6511 * @see #getPivotY()
6512 * @return The scaling factor.
6513 */
6514 public float getScaleY() {
6515 return mScaleY;
6516 }
6517
6518 /**
6519 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
6520 * the view's unscaled width. A value of 1 means that no scaling is applied.
6521 *
6522 * @param scaleY The scaling factor.
6523 * @see #getPivotX()
6524 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006525 *
6526 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07006527 */
6528 public void setScaleY(float scaleY) {
6529 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006530 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006531 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006532 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006533 mScaleY = scaleY;
6534 mMatrixDirty = true;
6535 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006536 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006537 }
6538 }
6539
6540 /**
6541 * The x location of the point around which the view is {@link #setRotation(float) rotated}
6542 * and {@link #setScaleX(float) scaled}.
6543 *
6544 * @see #getRotation()
6545 * @see #getScaleX()
6546 * @see #getScaleY()
6547 * @see #getPivotY()
6548 * @return The x location of the pivot point.
6549 */
6550 public float getPivotX() {
6551 return mPivotX;
6552 }
6553
6554 /**
6555 * Sets the x location of the point around which the view is
6556 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07006557 * By default, the pivot point is centered on the object.
6558 * Setting this property disables this behavior and causes the view to use only the
6559 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006560 *
6561 * @param pivotX The x location of the pivot point.
6562 * @see #getRotation()
6563 * @see #getScaleX()
6564 * @see #getScaleY()
6565 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006566 *
6567 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07006568 */
6569 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006570 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006571 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006572 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006573 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006574 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006575 mPivotX = pivotX;
6576 mMatrixDirty = true;
6577 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006578 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006579 }
6580 }
6581
6582 /**
6583 * The y location of the point around which the view is {@link #setRotation(float) rotated}
6584 * and {@link #setScaleY(float) scaled}.
6585 *
6586 * @see #getRotation()
6587 * @see #getScaleX()
6588 * @see #getScaleY()
6589 * @see #getPivotY()
6590 * @return The y location of the pivot point.
6591 */
6592 public float getPivotY() {
6593 return mPivotY;
6594 }
6595
6596 /**
6597 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07006598 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
6599 * Setting this property disables this behavior and causes the view to use only the
6600 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006601 *
6602 * @param pivotY The y location of the pivot point.
6603 * @see #getRotation()
6604 * @see #getScaleX()
6605 * @see #getScaleY()
6606 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006607 *
6608 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07006609 */
6610 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006611 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006612 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006613 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006614 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006615 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006616 mPivotY = pivotY;
6617 mMatrixDirty = true;
6618 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006619 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006620 }
6621 }
6622
6623 /**
6624 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
6625 * completely transparent and 1 means the view is completely opaque.
6626 *
Joe Onorato93162322010-09-16 15:42:01 -04006627 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07006628 * @return The opacity of the view.
6629 */
6630 public float getAlpha() {
6631 return mAlpha;
6632 }
6633
6634 /**
Romain Guy171c5922011-01-06 10:04:23 -08006635 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
6636 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08006637 *
Romain Guy171c5922011-01-06 10:04:23 -08006638 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
6639 * responsible for applying the opacity itself. Otherwise, calling this method is
6640 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08006641 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07006642 *
6643 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08006644 *
Joe Malin32736f02011-01-19 16:14:20 -08006645 * @see #setLayerType(int, android.graphics.Paint)
6646 *
Chet Haase73066682010-11-29 15:55:32 -08006647 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07006648 */
6649 public void setAlpha(float alpha) {
6650 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006651 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07006652 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07006653 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006654 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006655 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07006656 } else {
Romain Guya3496a92010-10-12 11:53:24 -07006657 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006658 invalidate(false);
6659 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006660 }
6661
6662 /**
Chet Haasea00f3862011-02-22 06:34:40 -08006663 * Faster version of setAlpha() which performs the same steps except there are
6664 * no calls to invalidate(). The caller of this function should perform proper invalidation
6665 * on the parent and this object. The return value indicates whether the subclass handles
6666 * alpha (the return value for onSetAlpha()).
6667 *
6668 * @param alpha The new value for the alpha property
6669 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
6670 */
6671 boolean setAlphaNoInvalidation(float alpha) {
6672 mAlpha = alpha;
6673 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
6674 if (subclassHandlesAlpha) {
6675 mPrivateFlags |= ALPHA_SET;
6676 } else {
6677 mPrivateFlags &= ~ALPHA_SET;
6678 }
6679 return subclassHandlesAlpha;
6680 }
6681
6682 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006683 * Top position of this view relative to its parent.
6684 *
6685 * @return The top of this view, in pixels.
6686 */
6687 @ViewDebug.CapturedViewProperty
6688 public final int getTop() {
6689 return mTop;
6690 }
6691
6692 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006693 * Sets the top position of this view relative to its parent. This method is meant to be called
6694 * by the layout system and should not generally be called otherwise, because the property
6695 * may be changed at any time by the layout.
6696 *
6697 * @param top The top of this view, in pixels.
6698 */
6699 public final void setTop(int top) {
6700 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07006701 updateMatrix();
6702 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006703 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006704 int minTop;
6705 int yLoc;
6706 if (top < mTop) {
6707 minTop = top;
6708 yLoc = top - mTop;
6709 } else {
6710 minTop = mTop;
6711 yLoc = 0;
6712 }
Chet Haasee9140a72011-02-16 16:23:29 -08006713 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006714 }
6715 } else {
6716 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006717 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006718 }
6719
Chet Haaseed032702010-10-01 14:05:54 -07006720 int width = mRight - mLeft;
6721 int oldHeight = mBottom - mTop;
6722
Chet Haase21cd1382010-09-01 17:42:29 -07006723 mTop = top;
6724
Chet Haaseed032702010-10-01 14:05:54 -07006725 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6726
Chet Haase21cd1382010-09-01 17:42:29 -07006727 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006728 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6729 // A change in dimension means an auto-centered pivot point changes, too
6730 mMatrixDirty = true;
6731 }
Chet Haase21cd1382010-09-01 17:42:29 -07006732 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006733 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006734 }
Chet Haase55dbb652010-12-21 20:15:08 -08006735 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006736 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006737 }
6738 }
6739
6740 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006741 * Bottom position of this view relative to its parent.
6742 *
6743 * @return The bottom of this view, in pixels.
6744 */
6745 @ViewDebug.CapturedViewProperty
6746 public final int getBottom() {
6747 return mBottom;
6748 }
6749
6750 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08006751 * True if this view has changed since the last time being drawn.
6752 *
6753 * @return The dirty state of this view.
6754 */
6755 public boolean isDirty() {
6756 return (mPrivateFlags & DIRTY_MASK) != 0;
6757 }
6758
6759 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006760 * Sets the bottom position of this view relative to its parent. This method is meant to be
6761 * called by the layout system and should not generally be called otherwise, because the
6762 * property may be changed at any time by the layout.
6763 *
6764 * @param bottom The bottom of this view, in pixels.
6765 */
6766 public final void setBottom(int bottom) {
6767 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07006768 updateMatrix();
6769 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006770 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006771 int maxBottom;
6772 if (bottom < mBottom) {
6773 maxBottom = mBottom;
6774 } else {
6775 maxBottom = bottom;
6776 }
Chet Haasee9140a72011-02-16 16:23:29 -08006777 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006778 }
6779 } else {
6780 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006781 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006782 }
6783
Chet Haaseed032702010-10-01 14:05:54 -07006784 int width = mRight - mLeft;
6785 int oldHeight = mBottom - mTop;
6786
Chet Haase21cd1382010-09-01 17:42:29 -07006787 mBottom = bottom;
6788
Chet Haaseed032702010-10-01 14:05:54 -07006789 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6790
Chet Haase21cd1382010-09-01 17:42:29 -07006791 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006792 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6793 // A change in dimension means an auto-centered pivot point changes, too
6794 mMatrixDirty = true;
6795 }
Chet Haase21cd1382010-09-01 17:42:29 -07006796 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006797 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006798 }
Chet Haase55dbb652010-12-21 20:15:08 -08006799 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006800 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006801 }
6802 }
6803
6804 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006805 * Left position of this view relative to its parent.
6806 *
6807 * @return The left edge of this view, in pixels.
6808 */
6809 @ViewDebug.CapturedViewProperty
6810 public final int getLeft() {
6811 return mLeft;
6812 }
6813
6814 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006815 * Sets the left position of this view relative to its parent. This method is meant to be called
6816 * by the layout system and should not generally be called otherwise, because the property
6817 * may be changed at any time by the layout.
6818 *
6819 * @param left The bottom of this view, in pixels.
6820 */
6821 public final void setLeft(int left) {
6822 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07006823 updateMatrix();
6824 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006825 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006826 int minLeft;
6827 int xLoc;
6828 if (left < mLeft) {
6829 minLeft = left;
6830 xLoc = left - mLeft;
6831 } else {
6832 minLeft = mLeft;
6833 xLoc = 0;
6834 }
Chet Haasee9140a72011-02-16 16:23:29 -08006835 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006836 }
6837 } else {
6838 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006839 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006840 }
6841
Chet Haaseed032702010-10-01 14:05:54 -07006842 int oldWidth = mRight - mLeft;
6843 int height = mBottom - mTop;
6844
Chet Haase21cd1382010-09-01 17:42:29 -07006845 mLeft = left;
6846
Chet Haaseed032702010-10-01 14:05:54 -07006847 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6848
Chet Haase21cd1382010-09-01 17:42:29 -07006849 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006850 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6851 // A change in dimension means an auto-centered pivot point changes, too
6852 mMatrixDirty = true;
6853 }
Chet Haase21cd1382010-09-01 17:42:29 -07006854 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006855 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006856 }
Chet Haase55dbb652010-12-21 20:15:08 -08006857 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006858 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006859 }
6860 }
6861
6862 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006863 * Right position of this view relative to its parent.
6864 *
6865 * @return The right edge of this view, in pixels.
6866 */
6867 @ViewDebug.CapturedViewProperty
6868 public final int getRight() {
6869 return mRight;
6870 }
6871
6872 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006873 * Sets the right position of this view relative to its parent. This method is meant to be called
6874 * by the layout system and should not generally be called otherwise, because the property
6875 * may be changed at any time by the layout.
6876 *
6877 * @param right The bottom of this view, in pixels.
6878 */
6879 public final void setRight(int right) {
6880 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07006881 updateMatrix();
6882 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006883 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006884 int maxRight;
6885 if (right < mRight) {
6886 maxRight = mRight;
6887 } else {
6888 maxRight = right;
6889 }
Chet Haasee9140a72011-02-16 16:23:29 -08006890 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006891 }
6892 } else {
6893 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006894 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006895 }
6896
Chet Haaseed032702010-10-01 14:05:54 -07006897 int oldWidth = mRight - mLeft;
6898 int height = mBottom - mTop;
6899
Chet Haase21cd1382010-09-01 17:42:29 -07006900 mRight = right;
6901
Chet Haaseed032702010-10-01 14:05:54 -07006902 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6903
Chet Haase21cd1382010-09-01 17:42:29 -07006904 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006905 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6906 // A change in dimension means an auto-centered pivot point changes, too
6907 mMatrixDirty = true;
6908 }
Chet Haase21cd1382010-09-01 17:42:29 -07006909 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006910 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006911 }
Chet Haase55dbb652010-12-21 20:15:08 -08006912 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006913 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006914 }
6915 }
6916
6917 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006918 * The visual x position of this view, in pixels. This is equivalent to the
6919 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08006920 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07006921 *
Chet Haasedf030d22010-07-30 17:22:38 -07006922 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006923 */
Chet Haasedf030d22010-07-30 17:22:38 -07006924 public float getX() {
6925 return mLeft + mTranslationX;
6926 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006927
Chet Haasedf030d22010-07-30 17:22:38 -07006928 /**
6929 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
6930 * {@link #setTranslationX(float) translationX} property to be the difference between
6931 * the x value passed in and the current {@link #getLeft() left} property.
6932 *
6933 * @param x The visual x position of this view, in pixels.
6934 */
6935 public void setX(float x) {
6936 setTranslationX(x - mLeft);
6937 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006938
Chet Haasedf030d22010-07-30 17:22:38 -07006939 /**
6940 * The visual y position of this view, in pixels. This is equivalent to the
6941 * {@link #setTranslationY(float) translationY} property plus the current
6942 * {@link #getTop() top} property.
6943 *
6944 * @return The visual y position of this view, in pixels.
6945 */
6946 public float getY() {
6947 return mTop + mTranslationY;
6948 }
6949
6950 /**
6951 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
6952 * {@link #setTranslationY(float) translationY} property to be the difference between
6953 * the y value passed in and the current {@link #getTop() top} property.
6954 *
6955 * @param y The visual y position of this view, in pixels.
6956 */
6957 public void setY(float y) {
6958 setTranslationY(y - mTop);
6959 }
6960
6961
6962 /**
6963 * The horizontal location of this view relative to its {@link #getLeft() left} position.
6964 * This position is post-layout, in addition to wherever the object's
6965 * layout placed it.
6966 *
6967 * @return The horizontal position of this view relative to its left position, in pixels.
6968 */
6969 public float getTranslationX() {
6970 return mTranslationX;
6971 }
6972
6973 /**
6974 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
6975 * This effectively positions the object post-layout, in addition to wherever the object's
6976 * layout placed it.
6977 *
6978 * @param translationX The horizontal position of this view relative to its left position,
6979 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006980 *
6981 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07006982 */
6983 public void setTranslationX(float translationX) {
6984 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006985 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006986 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006987 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006988 mTranslationX = translationX;
6989 mMatrixDirty = true;
6990 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006991 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006992 }
6993 }
6994
6995 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006996 * The horizontal location of this view relative to its {@link #getTop() top} position.
6997 * This position is post-layout, in addition to wherever the object's
6998 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006999 *
Chet Haasedf030d22010-07-30 17:22:38 -07007000 * @return The vertical position of this view relative to its top position,
7001 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007002 */
Chet Haasedf030d22010-07-30 17:22:38 -07007003 public float getTranslationY() {
7004 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07007005 }
7006
7007 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007008 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7009 * This effectively positions the object post-layout, in addition to wherever the object's
7010 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007011 *
Chet Haasedf030d22010-07-30 17:22:38 -07007012 * @param translationY The vertical position of this view relative to its top position,
7013 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007014 *
7015 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007016 */
Chet Haasedf030d22010-07-30 17:22:38 -07007017 public void setTranslationY(float translationY) {
7018 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007019 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007020 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007021 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007022 mTranslationY = translationY;
7023 mMatrixDirty = true;
7024 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007025 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007026 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007027 }
7028
7029 /**
Romain Guyda489792011-02-03 01:05:15 -08007030 * @hide
7031 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007032 public void setFastTranslationX(float x) {
7033 mTranslationX = x;
7034 mMatrixDirty = true;
7035 }
7036
7037 /**
7038 * @hide
7039 */
7040 public void setFastTranslationY(float y) {
7041 mTranslationY = y;
7042 mMatrixDirty = true;
7043 }
7044
7045 /**
7046 * @hide
7047 */
Romain Guyda489792011-02-03 01:05:15 -08007048 public void setFastX(float x) {
7049 mTranslationX = x - mLeft;
7050 mMatrixDirty = true;
7051 }
7052
7053 /**
7054 * @hide
7055 */
7056 public void setFastY(float y) {
7057 mTranslationY = y - mTop;
7058 mMatrixDirty = true;
7059 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007060
Romain Guyda489792011-02-03 01:05:15 -08007061 /**
7062 * @hide
7063 */
7064 public void setFastScaleX(float x) {
7065 mScaleX = x;
7066 mMatrixDirty = true;
7067 }
7068
7069 /**
7070 * @hide
7071 */
7072 public void setFastScaleY(float y) {
7073 mScaleY = y;
7074 mMatrixDirty = true;
7075 }
7076
7077 /**
7078 * @hide
7079 */
7080 public void setFastAlpha(float alpha) {
7081 mAlpha = alpha;
7082 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007083
Romain Guyda489792011-02-03 01:05:15 -08007084 /**
7085 * @hide
7086 */
7087 public void setFastRotationY(float y) {
7088 mRotationY = y;
7089 mMatrixDirty = true;
7090 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007091
Romain Guyda489792011-02-03 01:05:15 -08007092 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007093 * Hit rectangle in parent's coordinates
7094 *
7095 * @param outRect The hit rectangle of the view.
7096 */
7097 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007098 updateMatrix();
7099 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007100 outRect.set(mLeft, mTop, mRight, mBottom);
7101 } else {
7102 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07007103 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07007104 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007105 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7106 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007107 }
7108 }
7109
7110 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007111 * Determines whether the given point, in local coordinates is inside the view.
7112 */
7113 /*package*/ final boolean pointInView(float localX, float localY) {
7114 return localX >= 0 && localX < (mRight - mLeft)
7115 && localY >= 0 && localY < (mBottom - mTop);
7116 }
7117
7118 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007119 * Utility method to determine whether the given point, in local coordinates,
7120 * is inside the view, where the area of the view is expanded by the slop factor.
7121 * This method is called while processing touch-move events to determine if the event
7122 * is still within the view.
7123 */
7124 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007125 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007126 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007127 }
7128
7129 /**
7130 * When a view has focus and the user navigates away from it, the next view is searched for
7131 * starting from the rectangle filled in by this method.
7132 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007133 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7134 * of the view. However, if your view maintains some idea of internal selection,
7135 * such as a cursor, or a selected row or column, you should override this method and
7136 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007137 *
7138 * @param r The rectangle to fill in, in this view's coordinates.
7139 */
7140 public void getFocusedRect(Rect r) {
7141 getDrawingRect(r);
7142 }
7143
7144 /**
7145 * If some part of this view is not clipped by any of its parents, then
7146 * return that area in r in global (root) coordinates. To convert r to local
7147 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7148 * -globalOffset.y)) If the view is completely clipped or translated out,
7149 * return false.
7150 *
7151 * @param r If true is returned, r holds the global coordinates of the
7152 * visible portion of this view.
7153 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7154 * between this view and its root. globalOffet may be null.
7155 * @return true if r is non-empty (i.e. part of the view is visible at the
7156 * root level.
7157 */
7158 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7159 int width = mRight - mLeft;
7160 int height = mBottom - mTop;
7161 if (width > 0 && height > 0) {
7162 r.set(0, 0, width, height);
7163 if (globalOffset != null) {
7164 globalOffset.set(-mScrollX, -mScrollY);
7165 }
7166 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7167 }
7168 return false;
7169 }
7170
7171 public final boolean getGlobalVisibleRect(Rect r) {
7172 return getGlobalVisibleRect(r, null);
7173 }
7174
7175 public final boolean getLocalVisibleRect(Rect r) {
7176 Point offset = new Point();
7177 if (getGlobalVisibleRect(r, offset)) {
7178 r.offset(-offset.x, -offset.y); // make r local
7179 return true;
7180 }
7181 return false;
7182 }
7183
7184 /**
7185 * Offset this view's vertical location by the specified number of pixels.
7186 *
7187 * @param offset the number of pixels to offset the view by
7188 */
7189 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007190 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007191 updateMatrix();
7192 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007193 final ViewParent p = mParent;
7194 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007195 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007196 int minTop;
7197 int maxBottom;
7198 int yLoc;
7199 if (offset < 0) {
7200 minTop = mTop + offset;
7201 maxBottom = mBottom;
7202 yLoc = offset;
7203 } else {
7204 minTop = mTop;
7205 maxBottom = mBottom + offset;
7206 yLoc = 0;
7207 }
7208 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
7209 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007210 }
7211 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007212 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007213 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007214
Chet Haasec3aa3612010-06-17 08:50:37 -07007215 mTop += offset;
7216 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007217
Chet Haasec3aa3612010-06-17 08:50:37 -07007218 if (!mMatrixIsIdentity) {
7219 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007220 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007221 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007222 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007224 }
7225
7226 /**
7227 * Offset this view's horizontal location by the specified amount of pixels.
7228 *
7229 * @param offset the numer of pixels to offset the view by
7230 */
7231 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007232 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007233 updateMatrix();
7234 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007235 final ViewParent p = mParent;
7236 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007237 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007238 int minLeft;
7239 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007240 if (offset < 0) {
7241 minLeft = mLeft + offset;
7242 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007243 } else {
7244 minLeft = mLeft;
7245 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007246 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007247 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07007248 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007249 }
7250 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007251 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007252 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007253
Chet Haasec3aa3612010-06-17 08:50:37 -07007254 mLeft += offset;
7255 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007256
Chet Haasec3aa3612010-06-17 08:50:37 -07007257 if (!mMatrixIsIdentity) {
7258 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007259 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007260 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007261 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007263 }
7264
7265 /**
7266 * Get the LayoutParams associated with this view. All views should have
7267 * layout parameters. These supply parameters to the <i>parent</i> of this
7268 * view specifying how it should be arranged. There are many subclasses of
7269 * ViewGroup.LayoutParams, and these correspond to the different subclasses
7270 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08007271 *
7272 * This method may return null if this View is not attached to a parent
7273 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
7274 * was not invoked successfully. When a View is attached to a parent
7275 * ViewGroup, this method must not return null.
7276 *
7277 * @return The LayoutParams associated with this view, or null if no
7278 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007279 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07007280 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007281 public ViewGroup.LayoutParams getLayoutParams() {
7282 return mLayoutParams;
7283 }
7284
7285 /**
7286 * Set the layout parameters associated with this view. These supply
7287 * parameters to the <i>parent</i> of this view specifying how it should be
7288 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
7289 * correspond to the different subclasses of ViewGroup that are responsible
7290 * for arranging their children.
7291 *
Romain Guy01c174b2011-02-22 11:51:06 -08007292 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007293 */
7294 public void setLayoutParams(ViewGroup.LayoutParams params) {
7295 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007296 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007297 }
7298 mLayoutParams = params;
7299 requestLayout();
7300 }
7301
7302 /**
7303 * Set the scrolled position of your view. This will cause a call to
7304 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7305 * invalidated.
7306 * @param x the x position to scroll to
7307 * @param y the y position to scroll to
7308 */
7309 public void scrollTo(int x, int y) {
7310 if (mScrollX != x || mScrollY != y) {
7311 int oldX = mScrollX;
7312 int oldY = mScrollY;
7313 mScrollX = x;
7314 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007315 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007316 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007317 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007318 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007320 }
7321 }
7322
7323 /**
7324 * Move the scrolled position of your view. This will cause a call to
7325 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7326 * invalidated.
7327 * @param x the amount of pixels to scroll by horizontally
7328 * @param y the amount of pixels to scroll by vertically
7329 */
7330 public void scrollBy(int x, int y) {
7331 scrollTo(mScrollX + x, mScrollY + y);
7332 }
7333
7334 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007335 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7336 * animation to fade the scrollbars out after a default delay. If a subclass
7337 * provides animated scrolling, the start delay should equal the duration
7338 * of the scrolling animation.</p>
7339 *
7340 * <p>The animation starts only if at least one of the scrollbars is
7341 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7342 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7343 * this method returns true, and false otherwise. If the animation is
7344 * started, this method calls {@link #invalidate()}; in that case the
7345 * caller should not call {@link #invalidate()}.</p>
7346 *
7347 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007348 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007349 *
7350 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7351 * and {@link #scrollTo(int, int)}.</p>
7352 *
7353 * @return true if the animation is played, false otherwise
7354 *
7355 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007356 * @see #scrollBy(int, int)
7357 * @see #scrollTo(int, int)
7358 * @see #isHorizontalScrollBarEnabled()
7359 * @see #isVerticalScrollBarEnabled()
7360 * @see #setHorizontalScrollBarEnabled(boolean)
7361 * @see #setVerticalScrollBarEnabled(boolean)
7362 */
7363 protected boolean awakenScrollBars() {
7364 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007365 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007366 }
7367
7368 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007369 * Trigger the scrollbars to draw.
7370 * This method differs from awakenScrollBars() only in its default duration.
7371 * initialAwakenScrollBars() will show the scroll bars for longer than
7372 * usual to give the user more of a chance to notice them.
7373 *
7374 * @return true if the animation is played, false otherwise.
7375 */
7376 private boolean initialAwakenScrollBars() {
7377 return mScrollCache != null &&
7378 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7379 }
7380
7381 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007382 * <p>
7383 * Trigger the scrollbars to draw. When invoked this method starts an
7384 * animation to fade the scrollbars out after a fixed delay. If a subclass
7385 * provides animated scrolling, the start delay should equal the duration of
7386 * the scrolling animation.
7387 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007388 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007389 * <p>
7390 * The animation starts only if at least one of the scrollbars is enabled,
7391 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7392 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7393 * this method returns true, and false otherwise. If the animation is
7394 * started, this method calls {@link #invalidate()}; in that case the caller
7395 * should not call {@link #invalidate()}.
7396 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007397 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007398 * <p>
7399 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007400 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007401 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007402 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007403 * @param startDelay the delay, in milliseconds, after which the animation
7404 * should start; when the delay is 0, the animation starts
7405 * immediately
7406 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007407 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007408 * @see #scrollBy(int, int)
7409 * @see #scrollTo(int, int)
7410 * @see #isHorizontalScrollBarEnabled()
7411 * @see #isVerticalScrollBarEnabled()
7412 * @see #setHorizontalScrollBarEnabled(boolean)
7413 * @see #setVerticalScrollBarEnabled(boolean)
7414 */
7415 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007416 return awakenScrollBars(startDelay, true);
7417 }
Joe Malin32736f02011-01-19 16:14:20 -08007418
Mike Cleron290947b2009-09-29 18:34:32 -07007419 /**
7420 * <p>
7421 * Trigger the scrollbars to draw. When invoked this method starts an
7422 * animation to fade the scrollbars out after a fixed delay. If a subclass
7423 * provides animated scrolling, the start delay should equal the duration of
7424 * the scrolling animation.
7425 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007426 *
Mike Cleron290947b2009-09-29 18:34:32 -07007427 * <p>
7428 * The animation starts only if at least one of the scrollbars is enabled,
7429 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7430 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7431 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08007432 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07007433 * is set to true; in that case the caller
7434 * should not call {@link #invalidate()}.
7435 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007436 *
Mike Cleron290947b2009-09-29 18:34:32 -07007437 * <p>
7438 * This method should be invoked everytime a subclass directly updates the
7439 * scroll parameters.
7440 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007441 *
Mike Cleron290947b2009-09-29 18:34:32 -07007442 * @param startDelay the delay, in milliseconds, after which the animation
7443 * should start; when the delay is 0, the animation starts
7444 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007445 *
Mike Cleron290947b2009-09-29 18:34:32 -07007446 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007447 *
Mike Cleron290947b2009-09-29 18:34:32 -07007448 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007449 *
Mike Cleron290947b2009-09-29 18:34:32 -07007450 * @see #scrollBy(int, int)
7451 * @see #scrollTo(int, int)
7452 * @see #isHorizontalScrollBarEnabled()
7453 * @see #isVerticalScrollBarEnabled()
7454 * @see #setHorizontalScrollBarEnabled(boolean)
7455 * @see #setVerticalScrollBarEnabled(boolean)
7456 */
7457 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007458 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007459
Mike Cleronf116bf82009-09-27 19:14:12 -07007460 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7461 return false;
7462 }
7463
7464 if (scrollCache.scrollBar == null) {
7465 scrollCache.scrollBar = new ScrollBarDrawable();
7466 }
7467
7468 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7469
Mike Cleron290947b2009-09-29 18:34:32 -07007470 if (invalidate) {
7471 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007472 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007473 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007474
7475 if (scrollCache.state == ScrollabilityCache.OFF) {
7476 // FIXME: this is copied from WindowManagerService.
7477 // We should get this value from the system when it
7478 // is possible to do so.
7479 final int KEY_REPEAT_FIRST_DELAY = 750;
7480 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7481 }
7482
7483 // Tell mScrollCache when we should start fading. This may
7484 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007485 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007486 scrollCache.fadeStartTime = fadeStartTime;
7487 scrollCache.state = ScrollabilityCache.ON;
7488
7489 // Schedule our fader to run, unscheduling any old ones first
7490 if (mAttachInfo != null) {
7491 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7492 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7493 }
7494
7495 return true;
7496 }
7497
7498 return false;
7499 }
7500
7501 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007502 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07007503 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
7504 * in the future. This must be called from a UI thread. To call from a non-UI
7505 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007506 *
7507 * WARNING: This method is destructive to dirty.
7508 * @param dirty the rectangle representing the bounds of the dirty region
7509 */
7510 public void invalidate(Rect dirty) {
7511 if (ViewDebug.TRACE_HIERARCHY) {
7512 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7513 }
7514
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007515 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007516 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7517 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007518 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007519 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007520 final ViewParent p = mParent;
7521 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007522 //noinspection PointlessBooleanExpression,ConstantConditions
7523 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7524 if (p != null && ai != null && ai.mHardwareAccelerated) {
7525 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007526 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007527 p.invalidateChild(this, null);
7528 return;
7529 }
Romain Guyaf636eb2010-12-09 17:47:21 -08007530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007531 if (p != null && ai != null) {
7532 final int scrollX = mScrollX;
7533 final int scrollY = mScrollY;
7534 final Rect r = ai.mTmpInvalRect;
7535 r.set(dirty.left - scrollX, dirty.top - scrollY,
7536 dirty.right - scrollX, dirty.bottom - scrollY);
7537 mParent.invalidateChild(this, r);
7538 }
7539 }
7540 }
7541
7542 /**
7543 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
7544 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007545 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
7546 * will be called at some point in the future. This must be called from
7547 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007548 * @param l the left position of the dirty region
7549 * @param t the top position of the dirty region
7550 * @param r the right position of the dirty region
7551 * @param b the bottom position of the dirty region
7552 */
7553 public void invalidate(int l, int t, int r, int b) {
7554 if (ViewDebug.TRACE_HIERARCHY) {
7555 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7556 }
7557
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007558 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007559 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7560 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007561 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007562 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007563 final ViewParent p = mParent;
7564 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007565 //noinspection PointlessBooleanExpression,ConstantConditions
7566 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7567 if (p != null && ai != null && ai.mHardwareAccelerated) {
7568 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007569 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007570 p.invalidateChild(this, null);
7571 return;
7572 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007573 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007574 if (p != null && ai != null && l < r && t < b) {
7575 final int scrollX = mScrollX;
7576 final int scrollY = mScrollY;
7577 final Rect tmpr = ai.mTmpInvalRect;
7578 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
7579 p.invalidateChild(this, tmpr);
7580 }
7581 }
7582 }
7583
7584 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07007585 * Invalidate the whole view. If the view is visible,
7586 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
7587 * the future. This must be called from a UI thread. To call from a non-UI thread,
7588 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007589 */
7590 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07007591 invalidate(true);
7592 }
Joe Malin32736f02011-01-19 16:14:20 -08007593
Chet Haaseed032702010-10-01 14:05:54 -07007594 /**
7595 * This is where the invalidate() work actually happens. A full invalidate()
7596 * causes the drawing cache to be invalidated, but this function can be called with
7597 * invalidateCache set to false to skip that invalidation step for cases that do not
7598 * need it (for example, a component that remains at the same dimensions with the same
7599 * content).
7600 *
7601 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
7602 * well. This is usually true for a full invalidate, but may be set to false if the
7603 * View's contents or dimensions have not changed.
7604 */
Romain Guy849d0a32011-02-01 17:20:48 -08007605 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007606 if (ViewDebug.TRACE_HIERARCHY) {
7607 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7608 }
7609
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007610 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08007611 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08007612 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
7613 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07007614 mPrivateFlags &= ~DRAWN;
7615 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08007616 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07007617 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007619 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07007620 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08007621 //noinspection PointlessBooleanExpression,ConstantConditions
7622 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7623 if (p != null && ai != null && ai.mHardwareAccelerated) {
7624 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007625 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007626 p.invalidateChild(this, null);
7627 return;
7628 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007629 }
Michael Jurkaebefea42010-11-15 16:04:01 -08007630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007631 if (p != null && ai != null) {
7632 final Rect r = ai.mTmpInvalRect;
7633 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7634 // Don't call invalidate -- we don't want to internally scroll
7635 // our own bounds
7636 p.invalidateChild(this, r);
7637 }
7638 }
7639 }
7640
7641 /**
Romain Guyda489792011-02-03 01:05:15 -08007642 * @hide
7643 */
7644 public void fastInvalidate() {
7645 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
7646 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7647 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
7648 if (mParent instanceof View) {
7649 ((View) mParent).mPrivateFlags |= INVALIDATED;
7650 }
7651 mPrivateFlags &= ~DRAWN;
7652 mPrivateFlags |= INVALIDATED;
7653 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07007654 if (mParent != null && mAttachInfo != null) {
7655 if (mAttachInfo.mHardwareAccelerated) {
7656 mParent.invalidateChild(this, null);
7657 } else {
7658 final Rect r = mAttachInfo.mTmpInvalRect;
7659 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7660 // Don't call invalidate -- we don't want to internally scroll
7661 // our own bounds
7662 mParent.invalidateChild(this, r);
7663 }
Romain Guyda489792011-02-03 01:05:15 -08007664 }
7665 }
7666 }
7667
7668 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08007669 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08007670 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7671 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08007672 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
7673 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08007674 *
7675 * @hide
7676 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08007677 protected void invalidateParentCaches() {
7678 if (mParent instanceof View) {
7679 ((View) mParent).mPrivateFlags |= INVALIDATED;
7680 }
7681 }
Joe Malin32736f02011-01-19 16:14:20 -08007682
Romain Guy0fd89bf2011-01-26 15:41:30 -08007683 /**
7684 * Used to indicate that the parent of this view should be invalidated. This functionality
7685 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7686 * which is necessary when various parent-managed properties of the view change, such as
7687 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
7688 * an invalidation event to the parent.
7689 *
7690 * @hide
7691 */
7692 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08007693 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007694 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08007695 }
7696 }
7697
7698 /**
Romain Guy24443ea2009-05-11 11:56:30 -07007699 * Indicates whether this View is opaque. An opaque View guarantees that it will
7700 * draw all the pixels overlapping its bounds using a fully opaque color.
7701 *
7702 * Subclasses of View should override this method whenever possible to indicate
7703 * whether an instance is opaque. Opaque Views are treated in a special way by
7704 * the View hierarchy, possibly allowing it to perform optimizations during
7705 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07007706 *
Romain Guy24443ea2009-05-11 11:56:30 -07007707 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07007708 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007709 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07007710 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07007711 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
7712 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07007713 }
7714
Adam Powell20232d02010-12-08 21:08:53 -08007715 /**
7716 * @hide
7717 */
7718 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07007719 // Opaque if:
7720 // - Has a background
7721 // - Background is opaque
7722 // - Doesn't have scrollbars or scrollbars are inside overlay
7723
7724 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
7725 mPrivateFlags |= OPAQUE_BACKGROUND;
7726 } else {
7727 mPrivateFlags &= ~OPAQUE_BACKGROUND;
7728 }
7729
7730 final int flags = mViewFlags;
7731 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
7732 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
7733 mPrivateFlags |= OPAQUE_SCROLLBARS;
7734 } else {
7735 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
7736 }
7737 }
7738
7739 /**
7740 * @hide
7741 */
7742 protected boolean hasOpaqueScrollbars() {
7743 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07007744 }
7745
7746 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007747 * @return A handler associated with the thread running the View. This
7748 * handler can be used to pump events in the UI events queue.
7749 */
7750 public Handler getHandler() {
7751 if (mAttachInfo != null) {
7752 return mAttachInfo.mHandler;
7753 }
7754 return null;
7755 }
7756
7757 /**
7758 * Causes the Runnable to be added to the message queue.
7759 * The runnable will be run on the user interface thread.
7760 *
7761 * @param action The Runnable that will be executed.
7762 *
7763 * @return Returns true if the Runnable was successfully placed in to the
7764 * message queue. Returns false on failure, usually because the
7765 * looper processing the message queue is exiting.
7766 */
7767 public boolean post(Runnable action) {
7768 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007769 AttachInfo attachInfo = mAttachInfo;
7770 if (attachInfo != null) {
7771 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007772 } else {
7773 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007774 ViewAncestor.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007775 return true;
7776 }
7777
7778 return handler.post(action);
7779 }
7780
7781 /**
7782 * Causes the Runnable to be added to the message queue, to be run
7783 * after the specified amount of time elapses.
7784 * The runnable will be run on the user interface thread.
7785 *
7786 * @param action The Runnable that will be executed.
7787 * @param delayMillis The delay (in milliseconds) until the Runnable
7788 * will be executed.
7789 *
7790 * @return true if the Runnable was successfully placed in to the
7791 * message queue. Returns false on failure, usually because the
7792 * looper processing the message queue is exiting. Note that a
7793 * result of true does not mean the Runnable will be processed --
7794 * if the looper is quit before the delivery time of the message
7795 * occurs then the message will be dropped.
7796 */
7797 public boolean postDelayed(Runnable action, long delayMillis) {
7798 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007799 AttachInfo attachInfo = mAttachInfo;
7800 if (attachInfo != null) {
7801 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007802 } else {
7803 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007804 ViewAncestor.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007805 return true;
7806 }
7807
7808 return handler.postDelayed(action, delayMillis);
7809 }
7810
7811 /**
7812 * Removes the specified Runnable from the message queue.
7813 *
7814 * @param action The Runnable to remove from the message handling queue
7815 *
7816 * @return true if this view could ask the Handler to remove the Runnable,
7817 * false otherwise. When the returned value is true, the Runnable
7818 * may or may not have been actually removed from the message queue
7819 * (for instance, if the Runnable was not in the queue already.)
7820 */
7821 public boolean removeCallbacks(Runnable action) {
7822 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007823 AttachInfo attachInfo = mAttachInfo;
7824 if (attachInfo != null) {
7825 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007826 } else {
7827 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007828 ViewAncestor.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007829 return true;
7830 }
7831
7832 handler.removeCallbacks(action);
7833 return true;
7834 }
7835
7836 /**
7837 * Cause an invalidate to happen on a subsequent cycle through the event loop.
7838 * Use this to invalidate the View from a non-UI thread.
7839 *
7840 * @see #invalidate()
7841 */
7842 public void postInvalidate() {
7843 postInvalidateDelayed(0);
7844 }
7845
7846 /**
7847 * Cause an invalidate of the specified area to happen on a subsequent cycle
7848 * through the event loop. Use this to invalidate the View from a non-UI thread.
7849 *
7850 * @param left The left coordinate of the rectangle to invalidate.
7851 * @param top The top coordinate of the rectangle to invalidate.
7852 * @param right The right coordinate of the rectangle to invalidate.
7853 * @param bottom The bottom coordinate of the rectangle to invalidate.
7854 *
7855 * @see #invalidate(int, int, int, int)
7856 * @see #invalidate(Rect)
7857 */
7858 public void postInvalidate(int left, int top, int right, int bottom) {
7859 postInvalidateDelayed(0, left, top, right, bottom);
7860 }
7861
7862 /**
7863 * Cause an invalidate to happen on a subsequent cycle through the event
7864 * loop. Waits for the specified amount of time.
7865 *
7866 * @param delayMilliseconds the duration in milliseconds to delay the
7867 * invalidation by
7868 */
7869 public void postInvalidateDelayed(long delayMilliseconds) {
7870 // We try only with the AttachInfo because there's no point in invalidating
7871 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07007872 AttachInfo attachInfo = mAttachInfo;
7873 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007874 Message msg = Message.obtain();
7875 msg.what = AttachInfo.INVALIDATE_MSG;
7876 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07007877 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007878 }
7879 }
7880
7881 /**
7882 * Cause an invalidate of the specified area to happen on a subsequent cycle
7883 * through the event loop. Waits for the specified amount of time.
7884 *
7885 * @param delayMilliseconds the duration in milliseconds to delay the
7886 * invalidation by
7887 * @param left The left coordinate of the rectangle to invalidate.
7888 * @param top The top coordinate of the rectangle to invalidate.
7889 * @param right The right coordinate of the rectangle to invalidate.
7890 * @param bottom The bottom coordinate of the rectangle to invalidate.
7891 */
7892 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
7893 int right, int bottom) {
7894
7895 // We try only with the AttachInfo because there's no point in invalidating
7896 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07007897 AttachInfo attachInfo = mAttachInfo;
7898 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007899 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
7900 info.target = this;
7901 info.left = left;
7902 info.top = top;
7903 info.right = right;
7904 info.bottom = bottom;
7905
7906 final Message msg = Message.obtain();
7907 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
7908 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07007909 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007910 }
7911 }
7912
7913 /**
7914 * Called by a parent to request that a child update its values for mScrollX
7915 * and mScrollY if necessary. This will typically be done if the child is
7916 * animating a scroll using a {@link android.widget.Scroller Scroller}
7917 * object.
7918 */
7919 public void computeScroll() {
7920 }
7921
7922 /**
7923 * <p>Indicate whether the horizontal edges are faded when the view is
7924 * scrolled horizontally.</p>
7925 *
7926 * @return true if the horizontal edges should are faded on scroll, false
7927 * otherwise
7928 *
7929 * @see #setHorizontalFadingEdgeEnabled(boolean)
7930 * @attr ref android.R.styleable#View_fadingEdge
7931 */
7932 public boolean isHorizontalFadingEdgeEnabled() {
7933 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
7934 }
7935
7936 /**
7937 * <p>Define whether the horizontal edges should be faded when this view
7938 * is scrolled horizontally.</p>
7939 *
7940 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
7941 * be faded when the view is scrolled
7942 * horizontally
7943 *
7944 * @see #isHorizontalFadingEdgeEnabled()
7945 * @attr ref android.R.styleable#View_fadingEdge
7946 */
7947 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
7948 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
7949 if (horizontalFadingEdgeEnabled) {
7950 initScrollCache();
7951 }
7952
7953 mViewFlags ^= FADING_EDGE_HORIZONTAL;
7954 }
7955 }
7956
7957 /**
7958 * <p>Indicate whether the vertical edges are faded when the view is
7959 * scrolled horizontally.</p>
7960 *
7961 * @return true if the vertical edges should are faded on scroll, false
7962 * otherwise
7963 *
7964 * @see #setVerticalFadingEdgeEnabled(boolean)
7965 * @attr ref android.R.styleable#View_fadingEdge
7966 */
7967 public boolean isVerticalFadingEdgeEnabled() {
7968 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
7969 }
7970
7971 /**
7972 * <p>Define whether the vertical edges should be faded when this view
7973 * is scrolled vertically.</p>
7974 *
7975 * @param verticalFadingEdgeEnabled true if the vertical edges should
7976 * be faded when the view is scrolled
7977 * vertically
7978 *
7979 * @see #isVerticalFadingEdgeEnabled()
7980 * @attr ref android.R.styleable#View_fadingEdge
7981 */
7982 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
7983 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
7984 if (verticalFadingEdgeEnabled) {
7985 initScrollCache();
7986 }
7987
7988 mViewFlags ^= FADING_EDGE_VERTICAL;
7989 }
7990 }
7991
7992 /**
7993 * Returns the strength, or intensity, of the top faded edge. The strength is
7994 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7995 * returns 0.0 or 1.0 but no value in between.
7996 *
7997 * Subclasses should override this method to provide a smoother fade transition
7998 * when scrolling occurs.
7999 *
8000 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8001 */
8002 protected float getTopFadingEdgeStrength() {
8003 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8004 }
8005
8006 /**
8007 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8008 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8009 * returns 0.0 or 1.0 but no value in between.
8010 *
8011 * Subclasses should override this method to provide a smoother fade transition
8012 * when scrolling occurs.
8013 *
8014 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8015 */
8016 protected float getBottomFadingEdgeStrength() {
8017 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8018 computeVerticalScrollRange() ? 1.0f : 0.0f;
8019 }
8020
8021 /**
8022 * Returns the strength, or intensity, of the left faded edge. The strength is
8023 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8024 * returns 0.0 or 1.0 but no value in between.
8025 *
8026 * Subclasses should override this method to provide a smoother fade transition
8027 * when scrolling occurs.
8028 *
8029 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8030 */
8031 protected float getLeftFadingEdgeStrength() {
8032 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8033 }
8034
8035 /**
8036 * Returns the strength, or intensity, of the right faded edge. The strength is
8037 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8038 * returns 0.0 or 1.0 but no value in between.
8039 *
8040 * Subclasses should override this method to provide a smoother fade transition
8041 * when scrolling occurs.
8042 *
8043 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8044 */
8045 protected float getRightFadingEdgeStrength() {
8046 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8047 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8048 }
8049
8050 /**
8051 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8052 * scrollbar is not drawn by default.</p>
8053 *
8054 * @return true if the horizontal scrollbar should be painted, false
8055 * otherwise
8056 *
8057 * @see #setHorizontalScrollBarEnabled(boolean)
8058 */
8059 public boolean isHorizontalScrollBarEnabled() {
8060 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8061 }
8062
8063 /**
8064 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8065 * scrollbar is not drawn by default.</p>
8066 *
8067 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8068 * be painted
8069 *
8070 * @see #isHorizontalScrollBarEnabled()
8071 */
8072 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8073 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8074 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008075 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008076 recomputePadding();
8077 }
8078 }
8079
8080 /**
8081 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
8082 * scrollbar is not drawn by default.</p>
8083 *
8084 * @return true if the vertical scrollbar should be painted, false
8085 * otherwise
8086 *
8087 * @see #setVerticalScrollBarEnabled(boolean)
8088 */
8089 public boolean isVerticalScrollBarEnabled() {
8090 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
8091 }
8092
8093 /**
8094 * <p>Define whether the vertical scrollbar should be drawn or not. The
8095 * scrollbar is not drawn by default.</p>
8096 *
8097 * @param verticalScrollBarEnabled true if the vertical scrollbar should
8098 * be painted
8099 *
8100 * @see #isVerticalScrollBarEnabled()
8101 */
8102 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
8103 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
8104 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008105 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008106 recomputePadding();
8107 }
8108 }
8109
Adam Powell20232d02010-12-08 21:08:53 -08008110 /**
8111 * @hide
8112 */
8113 protected void recomputePadding() {
8114 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008115 }
Joe Malin32736f02011-01-19 16:14:20 -08008116
Mike Cleronfe81d382009-09-28 14:22:16 -07008117 /**
8118 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08008119 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008120 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08008121 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008122 */
8123 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
8124 initScrollCache();
8125 final ScrollabilityCache scrollabilityCache = mScrollCache;
8126 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008127 if (fadeScrollbars) {
8128 scrollabilityCache.state = ScrollabilityCache.OFF;
8129 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07008130 scrollabilityCache.state = ScrollabilityCache.ON;
8131 }
8132 }
Joe Malin32736f02011-01-19 16:14:20 -08008133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008134 /**
Joe Malin32736f02011-01-19 16:14:20 -08008135 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008136 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08008137 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008138 * @return true if scrollbar fading is enabled
8139 */
8140 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08008141 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008142 }
Joe Malin32736f02011-01-19 16:14:20 -08008143
Mike Cleron52f0a642009-09-28 18:21:37 -07008144 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008145 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
8146 * inset. When inset, they add to the padding of the view. And the scrollbars
8147 * can be drawn inside the padding area or on the edge of the view. For example,
8148 * if a view has a background drawable and you want to draw the scrollbars
8149 * inside the padding specified by the drawable, you can use
8150 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
8151 * appear at the edge of the view, ignoring the padding, then you can use
8152 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
8153 * @param style the style of the scrollbars. Should be one of
8154 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
8155 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
8156 * @see #SCROLLBARS_INSIDE_OVERLAY
8157 * @see #SCROLLBARS_INSIDE_INSET
8158 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8159 * @see #SCROLLBARS_OUTSIDE_INSET
8160 */
8161 public void setScrollBarStyle(int style) {
8162 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
8163 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07008164 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008165 recomputePadding();
8166 }
8167 }
8168
8169 /**
8170 * <p>Returns the current scrollbar style.</p>
8171 * @return the current scrollbar style
8172 * @see #SCROLLBARS_INSIDE_OVERLAY
8173 * @see #SCROLLBARS_INSIDE_INSET
8174 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8175 * @see #SCROLLBARS_OUTSIDE_INSET
8176 */
8177 public int getScrollBarStyle() {
8178 return mViewFlags & SCROLLBARS_STYLE_MASK;
8179 }
8180
8181 /**
8182 * <p>Compute the horizontal range that the horizontal scrollbar
8183 * represents.</p>
8184 *
8185 * <p>The range is expressed in arbitrary units that must be the same as the
8186 * units used by {@link #computeHorizontalScrollExtent()} and
8187 * {@link #computeHorizontalScrollOffset()}.</p>
8188 *
8189 * <p>The default range is the drawing width of this view.</p>
8190 *
8191 * @return the total horizontal range represented by the horizontal
8192 * scrollbar
8193 *
8194 * @see #computeHorizontalScrollExtent()
8195 * @see #computeHorizontalScrollOffset()
8196 * @see android.widget.ScrollBarDrawable
8197 */
8198 protected int computeHorizontalScrollRange() {
8199 return getWidth();
8200 }
8201
8202 /**
8203 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
8204 * within the horizontal range. This value is used to compute the position
8205 * of the thumb within the scrollbar's track.</p>
8206 *
8207 * <p>The range is expressed in arbitrary units that must be the same as the
8208 * units used by {@link #computeHorizontalScrollRange()} and
8209 * {@link #computeHorizontalScrollExtent()}.</p>
8210 *
8211 * <p>The default offset is the scroll offset of this view.</p>
8212 *
8213 * @return the horizontal offset of the scrollbar's thumb
8214 *
8215 * @see #computeHorizontalScrollRange()
8216 * @see #computeHorizontalScrollExtent()
8217 * @see android.widget.ScrollBarDrawable
8218 */
8219 protected int computeHorizontalScrollOffset() {
8220 return mScrollX;
8221 }
8222
8223 /**
8224 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
8225 * within the horizontal range. This value is used to compute the length
8226 * of the thumb within the scrollbar's track.</p>
8227 *
8228 * <p>The range is expressed in arbitrary units that must be the same as the
8229 * units used by {@link #computeHorizontalScrollRange()} and
8230 * {@link #computeHorizontalScrollOffset()}.</p>
8231 *
8232 * <p>The default extent is the drawing width of this view.</p>
8233 *
8234 * @return the horizontal extent of the scrollbar's thumb
8235 *
8236 * @see #computeHorizontalScrollRange()
8237 * @see #computeHorizontalScrollOffset()
8238 * @see android.widget.ScrollBarDrawable
8239 */
8240 protected int computeHorizontalScrollExtent() {
8241 return getWidth();
8242 }
8243
8244 /**
8245 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
8246 *
8247 * <p>The range is expressed in arbitrary units that must be the same as the
8248 * units used by {@link #computeVerticalScrollExtent()} and
8249 * {@link #computeVerticalScrollOffset()}.</p>
8250 *
8251 * @return the total vertical range represented by the vertical scrollbar
8252 *
8253 * <p>The default range is the drawing height of this view.</p>
8254 *
8255 * @see #computeVerticalScrollExtent()
8256 * @see #computeVerticalScrollOffset()
8257 * @see android.widget.ScrollBarDrawable
8258 */
8259 protected int computeVerticalScrollRange() {
8260 return getHeight();
8261 }
8262
8263 /**
8264 * <p>Compute the vertical offset of the vertical scrollbar's thumb
8265 * within the horizontal range. This value is used to compute the position
8266 * of the thumb within the scrollbar's track.</p>
8267 *
8268 * <p>The range is expressed in arbitrary units that must be the same as the
8269 * units used by {@link #computeVerticalScrollRange()} and
8270 * {@link #computeVerticalScrollExtent()}.</p>
8271 *
8272 * <p>The default offset is the scroll offset of this view.</p>
8273 *
8274 * @return the vertical offset of the scrollbar's thumb
8275 *
8276 * @see #computeVerticalScrollRange()
8277 * @see #computeVerticalScrollExtent()
8278 * @see android.widget.ScrollBarDrawable
8279 */
8280 protected int computeVerticalScrollOffset() {
8281 return mScrollY;
8282 }
8283
8284 /**
8285 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
8286 * within the vertical range. This value is used to compute the length
8287 * of the thumb within the scrollbar's track.</p>
8288 *
8289 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08008290 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008291 * {@link #computeVerticalScrollOffset()}.</p>
8292 *
8293 * <p>The default extent is the drawing height of this view.</p>
8294 *
8295 * @return the vertical extent of the scrollbar's thumb
8296 *
8297 * @see #computeVerticalScrollRange()
8298 * @see #computeVerticalScrollOffset()
8299 * @see android.widget.ScrollBarDrawable
8300 */
8301 protected int computeVerticalScrollExtent() {
8302 return getHeight();
8303 }
8304
8305 /**
8306 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8307 * scrollbars are painted only if they have been awakened first.</p>
8308 *
8309 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008310 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008311 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008312 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008313 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008314 // scrollbars are drawn only when the animation is running
8315 final ScrollabilityCache cache = mScrollCache;
8316 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008317
Mike Cleronf116bf82009-09-27 19:14:12 -07008318 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008319
Mike Cleronf116bf82009-09-27 19:14:12 -07008320 if (state == ScrollabilityCache.OFF) {
8321 return;
8322 }
Joe Malin32736f02011-01-19 16:14:20 -08008323
Mike Cleronf116bf82009-09-27 19:14:12 -07008324 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008325
Mike Cleronf116bf82009-09-27 19:14:12 -07008326 if (state == ScrollabilityCache.FADING) {
8327 // We're fading -- get our fade interpolation
8328 if (cache.interpolatorValues == null) {
8329 cache.interpolatorValues = new float[1];
8330 }
Joe Malin32736f02011-01-19 16:14:20 -08008331
Mike Cleronf116bf82009-09-27 19:14:12 -07008332 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008333
Mike Cleronf116bf82009-09-27 19:14:12 -07008334 // Stops the animation if we're done
8335 if (cache.scrollBarInterpolator.timeToValues(values) ==
8336 Interpolator.Result.FREEZE_END) {
8337 cache.state = ScrollabilityCache.OFF;
8338 } else {
8339 cache.scrollBar.setAlpha(Math.round(values[0]));
8340 }
Joe Malin32736f02011-01-19 16:14:20 -08008341
8342 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008343 // drawing. We only want this when we're fading so that
8344 // we prevent excessive redraws
8345 invalidate = true;
8346 } else {
8347 // We're just on -- but we may have been fading before so
8348 // reset alpha
8349 cache.scrollBar.setAlpha(255);
8350 }
8351
Joe Malin32736f02011-01-19 16:14:20 -08008352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008353 final int viewFlags = mViewFlags;
8354
8355 final boolean drawHorizontalScrollBar =
8356 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8357 final boolean drawVerticalScrollBar =
8358 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8359 && !isVerticalScrollBarHidden();
8360
8361 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8362 final int width = mRight - mLeft;
8363 final int height = mBottom - mTop;
8364
8365 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008366
Mike Reede8853fc2009-09-04 14:01:48 -04008367 final int scrollX = mScrollX;
8368 final int scrollY = mScrollY;
8369 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8370
Mike Cleronf116bf82009-09-27 19:14:12 -07008371 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008373 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008374 int size = scrollBar.getSize(false);
8375 if (size <= 0) {
8376 size = cache.scrollBarSize;
8377 }
8378
Mike Cleronf116bf82009-09-27 19:14:12 -07008379 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008380 computeHorizontalScrollOffset(),
8381 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008382 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008383 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008384 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008385 left = scrollX + (mPaddingLeft & inside);
8386 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8387 bottom = top + size;
8388 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8389 if (invalidate) {
8390 invalidate(left, top, right, bottom);
8391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008392 }
8393
8394 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008395 int size = scrollBar.getSize(true);
8396 if (size <= 0) {
8397 size = cache.scrollBarSize;
8398 }
8399
Mike Reede8853fc2009-09-04 14:01:48 -04008400 scrollBar.setParameters(computeVerticalScrollRange(),
8401 computeVerticalScrollOffset(),
8402 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008403 switch (mVerticalScrollbarPosition) {
8404 default:
8405 case SCROLLBAR_POSITION_DEFAULT:
8406 case SCROLLBAR_POSITION_RIGHT:
8407 left = scrollX + width - size - (mUserPaddingRight & inside);
8408 break;
8409 case SCROLLBAR_POSITION_LEFT:
8410 left = scrollX + (mUserPaddingLeft & inside);
8411 break;
8412 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008413 top = scrollY + (mPaddingTop & inside);
8414 right = left + size;
8415 bottom = scrollY + height - (mUserPaddingBottom & inside);
8416 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8417 if (invalidate) {
8418 invalidate(left, top, right, bottom);
8419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008420 }
8421 }
8422 }
8423 }
Romain Guy8506ab42009-06-11 17:35:47 -07008424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008425 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008426 * 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 -08008427 * FastScroller is visible.
8428 * @return whether to temporarily hide the vertical scrollbar
8429 * @hide
8430 */
8431 protected boolean isVerticalScrollBarHidden() {
8432 return false;
8433 }
8434
8435 /**
8436 * <p>Draw the horizontal scrollbar if
8437 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
8438 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008439 * @param canvas the canvas on which to draw the scrollbar
8440 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008441 *
8442 * @see #isHorizontalScrollBarEnabled()
8443 * @see #computeHorizontalScrollRange()
8444 * @see #computeHorizontalScrollExtent()
8445 * @see #computeHorizontalScrollOffset()
8446 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008447 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008448 */
Romain Guy8fb95422010-08-17 18:38:51 -07008449 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8450 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008451 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008452 scrollBar.draw(canvas);
8453 }
Mike Reede8853fc2009-09-04 14:01:48 -04008454
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008455 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008456 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8457 * returns true.</p>
8458 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008459 * @param canvas the canvas on which to draw the scrollbar
8460 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008461 *
8462 * @see #isVerticalScrollBarEnabled()
8463 * @see #computeVerticalScrollRange()
8464 * @see #computeVerticalScrollExtent()
8465 * @see #computeVerticalScrollOffset()
8466 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008467 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008468 */
Romain Guy8fb95422010-08-17 18:38:51 -07008469 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
8470 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04008471 scrollBar.setBounds(l, t, r, b);
8472 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008473 }
8474
8475 /**
8476 * Implement this to do your drawing.
8477 *
8478 * @param canvas the canvas on which the background will be drawn
8479 */
8480 protected void onDraw(Canvas canvas) {
8481 }
8482
8483 /*
8484 * Caller is responsible for calling requestLayout if necessary.
8485 * (This allows addViewInLayout to not request a new layout.)
8486 */
8487 void assignParent(ViewParent parent) {
8488 if (mParent == null) {
8489 mParent = parent;
8490 } else if (parent == null) {
8491 mParent = null;
8492 } else {
8493 throw new RuntimeException("view " + this + " being added, but"
8494 + " it already has a parent");
8495 }
8496 }
8497
8498 /**
8499 * This is called when the view is attached to a window. At this point it
8500 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008501 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
8502 * however it may be called any time before the first onDraw -- including
8503 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008504 *
8505 * @see #onDetachedFromWindow()
8506 */
8507 protected void onAttachedToWindow() {
8508 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
8509 mParent.requestTransparentRegion(this);
8510 }
Adam Powell8568c3a2010-04-19 14:26:11 -07008511 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
8512 initialAwakenScrollBars();
8513 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
8514 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08008515 jumpDrawablesToCurrentState();
Cibu Johny86666632010-02-22 13:01:02 -08008516
Cibu Johny1714c212010-02-22 13:01:02 -08008517 // We are supposing here that the parent directionality will be resolved before its children
8518 // View horizontalDirection public attribute resolution to an internal var.
Cibu Johny86666632010-02-22 13:01:02 -08008519 // Resolving the layout direction. LTR is set initially.
8520 mPrivateFlags2 &= ~RESOLVED_LAYOUT_RTL;
8521 switch (getHorizontalDirection()) {
8522 case HORIZONTAL_DIRECTION_INHERIT:
8523 // If this is root view, no need to look at parent's layout dir.
8524 if (mParent != null && mParent instanceof ViewGroup &&
8525 ((ViewGroup) mParent).isLayoutRtl()) {
8526 mPrivateFlags2 |= RESOLVED_LAYOUT_RTL;
8527 }
8528 break;
8529 case HORIZONTAL_DIRECTION_RTL:
8530 mPrivateFlags2 |= RESOLVED_LAYOUT_RTL;
8531 break;
8532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008533 }
8534
8535 /**
8536 * This is called when the view is detached from a window. At this point it
8537 * no longer has a surface for drawing.
8538 *
8539 * @see #onAttachedToWindow()
8540 */
8541 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08008542 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08008543
Romain Guya440b002010-02-24 15:57:54 -08008544 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05008545 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08008546 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08008547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008548 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08008549
8550 if (mHardwareLayer != null) {
8551 mHardwareLayer.destroy();
8552 mHardwareLayer = null;
8553 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008554
Chet Haasedaf98e92011-01-10 14:10:36 -08008555 if (mDisplayList != null) {
8556 mDisplayList.invalidate();
8557 }
8558
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008559 if (mAttachInfo != null) {
8560 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
8561 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
8562 }
8563
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08008564 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008565 }
8566
8567 /**
8568 * @return The number of times this view has been attached to a window
8569 */
8570 protected int getWindowAttachCount() {
8571 return mWindowAttachCount;
8572 }
8573
8574 /**
8575 * Retrieve a unique token identifying the window this view is attached to.
8576 * @return Return the window's token for use in
8577 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
8578 */
8579 public IBinder getWindowToken() {
8580 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
8581 }
8582
8583 /**
8584 * Retrieve a unique token identifying the top-level "real" window of
8585 * the window that this view is attached to. That is, this is like
8586 * {@link #getWindowToken}, except if the window this view in is a panel
8587 * window (attached to another containing window), then the token of
8588 * the containing window is returned instead.
8589 *
8590 * @return Returns the associated window token, either
8591 * {@link #getWindowToken()} or the containing window's token.
8592 */
8593 public IBinder getApplicationWindowToken() {
8594 AttachInfo ai = mAttachInfo;
8595 if (ai != null) {
8596 IBinder appWindowToken = ai.mPanelParentWindowToken;
8597 if (appWindowToken == null) {
8598 appWindowToken = ai.mWindowToken;
8599 }
8600 return appWindowToken;
8601 }
8602 return null;
8603 }
8604
8605 /**
8606 * Retrieve private session object this view hierarchy is using to
8607 * communicate with the window manager.
8608 * @return the session object to communicate with the window manager
8609 */
8610 /*package*/ IWindowSession getWindowSession() {
8611 return mAttachInfo != null ? mAttachInfo.mSession : null;
8612 }
8613
8614 /**
8615 * @param info the {@link android.view.View.AttachInfo} to associated with
8616 * this view
8617 */
8618 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
8619 //System.out.println("Attached! " + this);
8620 mAttachInfo = info;
8621 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008622 // We will need to evaluate the drawable state at least once.
8623 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008624 if (mFloatingTreeObserver != null) {
8625 info.mTreeObserver.merge(mFloatingTreeObserver);
8626 mFloatingTreeObserver = null;
8627 }
8628 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
8629 mAttachInfo.mScrollContainers.add(this);
8630 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
8631 }
8632 performCollectViewAttributes(visibility);
8633 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08008634
8635 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8636 mOnAttachStateChangeListeners;
8637 if (listeners != null && listeners.size() > 0) {
8638 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8639 // perform the dispatching. The iterator is a safe guard against listeners that
8640 // could mutate the list by calling the various add/remove methods. This prevents
8641 // the array from being modified while we iterate it.
8642 for (OnAttachStateChangeListener listener : listeners) {
8643 listener.onViewAttachedToWindow(this);
8644 }
8645 }
8646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008647 int vis = info.mWindowVisibility;
8648 if (vis != GONE) {
8649 onWindowVisibilityChanged(vis);
8650 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008651 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
8652 // If nobody has evaluated the drawable state yet, then do it now.
8653 refreshDrawableState();
8654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008655 }
8656
8657 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008658 AttachInfo info = mAttachInfo;
8659 if (info != null) {
8660 int vis = info.mWindowVisibility;
8661 if (vis != GONE) {
8662 onWindowVisibilityChanged(GONE);
8663 }
8664 }
8665
8666 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08008667
Adam Powell4afd62b2011-02-18 15:02:18 -08008668 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8669 mOnAttachStateChangeListeners;
8670 if (listeners != null && listeners.size() > 0) {
8671 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8672 // perform the dispatching. The iterator is a safe guard against listeners that
8673 // could mutate the list by calling the various add/remove methods. This prevents
8674 // the array from being modified while we iterate it.
8675 for (OnAttachStateChangeListener listener : listeners) {
8676 listener.onViewDetachedFromWindow(this);
8677 }
8678 }
8679
Romain Guy01d5edc2011-01-28 11:28:53 -08008680 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008681 mAttachInfo.mScrollContainers.remove(this);
8682 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
8683 }
Romain Guy01d5edc2011-01-28 11:28:53 -08008684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008685 mAttachInfo = null;
8686 }
8687
8688 /**
8689 * Store this view hierarchy's frozen state into the given container.
8690 *
8691 * @param container The SparseArray in which to save the view's state.
8692 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008693 * @see #restoreHierarchyState(android.util.SparseArray)
8694 * @see #dispatchSaveInstanceState(android.util.SparseArray)
8695 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008696 */
8697 public void saveHierarchyState(SparseArray<Parcelable> container) {
8698 dispatchSaveInstanceState(container);
8699 }
8700
8701 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008702 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
8703 * this view and its children. May be overridden to modify how freezing happens to a
8704 * 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 -08008705 *
8706 * @param container The SparseArray in which to save the view's state.
8707 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008708 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
8709 * @see #saveHierarchyState(android.util.SparseArray)
8710 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008711 */
8712 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
8713 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
8714 mPrivateFlags &= ~SAVE_STATE_CALLED;
8715 Parcelable state = onSaveInstanceState();
8716 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8717 throw new IllegalStateException(
8718 "Derived class did not call super.onSaveInstanceState()");
8719 }
8720 if (state != null) {
8721 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
8722 // + ": " + state);
8723 container.put(mID, state);
8724 }
8725 }
8726 }
8727
8728 /**
8729 * Hook allowing a view to generate a representation of its internal state
8730 * that can later be used to create a new instance with that same state.
8731 * This state should only contain information that is not persistent or can
8732 * not be reconstructed later. For example, you will never store your
8733 * current position on screen because that will be computed again when a
8734 * new instance of the view is placed in its view hierarchy.
8735 * <p>
8736 * Some examples of things you may store here: the current cursor position
8737 * in a text view (but usually not the text itself since that is stored in a
8738 * content provider or other persistent storage), the currently selected
8739 * item in a list view.
8740 *
8741 * @return Returns a Parcelable object containing the view's current dynamic
8742 * state, or null if there is nothing interesting to save. The
8743 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008744 * @see #onRestoreInstanceState(android.os.Parcelable)
8745 * @see #saveHierarchyState(android.util.SparseArray)
8746 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008747 * @see #setSaveEnabled(boolean)
8748 */
8749 protected Parcelable onSaveInstanceState() {
8750 mPrivateFlags |= SAVE_STATE_CALLED;
8751 return BaseSavedState.EMPTY_STATE;
8752 }
8753
8754 /**
8755 * Restore this view hierarchy's frozen state from the given container.
8756 *
8757 * @param container The SparseArray which holds previously frozen states.
8758 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008759 * @see #saveHierarchyState(android.util.SparseArray)
8760 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
8761 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008762 */
8763 public void restoreHierarchyState(SparseArray<Parcelable> container) {
8764 dispatchRestoreInstanceState(container);
8765 }
8766
8767 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008768 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
8769 * state for this view and its children. May be overridden to modify how restoring
8770 * happens to a view's children; for example, some views may want to not store state
8771 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008772 *
8773 * @param container The SparseArray which holds previously saved state.
8774 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008775 * @see #dispatchSaveInstanceState(android.util.SparseArray)
8776 * @see #restoreHierarchyState(android.util.SparseArray)
8777 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008778 */
8779 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
8780 if (mID != NO_ID) {
8781 Parcelable state = container.get(mID);
8782 if (state != null) {
8783 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
8784 // + ": " + state);
8785 mPrivateFlags &= ~SAVE_STATE_CALLED;
8786 onRestoreInstanceState(state);
8787 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8788 throw new IllegalStateException(
8789 "Derived class did not call super.onRestoreInstanceState()");
8790 }
8791 }
8792 }
8793 }
8794
8795 /**
8796 * Hook allowing a view to re-apply a representation of its internal state that had previously
8797 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
8798 * null state.
8799 *
8800 * @param state The frozen state that had previously been returned by
8801 * {@link #onSaveInstanceState}.
8802 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008803 * @see #onSaveInstanceState()
8804 * @see #restoreHierarchyState(android.util.SparseArray)
8805 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008806 */
8807 protected void onRestoreInstanceState(Parcelable state) {
8808 mPrivateFlags |= SAVE_STATE_CALLED;
8809 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08008810 throw new IllegalArgumentException("Wrong state class, expecting View State but "
8811 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08008812 + "when two views of different type have the same id in the same hierarchy. "
8813 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08008814 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008815 }
8816 }
8817
8818 /**
8819 * <p>Return the time at which the drawing of the view hierarchy started.</p>
8820 *
8821 * @return the drawing start time in milliseconds
8822 */
8823 public long getDrawingTime() {
8824 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
8825 }
8826
8827 /**
8828 * <p>Enables or disables the duplication of the parent's state into this view. When
8829 * duplication is enabled, this view gets its drawable state from its parent rather
8830 * than from its own internal properties.</p>
8831 *
8832 * <p>Note: in the current implementation, setting this property to true after the
8833 * view was added to a ViewGroup might have no effect at all. This property should
8834 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
8835 *
8836 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
8837 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008838 *
Gilles Debunnefb817032011-01-13 13:52:49 -08008839 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
8840 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008841 *
8842 * @param enabled True to enable duplication of the parent's drawable state, false
8843 * to disable it.
8844 *
8845 * @see #getDrawableState()
8846 * @see #isDuplicateParentStateEnabled()
8847 */
8848 public void setDuplicateParentStateEnabled(boolean enabled) {
8849 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
8850 }
8851
8852 /**
8853 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
8854 *
8855 * @return True if this view's drawable state is duplicated from the parent,
8856 * false otherwise
8857 *
8858 * @see #getDrawableState()
8859 * @see #setDuplicateParentStateEnabled(boolean)
8860 */
8861 public boolean isDuplicateParentStateEnabled() {
8862 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
8863 }
8864
8865 /**
Romain Guy171c5922011-01-06 10:04:23 -08008866 * <p>Specifies the type of layer backing this view. The layer can be
8867 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
8868 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008869 *
Romain Guy171c5922011-01-06 10:04:23 -08008870 * <p>A layer is associated with an optional {@link android.graphics.Paint}
8871 * instance that controls how the layer is composed on screen. The following
8872 * properties of the paint are taken into account when composing the layer:</p>
8873 * <ul>
8874 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
8875 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
8876 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
8877 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08008878 *
Romain Guy171c5922011-01-06 10:04:23 -08008879 * <p>If this view has an alpha value set to < 1.0 by calling
8880 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
8881 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
8882 * equivalent to setting a hardware layer on this view and providing a paint with
8883 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08008884 *
Romain Guy171c5922011-01-06 10:04:23 -08008885 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
8886 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
8887 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008888 *
Romain Guy171c5922011-01-06 10:04:23 -08008889 * @param layerType The ype of layer to use with this view, must be one of
8890 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8891 * {@link #LAYER_TYPE_HARDWARE}
8892 * @param paint The paint used to compose the layer. This argument is optional
8893 * and can be null. It is ignored when the layer type is
8894 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08008895 *
8896 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08008897 * @see #LAYER_TYPE_NONE
8898 * @see #LAYER_TYPE_SOFTWARE
8899 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08008900 * @see #setAlpha(float)
8901 *
Romain Guy171c5922011-01-06 10:04:23 -08008902 * @attr ref android.R.styleable#View_layerType
8903 */
8904 public void setLayerType(int layerType, Paint paint) {
8905 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08008906 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08008907 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
8908 }
Chet Haasedaf98e92011-01-10 14:10:36 -08008909
Romain Guyd6cd5722011-01-17 14:42:41 -08008910 if (layerType == mLayerType) {
8911 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
8912 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008913 invalidateParentCaches();
8914 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08008915 }
8916 return;
8917 }
Romain Guy171c5922011-01-06 10:04:23 -08008918
8919 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08008920 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -07008921 case LAYER_TYPE_HARDWARE:
8922 if (mHardwareLayer != null) {
8923 mHardwareLayer.destroy();
8924 mHardwareLayer = null;
8925 }
8926 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -08008927 case LAYER_TYPE_SOFTWARE:
8928 if (mDrawingCache != null) {
8929 mDrawingCache.recycle();
8930 mDrawingCache = null;
8931 }
Joe Malin32736f02011-01-19 16:14:20 -08008932
Romain Guy6c319ca2011-01-11 14:29:25 -08008933 if (mUnscaledDrawingCache != null) {
8934 mUnscaledDrawingCache.recycle();
8935 mUnscaledDrawingCache = null;
8936 }
8937 break;
Romain Guy6c319ca2011-01-11 14:29:25 -08008938 default:
8939 break;
Romain Guy171c5922011-01-06 10:04:23 -08008940 }
8941
8942 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08008943 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
8944 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
8945 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08008946
Romain Guy0fd89bf2011-01-26 15:41:30 -08008947 invalidateParentCaches();
8948 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08008949 }
8950
8951 /**
8952 * Indicates what type of layer is currently associated with this view. By default
8953 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
8954 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
8955 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08008956 *
Romain Guy171c5922011-01-06 10:04:23 -08008957 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8958 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08008959 *
8960 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08008961 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08008962 * @see #LAYER_TYPE_NONE
8963 * @see #LAYER_TYPE_SOFTWARE
8964 * @see #LAYER_TYPE_HARDWARE
8965 */
8966 public int getLayerType() {
8967 return mLayerType;
8968 }
Joe Malin32736f02011-01-19 16:14:20 -08008969
Romain Guy6c319ca2011-01-11 14:29:25 -08008970 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08008971 * Forces this view's layer to be created and this view to be rendered
8972 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
8973 * invoking this method will have no effect.
8974 *
8975 * This method can for instance be used to render a view into its layer before
8976 * starting an animation. If this view is complex, rendering into the layer
8977 * before starting the animation will avoid skipping frames.
8978 *
8979 * @throws IllegalStateException If this view is not attached to a window
8980 *
8981 * @see #setLayerType(int, android.graphics.Paint)
8982 */
8983 public void buildLayer() {
8984 if (mLayerType == LAYER_TYPE_NONE) return;
8985
8986 if (mAttachInfo == null) {
8987 throw new IllegalStateException("This view must be attached to a window first");
8988 }
8989
8990 switch (mLayerType) {
8991 case LAYER_TYPE_HARDWARE:
8992 getHardwareLayer();
8993 break;
8994 case LAYER_TYPE_SOFTWARE:
8995 buildDrawingCache(true);
8996 break;
8997 }
8998 }
8999
9000 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08009001 * <p>Returns a hardware layer that can be used to draw this view again
9002 * without executing its draw method.</p>
9003 *
9004 * @return A HardwareLayer ready to render, or null if an error occurred.
9005 */
Romain Guy5e7f7662011-01-24 22:35:56 -08009006 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08009007 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
9008 return null;
9009 }
9010
9011 final int width = mRight - mLeft;
9012 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08009013
Romain Guy6c319ca2011-01-11 14:29:25 -08009014 if (width == 0 || height == 0) {
9015 return null;
9016 }
9017
9018 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
9019 if (mHardwareLayer == null) {
9020 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
9021 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08009022 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009023 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
9024 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08009025 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009026 }
9027
Romain Guy5e7f7662011-01-24 22:35:56 -08009028 Canvas currentCanvas = mAttachInfo.mHardwareCanvas;
9029 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
9030 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009031 try {
9032 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08009033 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08009034 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009035
Chet Haase88172fe2011-03-07 17:36:33 -08009036 final int restoreCount = canvas.save();
9037
Romain Guy6c319ca2011-01-11 14:29:25 -08009038 computeScroll();
9039 canvas.translate(-mScrollX, -mScrollY);
9040
Romain Guy6c319ca2011-01-11 14:29:25 -08009041 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009042
Romain Guy6c319ca2011-01-11 14:29:25 -08009043 // Fast path for layouts with no backgrounds
9044 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9045 mPrivateFlags &= ~DIRTY_MASK;
9046 dispatchDraw(canvas);
9047 } else {
9048 draw(canvas);
9049 }
Joe Malin32736f02011-01-19 16:14:20 -08009050
Chet Haase88172fe2011-03-07 17:36:33 -08009051 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08009052 } finally {
9053 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08009054 mHardwareLayer.end(currentCanvas);
9055 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009056 }
9057 }
9058
9059 return mHardwareLayer;
9060 }
Romain Guy171c5922011-01-06 10:04:23 -08009061
9062 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009063 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
9064 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
9065 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
9066 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
9067 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
9068 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009069 *
Romain Guy171c5922011-01-06 10:04:23 -08009070 * <p>Enabling the drawing cache is similar to
9071 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08009072 * acceleration is turned off. When hardware acceleration is turned on, enabling the
9073 * drawing cache has no effect on rendering because the system uses a different mechanism
9074 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
9075 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
9076 * for information on how to enable software and hardware layers.</p>
9077 *
9078 * <p>This API can be used to manually generate
9079 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
9080 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009081 *
9082 * @param enabled true to enable the drawing cache, false otherwise
9083 *
9084 * @see #isDrawingCacheEnabled()
9085 * @see #getDrawingCache()
9086 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08009087 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009088 */
9089 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009090 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009091 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
9092 }
9093
9094 /**
9095 * <p>Indicates whether the drawing cache is enabled for this view.</p>
9096 *
9097 * @return true if the drawing cache is enabled
9098 *
9099 * @see #setDrawingCacheEnabled(boolean)
9100 * @see #getDrawingCache()
9101 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009102 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009103 public boolean isDrawingCacheEnabled() {
9104 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
9105 }
9106
9107 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08009108 * Debugging utility which recursively outputs the dirty state of a view and its
9109 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08009110 *
Chet Haasedaf98e92011-01-10 14:10:36 -08009111 * @hide
9112 */
Romain Guy676b1732011-02-14 14:45:33 -08009113 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08009114 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
9115 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
9116 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
9117 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
9118 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
9119 if (clear) {
9120 mPrivateFlags &= clearMask;
9121 }
9122 if (this instanceof ViewGroup) {
9123 ViewGroup parent = (ViewGroup) this;
9124 final int count = parent.getChildCount();
9125 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08009126 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08009127 child.outputDirtyFlags(indent + " ", clear, clearMask);
9128 }
9129 }
9130 }
9131
9132 /**
9133 * This method is used by ViewGroup to cause its children to restore or recreate their
9134 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
9135 * to recreate its own display list, which would happen if it went through the normal
9136 * draw/dispatchDraw mechanisms.
9137 *
9138 * @hide
9139 */
9140 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08009141
9142 /**
9143 * A view that is not attached or hardware accelerated cannot create a display list.
9144 * This method checks these conditions and returns the appropriate result.
9145 *
9146 * @return true if view has the ability to create a display list, false otherwise.
9147 *
9148 * @hide
9149 */
9150 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08009151 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08009152 }
Joe Malin32736f02011-01-19 16:14:20 -08009153
Chet Haasedaf98e92011-01-10 14:10:36 -08009154 /**
Romain Guyb051e892010-09-28 19:09:36 -07009155 * <p>Returns a display list that can be used to draw this view again
9156 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009157 *
Romain Guyb051e892010-09-28 19:09:36 -07009158 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08009159 *
9160 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07009161 */
Chet Haasedaf98e92011-01-10 14:10:36 -08009162 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08009163 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07009164 return null;
9165 }
9166
Chet Haasedaf98e92011-01-10 14:10:36 -08009167 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
9168 mDisplayList == null || !mDisplayList.isValid() ||
9169 mRecreateDisplayList)) {
9170 // Don't need to recreate the display list, just need to tell our
9171 // children to restore/recreate theirs
9172 if (mDisplayList != null && mDisplayList.isValid() &&
9173 !mRecreateDisplayList) {
9174 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9175 mPrivateFlags &= ~DIRTY_MASK;
9176 dispatchGetDisplayList();
9177
9178 return mDisplayList;
9179 }
9180
9181 // If we got here, we're recreating it. Mark it as such to ensure that
9182 // we copy in child display lists into ours in drawChild()
9183 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08009184 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009185 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
9186 // If we're creating a new display list, make sure our parent gets invalidated
9187 // since they will need to recreate their display list to account for this
9188 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08009189 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08009190 }
Romain Guyb051e892010-09-28 19:09:36 -07009191
9192 final HardwareCanvas canvas = mDisplayList.start();
9193 try {
9194 int width = mRight - mLeft;
9195 int height = mBottom - mTop;
9196
9197 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08009198 // The dirty rect should always be null for a display list
9199 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07009200
Chet Haase88172fe2011-03-07 17:36:33 -08009201 final int restoreCount = canvas.save();
9202
Chet Haasedaf98e92011-01-10 14:10:36 -08009203 computeScroll();
9204 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009205 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009206
Romain Guyb051e892010-09-28 19:09:36 -07009207 // Fast path for layouts with no backgrounds
9208 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9209 mPrivateFlags &= ~DIRTY_MASK;
9210 dispatchDraw(canvas);
9211 } else {
9212 draw(canvas);
9213 }
Joe Malin32736f02011-01-19 16:14:20 -08009214
Chet Haase88172fe2011-03-07 17:36:33 -08009215 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07009216 } finally {
9217 canvas.onPostDraw();
9218
9219 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07009220 }
Chet Haase77785f92011-01-25 23:22:09 -08009221 } else {
9222 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9223 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07009224 }
9225
9226 return mDisplayList;
9227 }
9228
9229 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009230 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009231 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009232 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009233 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009234 * @see #getDrawingCache(boolean)
9235 */
9236 public Bitmap getDrawingCache() {
9237 return getDrawingCache(false);
9238 }
9239
9240 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009241 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
9242 * is null when caching is disabled. If caching is enabled and the cache is not ready,
9243 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
9244 * draw from the cache when the cache is enabled. To benefit from the cache, you must
9245 * request the drawing cache by calling this method and draw it on screen if the
9246 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009247 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009248 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9249 * this method will create a bitmap of the same size as this view. Because this bitmap
9250 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9251 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9252 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9253 * size than the view. This implies that your application must be able to handle this
9254 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009255 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009256 * @param autoScale Indicates whether the generated bitmap should be scaled based on
9257 * the current density of the screen when the application is in compatibility
9258 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009259 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009260 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009261 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009262 * @see #setDrawingCacheEnabled(boolean)
9263 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07009264 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009265 * @see #destroyDrawingCache()
9266 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009267 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009268 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
9269 return null;
9270 }
9271 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009272 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009273 }
Romain Guy02890fd2010-08-06 17:58:44 -07009274 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009275 }
9276
9277 /**
9278 * <p>Frees the resources used by the drawing cache. If you call
9279 * {@link #buildDrawingCache()} manually without calling
9280 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9281 * should cleanup the cache with this method afterwards.</p>
9282 *
9283 * @see #setDrawingCacheEnabled(boolean)
9284 * @see #buildDrawingCache()
9285 * @see #getDrawingCache()
9286 */
9287 public void destroyDrawingCache() {
9288 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009289 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009290 mDrawingCache = null;
9291 }
Romain Guyfbd8f692009-06-26 14:51:58 -07009292 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009293 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07009294 mUnscaledDrawingCache = null;
9295 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009296 }
9297
9298 /**
9299 * Setting a solid background color for the drawing cache's bitmaps will improve
9300 * perfromance and memory usage. Note, though that this should only be used if this
9301 * view will always be drawn on top of a solid color.
9302 *
9303 * @param color The background color to use for the drawing cache's bitmap
9304 *
9305 * @see #setDrawingCacheEnabled(boolean)
9306 * @see #buildDrawingCache()
9307 * @see #getDrawingCache()
9308 */
9309 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08009310 if (color != mDrawingCacheBackgroundColor) {
9311 mDrawingCacheBackgroundColor = color;
9312 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9313 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009314 }
9315
9316 /**
9317 * @see #setDrawingCacheBackgroundColor(int)
9318 *
9319 * @return The background color to used for the drawing cache's bitmap
9320 */
9321 public int getDrawingCacheBackgroundColor() {
9322 return mDrawingCacheBackgroundColor;
9323 }
9324
9325 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009326 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009327 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009328 * @see #buildDrawingCache(boolean)
9329 */
9330 public void buildDrawingCache() {
9331 buildDrawingCache(false);
9332 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08009333
Romain Guyfbd8f692009-06-26 14:51:58 -07009334 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009335 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
9336 *
9337 * <p>If you call {@link #buildDrawingCache()} manually without calling
9338 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9339 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009340 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009341 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9342 * this method will create a bitmap of the same size as this view. Because this bitmap
9343 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9344 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9345 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9346 * size than the view. This implies that your application must be able to handle this
9347 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009348 *
Romain Guy0d9275e2010-10-26 14:22:30 -07009349 * <p>You should avoid calling this method when hardware acceleration is enabled. If
9350 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08009351 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07009352 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009353 *
9354 * @see #getDrawingCache()
9355 * @see #destroyDrawingCache()
9356 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009357 public void buildDrawingCache(boolean autoScale) {
9358 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07009359 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009360 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009361
9362 if (ViewDebug.TRACE_HIERARCHY) {
9363 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
9364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009365
Romain Guy8506ab42009-06-11 17:35:47 -07009366 int width = mRight - mLeft;
9367 int height = mBottom - mTop;
9368
9369 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07009370 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07009371
Romain Guye1123222009-06-29 14:24:56 -07009372 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009373 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
9374 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07009375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009376
9377 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07009378 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08009379 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009380
9381 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07009382 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08009383 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009384 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
9385 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08009386 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009387 return;
9388 }
9389
9390 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07009391 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009392
9393 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009394 Bitmap.Config quality;
9395 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08009396 // Never pick ARGB_4444 because it looks awful
9397 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009398 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
9399 case DRAWING_CACHE_QUALITY_AUTO:
9400 quality = Bitmap.Config.ARGB_8888;
9401 break;
9402 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08009403 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009404 break;
9405 case DRAWING_CACHE_QUALITY_HIGH:
9406 quality = Bitmap.Config.ARGB_8888;
9407 break;
9408 default:
9409 quality = Bitmap.Config.ARGB_8888;
9410 break;
9411 }
9412 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07009413 // Optimization for translucent windows
9414 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08009415 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009416 }
9417
9418 // Try to cleanup memory
9419 if (bitmap != null) bitmap.recycle();
9420
9421 try {
9422 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07009423 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07009424 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07009425 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009426 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07009427 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009428 }
Adam Powell26153a32010-11-08 15:22:27 -08009429 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009430 } catch (OutOfMemoryError e) {
9431 // If there is not enough memory to create the bitmap cache, just
9432 // ignore the issue as bitmap caches are not required to draw the
9433 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07009434 if (autoScale) {
9435 mDrawingCache = null;
9436 } else {
9437 mUnscaledDrawingCache = null;
9438 }
Romain Guy0211a0a2011-02-14 16:34:59 -08009439 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009440 return;
9441 }
9442
9443 clear = drawingCacheBackgroundColor != 0;
9444 }
9445
9446 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009447 if (attachInfo != null) {
9448 canvas = attachInfo.mCanvas;
9449 if (canvas == null) {
9450 canvas = new Canvas();
9451 }
9452 canvas.setBitmap(bitmap);
9453 // Temporarily clobber the cached Canvas in case one of our children
9454 // is also using a drawing cache. Without this, the children would
9455 // steal the canvas by attaching their own bitmap to it and bad, bad
9456 // thing would happen (invisible views, corrupted drawings, etc.)
9457 attachInfo.mCanvas = null;
9458 } else {
9459 // This case should hopefully never or seldom happen
9460 canvas = new Canvas(bitmap);
9461 }
9462
9463 if (clear) {
9464 bitmap.eraseColor(drawingCacheBackgroundColor);
9465 }
9466
9467 computeScroll();
9468 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08009469
Romain Guye1123222009-06-29 14:24:56 -07009470 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009471 final float scale = attachInfo.mApplicationScale;
9472 canvas.scale(scale, scale);
9473 }
Joe Malin32736f02011-01-19 16:14:20 -08009474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009475 canvas.translate(-mScrollX, -mScrollY);
9476
Romain Guy5bcdff42009-05-14 21:27:18 -07009477 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08009478 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
9479 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07009480 mPrivateFlags |= DRAWING_CACHE_VALID;
9481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009482
9483 // Fast path for layouts with no backgrounds
9484 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9485 if (ViewDebug.TRACE_HIERARCHY) {
9486 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9487 }
Romain Guy5bcdff42009-05-14 21:27:18 -07009488 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009489 dispatchDraw(canvas);
9490 } else {
9491 draw(canvas);
9492 }
9493
9494 canvas.restoreToCount(restoreCount);
9495
9496 if (attachInfo != null) {
9497 // Restore the cached Canvas for our siblings
9498 attachInfo.mCanvas = canvas;
9499 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009500 }
9501 }
9502
9503 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009504 * Create a snapshot of the view into a bitmap. We should probably make
9505 * some form of this public, but should think about the API.
9506 */
Romain Guy223ff5c2010-03-02 17:07:47 -08009507 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009508 int width = mRight - mLeft;
9509 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009510
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009511 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07009512 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009513 width = (int) ((width * scale) + 0.5f);
9514 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08009515
Romain Guy8c11e312009-09-14 15:15:30 -07009516 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009517 if (bitmap == null) {
9518 throw new OutOfMemoryError();
9519 }
9520
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009521 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08009522
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009523 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009524 if (attachInfo != null) {
9525 canvas = attachInfo.mCanvas;
9526 if (canvas == null) {
9527 canvas = new Canvas();
9528 }
9529 canvas.setBitmap(bitmap);
9530 // Temporarily clobber the cached Canvas in case one of our children
9531 // is also using a drawing cache. Without this, the children would
9532 // steal the canvas by attaching their own bitmap to it and bad, bad
9533 // things would happen (invisible views, corrupted drawings, etc.)
9534 attachInfo.mCanvas = null;
9535 } else {
9536 // This case should hopefully never or seldom happen
9537 canvas = new Canvas(bitmap);
9538 }
9539
Romain Guy5bcdff42009-05-14 21:27:18 -07009540 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009541 bitmap.eraseColor(backgroundColor);
9542 }
9543
9544 computeScroll();
9545 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009546 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009547 canvas.translate(-mScrollX, -mScrollY);
9548
Romain Guy5bcdff42009-05-14 21:27:18 -07009549 // Temporarily remove the dirty mask
9550 int flags = mPrivateFlags;
9551 mPrivateFlags &= ~DIRTY_MASK;
9552
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009553 // Fast path for layouts with no backgrounds
9554 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9555 dispatchDraw(canvas);
9556 } else {
9557 draw(canvas);
9558 }
9559
Romain Guy5bcdff42009-05-14 21:27:18 -07009560 mPrivateFlags = flags;
9561
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009562 canvas.restoreToCount(restoreCount);
9563
9564 if (attachInfo != null) {
9565 // Restore the cached Canvas for our siblings
9566 attachInfo.mCanvas = canvas;
9567 }
Romain Guy8506ab42009-06-11 17:35:47 -07009568
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009569 return bitmap;
9570 }
9571
9572 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009573 * Indicates whether this View is currently in edit mode. A View is usually
9574 * in edit mode when displayed within a developer tool. For instance, if
9575 * this View is being drawn by a visual user interface builder, this method
9576 * should return true.
9577 *
9578 * Subclasses should check the return value of this method to provide
9579 * different behaviors if their normal behavior might interfere with the
9580 * host environment. For instance: the class spawns a thread in its
9581 * constructor, the drawing code relies on device-specific features, etc.
9582 *
9583 * This method is usually checked in the drawing code of custom widgets.
9584 *
9585 * @return True if this View is in edit mode, false otherwise.
9586 */
9587 public boolean isInEditMode() {
9588 return false;
9589 }
9590
9591 /**
9592 * If the View draws content inside its padding and enables fading edges,
9593 * it needs to support padding offsets. Padding offsets are added to the
9594 * fading edges to extend the length of the fade so that it covers pixels
9595 * drawn inside the padding.
9596 *
9597 * Subclasses of this class should override this method if they need
9598 * to draw content inside the padding.
9599 *
9600 * @return True if padding offset must be applied, false otherwise.
9601 *
9602 * @see #getLeftPaddingOffset()
9603 * @see #getRightPaddingOffset()
9604 * @see #getTopPaddingOffset()
9605 * @see #getBottomPaddingOffset()
9606 *
9607 * @since CURRENT
9608 */
9609 protected boolean isPaddingOffsetRequired() {
9610 return false;
9611 }
9612
9613 /**
9614 * Amount by which to extend the left fading region. Called only when
9615 * {@link #isPaddingOffsetRequired()} returns true.
9616 *
9617 * @return The left padding offset in pixels.
9618 *
9619 * @see #isPaddingOffsetRequired()
9620 *
9621 * @since CURRENT
9622 */
9623 protected int getLeftPaddingOffset() {
9624 return 0;
9625 }
9626
9627 /**
9628 * Amount by which to extend the right fading region. Called only when
9629 * {@link #isPaddingOffsetRequired()} returns true.
9630 *
9631 * @return The right padding offset in pixels.
9632 *
9633 * @see #isPaddingOffsetRequired()
9634 *
9635 * @since CURRENT
9636 */
9637 protected int getRightPaddingOffset() {
9638 return 0;
9639 }
9640
9641 /**
9642 * Amount by which to extend the top fading region. Called only when
9643 * {@link #isPaddingOffsetRequired()} returns true.
9644 *
9645 * @return The top padding offset in pixels.
9646 *
9647 * @see #isPaddingOffsetRequired()
9648 *
9649 * @since CURRENT
9650 */
9651 protected int getTopPaddingOffset() {
9652 return 0;
9653 }
9654
9655 /**
9656 * Amount by which to extend the bottom fading region. Called only when
9657 * {@link #isPaddingOffsetRequired()} returns true.
9658 *
9659 * @return The bottom padding offset in pixels.
9660 *
9661 * @see #isPaddingOffsetRequired()
9662 *
9663 * @since CURRENT
9664 */
9665 protected int getBottomPaddingOffset() {
9666 return 0;
9667 }
9668
9669 /**
Romain Guy2bffd262010-09-12 17:40:02 -07009670 * <p>Indicates whether this view is attached to an hardware accelerated
9671 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009672 *
Romain Guy2bffd262010-09-12 17:40:02 -07009673 * <p>Even if this method returns true, it does not mean that every call
9674 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
9675 * accelerated {@link android.graphics.Canvas}. For instance, if this view
9676 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
9677 * window is hardware accelerated,
9678 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
9679 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009680 *
Romain Guy2bffd262010-09-12 17:40:02 -07009681 * @return True if the view is attached to a window and the window is
9682 * hardware accelerated; false in any other case.
9683 */
9684 public boolean isHardwareAccelerated() {
9685 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
9686 }
Joe Malin32736f02011-01-19 16:14:20 -08009687
Romain Guy2bffd262010-09-12 17:40:02 -07009688 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009689 * Manually render this view (and all of its children) to the given Canvas.
9690 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009691 * called. When implementing a view, implement
9692 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
9693 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009694 *
9695 * @param canvas The Canvas to which the View is rendered.
9696 */
9697 public void draw(Canvas canvas) {
9698 if (ViewDebug.TRACE_HIERARCHY) {
9699 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9700 }
9701
Romain Guy5bcdff42009-05-14 21:27:18 -07009702 final int privateFlags = mPrivateFlags;
9703 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
9704 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
9705 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07009706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009707 /*
9708 * Draw traversal performs several drawing steps which must be executed
9709 * in the appropriate order:
9710 *
9711 * 1. Draw the background
9712 * 2. If necessary, save the canvas' layers to prepare for fading
9713 * 3. Draw view's content
9714 * 4. Draw children
9715 * 5. If necessary, draw the fading edges and restore layers
9716 * 6. Draw decorations (scrollbars for instance)
9717 */
9718
9719 // Step 1, draw the background, if needed
9720 int saveCount;
9721
Romain Guy24443ea2009-05-11 11:56:30 -07009722 if (!dirtyOpaque) {
9723 final Drawable background = mBGDrawable;
9724 if (background != null) {
9725 final int scrollX = mScrollX;
9726 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009727
Romain Guy24443ea2009-05-11 11:56:30 -07009728 if (mBackgroundSizeChanged) {
9729 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
9730 mBackgroundSizeChanged = false;
9731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009732
Romain Guy24443ea2009-05-11 11:56:30 -07009733 if ((scrollX | scrollY) == 0) {
9734 background.draw(canvas);
9735 } else {
9736 canvas.translate(scrollX, scrollY);
9737 background.draw(canvas);
9738 canvas.translate(-scrollX, -scrollY);
9739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009740 }
9741 }
9742
9743 // skip step 2 & 5 if possible (common case)
9744 final int viewFlags = mViewFlags;
9745 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
9746 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
9747 if (!verticalEdges && !horizontalEdges) {
9748 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009749 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009750
9751 // Step 4, draw the children
9752 dispatchDraw(canvas);
9753
9754 // Step 6, draw decorations (scrollbars)
9755 onDrawScrollBars(canvas);
9756
9757 // we're done...
9758 return;
9759 }
9760
9761 /*
9762 * Here we do the full fledged routine...
9763 * (this is an uncommon case where speed matters less,
9764 * this is why we repeat some of the tests that have been
9765 * done above)
9766 */
9767
9768 boolean drawTop = false;
9769 boolean drawBottom = false;
9770 boolean drawLeft = false;
9771 boolean drawRight = false;
9772
9773 float topFadeStrength = 0.0f;
9774 float bottomFadeStrength = 0.0f;
9775 float leftFadeStrength = 0.0f;
9776 float rightFadeStrength = 0.0f;
9777
9778 // Step 2, save the canvas' layers
9779 int paddingLeft = mPaddingLeft;
9780 int paddingTop = mPaddingTop;
9781
9782 final boolean offsetRequired = isPaddingOffsetRequired();
9783 if (offsetRequired) {
9784 paddingLeft += getLeftPaddingOffset();
9785 paddingTop += getTopPaddingOffset();
9786 }
9787
9788 int left = mScrollX + paddingLeft;
9789 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
9790 int top = mScrollY + paddingTop;
9791 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
9792
9793 if (offsetRequired) {
9794 right += getRightPaddingOffset();
9795 bottom += getBottomPaddingOffset();
9796 }
9797
9798 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009799 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
9800 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009801
9802 // clip the fade length if top and bottom fades overlap
9803 // overlapping fades produce odd-looking artifacts
9804 if (verticalEdges && (top + length > bottom - length)) {
9805 length = (bottom - top) / 2;
9806 }
9807
9808 // also clip horizontal fades if necessary
9809 if (horizontalEdges && (left + length > right - length)) {
9810 length = (right - left) / 2;
9811 }
9812
9813 if (verticalEdges) {
9814 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009815 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009816 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009817 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009818 }
9819
9820 if (horizontalEdges) {
9821 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009822 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009823 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009824 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009825 }
9826
9827 saveCount = canvas.getSaveCount();
9828
9829 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -07009830 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009831 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
9832
9833 if (drawTop) {
9834 canvas.saveLayer(left, top, right, top + length, null, flags);
9835 }
9836
9837 if (drawBottom) {
9838 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
9839 }
9840
9841 if (drawLeft) {
9842 canvas.saveLayer(left, top, left + length, bottom, null, flags);
9843 }
9844
9845 if (drawRight) {
9846 canvas.saveLayer(right - length, top, right, bottom, null, flags);
9847 }
9848 } else {
9849 scrollabilityCache.setFadeColor(solidColor);
9850 }
9851
9852 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009853 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009854
9855 // Step 4, draw the children
9856 dispatchDraw(canvas);
9857
9858 // Step 5, draw the fade effect and restore layers
9859 final Paint p = scrollabilityCache.paint;
9860 final Matrix matrix = scrollabilityCache.matrix;
9861 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009862
9863 if (drawTop) {
9864 matrix.setScale(1, fadeHeight * topFadeStrength);
9865 matrix.postTranslate(left, top);
9866 fade.setLocalMatrix(matrix);
9867 canvas.drawRect(left, top, right, top + length, p);
9868 }
9869
9870 if (drawBottom) {
9871 matrix.setScale(1, fadeHeight * bottomFadeStrength);
9872 matrix.postRotate(180);
9873 matrix.postTranslate(left, bottom);
9874 fade.setLocalMatrix(matrix);
9875 canvas.drawRect(left, bottom - length, right, bottom, p);
9876 }
9877
9878 if (drawLeft) {
9879 matrix.setScale(1, fadeHeight * leftFadeStrength);
9880 matrix.postRotate(-90);
9881 matrix.postTranslate(left, top);
9882 fade.setLocalMatrix(matrix);
9883 canvas.drawRect(left, top, left + length, bottom, p);
9884 }
9885
9886 if (drawRight) {
9887 matrix.setScale(1, fadeHeight * rightFadeStrength);
9888 matrix.postRotate(90);
9889 matrix.postTranslate(right, top);
9890 fade.setLocalMatrix(matrix);
9891 canvas.drawRect(right - length, top, right, bottom, p);
9892 }
9893
9894 canvas.restoreToCount(saveCount);
9895
9896 // Step 6, draw decorations (scrollbars)
9897 onDrawScrollBars(canvas);
9898 }
9899
9900 /**
9901 * Override this if your view is known to always be drawn on top of a solid color background,
9902 * and needs to draw fading edges. Returning a non-zero color enables the view system to
9903 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
9904 * should be set to 0xFF.
9905 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009906 * @see #setVerticalFadingEdgeEnabled(boolean)
9907 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009908 *
9909 * @return The known solid color background for this view, or 0 if the color may vary
9910 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009911 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009912 public int getSolidColor() {
9913 return 0;
9914 }
9915
9916 /**
9917 * Build a human readable string representation of the specified view flags.
9918 *
9919 * @param flags the view flags to convert to a string
9920 * @return a String representing the supplied flags
9921 */
9922 private static String printFlags(int flags) {
9923 String output = "";
9924 int numFlags = 0;
9925 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
9926 output += "TAKES_FOCUS";
9927 numFlags++;
9928 }
9929
9930 switch (flags & VISIBILITY_MASK) {
9931 case INVISIBLE:
9932 if (numFlags > 0) {
9933 output += " ";
9934 }
9935 output += "INVISIBLE";
9936 // USELESS HERE numFlags++;
9937 break;
9938 case GONE:
9939 if (numFlags > 0) {
9940 output += " ";
9941 }
9942 output += "GONE";
9943 // USELESS HERE numFlags++;
9944 break;
9945 default:
9946 break;
9947 }
9948 return output;
9949 }
9950
9951 /**
9952 * Build a human readable string representation of the specified private
9953 * view flags.
9954 *
9955 * @param privateFlags the private view flags to convert to a string
9956 * @return a String representing the supplied flags
9957 */
9958 private static String printPrivateFlags(int privateFlags) {
9959 String output = "";
9960 int numFlags = 0;
9961
9962 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
9963 output += "WANTS_FOCUS";
9964 numFlags++;
9965 }
9966
9967 if ((privateFlags & FOCUSED) == FOCUSED) {
9968 if (numFlags > 0) {
9969 output += " ";
9970 }
9971 output += "FOCUSED";
9972 numFlags++;
9973 }
9974
9975 if ((privateFlags & SELECTED) == SELECTED) {
9976 if (numFlags > 0) {
9977 output += " ";
9978 }
9979 output += "SELECTED";
9980 numFlags++;
9981 }
9982
9983 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
9984 if (numFlags > 0) {
9985 output += " ";
9986 }
9987 output += "IS_ROOT_NAMESPACE";
9988 numFlags++;
9989 }
9990
9991 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
9992 if (numFlags > 0) {
9993 output += " ";
9994 }
9995 output += "HAS_BOUNDS";
9996 numFlags++;
9997 }
9998
9999 if ((privateFlags & DRAWN) == DRAWN) {
10000 if (numFlags > 0) {
10001 output += " ";
10002 }
10003 output += "DRAWN";
10004 // USELESS HERE numFlags++;
10005 }
10006 return output;
10007 }
10008
10009 /**
10010 * <p>Indicates whether or not this view's layout will be requested during
10011 * the next hierarchy layout pass.</p>
10012 *
10013 * @return true if the layout will be forced during next layout pass
10014 */
10015 public boolean isLayoutRequested() {
10016 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
10017 }
10018
10019 /**
Cibu Johny86666632010-02-22 13:01:02 -080010020 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
10021 * layout attribute and/or the inherited value from the parent.</p>
10022 *
10023 * @return true if the layout is right-to-left.
10024 */
10025 public boolean isLayoutRtl() {
10026 return (mPrivateFlags2 & RESOLVED_LAYOUT_RTL) == RESOLVED_LAYOUT_RTL;
10027 }
10028
10029 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010030 * Assign a size and position to a view and all of its
10031 * descendants
10032 *
10033 * <p>This is the second phase of the layout mechanism.
10034 * (The first is measuring). In this phase, each parent calls
10035 * layout on all of its children to position them.
10036 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080010037 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010038 *
Chet Haase9c087442011-01-12 16:20:16 -080010039 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010040 * Derived classes with children should override
10041 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080010042 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010043 *
10044 * @param l Left position, relative to parent
10045 * @param t Top position, relative to parent
10046 * @param r Right position, relative to parent
10047 * @param b Bottom position, relative to parent
10048 */
Romain Guy5429e1d2010-09-07 12:38:00 -070010049 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080010050 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070010051 int oldL = mLeft;
10052 int oldT = mTop;
10053 int oldB = mBottom;
10054 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010055 boolean changed = setFrame(l, t, r, b);
10056 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
10057 if (ViewDebug.TRACE_HIERARCHY) {
10058 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
10059 }
10060
10061 onLayout(changed, l, t, r, b);
10062 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070010063
10064 if (mOnLayoutChangeListeners != null) {
10065 ArrayList<OnLayoutChangeListener> listenersCopy =
10066 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
10067 int numListeners = listenersCopy.size();
10068 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070010069 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070010070 }
10071 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010072 }
10073 mPrivateFlags &= ~FORCE_LAYOUT;
10074 }
10075
10076 /**
10077 * Called from layout when this view should
10078 * assign a size and position to each of its children.
10079 *
10080 * Derived classes with children should override
10081 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070010082 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010083 * @param changed This is a new size or position for this view
10084 * @param left Left position, relative to parent
10085 * @param top Top position, relative to parent
10086 * @param right Right position, relative to parent
10087 * @param bottom Bottom position, relative to parent
10088 */
10089 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
10090 }
10091
10092 /**
10093 * Assign a size and position to this view.
10094 *
10095 * This is called from layout.
10096 *
10097 * @param left Left position, relative to parent
10098 * @param top Top position, relative to parent
10099 * @param right Right position, relative to parent
10100 * @param bottom Bottom position, relative to parent
10101 * @return true if the new size and position are different than the
10102 * previous ones
10103 * {@hide}
10104 */
10105 protected boolean setFrame(int left, int top, int right, int bottom) {
10106 boolean changed = false;
10107
10108 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070010109 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010110 + right + "," + bottom + ")");
10111 }
10112
10113 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
10114 changed = true;
10115
10116 // Remember our drawn bit
10117 int drawn = mPrivateFlags & DRAWN;
10118
10119 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -080010120 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010121
10122
10123 int oldWidth = mRight - mLeft;
10124 int oldHeight = mBottom - mTop;
10125
10126 mLeft = left;
10127 mTop = top;
10128 mRight = right;
10129 mBottom = bottom;
10130
10131 mPrivateFlags |= HAS_BOUNDS;
10132
10133 int newWidth = right - left;
10134 int newHeight = bottom - top;
10135
10136 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010137 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
10138 // A change in dimension means an auto-centered pivot point changes, too
10139 mMatrixDirty = true;
10140 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010141 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
10142 }
10143
10144 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
10145 // If we are visible, force the DRAWN bit to on so that
10146 // this invalidate will go through (at least to our parent).
10147 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010148 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010149 // the DRAWN bit.
10150 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010151 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -080010152 // parent display list may need to be recreated based on a change in the bounds
10153 // of any child
10154 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010155 }
10156
10157 // Reset drawn bit to original value (invalidate turns it off)
10158 mPrivateFlags |= drawn;
10159
10160 mBackgroundSizeChanged = true;
10161 }
10162 return changed;
10163 }
10164
10165 /**
10166 * Finalize inflating a view from XML. This is called as the last phase
10167 * of inflation, after all child views have been added.
10168 *
10169 * <p>Even if the subclass overrides onFinishInflate, they should always be
10170 * sure to call the super method, so that we get called.
10171 */
10172 protected void onFinishInflate() {
10173 }
10174
10175 /**
10176 * Returns the resources associated with this view.
10177 *
10178 * @return Resources object.
10179 */
10180 public Resources getResources() {
10181 return mResources;
10182 }
10183
10184 /**
10185 * Invalidates the specified Drawable.
10186 *
10187 * @param drawable the drawable to invalidate
10188 */
10189 public void invalidateDrawable(Drawable drawable) {
10190 if (verifyDrawable(drawable)) {
10191 final Rect dirty = drawable.getBounds();
10192 final int scrollX = mScrollX;
10193 final int scrollY = mScrollY;
10194
10195 invalidate(dirty.left + scrollX, dirty.top + scrollY,
10196 dirty.right + scrollX, dirty.bottom + scrollY);
10197 }
10198 }
10199
10200 /**
10201 * Schedules an action on a drawable to occur at a specified time.
10202 *
10203 * @param who the recipient of the action
10204 * @param what the action to run on the drawable
10205 * @param when the time at which the action must occur. Uses the
10206 * {@link SystemClock#uptimeMillis} timebase.
10207 */
10208 public void scheduleDrawable(Drawable who, Runnable what, long when) {
10209 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10210 mAttachInfo.mHandler.postAtTime(what, who, when);
10211 }
10212 }
10213
10214 /**
10215 * Cancels a scheduled action on a drawable.
10216 *
10217 * @param who the recipient of the action
10218 * @param what the action to cancel
10219 */
10220 public void unscheduleDrawable(Drawable who, Runnable what) {
10221 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10222 mAttachInfo.mHandler.removeCallbacks(what, who);
10223 }
10224 }
10225
10226 /**
10227 * Unschedule any events associated with the given Drawable. This can be
10228 * used when selecting a new Drawable into a view, so that the previous
10229 * one is completely unscheduled.
10230 *
10231 * @param who The Drawable to unschedule.
10232 *
10233 * @see #drawableStateChanged
10234 */
10235 public void unscheduleDrawable(Drawable who) {
10236 if (mAttachInfo != null) {
10237 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
10238 }
10239 }
10240
10241 /**
10242 * If your view subclass is displaying its own Drawable objects, it should
10243 * override this function and return true for any Drawable it is
10244 * displaying. This allows animations for those drawables to be
10245 * scheduled.
10246 *
10247 * <p>Be sure to call through to the super class when overriding this
10248 * function.
10249 *
10250 * @param who The Drawable to verify. Return true if it is one you are
10251 * displaying, else return the result of calling through to the
10252 * super class.
10253 *
10254 * @return boolean If true than the Drawable is being displayed in the
10255 * view; else false and it is not allowed to animate.
10256 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010257 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
10258 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010259 */
10260 protected boolean verifyDrawable(Drawable who) {
10261 return who == mBGDrawable;
10262 }
10263
10264 /**
10265 * This function is called whenever the state of the view changes in such
10266 * a way that it impacts the state of drawables being shown.
10267 *
10268 * <p>Be sure to call through to the superclass when overriding this
10269 * function.
10270 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010271 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010272 */
10273 protected void drawableStateChanged() {
10274 Drawable d = mBGDrawable;
10275 if (d != null && d.isStateful()) {
10276 d.setState(getDrawableState());
10277 }
10278 }
10279
10280 /**
10281 * Call this to force a view to update its drawable state. This will cause
10282 * drawableStateChanged to be called on this view. Views that are interested
10283 * in the new state should call getDrawableState.
10284 *
10285 * @see #drawableStateChanged
10286 * @see #getDrawableState
10287 */
10288 public void refreshDrawableState() {
10289 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
10290 drawableStateChanged();
10291
10292 ViewParent parent = mParent;
10293 if (parent != null) {
10294 parent.childDrawableStateChanged(this);
10295 }
10296 }
10297
10298 /**
10299 * Return an array of resource IDs of the drawable states representing the
10300 * current state of the view.
10301 *
10302 * @return The current drawable state
10303 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010304 * @see Drawable#setState(int[])
10305 * @see #drawableStateChanged()
10306 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010307 */
10308 public final int[] getDrawableState() {
10309 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
10310 return mDrawableState;
10311 } else {
10312 mDrawableState = onCreateDrawableState(0);
10313 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
10314 return mDrawableState;
10315 }
10316 }
10317
10318 /**
10319 * Generate the new {@link android.graphics.drawable.Drawable} state for
10320 * this view. This is called by the view
10321 * system when the cached Drawable state is determined to be invalid. To
10322 * retrieve the current state, you should use {@link #getDrawableState}.
10323 *
10324 * @param extraSpace if non-zero, this is the number of extra entries you
10325 * would like in the returned array in which you can place your own
10326 * states.
10327 *
10328 * @return Returns an array holding the current {@link Drawable} state of
10329 * the view.
10330 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010331 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010332 */
10333 protected int[] onCreateDrawableState(int extraSpace) {
10334 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
10335 mParent instanceof View) {
10336 return ((View) mParent).onCreateDrawableState(extraSpace);
10337 }
10338
10339 int[] drawableState;
10340
10341 int privateFlags = mPrivateFlags;
10342
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010343 int viewStateIndex = 0;
10344 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
10345 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
10346 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070010347 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010348 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
10349 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070010350 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
10351 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010352 // This is set if HW acceleration is requested, even if the current
10353 // process doesn't allow it. This is just to allow app preview
10354 // windows to better match their app.
10355 viewStateIndex |= VIEW_STATE_ACCELERATED;
10356 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070010357 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010358
Christopher Tate3d4bf172011-03-28 16:16:46 -070010359 final int privateFlags2 = mPrivateFlags2;
10360 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
10361 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
10362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010363 drawableState = VIEW_STATE_SETS[viewStateIndex];
10364
10365 //noinspection ConstantIfStatement
10366 if (false) {
10367 Log.i("View", "drawableStateIndex=" + viewStateIndex);
10368 Log.i("View", toString()
10369 + " pressed=" + ((privateFlags & PRESSED) != 0)
10370 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
10371 + " fo=" + hasFocus()
10372 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010373 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010374 + ": " + Arrays.toString(drawableState));
10375 }
10376
10377 if (extraSpace == 0) {
10378 return drawableState;
10379 }
10380
10381 final int[] fullState;
10382 if (drawableState != null) {
10383 fullState = new int[drawableState.length + extraSpace];
10384 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
10385 } else {
10386 fullState = new int[extraSpace];
10387 }
10388
10389 return fullState;
10390 }
10391
10392 /**
10393 * Merge your own state values in <var>additionalState</var> into the base
10394 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070010395 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010396 *
10397 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070010398 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010399 * own additional state values.
10400 *
10401 * @param additionalState The additional state values you would like
10402 * added to <var>baseState</var>; this array is not modified.
10403 *
10404 * @return As a convenience, the <var>baseState</var> array you originally
10405 * passed into the function is returned.
10406 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010407 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010408 */
10409 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
10410 final int N = baseState.length;
10411 int i = N - 1;
10412 while (i >= 0 && baseState[i] == 0) {
10413 i--;
10414 }
10415 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
10416 return baseState;
10417 }
10418
10419 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070010420 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
10421 * on all Drawable objects associated with this view.
10422 */
10423 public void jumpDrawablesToCurrentState() {
10424 if (mBGDrawable != null) {
10425 mBGDrawable.jumpToCurrentState();
10426 }
10427 }
10428
10429 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010430 * Sets the background color for this view.
10431 * @param color the color of the background
10432 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010433 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010434 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070010435 if (mBGDrawable instanceof ColorDrawable) {
10436 ((ColorDrawable) mBGDrawable).setColor(color);
10437 } else {
10438 setBackgroundDrawable(new ColorDrawable(color));
10439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010440 }
10441
10442 /**
10443 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070010444 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010445 * @param resid The identifier of the resource.
10446 * @attr ref android.R.styleable#View_background
10447 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010448 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010449 public void setBackgroundResource(int resid) {
10450 if (resid != 0 && resid == mBackgroundResource) {
10451 return;
10452 }
10453
10454 Drawable d= null;
10455 if (resid != 0) {
10456 d = mResources.getDrawable(resid);
10457 }
10458 setBackgroundDrawable(d);
10459
10460 mBackgroundResource = resid;
10461 }
10462
10463 /**
10464 * Set the background to a given Drawable, or remove the background. If the
10465 * background has padding, this View's padding is set to the background's
10466 * padding. However, when a background is removed, this View's padding isn't
10467 * touched. If setting the padding is desired, please use
10468 * {@link #setPadding(int, int, int, int)}.
10469 *
10470 * @param d The Drawable to use as the background, or null to remove the
10471 * background
10472 */
10473 public void setBackgroundDrawable(Drawable d) {
10474 boolean requestLayout = false;
10475
10476 mBackgroundResource = 0;
10477
10478 /*
10479 * Regardless of whether we're setting a new background or not, we want
10480 * to clear the previous drawable.
10481 */
10482 if (mBGDrawable != null) {
10483 mBGDrawable.setCallback(null);
10484 unscheduleDrawable(mBGDrawable);
10485 }
10486
10487 if (d != null) {
10488 Rect padding = sThreadLocal.get();
10489 if (padding == null) {
10490 padding = new Rect();
10491 sThreadLocal.set(padding);
10492 }
10493 if (d.getPadding(padding)) {
10494 setPadding(padding.left, padding.top, padding.right, padding.bottom);
10495 }
10496
10497 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
10498 // if it has a different minimum size, we should layout again
10499 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
10500 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
10501 requestLayout = true;
10502 }
10503
10504 d.setCallback(this);
10505 if (d.isStateful()) {
10506 d.setState(getDrawableState());
10507 }
10508 d.setVisible(getVisibility() == VISIBLE, false);
10509 mBGDrawable = d;
10510
10511 if ((mPrivateFlags & SKIP_DRAW) != 0) {
10512 mPrivateFlags &= ~SKIP_DRAW;
10513 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
10514 requestLayout = true;
10515 }
10516 } else {
10517 /* Remove the background */
10518 mBGDrawable = null;
10519
10520 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
10521 /*
10522 * This view ONLY drew the background before and we're removing
10523 * the background, so now it won't draw anything
10524 * (hence we SKIP_DRAW)
10525 */
10526 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
10527 mPrivateFlags |= SKIP_DRAW;
10528 }
10529
10530 /*
10531 * When the background is set, we try to apply its padding to this
10532 * View. When the background is removed, we don't touch this View's
10533 * padding. This is noted in the Javadocs. Hence, we don't need to
10534 * requestLayout(), the invalidate() below is sufficient.
10535 */
10536
10537 // The old background's minimum size could have affected this
10538 // View's layout, so let's requestLayout
10539 requestLayout = true;
10540 }
10541
Romain Guy8f1344f52009-05-15 16:03:59 -070010542 computeOpaqueFlags();
10543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010544 if (requestLayout) {
10545 requestLayout();
10546 }
10547
10548 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010549 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010550 }
10551
10552 /**
10553 * Gets the background drawable
10554 * @return The drawable used as the background for this view, if any.
10555 */
10556 public Drawable getBackground() {
10557 return mBGDrawable;
10558 }
10559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010560 /**
10561 * Sets the padding. The view may add on the space required to display
10562 * the scrollbars, depending on the style and visibility of the scrollbars.
10563 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
10564 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
10565 * from the values set in this call.
10566 *
10567 * @attr ref android.R.styleable#View_padding
10568 * @attr ref android.R.styleable#View_paddingBottom
10569 * @attr ref android.R.styleable#View_paddingLeft
10570 * @attr ref android.R.styleable#View_paddingRight
10571 * @attr ref android.R.styleable#View_paddingTop
10572 * @param left the left padding in pixels
10573 * @param top the top padding in pixels
10574 * @param right the right padding in pixels
10575 * @param bottom the bottom padding in pixels
10576 */
10577 public void setPadding(int left, int top, int right, int bottom) {
10578 boolean changed = false;
10579
Adam Powell20232d02010-12-08 21:08:53 -080010580 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010581 mUserPaddingRight = right;
10582 mUserPaddingBottom = bottom;
10583
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010584 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070010585
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010586 // Common case is there are no scroll bars.
10587 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010588 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080010589 // TODO Determine what to do with SCROLLBAR_POSITION_DEFAULT based on RTL settings.
10590 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010591 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080010592 switch (mVerticalScrollbarPosition) {
10593 case SCROLLBAR_POSITION_DEFAULT:
10594 case SCROLLBAR_POSITION_RIGHT:
10595 right += offset;
10596 break;
10597 case SCROLLBAR_POSITION_LEFT:
10598 left += offset;
10599 break;
10600 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010601 }
Adam Powell20232d02010-12-08 21:08:53 -080010602 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010603 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
10604 ? 0 : getHorizontalScrollbarHeight();
10605 }
10606 }
Romain Guy8506ab42009-06-11 17:35:47 -070010607
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010608 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010609 changed = true;
10610 mPaddingLeft = left;
10611 }
10612 if (mPaddingTop != top) {
10613 changed = true;
10614 mPaddingTop = top;
10615 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010616 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010617 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010618 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010619 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010620 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010621 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010622 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010623 }
10624
10625 if (changed) {
10626 requestLayout();
10627 }
10628 }
10629
10630 /**
10631 * Returns the top padding of this view.
10632 *
10633 * @return the top padding in pixels
10634 */
10635 public int getPaddingTop() {
10636 return mPaddingTop;
10637 }
10638
10639 /**
10640 * Returns the bottom padding of this view. If there are inset and enabled
10641 * scrollbars, this value may include the space required to display the
10642 * scrollbars as well.
10643 *
10644 * @return the bottom padding in pixels
10645 */
10646 public int getPaddingBottom() {
10647 return mPaddingBottom;
10648 }
10649
10650 /**
10651 * Returns the left padding of this view. If there are inset and enabled
10652 * scrollbars, this value may include the space required to display the
10653 * scrollbars as well.
10654 *
10655 * @return the left padding in pixels
10656 */
10657 public int getPaddingLeft() {
10658 return mPaddingLeft;
10659 }
10660
10661 /**
10662 * Returns the right padding of this view. If there are inset and enabled
10663 * scrollbars, this value may include the space required to display the
10664 * scrollbars as well.
10665 *
10666 * @return the right padding in pixels
10667 */
10668 public int getPaddingRight() {
10669 return mPaddingRight;
10670 }
10671
10672 /**
10673 * Changes the selection state of this view. A view can be selected or not.
10674 * Note that selection is not the same as focus. Views are typically
10675 * selected in the context of an AdapterView like ListView or GridView;
10676 * the selected view is the view that is highlighted.
10677 *
10678 * @param selected true if the view must be selected, false otherwise
10679 */
10680 public void setSelected(boolean selected) {
10681 if (((mPrivateFlags & SELECTED) != 0) != selected) {
10682 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070010683 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080010684 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010685 refreshDrawableState();
10686 dispatchSetSelected(selected);
10687 }
10688 }
10689
10690 /**
10691 * Dispatch setSelected to all of this View's children.
10692 *
10693 * @see #setSelected(boolean)
10694 *
10695 * @param selected The new selected state
10696 */
10697 protected void dispatchSetSelected(boolean selected) {
10698 }
10699
10700 /**
10701 * Indicates the selection state of this view.
10702 *
10703 * @return true if the view is selected, false otherwise
10704 */
10705 @ViewDebug.ExportedProperty
10706 public boolean isSelected() {
10707 return (mPrivateFlags & SELECTED) != 0;
10708 }
10709
10710 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010711 * Changes the activated state of this view. A view can be activated or not.
10712 * Note that activation is not the same as selection. Selection is
10713 * a transient property, representing the view (hierarchy) the user is
10714 * currently interacting with. Activation is a longer-term state that the
10715 * user can move views in and out of. For example, in a list view with
10716 * single or multiple selection enabled, the views in the current selection
10717 * set are activated. (Um, yeah, we are deeply sorry about the terminology
10718 * here.) The activated state is propagated down to children of the view it
10719 * is set on.
10720 *
10721 * @param activated true if the view must be activated, false otherwise
10722 */
10723 public void setActivated(boolean activated) {
10724 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
10725 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010726 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010727 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070010728 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010729 }
10730 }
10731
10732 /**
10733 * Dispatch setActivated to all of this View's children.
10734 *
10735 * @see #setActivated(boolean)
10736 *
10737 * @param activated The new activated state
10738 */
10739 protected void dispatchSetActivated(boolean activated) {
10740 }
10741
10742 /**
10743 * Indicates the activation state of this view.
10744 *
10745 * @return true if the view is activated, false otherwise
10746 */
10747 @ViewDebug.ExportedProperty
10748 public boolean isActivated() {
10749 return (mPrivateFlags & ACTIVATED) != 0;
10750 }
10751
10752 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010753 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
10754 * observer can be used to get notifications when global events, like
10755 * layout, happen.
10756 *
10757 * The returned ViewTreeObserver observer is not guaranteed to remain
10758 * valid for the lifetime of this View. If the caller of this method keeps
10759 * a long-lived reference to ViewTreeObserver, it should always check for
10760 * the return value of {@link ViewTreeObserver#isAlive()}.
10761 *
10762 * @return The ViewTreeObserver for this view's hierarchy.
10763 */
10764 public ViewTreeObserver getViewTreeObserver() {
10765 if (mAttachInfo != null) {
10766 return mAttachInfo.mTreeObserver;
10767 }
10768 if (mFloatingTreeObserver == null) {
10769 mFloatingTreeObserver = new ViewTreeObserver();
10770 }
10771 return mFloatingTreeObserver;
10772 }
10773
10774 /**
10775 * <p>Finds the topmost view in the current view hierarchy.</p>
10776 *
10777 * @return the topmost view containing this view
10778 */
10779 public View getRootView() {
10780 if (mAttachInfo != null) {
10781 final View v = mAttachInfo.mRootView;
10782 if (v != null) {
10783 return v;
10784 }
10785 }
Romain Guy8506ab42009-06-11 17:35:47 -070010786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010787 View parent = this;
10788
10789 while (parent.mParent != null && parent.mParent instanceof View) {
10790 parent = (View) parent.mParent;
10791 }
10792
10793 return parent;
10794 }
10795
10796 /**
10797 * <p>Computes the coordinates of this view on the screen. The argument
10798 * must be an array of two integers. After the method returns, the array
10799 * contains the x and y location in that order.</p>
10800 *
10801 * @param location an array of two integers in which to hold the coordinates
10802 */
10803 public void getLocationOnScreen(int[] location) {
10804 getLocationInWindow(location);
10805
10806 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070010807 if (info != null) {
10808 location[0] += info.mWindowLeft;
10809 location[1] += info.mWindowTop;
10810 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010811 }
10812
10813 /**
10814 * <p>Computes the coordinates of this view in its window. The argument
10815 * must be an array of two integers. After the method returns, the array
10816 * contains the x and y location in that order.</p>
10817 *
10818 * @param location an array of two integers in which to hold the coordinates
10819 */
10820 public void getLocationInWindow(int[] location) {
10821 if (location == null || location.length < 2) {
10822 throw new IllegalArgumentException("location must be an array of "
10823 + "two integers");
10824 }
10825
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010826 location[0] = mLeft + (int) (mTranslationX + 0.5f);
10827 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010828
10829 ViewParent viewParent = mParent;
10830 while (viewParent instanceof View) {
10831 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010832 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
10833 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010834 viewParent = view.mParent;
10835 }
Romain Guy8506ab42009-06-11 17:35:47 -070010836
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010837 if (viewParent instanceof ViewAncestor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010838 // *cough*
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010839 final ViewAncestor vr = (ViewAncestor)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010840 location[1] -= vr.mCurScrollY;
10841 }
10842 }
10843
10844 /**
10845 * {@hide}
10846 * @param id the id of the view to be found
10847 * @return the view of the specified id, null if cannot be found
10848 */
10849 protected View findViewTraversal(int id) {
10850 if (id == mID) {
10851 return this;
10852 }
10853 return null;
10854 }
10855
10856 /**
10857 * {@hide}
10858 * @param tag the tag of the view to be found
10859 * @return the view of specified tag, null if cannot be found
10860 */
10861 protected View findViewWithTagTraversal(Object tag) {
10862 if (tag != null && tag.equals(mTag)) {
10863 return this;
10864 }
10865 return null;
10866 }
10867
10868 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010869 * {@hide}
10870 * @param predicate The predicate to evaluate.
10871 * @return The first view that matches the predicate or null.
10872 */
10873 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
10874 if (predicate.apply(this)) {
10875 return this;
10876 }
10877 return null;
10878 }
10879
10880 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010881 * Look for a child view with the given id. If this view has the given
10882 * id, return this view.
10883 *
10884 * @param id The id to search for.
10885 * @return The view that has the given id in the hierarchy or null
10886 */
10887 public final View findViewById(int id) {
10888 if (id < 0) {
10889 return null;
10890 }
10891 return findViewTraversal(id);
10892 }
10893
10894 /**
10895 * Look for a child view with the given tag. If this view has the given
10896 * tag, return this view.
10897 *
10898 * @param tag The tag to search for, using "tag.equals(getTag())".
10899 * @return The View that has the given tag in the hierarchy or null
10900 */
10901 public final View findViewWithTag(Object tag) {
10902 if (tag == null) {
10903 return null;
10904 }
10905 return findViewWithTagTraversal(tag);
10906 }
10907
10908 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010909 * {@hide}
10910 * Look for a child view that matches the specified predicate.
10911 * If this view matches the predicate, return this view.
10912 *
10913 * @param predicate The predicate to evaluate.
10914 * @return The first view that matches the predicate or null.
10915 */
10916 public final View findViewByPredicate(Predicate<View> predicate) {
10917 return findViewByPredicateTraversal(predicate);
10918 }
10919
10920 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010921 * Sets the identifier for this view. The identifier does not have to be
10922 * unique in this view's hierarchy. The identifier should be a positive
10923 * number.
10924 *
10925 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070010926 * @see #getId()
10927 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010928 *
10929 * @param id a number used to identify the view
10930 *
10931 * @attr ref android.R.styleable#View_id
10932 */
10933 public void setId(int id) {
10934 mID = id;
10935 }
10936
10937 /**
10938 * {@hide}
10939 *
10940 * @param isRoot true if the view belongs to the root namespace, false
10941 * otherwise
10942 */
10943 public void setIsRootNamespace(boolean isRoot) {
10944 if (isRoot) {
10945 mPrivateFlags |= IS_ROOT_NAMESPACE;
10946 } else {
10947 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
10948 }
10949 }
10950
10951 /**
10952 * {@hide}
10953 *
10954 * @return true if the view belongs to the root namespace, false otherwise
10955 */
10956 public boolean isRootNamespace() {
10957 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
10958 }
10959
10960 /**
10961 * Returns this view's identifier.
10962 *
10963 * @return a positive integer used to identify the view or {@link #NO_ID}
10964 * if the view has no ID
10965 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010966 * @see #setId(int)
10967 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010968 * @attr ref android.R.styleable#View_id
10969 */
10970 @ViewDebug.CapturedViewProperty
10971 public int getId() {
10972 return mID;
10973 }
10974
10975 /**
10976 * Returns this view's tag.
10977 *
10978 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070010979 *
10980 * @see #setTag(Object)
10981 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010982 */
10983 @ViewDebug.ExportedProperty
10984 public Object getTag() {
10985 return mTag;
10986 }
10987
10988 /**
10989 * Sets the tag associated with this view. A tag can be used to mark
10990 * a view in its hierarchy and does not have to be unique within the
10991 * hierarchy. Tags can also be used to store data within a view without
10992 * resorting to another data structure.
10993 *
10994 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070010995 *
10996 * @see #getTag()
10997 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010998 */
10999 public void setTag(final Object tag) {
11000 mTag = tag;
11001 }
11002
11003 /**
Romain Guyd90a3312009-05-06 14:54:28 -070011004 * Returns the tag associated with this view and the specified key.
11005 *
11006 * @param key The key identifying the tag
11007 *
11008 * @return the Object stored in this view as a tag
11009 *
11010 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070011011 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070011012 */
11013 public Object getTag(int key) {
11014 SparseArray<Object> tags = null;
11015 synchronized (sTagsLock) {
11016 if (sTags != null) {
11017 tags = sTags.get(this);
11018 }
11019 }
11020
11021 if (tags != null) return tags.get(key);
11022 return null;
11023 }
11024
11025 /**
11026 * Sets a tag associated with this view and a key. A tag can be used
11027 * to mark a view in its hierarchy and does not have to be unique within
11028 * the hierarchy. Tags can also be used to store data within a view
11029 * without resorting to another data structure.
11030 *
11031 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070011032 * application to ensure it is unique (see the <a
11033 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
11034 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070011035 * the Android framework or not associated with any package will cause
11036 * an {@link IllegalArgumentException} to be thrown.
11037 *
11038 * @param key The key identifying the tag
11039 * @param tag An Object to tag the view with
11040 *
11041 * @throws IllegalArgumentException If they specified key is not valid
11042 *
11043 * @see #setTag(Object)
11044 * @see #getTag(int)
11045 */
11046 public void setTag(int key, final Object tag) {
11047 // If the package id is 0x00 or 0x01, it's either an undefined package
11048 // or a framework id
11049 if ((key >>> 24) < 2) {
11050 throw new IllegalArgumentException("The key must be an application-specific "
11051 + "resource id.");
11052 }
11053
11054 setTagInternal(this, key, tag);
11055 }
11056
11057 /**
11058 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
11059 * framework id.
11060 *
11061 * @hide
11062 */
11063 public void setTagInternal(int key, Object tag) {
11064 if ((key >>> 24) != 0x1) {
11065 throw new IllegalArgumentException("The key must be a framework-specific "
11066 + "resource id.");
11067 }
11068
Romain Guy8506ab42009-06-11 17:35:47 -070011069 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070011070 }
11071
11072 private static void setTagInternal(View view, int key, Object tag) {
11073 SparseArray<Object> tags = null;
11074 synchronized (sTagsLock) {
11075 if (sTags == null) {
11076 sTags = new WeakHashMap<View, SparseArray<Object>>();
11077 } else {
11078 tags = sTags.get(view);
11079 }
11080 }
11081
11082 if (tags == null) {
11083 tags = new SparseArray<Object>(2);
11084 synchronized (sTagsLock) {
11085 sTags.put(view, tags);
11086 }
11087 }
11088
11089 tags.put(key, tag);
11090 }
11091
11092 /**
Romain Guy13922e02009-05-12 17:56:14 -070011093 * @param consistency The type of consistency. See ViewDebug for more information.
11094 *
11095 * @hide
11096 */
11097 protected boolean dispatchConsistencyCheck(int consistency) {
11098 return onConsistencyCheck(consistency);
11099 }
11100
11101 /**
11102 * Method that subclasses should implement to check their consistency. The type of
11103 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070011104 *
Romain Guy13922e02009-05-12 17:56:14 -070011105 * @param consistency The type of consistency. See ViewDebug for more information.
11106 *
11107 * @throws IllegalStateException if the view is in an inconsistent state.
11108 *
11109 * @hide
11110 */
11111 protected boolean onConsistencyCheck(int consistency) {
11112 boolean result = true;
11113
11114 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
11115 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
11116
11117 if (checkLayout) {
11118 if (getParent() == null) {
11119 result = false;
11120 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11121 "View " + this + " does not have a parent.");
11122 }
11123
11124 if (mAttachInfo == null) {
11125 result = false;
11126 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11127 "View " + this + " is not attached to a window.");
11128 }
11129 }
11130
11131 if (checkDrawing) {
11132 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
11133 // from their draw() method
11134
11135 if ((mPrivateFlags & DRAWN) != DRAWN &&
11136 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
11137 result = false;
11138 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11139 "View " + this + " was invalidated but its drawing cache is valid.");
11140 }
11141 }
11142
11143 return result;
11144 }
11145
11146 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011147 * Prints information about this view in the log output, with the tag
11148 * {@link #VIEW_LOG_TAG}.
11149 *
11150 * @hide
11151 */
11152 public void debug() {
11153 debug(0);
11154 }
11155
11156 /**
11157 * Prints information about this view in the log output, with the tag
11158 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
11159 * indentation defined by the <code>depth</code>.
11160 *
11161 * @param depth the indentation level
11162 *
11163 * @hide
11164 */
11165 protected void debug(int depth) {
11166 String output = debugIndent(depth - 1);
11167
11168 output += "+ " + this;
11169 int id = getId();
11170 if (id != -1) {
11171 output += " (id=" + id + ")";
11172 }
11173 Object tag = getTag();
11174 if (tag != null) {
11175 output += " (tag=" + tag + ")";
11176 }
11177 Log.d(VIEW_LOG_TAG, output);
11178
11179 if ((mPrivateFlags & FOCUSED) != 0) {
11180 output = debugIndent(depth) + " FOCUSED";
11181 Log.d(VIEW_LOG_TAG, output);
11182 }
11183
11184 output = debugIndent(depth);
11185 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
11186 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
11187 + "} ";
11188 Log.d(VIEW_LOG_TAG, output);
11189
11190 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
11191 || mPaddingBottom != 0) {
11192 output = debugIndent(depth);
11193 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
11194 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
11195 Log.d(VIEW_LOG_TAG, output);
11196 }
11197
11198 output = debugIndent(depth);
11199 output += "mMeasureWidth=" + mMeasuredWidth +
11200 " mMeasureHeight=" + mMeasuredHeight;
11201 Log.d(VIEW_LOG_TAG, output);
11202
11203 output = debugIndent(depth);
11204 if (mLayoutParams == null) {
11205 output += "BAD! no layout params";
11206 } else {
11207 output = mLayoutParams.debug(output);
11208 }
11209 Log.d(VIEW_LOG_TAG, output);
11210
11211 output = debugIndent(depth);
11212 output += "flags={";
11213 output += View.printFlags(mViewFlags);
11214 output += "}";
11215 Log.d(VIEW_LOG_TAG, output);
11216
11217 output = debugIndent(depth);
11218 output += "privateFlags={";
11219 output += View.printPrivateFlags(mPrivateFlags);
11220 output += "}";
11221 Log.d(VIEW_LOG_TAG, output);
11222 }
11223
11224 /**
11225 * Creates an string of whitespaces used for indentation.
11226 *
11227 * @param depth the indentation level
11228 * @return a String containing (depth * 2 + 3) * 2 white spaces
11229 *
11230 * @hide
11231 */
11232 protected static String debugIndent(int depth) {
11233 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
11234 for (int i = 0; i < (depth * 2) + 3; i++) {
11235 spaces.append(' ').append(' ');
11236 }
11237 return spaces.toString();
11238 }
11239
11240 /**
11241 * <p>Return the offset of the widget's text baseline from the widget's top
11242 * boundary. If this widget does not support baseline alignment, this
11243 * method returns -1. </p>
11244 *
11245 * @return the offset of the baseline within the widget's bounds or -1
11246 * if baseline alignment is not supported
11247 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011248 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011249 public int getBaseline() {
11250 return -1;
11251 }
11252
11253 /**
11254 * Call this when something has changed which has invalidated the
11255 * layout of this view. This will schedule a layout pass of the view
11256 * tree.
11257 */
11258 public void requestLayout() {
11259 if (ViewDebug.TRACE_HIERARCHY) {
11260 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
11261 }
11262
11263 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011264 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011265
11266 if (mParent != null && !mParent.isLayoutRequested()) {
11267 mParent.requestLayout();
11268 }
11269 }
11270
11271 /**
11272 * Forces this view to be laid out during the next layout pass.
11273 * This method does not call requestLayout() or forceLayout()
11274 * on the parent.
11275 */
11276 public void forceLayout() {
11277 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011278 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011279 }
11280
11281 /**
11282 * <p>
11283 * This is called to find out how big a view should be. The parent
11284 * supplies constraint information in the width and height parameters.
11285 * </p>
11286 *
11287 * <p>
11288 * The actual mesurement work of a view is performed in
11289 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
11290 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
11291 * </p>
11292 *
11293 *
11294 * @param widthMeasureSpec Horizontal space requirements as imposed by the
11295 * parent
11296 * @param heightMeasureSpec Vertical space requirements as imposed by the
11297 * parent
11298 *
11299 * @see #onMeasure(int, int)
11300 */
11301 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
11302 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
11303 widthMeasureSpec != mOldWidthMeasureSpec ||
11304 heightMeasureSpec != mOldHeightMeasureSpec) {
11305
11306 // first clears the measured dimension flag
11307 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
11308
11309 if (ViewDebug.TRACE_HIERARCHY) {
11310 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
11311 }
11312
11313 // measure ourselves, this should set the measured dimension flag back
11314 onMeasure(widthMeasureSpec, heightMeasureSpec);
11315
11316 // flag not set, setMeasuredDimension() was not invoked, we raise
11317 // an exception to warn the developer
11318 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
11319 throw new IllegalStateException("onMeasure() did not set the"
11320 + " measured dimension by calling"
11321 + " setMeasuredDimension()");
11322 }
11323
11324 mPrivateFlags |= LAYOUT_REQUIRED;
11325 }
11326
11327 mOldWidthMeasureSpec = widthMeasureSpec;
11328 mOldHeightMeasureSpec = heightMeasureSpec;
11329 }
11330
11331 /**
11332 * <p>
11333 * Measure the view and its content to determine the measured width and the
11334 * measured height. This method is invoked by {@link #measure(int, int)} and
11335 * should be overriden by subclasses to provide accurate and efficient
11336 * measurement of their contents.
11337 * </p>
11338 *
11339 * <p>
11340 * <strong>CONTRACT:</strong> When overriding this method, you
11341 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
11342 * measured width and height of this view. Failure to do so will trigger an
11343 * <code>IllegalStateException</code>, thrown by
11344 * {@link #measure(int, int)}. Calling the superclass'
11345 * {@link #onMeasure(int, int)} is a valid use.
11346 * </p>
11347 *
11348 * <p>
11349 * The base class implementation of measure defaults to the background size,
11350 * unless a larger size is allowed by the MeasureSpec. Subclasses should
11351 * override {@link #onMeasure(int, int)} to provide better measurements of
11352 * their content.
11353 * </p>
11354 *
11355 * <p>
11356 * If this method is overridden, it is the subclass's responsibility to make
11357 * sure the measured height and width are at least the view's minimum height
11358 * and width ({@link #getSuggestedMinimumHeight()} and
11359 * {@link #getSuggestedMinimumWidth()}).
11360 * </p>
11361 *
11362 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
11363 * The requirements are encoded with
11364 * {@link android.view.View.MeasureSpec}.
11365 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
11366 * The requirements are encoded with
11367 * {@link android.view.View.MeasureSpec}.
11368 *
11369 * @see #getMeasuredWidth()
11370 * @see #getMeasuredHeight()
11371 * @see #setMeasuredDimension(int, int)
11372 * @see #getSuggestedMinimumHeight()
11373 * @see #getSuggestedMinimumWidth()
11374 * @see android.view.View.MeasureSpec#getMode(int)
11375 * @see android.view.View.MeasureSpec#getSize(int)
11376 */
11377 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
11378 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
11379 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
11380 }
11381
11382 /**
11383 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
11384 * measured width and measured height. Failing to do so will trigger an
11385 * exception at measurement time.</p>
11386 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080011387 * @param measuredWidth The measured width of this view. May be a complex
11388 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11389 * {@link #MEASURED_STATE_TOO_SMALL}.
11390 * @param measuredHeight The measured height of this view. May be a complex
11391 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11392 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011393 */
11394 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
11395 mMeasuredWidth = measuredWidth;
11396 mMeasuredHeight = measuredHeight;
11397
11398 mPrivateFlags |= MEASURED_DIMENSION_SET;
11399 }
11400
11401 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080011402 * Merge two states as returned by {@link #getMeasuredState()}.
11403 * @param curState The current state as returned from a view or the result
11404 * of combining multiple views.
11405 * @param newState The new view state to combine.
11406 * @return Returns a new integer reflecting the combination of the two
11407 * states.
11408 */
11409 public static int combineMeasuredStates(int curState, int newState) {
11410 return curState | newState;
11411 }
11412
11413 /**
11414 * Version of {@link #resolveSizeAndState(int, int, int)}
11415 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
11416 */
11417 public static int resolveSize(int size, int measureSpec) {
11418 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
11419 }
11420
11421 /**
11422 * Utility to reconcile a desired size and state, with constraints imposed
11423 * by a MeasureSpec. Will take the desired size, unless a different size
11424 * is imposed by the constraints. The returned value is a compound integer,
11425 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
11426 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
11427 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011428 *
11429 * @param size How big the view wants to be
11430 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080011431 * @return Size information bit mask as defined by
11432 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011433 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080011434 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011435 int result = size;
11436 int specMode = MeasureSpec.getMode(measureSpec);
11437 int specSize = MeasureSpec.getSize(measureSpec);
11438 switch (specMode) {
11439 case MeasureSpec.UNSPECIFIED:
11440 result = size;
11441 break;
11442 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080011443 if (specSize < size) {
11444 result = specSize | MEASURED_STATE_TOO_SMALL;
11445 } else {
11446 result = size;
11447 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011448 break;
11449 case MeasureSpec.EXACTLY:
11450 result = specSize;
11451 break;
11452 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080011453 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011454 }
11455
11456 /**
11457 * Utility to return a default size. Uses the supplied size if the
11458 * MeasureSpec imposed no contraints. Will get larger if allowed
11459 * by the MeasureSpec.
11460 *
11461 * @param size Default size for this view
11462 * @param measureSpec Constraints imposed by the parent
11463 * @return The size this view should be.
11464 */
11465 public static int getDefaultSize(int size, int measureSpec) {
11466 int result = size;
11467 int specMode = MeasureSpec.getMode(measureSpec);
11468 int specSize = MeasureSpec.getSize(measureSpec);
11469
11470 switch (specMode) {
11471 case MeasureSpec.UNSPECIFIED:
11472 result = size;
11473 break;
11474 case MeasureSpec.AT_MOST:
11475 case MeasureSpec.EXACTLY:
11476 result = specSize;
11477 break;
11478 }
11479 return result;
11480 }
11481
11482 /**
11483 * Returns the suggested minimum height that the view should use. This
11484 * returns the maximum of the view's minimum height
11485 * and the background's minimum height
11486 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
11487 * <p>
11488 * When being used in {@link #onMeasure(int, int)}, the caller should still
11489 * ensure the returned height is within the requirements of the parent.
11490 *
11491 * @return The suggested minimum height of the view.
11492 */
11493 protected int getSuggestedMinimumHeight() {
11494 int suggestedMinHeight = mMinHeight;
11495
11496 if (mBGDrawable != null) {
11497 final int bgMinHeight = mBGDrawable.getMinimumHeight();
11498 if (suggestedMinHeight < bgMinHeight) {
11499 suggestedMinHeight = bgMinHeight;
11500 }
11501 }
11502
11503 return suggestedMinHeight;
11504 }
11505
11506 /**
11507 * Returns the suggested minimum width that the view should use. This
11508 * returns the maximum of the view's minimum width)
11509 * and the background's minimum width
11510 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
11511 * <p>
11512 * When being used in {@link #onMeasure(int, int)}, the caller should still
11513 * ensure the returned width is within the requirements of the parent.
11514 *
11515 * @return The suggested minimum width of the view.
11516 */
11517 protected int getSuggestedMinimumWidth() {
11518 int suggestedMinWidth = mMinWidth;
11519
11520 if (mBGDrawable != null) {
11521 final int bgMinWidth = mBGDrawable.getMinimumWidth();
11522 if (suggestedMinWidth < bgMinWidth) {
11523 suggestedMinWidth = bgMinWidth;
11524 }
11525 }
11526
11527 return suggestedMinWidth;
11528 }
11529
11530 /**
11531 * Sets the minimum height of the view. It is not guaranteed the view will
11532 * be able to achieve this minimum height (for example, if its parent layout
11533 * constrains it with less available height).
11534 *
11535 * @param minHeight The minimum height the view will try to be.
11536 */
11537 public void setMinimumHeight(int minHeight) {
11538 mMinHeight = minHeight;
11539 }
11540
11541 /**
11542 * Sets the minimum width of the view. It is not guaranteed the view will
11543 * be able to achieve this minimum width (for example, if its parent layout
11544 * constrains it with less available width).
11545 *
11546 * @param minWidth The minimum width the view will try to be.
11547 */
11548 public void setMinimumWidth(int minWidth) {
11549 mMinWidth = minWidth;
11550 }
11551
11552 /**
11553 * Get the animation currently associated with this view.
11554 *
11555 * @return The animation that is currently playing or
11556 * scheduled to play for this view.
11557 */
11558 public Animation getAnimation() {
11559 return mCurrentAnimation;
11560 }
11561
11562 /**
11563 * Start the specified animation now.
11564 *
11565 * @param animation the animation to start now
11566 */
11567 public void startAnimation(Animation animation) {
11568 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
11569 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011570 invalidateParentCaches();
11571 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011572 }
11573
11574 /**
11575 * Cancels any animations for this view.
11576 */
11577 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080011578 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080011579 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080011580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011581 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011582 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011583 }
11584
11585 /**
11586 * Sets the next animation to play for this view.
11587 * If you want the animation to play immediately, use
11588 * startAnimation. This method provides allows fine-grained
11589 * control over the start time and invalidation, but you
11590 * must make sure that 1) the animation has a start time set, and
11591 * 2) the view will be invalidated when the animation is supposed to
11592 * start.
11593 *
11594 * @param animation The next animation, or null.
11595 */
11596 public void setAnimation(Animation animation) {
11597 mCurrentAnimation = animation;
11598 if (animation != null) {
11599 animation.reset();
11600 }
11601 }
11602
11603 /**
11604 * Invoked by a parent ViewGroup to notify the start of the animation
11605 * currently associated with this view. If you override this method,
11606 * always call super.onAnimationStart();
11607 *
11608 * @see #setAnimation(android.view.animation.Animation)
11609 * @see #getAnimation()
11610 */
11611 protected void onAnimationStart() {
11612 mPrivateFlags |= ANIMATION_STARTED;
11613 }
11614
11615 /**
11616 * Invoked by a parent ViewGroup to notify the end of the animation
11617 * currently associated with this view. If you override this method,
11618 * always call super.onAnimationEnd();
11619 *
11620 * @see #setAnimation(android.view.animation.Animation)
11621 * @see #getAnimation()
11622 */
11623 protected void onAnimationEnd() {
11624 mPrivateFlags &= ~ANIMATION_STARTED;
11625 }
11626
11627 /**
11628 * Invoked if there is a Transform that involves alpha. Subclass that can
11629 * draw themselves with the specified alpha should return true, and then
11630 * respect that alpha when their onDraw() is called. If this returns false
11631 * then the view may be redirected to draw into an offscreen buffer to
11632 * fulfill the request, which will look fine, but may be slower than if the
11633 * subclass handles it internally. The default implementation returns false.
11634 *
11635 * @param alpha The alpha (0..255) to apply to the view's drawing
11636 * @return true if the view can draw with the specified alpha.
11637 */
11638 protected boolean onSetAlpha(int alpha) {
11639 return false;
11640 }
11641
11642 /**
11643 * This is used by the RootView to perform an optimization when
11644 * the view hierarchy contains one or several SurfaceView.
11645 * SurfaceView is always considered transparent, but its children are not,
11646 * therefore all View objects remove themselves from the global transparent
11647 * region (passed as a parameter to this function).
11648 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011649 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011650 *
11651 * @return Returns true if the effective visibility of the view at this
11652 * point is opaque, regardless of the transparent region; returns false
11653 * if it is possible for underlying windows to be seen behind the view.
11654 *
11655 * {@hide}
11656 */
11657 public boolean gatherTransparentRegion(Region region) {
11658 final AttachInfo attachInfo = mAttachInfo;
11659 if (region != null && attachInfo != null) {
11660 final int pflags = mPrivateFlags;
11661 if ((pflags & SKIP_DRAW) == 0) {
11662 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
11663 // remove it from the transparent region.
11664 final int[] location = attachInfo.mTransparentLocation;
11665 getLocationInWindow(location);
11666 region.op(location[0], location[1], location[0] + mRight - mLeft,
11667 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
11668 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
11669 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
11670 // exists, so we remove the background drawable's non-transparent
11671 // parts from this transparent region.
11672 applyDrawableToTransparentRegion(mBGDrawable, region);
11673 }
11674 }
11675 return true;
11676 }
11677
11678 /**
11679 * Play a sound effect for this view.
11680 *
11681 * <p>The framework will play sound effects for some built in actions, such as
11682 * clicking, but you may wish to play these effects in your widget,
11683 * for instance, for internal navigation.
11684 *
11685 * <p>The sound effect will only be played if sound effects are enabled by the user, and
11686 * {@link #isSoundEffectsEnabled()} is true.
11687 *
11688 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
11689 */
11690 public void playSoundEffect(int soundConstant) {
11691 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
11692 return;
11693 }
11694 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
11695 }
11696
11697 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011698 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011699 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011700 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011701 *
11702 * <p>The framework will provide haptic feedback for some built in actions,
11703 * such as long presses, but you may wish to provide feedback for your
11704 * own widget.
11705 *
11706 * <p>The feedback will only be performed if
11707 * {@link #isHapticFeedbackEnabled()} is true.
11708 *
11709 * @param feedbackConstant One of the constants defined in
11710 * {@link HapticFeedbackConstants}
11711 */
11712 public boolean performHapticFeedback(int feedbackConstant) {
11713 return performHapticFeedback(feedbackConstant, 0);
11714 }
11715
11716 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011717 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011718 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011719 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011720 *
11721 * @param feedbackConstant One of the constants defined in
11722 * {@link HapticFeedbackConstants}
11723 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
11724 */
11725 public boolean performHapticFeedback(int feedbackConstant, int flags) {
11726 if (mAttachInfo == null) {
11727 return false;
11728 }
Romain Guyf607bdc2010-09-10 19:20:06 -070011729 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070011730 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011731 && !isHapticFeedbackEnabled()) {
11732 return false;
11733 }
Romain Guy812ccbe2010-06-01 14:07:24 -070011734 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
11735 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011736 }
11737
11738 /**
Joe Onorato664644d2011-01-23 17:53:23 -080011739 * Request that the visibility of the status bar be changed.
11740 */
11741 public void setSystemUiVisibility(int visibility) {
11742 if (visibility != mSystemUiVisibility) {
11743 mSystemUiVisibility = visibility;
11744 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11745 mParent.recomputeViewAttributes(this);
11746 }
11747 }
11748 }
11749
11750 /**
11751 * Returns the status bar visibility that this view has requested.
11752 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080011753 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080011754 return mSystemUiVisibility;
11755 }
11756
11757 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
11758 mOnSystemUiVisibilityChangeListener = l;
11759 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11760 mParent.recomputeViewAttributes(this);
11761 }
11762 }
11763
11764 /**
11765 */
11766 public void dispatchSystemUiVisibilityChanged(int visibility) {
11767 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080011768 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080011769 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080011770 }
11771 }
11772
11773 /**
Joe Malin32736f02011-01-19 16:14:20 -080011774 * Creates an image that the system displays during the drag and drop
11775 * operation. This is called a &quot;drag shadow&quot;. The default implementation
11776 * for a DragShadowBuilder based on a View returns an image that has exactly the same
11777 * appearance as the given View. The default also positions the center of the drag shadow
11778 * directly under the touch point. If no View is provided (the constructor with no parameters
11779 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
11780 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
11781 * default is an invisible drag shadow.
11782 * <p>
11783 * You are not required to use the View you provide to the constructor as the basis of the
11784 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
11785 * anything you want as the drag shadow.
11786 * </p>
11787 * <p>
11788 * You pass a DragShadowBuilder object to the system when you start the drag. The system
11789 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
11790 * size and position of the drag shadow. It uses this data to construct a
11791 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
11792 * so that your application can draw the shadow image in the Canvas.
11793 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011794 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011795 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070011796 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070011797
11798 /**
Joe Malin32736f02011-01-19 16:14:20 -080011799 * Constructs a shadow image builder based on a View. By default, the resulting drag
11800 * shadow will have the same appearance and dimensions as the View, with the touch point
11801 * over the center of the View.
11802 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070011803 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011804 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070011805 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070011806 }
11807
Christopher Tate17ed60c2011-01-18 12:50:26 -080011808 /**
11809 * Construct a shadow builder object with no associated View. This
11810 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
11811 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
11812 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080011813 * reference to any View object. If they are not overridden, then the result is an
11814 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080011815 */
11816 public DragShadowBuilder() {
11817 mView = new WeakReference<View>(null);
11818 }
11819
11820 /**
11821 * Returns the View object that had been passed to the
11822 * {@link #View.DragShadowBuilder(View)}
11823 * constructor. If that View parameter was {@code null} or if the
11824 * {@link #View.DragShadowBuilder()}
11825 * constructor was used to instantiate the builder object, this method will return
11826 * null.
11827 *
11828 * @return The View object associate with this builder object.
11829 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070011830 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070011831 final public View getView() {
11832 return mView.get();
11833 }
11834
Christopher Tate2c095f32010-10-04 14:13:40 -070011835 /**
Joe Malin32736f02011-01-19 16:14:20 -080011836 * Provides the metrics for the shadow image. These include the dimensions of
11837 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070011838 * be centered under the touch location while dragging.
11839 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011840 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080011841 * same as the dimensions of the View itself and centers the shadow under
11842 * the touch point.
11843 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011844 *
Joe Malin32736f02011-01-19 16:14:20 -080011845 * @param shadowSize A {@link android.graphics.Point} containing the width and height
11846 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
11847 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
11848 * image.
11849 *
11850 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
11851 * shadow image that should be underneath the touch point during the drag and drop
11852 * operation. Your application must set {@link android.graphics.Point#x} to the
11853 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070011854 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011855 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070011856 final View view = mView.get();
11857 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011858 shadowSize.set(view.getWidth(), view.getHeight());
11859 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070011860 } else {
11861 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
11862 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011863 }
11864
11865 /**
Joe Malin32736f02011-01-19 16:14:20 -080011866 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
11867 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011868 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070011869 *
Joe Malin32736f02011-01-19 16:14:20 -080011870 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070011871 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011872 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070011873 final View view = mView.get();
11874 if (view != null) {
11875 view.draw(canvas);
11876 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011877 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070011878 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011879 }
11880 }
11881
11882 /**
Joe Malin32736f02011-01-19 16:14:20 -080011883 * Starts a drag and drop operation. When your application calls this method, it passes a
11884 * {@link android.view.View.DragShadowBuilder} object to the system. The
11885 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
11886 * to get metrics for the drag shadow, and then calls the object's
11887 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
11888 * <p>
11889 * Once the system has the drag shadow, it begins the drag and drop operation by sending
11890 * drag events to all the View objects in your application that are currently visible. It does
11891 * this either by calling the View object's drag listener (an implementation of
11892 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
11893 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
11894 * Both are passed a {@link android.view.DragEvent} object that has a
11895 * {@link android.view.DragEvent#getAction()} value of
11896 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
11897 * </p>
11898 * <p>
11899 * Your application can invoke startDrag() on any attached View object. The View object does not
11900 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
11901 * be related to the View the user selected for dragging.
11902 * </p>
11903 * @param data A {@link android.content.ClipData} object pointing to the data to be
11904 * transferred by the drag and drop operation.
11905 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
11906 * drag shadow.
11907 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
11908 * drop operation. This Object is put into every DragEvent object sent by the system during the
11909 * current drag.
11910 * <p>
11911 * myLocalState is a lightweight mechanism for the sending information from the dragged View
11912 * to the target Views. For example, it can contain flags that differentiate between a
11913 * a copy operation and a move operation.
11914 * </p>
11915 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
11916 * so the parameter should be set to 0.
11917 * @return {@code true} if the method completes successfully, or
11918 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
11919 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070011920 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011921 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011922 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070011923 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011924 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070011925 }
11926 boolean okay = false;
11927
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011928 Point shadowSize = new Point();
11929 Point shadowTouchPoint = new Point();
11930 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070011931
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011932 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
11933 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
11934 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070011935 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011936
Chris Tatea32dcf72010-10-14 12:13:50 -070011937 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011938 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
11939 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070011940 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011941 Surface surface = new Surface();
11942 try {
11943 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011944 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070011945 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070011946 + " surface=" + surface);
11947 if (token != null) {
11948 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070011949 try {
Chris Tate6b391282010-10-14 15:48:59 -070011950 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011951 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070011952 } finally {
11953 surface.unlockCanvasAndPost(canvas);
11954 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011955
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011956 final ViewAncestor root = getViewAncestor();
Christopher Tate407b4e92010-11-30 17:14:08 -080011957
11958 // Cache the local state object for delivery with DragEvents
11959 root.setLocalDragState(myLocalState);
11960
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011961 // repurpose 'shadowSize' for the last touch point
11962 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070011963
Christopher Tatea53146c2010-09-07 11:57:52 -070011964 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011965 shadowSize.x, shadowSize.y,
11966 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070011967 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070011968 }
11969 } catch (Exception e) {
11970 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
11971 surface.destroy();
11972 }
11973
11974 return okay;
11975 }
11976
Christopher Tatea53146c2010-09-07 11:57:52 -070011977 /**
Joe Malin32736f02011-01-19 16:14:20 -080011978 * Handles drag events sent by the system following a call to
11979 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
11980 *<p>
11981 * When the system calls this method, it passes a
11982 * {@link android.view.DragEvent} object. A call to
11983 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
11984 * in DragEvent. The method uses these to determine what is happening in the drag and drop
11985 * operation.
11986 * @param event The {@link android.view.DragEvent} sent by the system.
11987 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
11988 * in DragEvent, indicating the type of drag event represented by this object.
11989 * @return {@code true} if the method was successful, otherwise {@code false}.
11990 * <p>
11991 * The method should return {@code true} in response to an action type of
11992 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
11993 * operation.
11994 * </p>
11995 * <p>
11996 * The method should also return {@code true} in response to an action type of
11997 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
11998 * {@code false} if it didn't.
11999 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012000 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070012001 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070012002 return false;
12003 }
12004
12005 /**
Joe Malin32736f02011-01-19 16:14:20 -080012006 * Detects if this View is enabled and has a drag event listener.
12007 * If both are true, then it calls the drag event listener with the
12008 * {@link android.view.DragEvent} it received. If the drag event listener returns
12009 * {@code true}, then dispatchDragEvent() returns {@code true}.
12010 * <p>
12011 * For all other cases, the method calls the
12012 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
12013 * method and returns its result.
12014 * </p>
12015 * <p>
12016 * This ensures that a drag event is always consumed, even if the View does not have a drag
12017 * event listener. However, if the View has a listener and the listener returns true, then
12018 * onDragEvent() is not called.
12019 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012020 */
12021 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080012022 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070012023 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
12024 && mOnDragListener.onDrag(this, event)) {
12025 return true;
12026 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012027 return onDragEvent(event);
12028 }
12029
Christopher Tate3d4bf172011-03-28 16:16:46 -070012030 boolean canAcceptDrag() {
12031 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
12032 }
12033
Christopher Tatea53146c2010-09-07 11:57:52 -070012034 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070012035 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
12036 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070012037 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070012038 */
12039 public void onCloseSystemDialogs(String reason) {
12040 }
Joe Malin32736f02011-01-19 16:14:20 -080012041
Dianne Hackbornffa42482009-09-23 22:20:11 -070012042 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012043 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070012044 * update a Region being computed for
12045 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012046 * that any non-transparent parts of the Drawable are removed from the
12047 * given transparent region.
12048 *
12049 * @param dr The Drawable whose transparency is to be applied to the region.
12050 * @param region A Region holding the current transparency information,
12051 * where any parts of the region that are set are considered to be
12052 * transparent. On return, this region will be modified to have the
12053 * transparency information reduced by the corresponding parts of the
12054 * Drawable that are not transparent.
12055 * {@hide}
12056 */
12057 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
12058 if (DBG) {
12059 Log.i("View", "Getting transparent region for: " + this);
12060 }
12061 final Region r = dr.getTransparentRegion();
12062 final Rect db = dr.getBounds();
12063 final AttachInfo attachInfo = mAttachInfo;
12064 if (r != null && attachInfo != null) {
12065 final int w = getRight()-getLeft();
12066 final int h = getBottom()-getTop();
12067 if (db.left > 0) {
12068 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
12069 r.op(0, 0, db.left, h, Region.Op.UNION);
12070 }
12071 if (db.right < w) {
12072 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
12073 r.op(db.right, 0, w, h, Region.Op.UNION);
12074 }
12075 if (db.top > 0) {
12076 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
12077 r.op(0, 0, w, db.top, Region.Op.UNION);
12078 }
12079 if (db.bottom < h) {
12080 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
12081 r.op(0, db.bottom, w, h, Region.Op.UNION);
12082 }
12083 final int[] location = attachInfo.mTransparentLocation;
12084 getLocationInWindow(location);
12085 r.translate(location[0], location[1]);
12086 region.op(r, Region.Op.INTERSECT);
12087 } else {
12088 region.op(db, Region.Op.DIFFERENCE);
12089 }
12090 }
12091
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012092 private void checkForLongClick(int delayOffset) {
12093 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
12094 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012095
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012096 if (mPendingCheckForLongPress == null) {
12097 mPendingCheckForLongPress = new CheckForLongPress();
12098 }
12099 mPendingCheckForLongPress.rememberWindowAttachCount();
12100 postDelayed(mPendingCheckForLongPress,
12101 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012102 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012103 }
12104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012105 /**
12106 * Inflate a view from an XML resource. This convenience method wraps the {@link
12107 * LayoutInflater} class, which provides a full range of options for view inflation.
12108 *
12109 * @param context The Context object for your activity or application.
12110 * @param resource The resource ID to inflate
12111 * @param root A view group that will be the parent. Used to properly inflate the
12112 * layout_* parameters.
12113 * @see LayoutInflater
12114 */
12115 public static View inflate(Context context, int resource, ViewGroup root) {
12116 LayoutInflater factory = LayoutInflater.from(context);
12117 return factory.inflate(resource, root);
12118 }
Romain Guy33e72ae2010-07-17 12:40:29 -070012119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012120 /**
Adam Powell637d3372010-08-25 14:37:03 -070012121 * Scroll the view with standard behavior for scrolling beyond the normal
12122 * content boundaries. Views that call this method should override
12123 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
12124 * results of an over-scroll operation.
12125 *
12126 * Views can use this method to handle any touch or fling-based scrolling.
12127 *
12128 * @param deltaX Change in X in pixels
12129 * @param deltaY Change in Y in pixels
12130 * @param scrollX Current X scroll value in pixels before applying deltaX
12131 * @param scrollY Current Y scroll value in pixels before applying deltaY
12132 * @param scrollRangeX Maximum content scroll range along the X axis
12133 * @param scrollRangeY Maximum content scroll range along the Y axis
12134 * @param maxOverScrollX Number of pixels to overscroll by in either direction
12135 * along the X axis.
12136 * @param maxOverScrollY Number of pixels to overscroll by in either direction
12137 * along the Y axis.
12138 * @param isTouchEvent true if this scroll operation is the result of a touch event.
12139 * @return true if scrolling was clamped to an over-scroll boundary along either
12140 * axis, false otherwise.
12141 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012142 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070012143 protected boolean overScrollBy(int deltaX, int deltaY,
12144 int scrollX, int scrollY,
12145 int scrollRangeX, int scrollRangeY,
12146 int maxOverScrollX, int maxOverScrollY,
12147 boolean isTouchEvent) {
12148 final int overScrollMode = mOverScrollMode;
12149 final boolean canScrollHorizontal =
12150 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
12151 final boolean canScrollVertical =
12152 computeVerticalScrollRange() > computeVerticalScrollExtent();
12153 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
12154 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
12155 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
12156 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
12157
12158 int newScrollX = scrollX + deltaX;
12159 if (!overScrollHorizontal) {
12160 maxOverScrollX = 0;
12161 }
12162
12163 int newScrollY = scrollY + deltaY;
12164 if (!overScrollVertical) {
12165 maxOverScrollY = 0;
12166 }
12167
12168 // Clamp values if at the limits and record
12169 final int left = -maxOverScrollX;
12170 final int right = maxOverScrollX + scrollRangeX;
12171 final int top = -maxOverScrollY;
12172 final int bottom = maxOverScrollY + scrollRangeY;
12173
12174 boolean clampedX = false;
12175 if (newScrollX > right) {
12176 newScrollX = right;
12177 clampedX = true;
12178 } else if (newScrollX < left) {
12179 newScrollX = left;
12180 clampedX = true;
12181 }
12182
12183 boolean clampedY = false;
12184 if (newScrollY > bottom) {
12185 newScrollY = bottom;
12186 clampedY = true;
12187 } else if (newScrollY < top) {
12188 newScrollY = top;
12189 clampedY = true;
12190 }
12191
12192 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
12193
12194 return clampedX || clampedY;
12195 }
12196
12197 /**
12198 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
12199 * respond to the results of an over-scroll operation.
12200 *
12201 * @param scrollX New X scroll value in pixels
12202 * @param scrollY New Y scroll value in pixels
12203 * @param clampedX True if scrollX was clamped to an over-scroll boundary
12204 * @param clampedY True if scrollY was clamped to an over-scroll boundary
12205 */
12206 protected void onOverScrolled(int scrollX, int scrollY,
12207 boolean clampedX, boolean clampedY) {
12208 // Intentionally empty.
12209 }
12210
12211 /**
12212 * Returns the over-scroll mode for this view. The result will be
12213 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12214 * (allow over-scrolling only if the view content is larger than the container),
12215 * or {@link #OVER_SCROLL_NEVER}.
12216 *
12217 * @return This view's over-scroll mode.
12218 */
12219 public int getOverScrollMode() {
12220 return mOverScrollMode;
12221 }
12222
12223 /**
12224 * Set the over-scroll mode for this view. Valid over-scroll modes are
12225 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12226 * (allow over-scrolling only if the view content is larger than the container),
12227 * or {@link #OVER_SCROLL_NEVER}.
12228 *
12229 * Setting the over-scroll mode of a view will have an effect only if the
12230 * view is capable of scrolling.
12231 *
12232 * @param overScrollMode The new over-scroll mode for this view.
12233 */
12234 public void setOverScrollMode(int overScrollMode) {
12235 if (overScrollMode != OVER_SCROLL_ALWAYS &&
12236 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
12237 overScrollMode != OVER_SCROLL_NEVER) {
12238 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
12239 }
12240 mOverScrollMode = overScrollMode;
12241 }
12242
12243 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012244 * Gets a scale factor that determines the distance the view should scroll
12245 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
12246 * @return The vertical scroll scale factor.
12247 * @hide
12248 */
12249 protected float getVerticalScrollFactor() {
12250 if (mVerticalScrollFactor == 0) {
12251 TypedValue outValue = new TypedValue();
12252 if (!mContext.getTheme().resolveAttribute(
12253 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
12254 throw new IllegalStateException(
12255 "Expected theme to define listPreferredItemHeight.");
12256 }
12257 mVerticalScrollFactor = outValue.getDimension(
12258 mContext.getResources().getDisplayMetrics());
12259 }
12260 return mVerticalScrollFactor;
12261 }
12262
12263 /**
12264 * Gets a scale factor that determines the distance the view should scroll
12265 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
12266 * @return The horizontal scroll scale factor.
12267 * @hide
12268 */
12269 protected float getHorizontalScrollFactor() {
12270 // TODO: Should use something else.
12271 return getVerticalScrollFactor();
12272 }
12273
12274 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012275 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
12276 * Each MeasureSpec represents a requirement for either the width or the height.
12277 * A MeasureSpec is comprised of a size and a mode. There are three possible
12278 * modes:
12279 * <dl>
12280 * <dt>UNSPECIFIED</dt>
12281 * <dd>
12282 * The parent has not imposed any constraint on the child. It can be whatever size
12283 * it wants.
12284 * </dd>
12285 *
12286 * <dt>EXACTLY</dt>
12287 * <dd>
12288 * The parent has determined an exact size for the child. The child is going to be
12289 * given those bounds regardless of how big it wants to be.
12290 * </dd>
12291 *
12292 * <dt>AT_MOST</dt>
12293 * <dd>
12294 * The child can be as large as it wants up to the specified size.
12295 * </dd>
12296 * </dl>
12297 *
12298 * MeasureSpecs are implemented as ints to reduce object allocation. This class
12299 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
12300 */
12301 public static class MeasureSpec {
12302 private static final int MODE_SHIFT = 30;
12303 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
12304
12305 /**
12306 * Measure specification mode: The parent has not imposed any constraint
12307 * on the child. It can be whatever size it wants.
12308 */
12309 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
12310
12311 /**
12312 * Measure specification mode: The parent has determined an exact size
12313 * for the child. The child is going to be given those bounds regardless
12314 * of how big it wants to be.
12315 */
12316 public static final int EXACTLY = 1 << MODE_SHIFT;
12317
12318 /**
12319 * Measure specification mode: The child can be as large as it wants up
12320 * to the specified size.
12321 */
12322 public static final int AT_MOST = 2 << MODE_SHIFT;
12323
12324 /**
12325 * Creates a measure specification based on the supplied size and mode.
12326 *
12327 * The mode must always be one of the following:
12328 * <ul>
12329 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
12330 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
12331 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
12332 * </ul>
12333 *
12334 * @param size the size of the measure specification
12335 * @param mode the mode of the measure specification
12336 * @return the measure specification based on size and mode
12337 */
12338 public static int makeMeasureSpec(int size, int mode) {
12339 return size + mode;
12340 }
12341
12342 /**
12343 * Extracts the mode from the supplied measure specification.
12344 *
12345 * @param measureSpec the measure specification to extract the mode from
12346 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
12347 * {@link android.view.View.MeasureSpec#AT_MOST} or
12348 * {@link android.view.View.MeasureSpec#EXACTLY}
12349 */
12350 public static int getMode(int measureSpec) {
12351 return (measureSpec & MODE_MASK);
12352 }
12353
12354 /**
12355 * Extracts the size from the supplied measure specification.
12356 *
12357 * @param measureSpec the measure specification to extract the size from
12358 * @return the size in pixels defined in the supplied measure specification
12359 */
12360 public static int getSize(int measureSpec) {
12361 return (measureSpec & ~MODE_MASK);
12362 }
12363
12364 /**
12365 * Returns a String representation of the specified measure
12366 * specification.
12367 *
12368 * @param measureSpec the measure specification to convert to a String
12369 * @return a String with the following format: "MeasureSpec: MODE SIZE"
12370 */
12371 public static String toString(int measureSpec) {
12372 int mode = getMode(measureSpec);
12373 int size = getSize(measureSpec);
12374
12375 StringBuilder sb = new StringBuilder("MeasureSpec: ");
12376
12377 if (mode == UNSPECIFIED)
12378 sb.append("UNSPECIFIED ");
12379 else if (mode == EXACTLY)
12380 sb.append("EXACTLY ");
12381 else if (mode == AT_MOST)
12382 sb.append("AT_MOST ");
12383 else
12384 sb.append(mode).append(" ");
12385
12386 sb.append(size);
12387 return sb.toString();
12388 }
12389 }
12390
12391 class CheckForLongPress implements Runnable {
12392
12393 private int mOriginalWindowAttachCount;
12394
12395 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070012396 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012397 && mOriginalWindowAttachCount == mWindowAttachCount) {
12398 if (performLongClick()) {
12399 mHasPerformedLongPress = true;
12400 }
12401 }
12402 }
12403
12404 public void rememberWindowAttachCount() {
12405 mOriginalWindowAttachCount = mWindowAttachCount;
12406 }
12407 }
Joe Malin32736f02011-01-19 16:14:20 -080012408
Adam Powelle14579b2009-12-16 18:39:52 -080012409 private final class CheckForTap implements Runnable {
12410 public void run() {
12411 mPrivateFlags &= ~PREPRESSED;
12412 mPrivateFlags |= PRESSED;
12413 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012414 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080012415 }
12416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012417
Adam Powella35d7682010-03-12 14:48:13 -080012418 private final class PerformClick implements Runnable {
12419 public void run() {
12420 performClick();
12421 }
12422 }
12423
Dianne Hackborn63042d62011-01-26 18:56:29 -080012424 /** @hide */
12425 public void hackTurnOffWindowResizeAnim(boolean off) {
12426 mAttachInfo.mTurnOffWindowResizeAnim = off;
12427 }
Joe Malin32736f02011-01-19 16:14:20 -080012428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012429 /**
Chet Haasea00f3862011-02-22 06:34:40 -080012430 * This method returns a ViewPropertyAnimator object, which can be used to animate
12431 * specific properties on this View.
12432 *
12433 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
12434 */
12435 public ViewPropertyAnimator animate() {
12436 if (mAnimator == null) {
12437 mAnimator = new ViewPropertyAnimator(this);
12438 }
12439 return mAnimator;
12440 }
12441
12442 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012443 * Interface definition for a callback to be invoked when a key event is
12444 * dispatched to this view. The callback will be invoked before the key
12445 * event is given to the view.
12446 */
12447 public interface OnKeyListener {
12448 /**
12449 * Called when a key is dispatched to a view. This allows listeners to
12450 * get a chance to respond before the target view.
12451 *
12452 * @param v The view the key has been dispatched to.
12453 * @param keyCode The code for the physical key that was pressed
12454 * @param event The KeyEvent object containing full information about
12455 * the event.
12456 * @return True if the listener has consumed the event, false otherwise.
12457 */
12458 boolean onKey(View v, int keyCode, KeyEvent event);
12459 }
12460
12461 /**
12462 * Interface definition for a callback to be invoked when a touch event is
12463 * dispatched to this view. The callback will be invoked before the touch
12464 * event is given to the view.
12465 */
12466 public interface OnTouchListener {
12467 /**
12468 * Called when a touch event is dispatched to a view. This allows listeners to
12469 * get a chance to respond before the target view.
12470 *
12471 * @param v The view the touch event has been dispatched to.
12472 * @param event The MotionEvent object containing full information about
12473 * the event.
12474 * @return True if the listener has consumed the event, false otherwise.
12475 */
12476 boolean onTouch(View v, MotionEvent event);
12477 }
12478
12479 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012480 * Interface definition for a callback to be invoked when a generic motion event is
12481 * dispatched to this view. The callback will be invoked before the generic motion
12482 * event is given to the view.
12483 */
12484 public interface OnGenericMotionListener {
12485 /**
12486 * Called when a generic motion event is dispatched to a view. This allows listeners to
12487 * get a chance to respond before the target view.
12488 *
12489 * @param v The view the generic motion event has been dispatched to.
12490 * @param event The MotionEvent object containing full information about
12491 * the event.
12492 * @return True if the listener has consumed the event, false otherwise.
12493 */
12494 boolean onGenericMotion(View v, MotionEvent event);
12495 }
12496
12497 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012498 * Interface definition for a callback to be invoked when a view has been clicked and held.
12499 */
12500 public interface OnLongClickListener {
12501 /**
12502 * Called when a view has been clicked and held.
12503 *
12504 * @param v The view that was clicked and held.
12505 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080012506 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012507 */
12508 boolean onLongClick(View v);
12509 }
12510
12511 /**
Chris Tate32affef2010-10-18 15:29:21 -070012512 * Interface definition for a callback to be invoked when a drag is being dispatched
12513 * to this view. The callback will be invoked before the hosting view's own
12514 * onDrag(event) method. If the listener wants to fall back to the hosting view's
12515 * onDrag(event) behavior, it should return 'false' from this callback.
12516 */
12517 public interface OnDragListener {
12518 /**
12519 * Called when a drag event is dispatched to a view. This allows listeners
12520 * to get a chance to override base View behavior.
12521 *
Joe Malin32736f02011-01-19 16:14:20 -080012522 * @param v The View that received the drag event.
12523 * @param event The {@link android.view.DragEvent} object for the drag event.
12524 * @return {@code true} if the drag event was handled successfully, or {@code false}
12525 * if the drag event was not handled. Note that {@code false} will trigger the View
12526 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070012527 */
12528 boolean onDrag(View v, DragEvent event);
12529 }
12530
12531 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012532 * Interface definition for a callback to be invoked when the focus state of
12533 * a view changed.
12534 */
12535 public interface OnFocusChangeListener {
12536 /**
12537 * Called when the focus state of a view has changed.
12538 *
12539 * @param v The view whose state has changed.
12540 * @param hasFocus The new focus state of v.
12541 */
12542 void onFocusChange(View v, boolean hasFocus);
12543 }
12544
12545 /**
12546 * Interface definition for a callback to be invoked when a view is clicked.
12547 */
12548 public interface OnClickListener {
12549 /**
12550 * Called when a view has been clicked.
12551 *
12552 * @param v The view that was clicked.
12553 */
12554 void onClick(View v);
12555 }
12556
12557 /**
12558 * Interface definition for a callback to be invoked when the context menu
12559 * for this view is being built.
12560 */
12561 public interface OnCreateContextMenuListener {
12562 /**
12563 * Called when the context menu for this view is being built. It is not
12564 * safe to hold onto the menu after this method returns.
12565 *
12566 * @param menu The context menu that is being built
12567 * @param v The view for which the context menu is being built
12568 * @param menuInfo Extra information about the item for which the
12569 * context menu should be shown. This information will vary
12570 * depending on the class of v.
12571 */
12572 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
12573 }
12574
Joe Onorato664644d2011-01-23 17:53:23 -080012575 /**
12576 * Interface definition for a callback to be invoked when the status bar changes
12577 * visibility.
12578 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012579 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080012580 */
12581 public interface OnSystemUiVisibilityChangeListener {
12582 /**
12583 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070012584 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080012585 *
12586 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
12587 */
12588 public void onSystemUiVisibilityChange(int visibility);
12589 }
12590
Adam Powell4afd62b2011-02-18 15:02:18 -080012591 /**
12592 * Interface definition for a callback to be invoked when this view is attached
12593 * or detached from its window.
12594 */
12595 public interface OnAttachStateChangeListener {
12596 /**
12597 * Called when the view is attached to a window.
12598 * @param v The view that was attached
12599 */
12600 public void onViewAttachedToWindow(View v);
12601 /**
12602 * Called when the view is detached from a window.
12603 * @param v The view that was detached
12604 */
12605 public void onViewDetachedFromWindow(View v);
12606 }
12607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012608 private final class UnsetPressedState implements Runnable {
12609 public void run() {
12610 setPressed(false);
12611 }
12612 }
12613
12614 /**
12615 * Base class for derived classes that want to save and restore their own
12616 * state in {@link android.view.View#onSaveInstanceState()}.
12617 */
12618 public static class BaseSavedState extends AbsSavedState {
12619 /**
12620 * Constructor used when reading from a parcel. Reads the state of the superclass.
12621 *
12622 * @param source
12623 */
12624 public BaseSavedState(Parcel source) {
12625 super(source);
12626 }
12627
12628 /**
12629 * Constructor called by derived classes when creating their SavedState objects
12630 *
12631 * @param superState The state of the superclass of this view
12632 */
12633 public BaseSavedState(Parcelable superState) {
12634 super(superState);
12635 }
12636
12637 public static final Parcelable.Creator<BaseSavedState> CREATOR =
12638 new Parcelable.Creator<BaseSavedState>() {
12639 public BaseSavedState createFromParcel(Parcel in) {
12640 return new BaseSavedState(in);
12641 }
12642
12643 public BaseSavedState[] newArray(int size) {
12644 return new BaseSavedState[size];
12645 }
12646 };
12647 }
12648
12649 /**
12650 * A set of information given to a view when it is attached to its parent
12651 * window.
12652 */
12653 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012654 interface Callbacks {
12655 void playSoundEffect(int effectId);
12656 boolean performHapticFeedback(int effectId, boolean always);
12657 }
12658
12659 /**
12660 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
12661 * to a Handler. This class contains the target (View) to invalidate and
12662 * the coordinates of the dirty rectangle.
12663 *
12664 * For performance purposes, this class also implements a pool of up to
12665 * POOL_LIMIT objects that get reused. This reduces memory allocations
12666 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012667 */
Romain Guyd928d682009-03-31 17:52:16 -070012668 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012669 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070012670 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
12671 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070012672 public InvalidateInfo newInstance() {
12673 return new InvalidateInfo();
12674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012675
Romain Guyd928d682009-03-31 17:52:16 -070012676 public void onAcquired(InvalidateInfo element) {
12677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012678
Romain Guyd928d682009-03-31 17:52:16 -070012679 public void onReleased(InvalidateInfo element) {
12680 }
12681 }, POOL_LIMIT)
12682 );
12683
12684 private InvalidateInfo mNext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012685
12686 View target;
12687
12688 int left;
12689 int top;
12690 int right;
12691 int bottom;
12692
Romain Guyd928d682009-03-31 17:52:16 -070012693 public void setNextPoolable(InvalidateInfo element) {
12694 mNext = element;
12695 }
12696
12697 public InvalidateInfo getNextPoolable() {
12698 return mNext;
12699 }
12700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012701 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070012702 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012703 }
12704
12705 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070012706 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012707 }
12708 }
12709
12710 final IWindowSession mSession;
12711
12712 final IWindow mWindow;
12713
12714 final IBinder mWindowToken;
12715
12716 final Callbacks mRootCallbacks;
12717
Chet Haasedaf98e92011-01-10 14:10:36 -080012718 Canvas mHardwareCanvas;
12719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012720 /**
12721 * The top view of the hierarchy.
12722 */
12723 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070012724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012725 IBinder mPanelParentWindowToken;
12726 Surface mSurface;
12727
Romain Guyb051e892010-09-28 19:09:36 -070012728 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012729 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070012730 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080012731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012732 /**
Romain Guy8506ab42009-06-11 17:35:47 -070012733 * Scale factor used by the compatibility mode
12734 */
12735 float mApplicationScale;
12736
12737 /**
12738 * Indicates whether the application is in compatibility mode
12739 */
12740 boolean mScalingRequired;
12741
12742 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012743 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080012744 */
12745 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080012746
Dianne Hackborn63042d62011-01-26 18:56:29 -080012747 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012748 * Left position of this view's window
12749 */
12750 int mWindowLeft;
12751
12752 /**
12753 * Top position of this view's window
12754 */
12755 int mWindowTop;
12756
12757 /**
Adam Powell26153a32010-11-08 15:22:27 -080012758 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070012759 */
Adam Powell26153a32010-11-08 15:22:27 -080012760 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070012761
12762 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012763 * For windows that are full-screen but using insets to layout inside
12764 * of the screen decorations, these are the current insets for the
12765 * content of the window.
12766 */
12767 final Rect mContentInsets = new Rect();
12768
12769 /**
12770 * For windows that are full-screen but using insets to layout inside
12771 * of the screen decorations, these are the current insets for the
12772 * actual visible parts of the window.
12773 */
12774 final Rect mVisibleInsets = new Rect();
12775
12776 /**
12777 * The internal insets given by this window. This value is
12778 * supplied by the client (through
12779 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
12780 * be given to the window manager when changed to be used in laying
12781 * out windows behind it.
12782 */
12783 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
12784 = new ViewTreeObserver.InternalInsetsInfo();
12785
12786 /**
12787 * All views in the window's hierarchy that serve as scroll containers,
12788 * used to determine if the window can be resized or must be panned
12789 * to adjust for a soft input area.
12790 */
12791 final ArrayList<View> mScrollContainers = new ArrayList<View>();
12792
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070012793 final KeyEvent.DispatcherState mKeyDispatchState
12794 = new KeyEvent.DispatcherState();
12795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012796 /**
12797 * Indicates whether the view's window currently has the focus.
12798 */
12799 boolean mHasWindowFocus;
12800
12801 /**
12802 * The current visibility of the window.
12803 */
12804 int mWindowVisibility;
12805
12806 /**
12807 * Indicates the time at which drawing started to occur.
12808 */
12809 long mDrawingTime;
12810
12811 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070012812 * Indicates whether or not ignoring the DIRTY_MASK flags.
12813 */
12814 boolean mIgnoreDirtyState;
12815
12816 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012817 * Indicates whether the view's window is currently in touch mode.
12818 */
12819 boolean mInTouchMode;
12820
12821 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012822 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012823 * the next time it performs a traversal
12824 */
12825 boolean mRecomputeGlobalAttributes;
12826
12827 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012828 * Set during a traveral if any views want to keep the screen on.
12829 */
12830 boolean mKeepScreenOn;
12831
12832 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012833 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
12834 */
12835 int mSystemUiVisibility;
12836
12837 /**
12838 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
12839 * attached.
12840 */
12841 boolean mHasSystemUiListeners;
12842
12843 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012844 * Set if the visibility of any views has changed.
12845 */
12846 boolean mViewVisibilityChanged;
12847
12848 /**
12849 * Set to true if a view has been scrolled.
12850 */
12851 boolean mViewScrollChanged;
12852
12853 /**
12854 * Global to the view hierarchy used as a temporary for dealing with
12855 * x/y points in the transparent region computations.
12856 */
12857 final int[] mTransparentLocation = new int[2];
12858
12859 /**
12860 * Global to the view hierarchy used as a temporary for dealing with
12861 * x/y points in the ViewGroup.invalidateChild implementation.
12862 */
12863 final int[] mInvalidateChildLocation = new int[2];
12864
Chet Haasec3aa3612010-06-17 08:50:37 -070012865
12866 /**
12867 * Global to the view hierarchy used as a temporary for dealing with
12868 * x/y location when view is transformed.
12869 */
12870 final float[] mTmpTransformLocation = new float[2];
12871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012872 /**
12873 * The view tree observer used to dispatch global events like
12874 * layout, pre-draw, touch mode change, etc.
12875 */
12876 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
12877
12878 /**
12879 * A Canvas used by the view hierarchy to perform bitmap caching.
12880 */
12881 Canvas mCanvas;
12882
12883 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012884 * A Handler supplied by a view's {@link android.view.ViewAncestor}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012885 * handler can be used to pump events in the UI events queue.
12886 */
12887 final Handler mHandler;
12888
12889 /**
12890 * Identifier for messages requesting the view to be invalidated.
12891 * Such messages should be sent to {@link #mHandler}.
12892 */
12893 static final int INVALIDATE_MSG = 0x1;
12894
12895 /**
12896 * Identifier for messages requesting the view to invalidate a region.
12897 * Such messages should be sent to {@link #mHandler}.
12898 */
12899 static final int INVALIDATE_RECT_MSG = 0x2;
12900
12901 /**
12902 * Temporary for use in computing invalidate rectangles while
12903 * calling up the hierarchy.
12904 */
12905 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070012906
12907 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070012908 * Temporary for use in computing hit areas with transformed views
12909 */
12910 final RectF mTmpTransformRect = new RectF();
12911
12912 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070012913 * Temporary list for use in collecting focusable descendents of a view.
12914 */
12915 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
12916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012917 /**
12918 * Creates a new set of attachment information with the specified
12919 * events handler and thread.
12920 *
12921 * @param handler the events handler the view must use
12922 */
12923 AttachInfo(IWindowSession session, IWindow window,
12924 Handler handler, Callbacks effectPlayer) {
12925 mSession = session;
12926 mWindow = window;
12927 mWindowToken = window.asBinder();
12928 mHandler = handler;
12929 mRootCallbacks = effectPlayer;
12930 }
12931 }
12932
12933 /**
12934 * <p>ScrollabilityCache holds various fields used by a View when scrolling
12935 * is supported. This avoids keeping too many unused fields in most
12936 * instances of View.</p>
12937 */
Mike Cleronf116bf82009-09-27 19:14:12 -070012938 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080012939
Mike Cleronf116bf82009-09-27 19:14:12 -070012940 /**
12941 * Scrollbars are not visible
12942 */
12943 public static final int OFF = 0;
12944
12945 /**
12946 * Scrollbars are visible
12947 */
12948 public static final int ON = 1;
12949
12950 /**
12951 * Scrollbars are fading away
12952 */
12953 public static final int FADING = 2;
12954
12955 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080012956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012957 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070012958 public int scrollBarDefaultDelayBeforeFade;
12959 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012960
12961 public int scrollBarSize;
12962 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070012963 public float[] interpolatorValues;
12964 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012965
12966 public final Paint paint;
12967 public final Matrix matrix;
12968 public Shader shader;
12969
Mike Cleronf116bf82009-09-27 19:14:12 -070012970 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
12971
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012972 private static final float[] OPAQUE = { 255 };
12973 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080012974
Mike Cleronf116bf82009-09-27 19:14:12 -070012975 /**
12976 * When fading should start. This time moves into the future every time
12977 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
12978 */
12979 public long fadeStartTime;
12980
12981
12982 /**
12983 * The current state of the scrollbars: ON, OFF, or FADING
12984 */
12985 public int state = OFF;
12986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012987 private int mLastColor;
12988
Mike Cleronf116bf82009-09-27 19:14:12 -070012989 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012990 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
12991 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070012992 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
12993 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012994
12995 paint = new Paint();
12996 matrix = new Matrix();
12997 // use use a height of 1, and then wack the matrix each time we
12998 // actually use it.
12999 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070013000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013001 paint.setShader(shader);
13002 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070013003 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013004 }
Romain Guy8506ab42009-06-11 17:35:47 -070013005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013006 public void setFadeColor(int color) {
13007 if (color != 0 && color != mLastColor) {
13008 mLastColor = color;
13009 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070013010
Romain Guye55e1a72009-08-27 10:42:26 -070013011 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
13012 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070013013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013014 paint.setShader(shader);
13015 // Restore the default transfer mode (src_over)
13016 paint.setXfermode(null);
13017 }
13018 }
Joe Malin32736f02011-01-19 16:14:20 -080013019
Mike Cleronf116bf82009-09-27 19:14:12 -070013020 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070013021 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070013022 if (now >= fadeStartTime) {
13023
13024 // the animation fades the scrollbars out by changing
13025 // the opacity (alpha) from fully opaque to fully
13026 // transparent
13027 int nextFrame = (int) now;
13028 int framesCount = 0;
13029
13030 Interpolator interpolator = scrollBarInterpolator;
13031
13032 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013033 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070013034
13035 // End transparent
13036 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013037 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070013038
13039 state = FADING;
13040
13041 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080013042 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070013043 }
13044 }
13045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013046 }
13047}