blob: 014f19f8293c4761fb065cd28bb0d6abc5bfcbb9 [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 */
Fabrice Di Meglio6a036402011-05-23 14:43:23 -0700634public class View implements Drawable.Callback2, 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
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700985 /*
986 * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct
987 * flag value.
988 * {@hide}
989 */
Cibu Johny7632cb92010-02-22 13:01:02 -0800990 private static final int[] HORIZONTAL_DIRECTION_FLAGS = { HORIZONTAL_DIRECTION_LTR,
991 HORIZONTAL_DIRECTION_RTL, HORIZONTAL_DIRECTION_INHERIT, HORIZONTAL_DIRECTION_LOCALE};
992
993 /**
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700994 * Default horizontalDirection.
995 * {@hide}
996 */
997 private static final int HORIZONTAL_DIRECTION_DEFAULT = HORIZONTAL_DIRECTION_INHERIT;
998
999 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001000 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1001 * should add all focusable Views regardless if they are focusable in touch mode.
1002 */
1003 public static final int FOCUSABLES_ALL = 0x00000000;
1004
1005 /**
1006 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1007 * should add only Views focusable in touch mode.
1008 */
1009 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1010
1011 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001012 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 * item.
1014 */
1015 public static final int FOCUS_BACKWARD = 0x00000001;
1016
1017 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001018 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 * item.
1020 */
1021 public static final int FOCUS_FORWARD = 0x00000002;
1022
1023 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001024 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 */
1026 public static final int FOCUS_LEFT = 0x00000011;
1027
1028 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001029 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 */
1031 public static final int FOCUS_UP = 0x00000021;
1032
1033 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001034 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 */
1036 public static final int FOCUS_RIGHT = 0x00000042;
1037
1038 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001039 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 */
1041 public static final int FOCUS_DOWN = 0x00000082;
1042
1043 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001044 * Bits of {@link #getMeasuredWidthAndState()} and
1045 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1046 */
1047 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1048
1049 /**
1050 * Bits of {@link #getMeasuredWidthAndState()} and
1051 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1052 */
1053 public static final int MEASURED_STATE_MASK = 0xff000000;
1054
1055 /**
1056 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1057 * for functions that combine both width and height into a single int,
1058 * such as {@link #getMeasuredState()} and the childState argument of
1059 * {@link #resolveSizeAndState(int, int, int)}.
1060 */
1061 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1062
1063 /**
1064 * Bit of {@link #getMeasuredWidthAndState()} and
1065 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1066 * is smaller that the space the view would like to have.
1067 */
1068 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1069
1070 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 * Base View state sets
1072 */
1073 // Singles
1074 /**
1075 * Indicates the view has no states set. States are used with
1076 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1077 * view depending on its state.
1078 *
1079 * @see android.graphics.drawable.Drawable
1080 * @see #getDrawableState()
1081 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001082 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 /**
1084 * Indicates the view is enabled. States are used with
1085 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1086 * view depending on its state.
1087 *
1088 * @see android.graphics.drawable.Drawable
1089 * @see #getDrawableState()
1090 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001091 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 /**
1093 * Indicates the view is focused. States are used with
1094 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1095 * view depending on its state.
1096 *
1097 * @see android.graphics.drawable.Drawable
1098 * @see #getDrawableState()
1099 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001100 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 /**
1102 * Indicates the view is selected. States are used with
1103 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1104 * view depending on its state.
1105 *
1106 * @see android.graphics.drawable.Drawable
1107 * @see #getDrawableState()
1108 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001109 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 /**
1111 * Indicates the view is pressed. States are used with
1112 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1113 * view depending on its state.
1114 *
1115 * @see android.graphics.drawable.Drawable
1116 * @see #getDrawableState()
1117 * @hide
1118 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001119 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 /**
1121 * Indicates the view's window has focus. States are used with
1122 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1123 * view depending on its state.
1124 *
1125 * @see android.graphics.drawable.Drawable
1126 * @see #getDrawableState()
1127 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001128 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 // Doubles
1130 /**
1131 * Indicates the view is enabled and has the focus.
1132 *
1133 * @see #ENABLED_STATE_SET
1134 * @see #FOCUSED_STATE_SET
1135 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001136 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 /**
1138 * Indicates the view is enabled and selected.
1139 *
1140 * @see #ENABLED_STATE_SET
1141 * @see #SELECTED_STATE_SET
1142 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001143 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 /**
1145 * Indicates the view is enabled and that its window has focus.
1146 *
1147 * @see #ENABLED_STATE_SET
1148 * @see #WINDOW_FOCUSED_STATE_SET
1149 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001150 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 /**
1152 * Indicates the view is focused and selected.
1153 *
1154 * @see #FOCUSED_STATE_SET
1155 * @see #SELECTED_STATE_SET
1156 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001157 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 /**
1159 * Indicates the view has the focus and that its window has the focus.
1160 *
1161 * @see #FOCUSED_STATE_SET
1162 * @see #WINDOW_FOCUSED_STATE_SET
1163 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001164 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 /**
1166 * Indicates the view is selected and that its window has the focus.
1167 *
1168 * @see #SELECTED_STATE_SET
1169 * @see #WINDOW_FOCUSED_STATE_SET
1170 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001171 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 // Triples
1173 /**
1174 * Indicates the view is enabled, focused and selected.
1175 *
1176 * @see #ENABLED_STATE_SET
1177 * @see #FOCUSED_STATE_SET
1178 * @see #SELECTED_STATE_SET
1179 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001180 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 /**
1182 * Indicates the view is enabled, focused and its window has the focus.
1183 *
1184 * @see #ENABLED_STATE_SET
1185 * @see #FOCUSED_STATE_SET
1186 * @see #WINDOW_FOCUSED_STATE_SET
1187 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001188 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 /**
1190 * Indicates the view is enabled, selected and its window has the focus.
1191 *
1192 * @see #ENABLED_STATE_SET
1193 * @see #SELECTED_STATE_SET
1194 * @see #WINDOW_FOCUSED_STATE_SET
1195 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001196 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 /**
1198 * Indicates the view is focused, selected and its window has the focus.
1199 *
1200 * @see #FOCUSED_STATE_SET
1201 * @see #SELECTED_STATE_SET
1202 * @see #WINDOW_FOCUSED_STATE_SET
1203 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001204 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 /**
1206 * Indicates the view is enabled, focused, selected and its window
1207 * has the focus.
1208 *
1209 * @see #ENABLED_STATE_SET
1210 * @see #FOCUSED_STATE_SET
1211 * @see #SELECTED_STATE_SET
1212 * @see #WINDOW_FOCUSED_STATE_SET
1213 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001214 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 /**
1216 * Indicates the view is pressed and its window has the focus.
1217 *
1218 * @see #PRESSED_STATE_SET
1219 * @see #WINDOW_FOCUSED_STATE_SET
1220 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001221 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 /**
1223 * Indicates the view is pressed and selected.
1224 *
1225 * @see #PRESSED_STATE_SET
1226 * @see #SELECTED_STATE_SET
1227 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001228 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 /**
1230 * Indicates the view is pressed, selected and its window has the focus.
1231 *
1232 * @see #PRESSED_STATE_SET
1233 * @see #SELECTED_STATE_SET
1234 * @see #WINDOW_FOCUSED_STATE_SET
1235 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001236 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 /**
1238 * Indicates the view is pressed and focused.
1239 *
1240 * @see #PRESSED_STATE_SET
1241 * @see #FOCUSED_STATE_SET
1242 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001243 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 /**
1245 * Indicates the view is pressed, focused and its window has the focus.
1246 *
1247 * @see #PRESSED_STATE_SET
1248 * @see #FOCUSED_STATE_SET
1249 * @see #WINDOW_FOCUSED_STATE_SET
1250 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001251 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 /**
1253 * Indicates the view is pressed, focused and selected.
1254 *
1255 * @see #PRESSED_STATE_SET
1256 * @see #SELECTED_STATE_SET
1257 * @see #FOCUSED_STATE_SET
1258 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001259 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 /**
1261 * Indicates the view is pressed, focused, selected and its window has the focus.
1262 *
1263 * @see #PRESSED_STATE_SET
1264 * @see #FOCUSED_STATE_SET
1265 * @see #SELECTED_STATE_SET
1266 * @see #WINDOW_FOCUSED_STATE_SET
1267 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001268 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 /**
1270 * Indicates the view is pressed and enabled.
1271 *
1272 * @see #PRESSED_STATE_SET
1273 * @see #ENABLED_STATE_SET
1274 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001275 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 /**
1277 * Indicates the view is pressed, enabled and its window has the focus.
1278 *
1279 * @see #PRESSED_STATE_SET
1280 * @see #ENABLED_STATE_SET
1281 * @see #WINDOW_FOCUSED_STATE_SET
1282 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001283 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 /**
1285 * Indicates the view is pressed, enabled and selected.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #ENABLED_STATE_SET
1289 * @see #SELECTED_STATE_SET
1290 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001291 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 /**
1293 * Indicates the view is pressed, enabled, selected and its window has the
1294 * focus.
1295 *
1296 * @see #PRESSED_STATE_SET
1297 * @see #ENABLED_STATE_SET
1298 * @see #SELECTED_STATE_SET
1299 * @see #WINDOW_FOCUSED_STATE_SET
1300 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001301 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 /**
1303 * Indicates the view is pressed, enabled and focused.
1304 *
1305 * @see #PRESSED_STATE_SET
1306 * @see #ENABLED_STATE_SET
1307 * @see #FOCUSED_STATE_SET
1308 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001309 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 /**
1311 * Indicates the view is pressed, enabled, focused and its window has the
1312 * focus.
1313 *
1314 * @see #PRESSED_STATE_SET
1315 * @see #ENABLED_STATE_SET
1316 * @see #FOCUSED_STATE_SET
1317 * @see #WINDOW_FOCUSED_STATE_SET
1318 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001319 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 /**
1321 * Indicates the view is pressed, enabled, focused and selected.
1322 *
1323 * @see #PRESSED_STATE_SET
1324 * @see #ENABLED_STATE_SET
1325 * @see #SELECTED_STATE_SET
1326 * @see #FOCUSED_STATE_SET
1327 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001328 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 /**
1330 * Indicates the view is pressed, enabled, focused, selected and its window
1331 * has the focus.
1332 *
1333 * @see #PRESSED_STATE_SET
1334 * @see #ENABLED_STATE_SET
1335 * @see #SELECTED_STATE_SET
1336 * @see #FOCUSED_STATE_SET
1337 * @see #WINDOW_FOCUSED_STATE_SET
1338 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001339 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340
1341 /**
1342 * The order here is very important to {@link #getDrawableState()}
1343 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001344 private static final int[][] VIEW_STATE_SETS;
1345
Romain Guyb051e892010-09-28 19:09:36 -07001346 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1347 static final int VIEW_STATE_SELECTED = 1 << 1;
1348 static final int VIEW_STATE_FOCUSED = 1 << 2;
1349 static final int VIEW_STATE_ENABLED = 1 << 3;
1350 static final int VIEW_STATE_PRESSED = 1 << 4;
1351 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001352 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001353 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001354 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1355 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001356
1357 static final int[] VIEW_STATE_IDS = new int[] {
1358 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1359 R.attr.state_selected, VIEW_STATE_SELECTED,
1360 R.attr.state_focused, VIEW_STATE_FOCUSED,
1361 R.attr.state_enabled, VIEW_STATE_ENABLED,
1362 R.attr.state_pressed, VIEW_STATE_PRESSED,
1363 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001364 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001365 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001366 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1367 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 };
1369
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001370 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001371 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1372 throw new IllegalStateException(
1373 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1374 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001375 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001376 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001377 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001378 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001379 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001380 orderedIds[i * 2] = viewState;
1381 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001382 }
1383 }
1384 }
Romain Guyb051e892010-09-28 19:09:36 -07001385 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1386 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1387 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001388 int numBits = Integer.bitCount(i);
1389 int[] set = new int[numBits];
1390 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001391 for (int j = 0; j < orderedIds.length; j += 2) {
1392 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001393 set[pos++] = orderedIds[j];
1394 }
1395 }
1396 VIEW_STATE_SETS[i] = set;
1397 }
1398
1399 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1400 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1401 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1402 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1403 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1404 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1405 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1406 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1407 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1408 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1409 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1410 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1411 | VIEW_STATE_FOCUSED];
1412 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1413 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1414 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1415 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1416 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1417 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1418 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1419 | VIEW_STATE_ENABLED];
1420 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1421 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1422 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1424 | VIEW_STATE_ENABLED];
1425 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1426 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1427 | VIEW_STATE_ENABLED];
1428 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1429 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1430 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1431
1432 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1433 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1434 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1435 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1436 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1437 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1438 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1439 | VIEW_STATE_PRESSED];
1440 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1441 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1442 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1444 | VIEW_STATE_PRESSED];
1445 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1447 | VIEW_STATE_PRESSED];
1448 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1449 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1450 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1451 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1452 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1453 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1454 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1455 | VIEW_STATE_PRESSED];
1456 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1457 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1458 | VIEW_STATE_PRESSED];
1459 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1460 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1461 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1462 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1464 | VIEW_STATE_PRESSED];
1465 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1467 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1468 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1470 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1471 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1472 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1473 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1474 | VIEW_STATE_PRESSED];
1475 }
1476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 * Temporary Rect currently for use in setBackground(). This will probably
1479 * be extended in the future to hold our own class with more than just
1480 * a Rect. :)
1481 */
1482 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001483
1484 /**
1485 * Map used to store views' tags.
1486 */
1487 private static WeakHashMap<View, SparseArray<Object>> sTags;
1488
1489 /**
1490 * Lock used to access sTags.
1491 */
1492 private static final Object sTagsLock = new Object();
1493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 /**
1495 * The animation currently associated with this view.
1496 * @hide
1497 */
1498 protected Animation mCurrentAnimation = null;
1499
1500 /**
1501 * Width as measured during measure pass.
1502 * {@hide}
1503 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001504 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001505 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506
1507 /**
1508 * Height as measured during measure pass.
1509 * {@hide}
1510 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001511 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001512 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513
1514 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001515 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1516 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1517 * its display list. This flag, used only when hw accelerated, allows us to clear the
1518 * flag while retaining this information until it's needed (at getDisplayList() time and
1519 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1520 *
1521 * {@hide}
1522 */
1523 boolean mRecreateDisplayList = false;
1524
1525 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 * The view's identifier.
1527 * {@hide}
1528 *
1529 * @see #setId(int)
1530 * @see #getId()
1531 */
1532 @ViewDebug.ExportedProperty(resolveId = true)
1533 int mID = NO_ID;
1534
1535 /**
1536 * The view's tag.
1537 * {@hide}
1538 *
1539 * @see #setTag(Object)
1540 * @see #getTag()
1541 */
1542 protected Object mTag;
1543
1544 // for mPrivateFlags:
1545 /** {@hide} */
1546 static final int WANTS_FOCUS = 0x00000001;
1547 /** {@hide} */
1548 static final int FOCUSED = 0x00000002;
1549 /** {@hide} */
1550 static final int SELECTED = 0x00000004;
1551 /** {@hide} */
1552 static final int IS_ROOT_NAMESPACE = 0x00000008;
1553 /** {@hide} */
1554 static final int HAS_BOUNDS = 0x00000010;
1555 /** {@hide} */
1556 static final int DRAWN = 0x00000020;
1557 /**
1558 * When this flag is set, this view is running an animation on behalf of its
1559 * children and should therefore not cancel invalidate requests, even if they
1560 * lie outside of this view's bounds.
1561 *
1562 * {@hide}
1563 */
1564 static final int DRAW_ANIMATION = 0x00000040;
1565 /** {@hide} */
1566 static final int SKIP_DRAW = 0x00000080;
1567 /** {@hide} */
1568 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1569 /** {@hide} */
1570 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1571 /** {@hide} */
1572 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1573 /** {@hide} */
1574 static final int MEASURED_DIMENSION_SET = 0x00000800;
1575 /** {@hide} */
1576 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001577 /** {@hide} */
1578 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579
1580 private static final int PRESSED = 0x00004000;
1581
1582 /** {@hide} */
1583 static final int DRAWING_CACHE_VALID = 0x00008000;
1584 /**
1585 * Flag used to indicate that this view should be drawn once more (and only once
1586 * more) after its animation has completed.
1587 * {@hide}
1588 */
1589 static final int ANIMATION_STARTED = 0x00010000;
1590
1591 private static final int SAVE_STATE_CALLED = 0x00020000;
1592
1593 /**
1594 * Indicates that the View returned true when onSetAlpha() was called and that
1595 * the alpha must be restored.
1596 * {@hide}
1597 */
1598 static final int ALPHA_SET = 0x00040000;
1599
1600 /**
1601 * Set by {@link #setScrollContainer(boolean)}.
1602 */
1603 static final int SCROLL_CONTAINER = 0x00080000;
1604
1605 /**
1606 * Set by {@link #setScrollContainer(boolean)}.
1607 */
1608 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1609
1610 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001611 * View flag indicating whether this view was invalidated (fully or partially.)
1612 *
1613 * @hide
1614 */
1615 static final int DIRTY = 0x00200000;
1616
1617 /**
1618 * View flag indicating whether this view was invalidated by an opaque
1619 * invalidate request.
1620 *
1621 * @hide
1622 */
1623 static final int DIRTY_OPAQUE = 0x00400000;
1624
1625 /**
1626 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1627 *
1628 * @hide
1629 */
1630 static final int DIRTY_MASK = 0x00600000;
1631
1632 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001633 * Indicates whether the background is opaque.
1634 *
1635 * @hide
1636 */
1637 static final int OPAQUE_BACKGROUND = 0x00800000;
1638
1639 /**
1640 * Indicates whether the scrollbars are opaque.
1641 *
1642 * @hide
1643 */
1644 static final int OPAQUE_SCROLLBARS = 0x01000000;
1645
1646 /**
1647 * Indicates whether the view is opaque.
1648 *
1649 * @hide
1650 */
1651 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001652
Adam Powelle14579b2009-12-16 18:39:52 -08001653 /**
1654 * Indicates a prepressed state;
1655 * the short time between ACTION_DOWN and recognizing
1656 * a 'real' press. Prepressed is used to recognize quick taps
1657 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001658 *
Adam Powelle14579b2009-12-16 18:39:52 -08001659 * @hide
1660 */
1661 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001662
Adam Powellc9fbaab2010-02-16 17:16:19 -08001663 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001664 * Indicates whether the view is temporarily detached.
1665 *
1666 * @hide
1667 */
1668 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001669
Adam Powell8568c3a2010-04-19 14:26:11 -07001670 /**
1671 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001672 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001673 * @hide
1674 */
1675 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001676
1677 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001678 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1679 * @hide
1680 */
1681 private static final int HOVERED = 0x10000000;
1682
1683 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001684 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1685 * for transform operations
1686 *
1687 * @hide
1688 */
Adam Powellf37df072010-09-17 16:22:49 -07001689 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001690
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001691 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001692 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001693
Chet Haasefd2b0022010-08-06 13:08:56 -07001694 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001695 * Indicates that this view was specifically invalidated, not just dirtied because some
1696 * child view was invalidated. The flag is used to determine when we need to recreate
1697 * a view's display list (as opposed to just returning a reference to its existing
1698 * display list).
1699 *
1700 * @hide
1701 */
1702 static final int INVALIDATED = 0x80000000;
1703
Christopher Tate3d4bf172011-03-28 16:16:46 -07001704 /* Masks for mPrivateFlags2 */
1705
1706 /**
1707 * Indicates that this view has reported that it can accept the current drag's content.
1708 * Cleared when the drag operation concludes.
1709 * @hide
1710 */
1711 static final int DRAG_CAN_ACCEPT = 0x00000001;
1712
1713 /**
1714 * Indicates that this view is currently directly under the drag location in a
1715 * drag-and-drop operation involving content that it can accept. Cleared when
1716 * the drag exits the view, or when the drag operation concludes.
1717 * @hide
1718 */
1719 static final int DRAG_HOVERED = 0x00000002;
1720
Cibu Johny86666632010-02-22 13:01:02 -08001721 /**
1722 * Indicates whether the view is drawn in right-to-left direction.
1723 *
1724 * @hide
1725 */
1726 static final int RESOLVED_LAYOUT_RTL = 0x00000004;
1727
Christopher Tate3d4bf172011-03-28 16:16:46 -07001728 /* End of masks for mPrivateFlags2 */
1729
1730 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1731
Chet Haasedaf98e92011-01-10 14:10:36 -08001732 /**
Adam Powell637d3372010-08-25 14:37:03 -07001733 * Always allow a user to over-scroll this view, provided it is a
1734 * view that can scroll.
1735 *
1736 * @see #getOverScrollMode()
1737 * @see #setOverScrollMode(int)
1738 */
1739 public static final int OVER_SCROLL_ALWAYS = 0;
1740
1741 /**
1742 * Allow a user to over-scroll this view only if the content is large
1743 * enough to meaningfully scroll, provided it is a view that can scroll.
1744 *
1745 * @see #getOverScrollMode()
1746 * @see #setOverScrollMode(int)
1747 */
1748 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1749
1750 /**
1751 * Never allow a user to over-scroll this view.
1752 *
1753 * @see #getOverScrollMode()
1754 * @see #setOverScrollMode(int)
1755 */
1756 public static final int OVER_SCROLL_NEVER = 2;
1757
1758 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001759 * View has requested the status bar to be visible (the default).
1760 *
Joe Malin32736f02011-01-19 16:14:20 -08001761 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001762 */
1763 public static final int STATUS_BAR_VISIBLE = 0;
1764
1765 /**
Jeff Brown05dc66a2011-03-02 14:41:58 -08001766 * View has requested the status bar to be hidden.
Joe Onorato664644d2011-01-23 17:53:23 -08001767 *
Joe Malin32736f02011-01-19 16:14:20 -08001768 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001769 */
1770 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1771
1772 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001773 * @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 make the status bar not expandable. Unless you also
1779 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1780 */
1781 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1782
1783 /**
1784 * @hide
1785 *
1786 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1787 * out of the public fields to keep the undefined bits out of the developer's way.
1788 *
1789 * Flag to hide notification icons and scrolling ticker text.
1790 */
1791 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
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 disable incoming notification alerts. This will not block
1800 * icons, but it will block sound, vibrating and other visual or aural notifications.
1801 */
1802 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1803
1804 /**
1805 * @hide
1806 *
1807 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1808 * out of the public fields to keep the undefined bits out of the developer's way.
1809 *
1810 * Flag to hide only the scrolling ticker. Note that
1811 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1812 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1813 */
1814 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1815
1816 /**
1817 * @hide
1818 *
1819 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1820 * out of the public fields to keep the undefined bits out of the developer's way.
1821 *
1822 * Flag to hide the center system info area.
1823 */
1824 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1825
1826 /**
1827 * @hide
1828 *
1829 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1830 * out of the public fields to keep the undefined bits out of the developer's way.
1831 *
1832 * Flag to hide only the navigation buttons. Don't use this
1833 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001834 *
1835 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001836 */
1837 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
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 Onorato6478adc2011-01-27 21:15:01 -08001845 * Flag to hide only the back button. Don't use this
1846 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1847 */
1848 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1849
1850 /**
1851 * @hide
1852 *
1853 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1854 * out of the public fields to keep the undefined bits out of the developer's way.
1855 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001856 * Flag to hide only the clock. You might use this if your activity has
1857 * its own clock making the status bar's clock redundant.
1858 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001859 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1860
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001861 /**
1862 * @hide
1863 */
1864 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001865
1866 /**
Adam Powell637d3372010-08-25 14:37:03 -07001867 * Controls the over-scroll mode for this view.
1868 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1869 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1870 * and {@link #OVER_SCROLL_NEVER}.
1871 */
1872 private int mOverScrollMode;
1873
1874 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 * The parent this view is attached to.
1876 * {@hide}
1877 *
1878 * @see #getParent()
1879 */
1880 protected ViewParent mParent;
1881
1882 /**
1883 * {@hide}
1884 */
1885 AttachInfo mAttachInfo;
1886
1887 /**
1888 * {@hide}
1889 */
Romain Guy809a7f62009-05-14 15:44:42 -07001890 @ViewDebug.ExportedProperty(flagMapping = {
1891 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1892 name = "FORCE_LAYOUT"),
1893 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1894 name = "LAYOUT_REQUIRED"),
1895 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001896 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001897 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1898 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1899 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1900 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1901 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001903 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904
1905 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001906 * This view's request for the visibility of the status bar.
1907 * @hide
1908 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001909 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001910 int mSystemUiVisibility;
1911
1912 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 * Count of how many windows this view has been attached to.
1914 */
1915 int mWindowAttachCount;
1916
1917 /**
1918 * The layout parameters associated with this view and used by the parent
1919 * {@link android.view.ViewGroup} to determine how this view should be
1920 * laid out.
1921 * {@hide}
1922 */
1923 protected ViewGroup.LayoutParams mLayoutParams;
1924
1925 /**
1926 * The view flags hold various views states.
1927 * {@hide}
1928 */
1929 @ViewDebug.ExportedProperty
1930 int mViewFlags;
1931
1932 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001933 * The transform matrix for the View. This transform is calculated internally
1934 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1935 * is used by default. Do *not* use this variable directly; instead call
1936 * getMatrix(), which will automatically recalculate the matrix if necessary
1937 * to get the correct matrix based on the latest rotation and scale properties.
1938 */
1939 private final Matrix mMatrix = new Matrix();
1940
1941 /**
1942 * The transform matrix for the View. This transform is calculated internally
1943 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1944 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001945 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001946 * to get the correct matrix based on the latest rotation and scale properties.
1947 */
1948 private Matrix mInverseMatrix;
1949
1950 /**
1951 * An internal variable that tracks whether we need to recalculate the
1952 * transform matrix, based on whether the rotation or scaleX/Y properties
1953 * have changed since the matrix was last calculated.
1954 */
Chet Haasea00f3862011-02-22 06:34:40 -08001955 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07001956
1957 /**
1958 * An internal variable that tracks whether we need to recalculate the
1959 * transform matrix, based on whether the rotation or scaleX/Y properties
1960 * have changed since the matrix was last calculated.
1961 */
1962 private boolean mInverseMatrixDirty = true;
1963
1964 /**
1965 * A variable that tracks whether we need to recalculate the
1966 * transform matrix, based on whether the rotation or scaleX/Y properties
1967 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001968 * is only valid after a call to updateMatrix() or to a function that
1969 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001970 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001971 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001972
1973 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001974 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
1975 */
1976 private Camera mCamera = null;
1977
1978 /**
1979 * This matrix is used when computing the matrix for 3D rotations.
1980 */
1981 private Matrix matrix3D = null;
1982
1983 /**
1984 * These prev values are used to recalculate a centered pivot point when necessary. The
1985 * pivot point is only used in matrix operations (when rotation, scale, or translation are
1986 * set), so thes values are only used then as well.
1987 */
1988 private int mPrevWidth = -1;
1989 private int mPrevHeight = -1;
1990
Joe Malin32736f02011-01-19 16:14:20 -08001991 private boolean mLastIsOpaque;
1992
Chet Haasefd2b0022010-08-06 13:08:56 -07001993 /**
1994 * Convenience value to check for float values that are close enough to zero to be considered
1995 * zero.
1996 */
Romain Guy2542d192010-08-18 11:47:12 -07001997 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001998
1999 /**
2000 * The degrees rotation around the vertical axis through the pivot point.
2001 */
2002 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002003 float mRotationY = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002004
2005 /**
2006 * The degrees rotation around the horizontal axis through the pivot point.
2007 */
2008 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002009 float mRotationX = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002010
2011 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002012 * The degrees rotation around the pivot point.
2013 */
2014 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002015 float mRotation = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002016
2017 /**
Chet Haasedf030d22010-07-30 17:22:38 -07002018 * The amount of translation of the object away from its left property (post-layout).
2019 */
2020 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002021 float mTranslationX = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002022
2023 /**
2024 * The amount of translation of the object away from its top property (post-layout).
2025 */
2026 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002027 float mTranslationY = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002028
2029 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002030 * The amount of scale in the x direction around the pivot point. A
2031 * value of 1 means no scaling is applied.
2032 */
2033 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002034 float mScaleX = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002035
2036 /**
2037 * The amount of scale in the y direction around the pivot point. A
2038 * value of 1 means no scaling is applied.
2039 */
2040 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002041 float mScaleY = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002042
2043 /**
2044 * The amount of scale in the x direction around the pivot point. A
2045 * value of 1 means no scaling is applied.
2046 */
2047 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002048 float mPivotX = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002049
2050 /**
2051 * The amount of scale in the y direction around the pivot point. A
2052 * value of 1 means no scaling is applied.
2053 */
2054 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002055 float mPivotY = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002056
2057 /**
2058 * The opacity of the View. This is a value from 0 to 1, where 0 means
2059 * completely transparent and 1 means completely opaque.
2060 */
2061 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002062 float mAlpha = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002063
2064 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 * The distance in pixels from the left edge of this view's parent
2066 * to the left edge of this view.
2067 * {@hide}
2068 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002069 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 protected int mLeft;
2071 /**
2072 * The distance in pixels from the left edge of this view's parent
2073 * to the right edge of this view.
2074 * {@hide}
2075 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002076 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 protected int mRight;
2078 /**
2079 * The distance in pixels from the top edge of this view's parent
2080 * to the top edge of this view.
2081 * {@hide}
2082 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002083 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 protected int mTop;
2085 /**
2086 * The distance in pixels from the top edge of this view's parent
2087 * to the bottom edge of this view.
2088 * {@hide}
2089 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002090 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 protected int mBottom;
2092
2093 /**
2094 * The offset, in pixels, by which the content of this view is scrolled
2095 * horizontally.
2096 * {@hide}
2097 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002098 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 protected int mScrollX;
2100 /**
2101 * The offset, in pixels, by which the content of this view is scrolled
2102 * vertically.
2103 * {@hide}
2104 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002105 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 protected int mScrollY;
2107
2108 /**
2109 * The left padding in pixels, that is the distance in pixels between the
2110 * left edge of this view and the left edge of its content.
2111 * {@hide}
2112 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002113 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 protected int mPaddingLeft;
2115 /**
2116 * The right padding in pixels, that is the distance in pixels between the
2117 * right edge of this view and the right edge of its content.
2118 * {@hide}
2119 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002120 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 protected int mPaddingRight;
2122 /**
2123 * The top padding in pixels, that is the distance in pixels between the
2124 * top edge of this view and the top edge of its content.
2125 * {@hide}
2126 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002127 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 protected int mPaddingTop;
2129 /**
2130 * The bottom padding in pixels, that is the distance in pixels between the
2131 * bottom edge of this view and the bottom edge of its content.
2132 * {@hide}
2133 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002134 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 protected int mPaddingBottom;
2136
2137 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002138 * Briefly describes the view and is primarily used for accessibility support.
2139 */
2140 private CharSequence mContentDescription;
2141
2142 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 * Cache the paddingRight set by the user to append to the scrollbar's size.
2144 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002145 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 int mUserPaddingRight;
2147
2148 /**
2149 * Cache the paddingBottom set by the user to append to the scrollbar's size.
2150 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002151 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 int mUserPaddingBottom;
2153
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002154 /**
Adam Powell20232d02010-12-08 21:08:53 -08002155 * Cache the paddingLeft set by the user to append to the scrollbar's size.
2156 */
2157 @ViewDebug.ExportedProperty(category = "padding")
2158 int mUserPaddingLeft;
2159
2160 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002161 * @hide
2162 */
2163 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2164 /**
2165 * @hide
2166 */
2167 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168
2169 private Resources mResources = null;
2170
2171 private Drawable mBGDrawable;
2172
2173 private int mBackgroundResource;
2174 private boolean mBackgroundSizeChanged;
2175
2176 /**
2177 * Listener used to dispatch focus change events.
2178 * This field should be made private, so it is hidden from the SDK.
2179 * {@hide}
2180 */
2181 protected OnFocusChangeListener mOnFocusChangeListener;
2182
2183 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002184 * Listeners for layout change events.
2185 */
2186 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2187
2188 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002189 * Listeners for attach events.
2190 */
2191 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2192
2193 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 * Listener used to dispatch click events.
2195 * This field should be made private, so it is hidden from the SDK.
2196 * {@hide}
2197 */
2198 protected OnClickListener mOnClickListener;
2199
2200 /**
2201 * Listener used to dispatch long click events.
2202 * This field should be made private, so it is hidden from the SDK.
2203 * {@hide}
2204 */
2205 protected OnLongClickListener mOnLongClickListener;
2206
2207 /**
2208 * Listener used to build the context menu.
2209 * This field should be made private, so it is hidden from the SDK.
2210 * {@hide}
2211 */
2212 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2213
2214 private OnKeyListener mOnKeyListener;
2215
2216 private OnTouchListener mOnTouchListener;
2217
Jeff Brown33bbfd22011-02-24 20:55:35 -08002218 private OnGenericMotionListener mOnGenericMotionListener;
2219
Chris Tate32affef2010-10-18 15:29:21 -07002220 private OnDragListener mOnDragListener;
2221
Joe Onorato664644d2011-01-23 17:53:23 -08002222 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 /**
2225 * The application environment this view lives in.
2226 * This field should be made private, so it is hidden from the SDK.
2227 * {@hide}
2228 */
2229 protected Context mContext;
2230
2231 private ScrollabilityCache mScrollCache;
2232
2233 private int[] mDrawableState = null;
2234
Romain Guy0211a0a2011-02-14 16:34:59 -08002235 /**
2236 * Set to true when drawing cache is enabled and cannot be created.
2237 *
2238 * @hide
2239 */
2240 public boolean mCachingFailed;
2241
Romain Guy02890fd2010-08-06 17:58:44 -07002242 private Bitmap mDrawingCache;
2243 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002244 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002245 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246
2247 /**
2248 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2249 * the user may specify which view to go to next.
2250 */
2251 private int mNextFocusLeftId = View.NO_ID;
2252
2253 /**
2254 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2255 * the user may specify which view to go to next.
2256 */
2257 private int mNextFocusRightId = View.NO_ID;
2258
2259 /**
2260 * When this view has focus and the next focus is {@link #FOCUS_UP},
2261 * the user may specify which view to go to next.
2262 */
2263 private int mNextFocusUpId = View.NO_ID;
2264
2265 /**
2266 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2267 * the user may specify which view to go to next.
2268 */
2269 private int mNextFocusDownId = View.NO_ID;
2270
Jeff Brown4e6319b2010-12-13 10:36:51 -08002271 /**
2272 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2273 * the user may specify which view to go to next.
2274 */
2275 int mNextFocusForwardId = View.NO_ID;
2276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002278 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002279 private PerformClick mPerformClick;
Joe Malin32736f02011-01-19 16:14:20 -08002280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 private UnsetPressedState mUnsetPressedState;
2282
2283 /**
2284 * Whether the long press's action has been invoked. The tap's action is invoked on the
2285 * up event while a long press is invoked as soon as the long press duration is reached, so
2286 * a long press could be performed before the tap is checked, in which case the tap's action
2287 * should not be invoked.
2288 */
2289 private boolean mHasPerformedLongPress;
2290
2291 /**
2292 * The minimum height of the view. We'll try our best to have the height
2293 * of this view to at least this amount.
2294 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002295 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 private int mMinHeight;
2297
2298 /**
2299 * The minimum width of the view. We'll try our best to have the width
2300 * of this view to at least this amount.
2301 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002302 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 private int mMinWidth;
2304
2305 /**
2306 * The delegate to handle touch events that are physically in this view
2307 * but should be handled by another view.
2308 */
2309 private TouchDelegate mTouchDelegate = null;
2310
2311 /**
2312 * Solid color to use as a background when creating the drawing cache. Enables
2313 * the cache to use 16 bit bitmaps instead of 32 bit.
2314 */
2315 private int mDrawingCacheBackgroundColor = 0;
2316
2317 /**
2318 * Special tree observer used when mAttachInfo is null.
2319 */
2320 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002321
Adam Powelle14579b2009-12-16 18:39:52 -08002322 /**
2323 * Cache the touch slop from the context that created the view.
2324 */
2325 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002328 * Object that handles automatic animation of view properties.
2329 */
2330 private ViewPropertyAnimator mAnimator = null;
2331
2332 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002333 * Flag indicating that a drag can cross window boundaries. When
2334 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2335 * with this flag set, all visible applications will be able to participate
2336 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002337 *
2338 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002339 */
2340 public static final int DRAG_FLAG_GLOBAL = 1;
2341
2342 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002343 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2344 */
2345 private float mVerticalScrollFactor;
2346
2347 /**
Adam Powell20232d02010-12-08 21:08:53 -08002348 * Position of the vertical scroll bar.
2349 */
2350 private int mVerticalScrollbarPosition;
2351
2352 /**
2353 * Position the scroll bar at the default position as determined by the system.
2354 */
2355 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2356
2357 /**
2358 * Position the scroll bar along the left edge.
2359 */
2360 public static final int SCROLLBAR_POSITION_LEFT = 1;
2361
2362 /**
2363 * Position the scroll bar along the right edge.
2364 */
2365 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2366
2367 /**
Romain Guy171c5922011-01-06 10:04:23 -08002368 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002369 *
2370 * @see #getLayerType()
2371 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002372 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002373 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002374 */
2375 public static final int LAYER_TYPE_NONE = 0;
2376
2377 /**
2378 * <p>Indicates that the view has a software layer. A software layer is backed
2379 * by a bitmap and causes the view to be rendered using Android's software
2380 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002381 *
Romain Guy171c5922011-01-06 10:04:23 -08002382 * <p>Software layers have various usages:</p>
2383 * <p>When the application is not using hardware acceleration, a software layer
2384 * is useful to apply a specific color filter and/or blending mode and/or
2385 * translucency to a view and all its children.</p>
2386 * <p>When the application is using hardware acceleration, a software layer
2387 * is useful to render drawing primitives not supported by the hardware
2388 * accelerated pipeline. It can also be used to cache a complex view tree
2389 * into a texture and reduce the complexity of drawing operations. For instance,
2390 * when animating a complex view tree with a translation, a software layer can
2391 * be used to render the view tree only once.</p>
2392 * <p>Software layers should be avoided when the affected view tree updates
2393 * often. Every update will require to re-render the software layer, which can
2394 * potentially be slow (particularly when hardware acceleration is turned on
2395 * since the layer will have to be uploaded into a hardware texture after every
2396 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002397 *
2398 * @see #getLayerType()
2399 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002400 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002401 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002402 */
2403 public static final int LAYER_TYPE_SOFTWARE = 1;
2404
2405 /**
2406 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2407 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2408 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2409 * rendering pipeline, but only if hardware acceleration is turned on for the
2410 * view hierarchy. When hardware acceleration is turned off, hardware layers
2411 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002412 *
Romain Guy171c5922011-01-06 10:04:23 -08002413 * <p>A hardware layer is useful to apply a specific color filter and/or
2414 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002415 * <p>A hardware layer can be used to cache a complex view tree into a
2416 * texture and reduce the complexity of drawing operations. For instance,
2417 * when animating a complex view tree with a translation, a hardware layer can
2418 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002419 * <p>A hardware layer can also be used to increase the rendering quality when
2420 * rotation transformations are applied on a view. It can also be used to
2421 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002422 *
2423 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002424 * @see #setLayerType(int, android.graphics.Paint)
2425 * @see #LAYER_TYPE_NONE
2426 * @see #LAYER_TYPE_SOFTWARE
2427 */
2428 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002429
Romain Guy3aaff3a2011-01-12 14:18:47 -08002430 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2431 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2432 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2433 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2434 })
Romain Guy171c5922011-01-06 10:04:23 -08002435 int mLayerType = LAYER_TYPE_NONE;
2436 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002437 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002438
2439 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002440 * Consistency verifier for debugging purposes.
2441 * @hide
2442 */
2443 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2444 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2445 new InputEventConsistencyVerifier(this, 0) : null;
2446
2447 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 * Simple constructor to use when creating a view from code.
2449 *
2450 * @param context The Context the view is running in, through which it can
2451 * access the current theme, resources, etc.
2452 */
2453 public View(Context context) {
2454 mContext = context;
2455 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002456 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | HORIZONTAL_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002457 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002458 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459 }
2460
2461 /**
2462 * Constructor that is called when inflating a view from XML. This is called
2463 * when a view is being constructed from an XML file, supplying attributes
2464 * that were specified in the XML file. This version uses a default style of
2465 * 0, so the only attribute values applied are those in the Context's Theme
2466 * and the given AttributeSet.
2467 *
2468 * <p>
2469 * The method onFinishInflate() will be called after all children have been
2470 * added.
2471 *
2472 * @param context The Context the view is running in, through which it can
2473 * access the current theme, resources, etc.
2474 * @param attrs The attributes of the XML tag that is inflating the view.
2475 * @see #View(Context, AttributeSet, int)
2476 */
2477 public View(Context context, AttributeSet attrs) {
2478 this(context, attrs, 0);
2479 }
2480
2481 /**
2482 * Perform inflation from XML and apply a class-specific base style. This
2483 * constructor of View allows subclasses to use their own base style when
2484 * they are inflating. For example, a Button class's constructor would call
2485 * this version of the super class constructor and supply
2486 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2487 * the theme's button style to modify all of the base view attributes (in
2488 * particular its background) as well as the Button class's attributes.
2489 *
2490 * @param context The Context the view is running in, through which it can
2491 * access the current theme, resources, etc.
2492 * @param attrs The attributes of the XML tag that is inflating the view.
2493 * @param defStyle The default style to apply to this view. If 0, no style
2494 * will be applied (beyond what is included in the theme). This may
2495 * either be an attribute resource, whose value will be retrieved
2496 * from the current theme, or an explicit style resource.
2497 * @see #View(Context, AttributeSet)
2498 */
2499 public View(Context context, AttributeSet attrs, int defStyle) {
2500 this(context);
2501
2502 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2503 defStyle, 0);
2504
2505 Drawable background = null;
2506
2507 int leftPadding = -1;
2508 int topPadding = -1;
2509 int rightPadding = -1;
2510 int bottomPadding = -1;
2511
2512 int padding = -1;
2513
2514 int viewFlagValues = 0;
2515 int viewFlagMasks = 0;
2516
2517 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 int x = 0;
2520 int y = 0;
2521
Chet Haase73066682010-11-29 15:55:32 -08002522 float tx = 0;
2523 float ty = 0;
2524 float rotation = 0;
2525 float rotationX = 0;
2526 float rotationY = 0;
2527 float sx = 1f;
2528 float sy = 1f;
2529 boolean transformSet = false;
2530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2532
Adam Powell637d3372010-08-25 14:37:03 -07002533 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 final int N = a.getIndexCount();
2535 for (int i = 0; i < N; i++) {
2536 int attr = a.getIndex(i);
2537 switch (attr) {
2538 case com.android.internal.R.styleable.View_background:
2539 background = a.getDrawable(attr);
2540 break;
2541 case com.android.internal.R.styleable.View_padding:
2542 padding = a.getDimensionPixelSize(attr, -1);
2543 break;
2544 case com.android.internal.R.styleable.View_paddingLeft:
2545 leftPadding = a.getDimensionPixelSize(attr, -1);
2546 break;
2547 case com.android.internal.R.styleable.View_paddingTop:
2548 topPadding = a.getDimensionPixelSize(attr, -1);
2549 break;
2550 case com.android.internal.R.styleable.View_paddingRight:
2551 rightPadding = a.getDimensionPixelSize(attr, -1);
2552 break;
2553 case com.android.internal.R.styleable.View_paddingBottom:
2554 bottomPadding = a.getDimensionPixelSize(attr, -1);
2555 break;
2556 case com.android.internal.R.styleable.View_scrollX:
2557 x = a.getDimensionPixelOffset(attr, 0);
2558 break;
2559 case com.android.internal.R.styleable.View_scrollY:
2560 y = a.getDimensionPixelOffset(attr, 0);
2561 break;
Chet Haase73066682010-11-29 15:55:32 -08002562 case com.android.internal.R.styleable.View_alpha:
2563 setAlpha(a.getFloat(attr, 1f));
2564 break;
2565 case com.android.internal.R.styleable.View_transformPivotX:
2566 setPivotX(a.getDimensionPixelOffset(attr, 0));
2567 break;
2568 case com.android.internal.R.styleable.View_transformPivotY:
2569 setPivotY(a.getDimensionPixelOffset(attr, 0));
2570 break;
2571 case com.android.internal.R.styleable.View_translationX:
2572 tx = a.getDimensionPixelOffset(attr, 0);
2573 transformSet = true;
2574 break;
2575 case com.android.internal.R.styleable.View_translationY:
2576 ty = a.getDimensionPixelOffset(attr, 0);
2577 transformSet = true;
2578 break;
2579 case com.android.internal.R.styleable.View_rotation:
2580 rotation = a.getFloat(attr, 0);
2581 transformSet = true;
2582 break;
2583 case com.android.internal.R.styleable.View_rotationX:
2584 rotationX = a.getFloat(attr, 0);
2585 transformSet = true;
2586 break;
2587 case com.android.internal.R.styleable.View_rotationY:
2588 rotationY = a.getFloat(attr, 0);
2589 transformSet = true;
2590 break;
2591 case com.android.internal.R.styleable.View_scaleX:
2592 sx = a.getFloat(attr, 1f);
2593 transformSet = true;
2594 break;
2595 case com.android.internal.R.styleable.View_scaleY:
2596 sy = a.getFloat(attr, 1f);
2597 transformSet = true;
2598 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 case com.android.internal.R.styleable.View_id:
2600 mID = a.getResourceId(attr, NO_ID);
2601 break;
2602 case com.android.internal.R.styleable.View_tag:
2603 mTag = a.getText(attr);
2604 break;
2605 case com.android.internal.R.styleable.View_fitsSystemWindows:
2606 if (a.getBoolean(attr, false)) {
2607 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2608 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2609 }
2610 break;
2611 case com.android.internal.R.styleable.View_focusable:
2612 if (a.getBoolean(attr, false)) {
2613 viewFlagValues |= FOCUSABLE;
2614 viewFlagMasks |= FOCUSABLE_MASK;
2615 }
2616 break;
2617 case com.android.internal.R.styleable.View_focusableInTouchMode:
2618 if (a.getBoolean(attr, false)) {
2619 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2620 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2621 }
2622 break;
2623 case com.android.internal.R.styleable.View_clickable:
2624 if (a.getBoolean(attr, false)) {
2625 viewFlagValues |= CLICKABLE;
2626 viewFlagMasks |= CLICKABLE;
2627 }
2628 break;
2629 case com.android.internal.R.styleable.View_longClickable:
2630 if (a.getBoolean(attr, false)) {
2631 viewFlagValues |= LONG_CLICKABLE;
2632 viewFlagMasks |= LONG_CLICKABLE;
2633 }
2634 break;
2635 case com.android.internal.R.styleable.View_saveEnabled:
2636 if (!a.getBoolean(attr, true)) {
2637 viewFlagValues |= SAVE_DISABLED;
2638 viewFlagMasks |= SAVE_DISABLED_MASK;
2639 }
2640 break;
2641 case com.android.internal.R.styleable.View_duplicateParentState:
2642 if (a.getBoolean(attr, false)) {
2643 viewFlagValues |= DUPLICATE_PARENT_STATE;
2644 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2645 }
2646 break;
2647 case com.android.internal.R.styleable.View_visibility:
2648 final int visibility = a.getInt(attr, 0);
2649 if (visibility != 0) {
2650 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2651 viewFlagMasks |= VISIBILITY_MASK;
2652 }
2653 break;
Cibu Johny7632cb92010-02-22 13:01:02 -08002654 case com.android.internal.R.styleable.View_horizontalDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002655 // Clear any HORIZONTAL_DIRECTION flag already set
2656 viewFlagValues &= ~HORIZONTAL_DIRECTION_MASK;
2657 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
2658 final int horizontalDirection = a.getInt(attr, -1);
2659 if (horizontalDirection != -1) {
2660 viewFlagValues |= HORIZONTAL_DIRECTION_FLAGS[horizontalDirection];
2661 } else {
2662 // Set to default (HORIZONTAL_DIRECTION_INHERIT)
2663 viewFlagValues |= HORIZONTAL_DIRECTION_DEFAULT;
2664 }
2665 viewFlagMasks |= HORIZONTAL_DIRECTION_MASK;
2666 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 case com.android.internal.R.styleable.View_drawingCacheQuality:
2668 final int cacheQuality = a.getInt(attr, 0);
2669 if (cacheQuality != 0) {
2670 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2671 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2672 }
2673 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002674 case com.android.internal.R.styleable.View_contentDescription:
2675 mContentDescription = a.getString(attr);
2676 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2678 if (!a.getBoolean(attr, true)) {
2679 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2680 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2681 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002682 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2684 if (!a.getBoolean(attr, true)) {
2685 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2686 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2687 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002688 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 case R.styleable.View_scrollbars:
2690 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2691 if (scrollbars != SCROLLBARS_NONE) {
2692 viewFlagValues |= scrollbars;
2693 viewFlagMasks |= SCROLLBARS_MASK;
2694 initializeScrollbars(a);
2695 }
2696 break;
2697 case R.styleable.View_fadingEdge:
2698 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2699 if (fadingEdge != FADING_EDGE_NONE) {
2700 viewFlagValues |= fadingEdge;
2701 viewFlagMasks |= FADING_EDGE_MASK;
2702 initializeFadingEdge(a);
2703 }
2704 break;
2705 case R.styleable.View_scrollbarStyle:
2706 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2707 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2708 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2709 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2710 }
2711 break;
2712 case R.styleable.View_isScrollContainer:
2713 setScrollContainer = true;
2714 if (a.getBoolean(attr, false)) {
2715 setScrollContainer(true);
2716 }
2717 break;
2718 case com.android.internal.R.styleable.View_keepScreenOn:
2719 if (a.getBoolean(attr, false)) {
2720 viewFlagValues |= KEEP_SCREEN_ON;
2721 viewFlagMasks |= KEEP_SCREEN_ON;
2722 }
2723 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002724 case R.styleable.View_filterTouchesWhenObscured:
2725 if (a.getBoolean(attr, false)) {
2726 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2727 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2728 }
2729 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 case R.styleable.View_nextFocusLeft:
2731 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2732 break;
2733 case R.styleable.View_nextFocusRight:
2734 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2735 break;
2736 case R.styleable.View_nextFocusUp:
2737 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2738 break;
2739 case R.styleable.View_nextFocusDown:
2740 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2741 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002742 case R.styleable.View_nextFocusForward:
2743 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2744 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 case R.styleable.View_minWidth:
2746 mMinWidth = a.getDimensionPixelSize(attr, 0);
2747 break;
2748 case R.styleable.View_minHeight:
2749 mMinHeight = a.getDimensionPixelSize(attr, 0);
2750 break;
Romain Guy9a817362009-05-01 10:57:14 -07002751 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002752 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002753 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002754 + "be used within a restricted context");
2755 }
2756
Romain Guy9a817362009-05-01 10:57:14 -07002757 final String handlerName = a.getString(attr);
2758 if (handlerName != null) {
2759 setOnClickListener(new OnClickListener() {
2760 private Method mHandler;
2761
2762 public void onClick(View v) {
2763 if (mHandler == null) {
2764 try {
2765 mHandler = getContext().getClass().getMethod(handlerName,
2766 View.class);
2767 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002768 int id = getId();
2769 String idText = id == NO_ID ? "" : " with id '"
2770 + getContext().getResources().getResourceEntryName(
2771 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002772 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002773 handlerName + "(View) in the activity "
2774 + getContext().getClass() + " for onClick handler"
2775 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002776 }
2777 }
2778
2779 try {
2780 mHandler.invoke(getContext(), View.this);
2781 } catch (IllegalAccessException e) {
2782 throw new IllegalStateException("Could not execute non "
2783 + "public method of the activity", e);
2784 } catch (InvocationTargetException e) {
2785 throw new IllegalStateException("Could not execute "
2786 + "method of the activity", e);
2787 }
2788 }
2789 });
2790 }
2791 break;
Adam Powell637d3372010-08-25 14:37:03 -07002792 case R.styleable.View_overScrollMode:
2793 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2794 break;
Adam Powell20232d02010-12-08 21:08:53 -08002795 case R.styleable.View_verticalScrollbarPosition:
2796 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2797 break;
Romain Guy171c5922011-01-06 10:04:23 -08002798 case R.styleable.View_layerType:
2799 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2800 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 }
2802 }
2803
Adam Powell637d3372010-08-25 14:37:03 -07002804 setOverScrollMode(overScrollMode);
2805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 if (background != null) {
2807 setBackgroundDrawable(background);
2808 }
2809
2810 if (padding >= 0) {
2811 leftPadding = padding;
2812 topPadding = padding;
2813 rightPadding = padding;
2814 bottomPadding = padding;
2815 }
2816
2817 // If the user specified the padding (either with android:padding or
2818 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2819 // use the default padding or the padding from the background drawable
2820 // (stored at this point in mPadding*)
2821 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2822 topPadding >= 0 ? topPadding : mPaddingTop,
2823 rightPadding >= 0 ? rightPadding : mPaddingRight,
2824 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2825
2826 if (viewFlagMasks != 0) {
2827 setFlags(viewFlagValues, viewFlagMasks);
2828 }
2829
2830 // Needs to be called after mViewFlags is set
2831 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2832 recomputePadding();
2833 }
2834
2835 if (x != 0 || y != 0) {
2836 scrollTo(x, y);
2837 }
2838
Chet Haase73066682010-11-29 15:55:32 -08002839 if (transformSet) {
2840 setTranslationX(tx);
2841 setTranslationY(ty);
2842 setRotation(rotation);
2843 setRotationX(rotationX);
2844 setRotationY(rotationY);
2845 setScaleX(sx);
2846 setScaleY(sy);
2847 }
2848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2850 setScrollContainer(true);
2851 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002852
2853 computeOpaqueFlags();
2854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 a.recycle();
2856 }
2857
2858 /**
2859 * Non-public constructor for use in testing
2860 */
2861 View() {
2862 }
2863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 /**
2865 * <p>
2866 * Initializes the fading edges from a given set of styled attributes. This
2867 * method should be called by subclasses that need fading edges and when an
2868 * instance of these subclasses is created programmatically rather than
2869 * being inflated from XML. This method is automatically called when the XML
2870 * is inflated.
2871 * </p>
2872 *
2873 * @param a the styled attributes set to initialize the fading edges from
2874 */
2875 protected void initializeFadingEdge(TypedArray a) {
2876 initScrollCache();
2877
2878 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2879 R.styleable.View_fadingEdgeLength,
2880 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2881 }
2882
2883 /**
2884 * Returns the size of the vertical faded edges used to indicate that more
2885 * content in this view is visible.
2886 *
2887 * @return The size in pixels of the vertical faded edge or 0 if vertical
2888 * faded edges are not enabled for this view.
2889 * @attr ref android.R.styleable#View_fadingEdgeLength
2890 */
2891 public int getVerticalFadingEdgeLength() {
2892 if (isVerticalFadingEdgeEnabled()) {
2893 ScrollabilityCache cache = mScrollCache;
2894 if (cache != null) {
2895 return cache.fadingEdgeLength;
2896 }
2897 }
2898 return 0;
2899 }
2900
2901 /**
2902 * Set the size of the faded edge used to indicate that more content in this
2903 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07002904 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
2905 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
2906 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 *
2908 * @param length The size in pixels of the faded edge used to indicate that more
2909 * content in this view is visible.
2910 */
2911 public void setFadingEdgeLength(int length) {
2912 initScrollCache();
2913 mScrollCache.fadingEdgeLength = length;
2914 }
2915
2916 /**
2917 * Returns the size of the horizontal faded edges used to indicate that more
2918 * content in this view is visible.
2919 *
2920 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2921 * faded edges are not enabled for this view.
2922 * @attr ref android.R.styleable#View_fadingEdgeLength
2923 */
2924 public int getHorizontalFadingEdgeLength() {
2925 if (isHorizontalFadingEdgeEnabled()) {
2926 ScrollabilityCache cache = mScrollCache;
2927 if (cache != null) {
2928 return cache.fadingEdgeLength;
2929 }
2930 }
2931 return 0;
2932 }
2933
2934 /**
2935 * Returns the width of the vertical scrollbar.
2936 *
2937 * @return The width in pixels of the vertical scrollbar or 0 if there
2938 * is no vertical scrollbar.
2939 */
2940 public int getVerticalScrollbarWidth() {
2941 ScrollabilityCache cache = mScrollCache;
2942 if (cache != null) {
2943 ScrollBarDrawable scrollBar = cache.scrollBar;
2944 if (scrollBar != null) {
2945 int size = scrollBar.getSize(true);
2946 if (size <= 0) {
2947 size = cache.scrollBarSize;
2948 }
2949 return size;
2950 }
2951 return 0;
2952 }
2953 return 0;
2954 }
2955
2956 /**
2957 * Returns the height of the horizontal scrollbar.
2958 *
2959 * @return The height in pixels of the horizontal scrollbar or 0 if
2960 * there is no horizontal scrollbar.
2961 */
2962 protected int getHorizontalScrollbarHeight() {
2963 ScrollabilityCache cache = mScrollCache;
2964 if (cache != null) {
2965 ScrollBarDrawable scrollBar = cache.scrollBar;
2966 if (scrollBar != null) {
2967 int size = scrollBar.getSize(false);
2968 if (size <= 0) {
2969 size = cache.scrollBarSize;
2970 }
2971 return size;
2972 }
2973 return 0;
2974 }
2975 return 0;
2976 }
2977
2978 /**
2979 * <p>
2980 * Initializes the scrollbars from a given set of styled attributes. This
2981 * method should be called by subclasses that need scrollbars and when an
2982 * instance of these subclasses is created programmatically rather than
2983 * being inflated from XML. This method is automatically called when the XML
2984 * is inflated.
2985 * </p>
2986 *
2987 * @param a the styled attributes set to initialize the scrollbars from
2988 */
2989 protected void initializeScrollbars(TypedArray a) {
2990 initScrollCache();
2991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08002993
Mike Cleronf116bf82009-09-27 19:14:12 -07002994 if (scrollabilityCache.scrollBar == null) {
2995 scrollabilityCache.scrollBar = new ScrollBarDrawable();
2996 }
Joe Malin32736f02011-01-19 16:14:20 -08002997
Romain Guy8bda2482010-03-02 11:42:11 -08002998 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002999
Mike Cleronf116bf82009-09-27 19:14:12 -07003000 if (!fadeScrollbars) {
3001 scrollabilityCache.state = ScrollabilityCache.ON;
3002 }
3003 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003004
3005
Mike Cleronf116bf82009-09-27 19:14:12 -07003006 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3007 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3008 .getScrollBarFadeDuration());
3009 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3010 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3011 ViewConfiguration.getScrollDefaultDelay());
3012
Joe Malin32736f02011-01-19 16:14:20 -08003013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3015 com.android.internal.R.styleable.View_scrollbarSize,
3016 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3017
3018 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3019 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3020
3021 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3022 if (thumb != null) {
3023 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3024 }
3025
3026 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3027 false);
3028 if (alwaysDraw) {
3029 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3030 }
3031
3032 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3033 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3034
3035 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3036 if (thumb != null) {
3037 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3038 }
3039
3040 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3041 false);
3042 if (alwaysDraw) {
3043 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3044 }
3045
3046 // Re-apply user/background padding so that scrollbar(s) get added
3047 recomputePadding();
3048 }
3049
3050 /**
3051 * <p>
3052 * Initalizes the scrollability cache if necessary.
3053 * </p>
3054 */
3055 private void initScrollCache() {
3056 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003057 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 }
3059 }
3060
3061 /**
Adam Powell20232d02010-12-08 21:08:53 -08003062 * Set the position of the vertical scroll bar. Should be one of
3063 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3064 * {@link #SCROLLBAR_POSITION_RIGHT}.
3065 *
3066 * @param position Where the vertical scroll bar should be positioned.
3067 */
3068 public void setVerticalScrollbarPosition(int position) {
3069 if (mVerticalScrollbarPosition != position) {
3070 mVerticalScrollbarPosition = position;
3071 computeOpaqueFlags();
3072 recomputePadding();
3073 }
3074 }
3075
3076 /**
3077 * @return The position where the vertical scroll bar will show, if applicable.
3078 * @see #setVerticalScrollbarPosition(int)
3079 */
3080 public int getVerticalScrollbarPosition() {
3081 return mVerticalScrollbarPosition;
3082 }
3083
3084 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 * Register a callback to be invoked when focus of this view changed.
3086 *
3087 * @param l The callback that will run.
3088 */
3089 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3090 mOnFocusChangeListener = l;
3091 }
3092
3093 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003094 * Add a listener that will be called when the bounds of the view change due to
3095 * layout processing.
3096 *
3097 * @param listener The listener that will be called when layout bounds change.
3098 */
3099 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3100 if (mOnLayoutChangeListeners == null) {
3101 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3102 }
3103 mOnLayoutChangeListeners.add(listener);
3104 }
3105
3106 /**
3107 * Remove a listener for layout changes.
3108 *
3109 * @param listener The listener for layout bounds change.
3110 */
3111 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3112 if (mOnLayoutChangeListeners == null) {
3113 return;
3114 }
3115 mOnLayoutChangeListeners.remove(listener);
3116 }
3117
3118 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003119 * Add a listener for attach state changes.
3120 *
3121 * This listener will be called whenever this view is attached or detached
3122 * from a window. Remove the listener using
3123 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3124 *
3125 * @param listener Listener to attach
3126 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3127 */
3128 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3129 if (mOnAttachStateChangeListeners == null) {
3130 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3131 }
3132 mOnAttachStateChangeListeners.add(listener);
3133 }
3134
3135 /**
3136 * Remove a listener for attach state changes. The listener will receive no further
3137 * notification of window attach/detach events.
3138 *
3139 * @param listener Listener to remove
3140 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3141 */
3142 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3143 if (mOnAttachStateChangeListeners == null) {
3144 return;
3145 }
3146 mOnAttachStateChangeListeners.remove(listener);
3147 }
3148
3149 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 * Returns the focus-change callback registered for this view.
3151 *
3152 * @return The callback, or null if one is not registered.
3153 */
3154 public OnFocusChangeListener getOnFocusChangeListener() {
3155 return mOnFocusChangeListener;
3156 }
3157
3158 /**
3159 * Register a callback to be invoked when this view is clicked. If this view is not
3160 * clickable, it becomes clickable.
3161 *
3162 * @param l The callback that will run
3163 *
3164 * @see #setClickable(boolean)
3165 */
3166 public void setOnClickListener(OnClickListener l) {
3167 if (!isClickable()) {
3168 setClickable(true);
3169 }
3170 mOnClickListener = l;
3171 }
3172
3173 /**
3174 * Register a callback to be invoked when this view is clicked and held. If this view is not
3175 * long clickable, it becomes long clickable.
3176 *
3177 * @param l The callback that will run
3178 *
3179 * @see #setLongClickable(boolean)
3180 */
3181 public void setOnLongClickListener(OnLongClickListener l) {
3182 if (!isLongClickable()) {
3183 setLongClickable(true);
3184 }
3185 mOnLongClickListener = l;
3186 }
3187
3188 /**
3189 * Register a callback to be invoked when the context menu for this view is
3190 * being built. If this view is not long clickable, it becomes long clickable.
3191 *
3192 * @param l The callback that will run
3193 *
3194 */
3195 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3196 if (!isLongClickable()) {
3197 setLongClickable(true);
3198 }
3199 mOnCreateContextMenuListener = l;
3200 }
3201
3202 /**
3203 * Call this view's OnClickListener, if it is defined.
3204 *
3205 * @return True there was an assigned OnClickListener that was called, false
3206 * otherwise is returned.
3207 */
3208 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003209 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 if (mOnClickListener != null) {
3212 playSoundEffect(SoundEffectConstants.CLICK);
3213 mOnClickListener.onClick(this);
3214 return true;
3215 }
3216
3217 return false;
3218 }
3219
3220 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003221 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3222 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003224 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 */
3226 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003227 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 boolean handled = false;
3230 if (mOnLongClickListener != null) {
3231 handled = mOnLongClickListener.onLongClick(View.this);
3232 }
3233 if (!handled) {
3234 handled = showContextMenu();
3235 }
3236 if (handled) {
3237 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3238 }
3239 return handled;
3240 }
3241
3242 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003243 * Performs button-related actions during a touch down event.
3244 *
3245 * @param event The event.
3246 * @return True if the down was consumed.
3247 *
3248 * @hide
3249 */
3250 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3251 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3252 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3253 return true;
3254 }
3255 }
3256 return false;
3257 }
3258
3259 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 * Bring up the context menu for this view.
3261 *
3262 * @return Whether a context menu was displayed.
3263 */
3264 public boolean showContextMenu() {
3265 return getParent().showContextMenuForChild(this);
3266 }
3267
3268 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003269 * Bring up the context menu for this view, referring to the item under the specified point.
3270 *
3271 * @param x The referenced x coordinate.
3272 * @param y The referenced y coordinate.
3273 * @param metaState The keyboard modifiers that were pressed.
3274 * @return Whether a context menu was displayed.
3275 *
3276 * @hide
3277 */
3278 public boolean showContextMenu(float x, float y, int metaState) {
3279 return showContextMenu();
3280 }
3281
3282 /**
Adam Powell6e346362010-07-23 10:18:23 -07003283 * Start an action mode.
3284 *
3285 * @param callback Callback that will control the lifecycle of the action mode
3286 * @return The new action mode if it is started, null otherwise
3287 *
3288 * @see ActionMode
3289 */
3290 public ActionMode startActionMode(ActionMode.Callback callback) {
3291 return getParent().startActionModeForChild(this, callback);
3292 }
3293
3294 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 * Register a callback to be invoked when a key is pressed in this view.
3296 * @param l the key listener to attach to this view
3297 */
3298 public void setOnKeyListener(OnKeyListener l) {
3299 mOnKeyListener = l;
3300 }
3301
3302 /**
3303 * Register a callback to be invoked when a touch event is sent to this view.
3304 * @param l the touch listener to attach to this view
3305 */
3306 public void setOnTouchListener(OnTouchListener l) {
3307 mOnTouchListener = l;
3308 }
3309
3310 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003311 * Register a callback to be invoked when a generic motion event is sent to this view.
3312 * @param l the generic motion listener to attach to this view
3313 */
3314 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3315 mOnGenericMotionListener = l;
3316 }
3317
3318 /**
Joe Malin32736f02011-01-19 16:14:20 -08003319 * Register a drag event listener callback object for this View. The parameter is
3320 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3321 * View, the system calls the
3322 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3323 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003324 */
3325 public void setOnDragListener(OnDragListener l) {
3326 mOnDragListener = l;
3327 }
3328
3329 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003330 * Give this view focus. This will cause
3331 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 *
3333 * Note: this does not check whether this {@link View} should get focus, it just
3334 * gives it focus no matter what. It should only be called internally by framework
3335 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3336 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003337 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3338 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339 * focus moved when requestFocus() is called. It may not always
3340 * apply, in which case use the default View.FOCUS_DOWN.
3341 * @param previouslyFocusedRect The rectangle of the view that had focus
3342 * prior in this View's coordinate system.
3343 */
3344 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3345 if (DBG) {
3346 System.out.println(this + " requestFocus()");
3347 }
3348
3349 if ((mPrivateFlags & FOCUSED) == 0) {
3350 mPrivateFlags |= FOCUSED;
3351
3352 if (mParent != null) {
3353 mParent.requestChildFocus(this, this);
3354 }
3355
3356 onFocusChanged(true, direction, previouslyFocusedRect);
3357 refreshDrawableState();
3358 }
3359 }
3360
3361 /**
3362 * Request that a rectangle of this view be visible on the screen,
3363 * scrolling if necessary just enough.
3364 *
3365 * <p>A View should call this if it maintains some notion of which part
3366 * of its content is interesting. For example, a text editing view
3367 * should call this when its cursor moves.
3368 *
3369 * @param rectangle The rectangle.
3370 * @return Whether any parent scrolled.
3371 */
3372 public boolean requestRectangleOnScreen(Rect rectangle) {
3373 return requestRectangleOnScreen(rectangle, false);
3374 }
3375
3376 /**
3377 * Request that a rectangle of this view be visible on the screen,
3378 * scrolling if necessary just enough.
3379 *
3380 * <p>A View should call this if it maintains some notion of which part
3381 * of its content is interesting. For example, a text editing view
3382 * should call this when its cursor moves.
3383 *
3384 * <p>When <code>immediate</code> is set to true, scrolling will not be
3385 * animated.
3386 *
3387 * @param rectangle The rectangle.
3388 * @param immediate True to forbid animated scrolling, false otherwise
3389 * @return Whether any parent scrolled.
3390 */
3391 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3392 View child = this;
3393 ViewParent parent = mParent;
3394 boolean scrolled = false;
3395 while (parent != null) {
3396 scrolled |= parent.requestChildRectangleOnScreen(child,
3397 rectangle, immediate);
3398
3399 // offset rect so next call has the rectangle in the
3400 // coordinate system of its direct child.
3401 rectangle.offset(child.getLeft(), child.getTop());
3402 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3403
3404 if (!(parent instanceof View)) {
3405 break;
3406 }
Romain Guy8506ab42009-06-11 17:35:47 -07003407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003408 child = (View) parent;
3409 parent = child.getParent();
3410 }
3411 return scrolled;
3412 }
3413
3414 /**
3415 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003416 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 */
3418 public void clearFocus() {
3419 if (DBG) {
3420 System.out.println(this + " clearFocus()");
3421 }
3422
3423 if ((mPrivateFlags & FOCUSED) != 0) {
3424 mPrivateFlags &= ~FOCUSED;
3425
3426 if (mParent != null) {
3427 mParent.clearChildFocus(this);
3428 }
3429
3430 onFocusChanged(false, 0, null);
3431 refreshDrawableState();
3432 }
3433 }
3434
3435 /**
3436 * Called to clear the focus of a view that is about to be removed.
3437 * Doesn't call clearChildFocus, which prevents this view from taking
3438 * focus again before it has been removed from the parent
3439 */
3440 void clearFocusForRemoval() {
3441 if ((mPrivateFlags & FOCUSED) != 0) {
3442 mPrivateFlags &= ~FOCUSED;
3443
3444 onFocusChanged(false, 0, null);
3445 refreshDrawableState();
3446 }
3447 }
3448
3449 /**
3450 * Called internally by the view system when a new view is getting focus.
3451 * This is what clears the old focus.
3452 */
3453 void unFocus() {
3454 if (DBG) {
3455 System.out.println(this + " unFocus()");
3456 }
3457
3458 if ((mPrivateFlags & FOCUSED) != 0) {
3459 mPrivateFlags &= ~FOCUSED;
3460
3461 onFocusChanged(false, 0, null);
3462 refreshDrawableState();
3463 }
3464 }
3465
3466 /**
3467 * Returns true if this view has focus iteself, or is the ancestor of the
3468 * view that has focus.
3469 *
3470 * @return True if this view has or contains focus, false otherwise.
3471 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003472 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003473 public boolean hasFocus() {
3474 return (mPrivateFlags & FOCUSED) != 0;
3475 }
3476
3477 /**
3478 * Returns true if this view is focusable or if it contains a reachable View
3479 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3480 * is a View whose parents do not block descendants focus.
3481 *
3482 * Only {@link #VISIBLE} views are considered focusable.
3483 *
3484 * @return True if the view is focusable or if the view contains a focusable
3485 * View, false otherwise.
3486 *
3487 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3488 */
3489 public boolean hasFocusable() {
3490 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3491 }
3492
3493 /**
3494 * Called by the view system when the focus state of this view changes.
3495 * When the focus change event is caused by directional navigation, direction
3496 * and previouslyFocusedRect provide insight into where the focus is coming from.
3497 * When overriding, be sure to call up through to the super class so that
3498 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003499 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003500 * @param gainFocus True if the View has focus; false otherwise.
3501 * @param direction The direction focus has moved when requestFocus()
3502 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003503 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3504 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3505 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003506 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3507 * system, of the previously focused view. If applicable, this will be
3508 * passed in as finer grained information about where the focus is coming
3509 * from (in addition to direction). Will be <code>null</code> otherwise.
3510 */
3511 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003512 if (gainFocus) {
3513 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3514 }
3515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003516 InputMethodManager imm = InputMethodManager.peekInstance();
3517 if (!gainFocus) {
3518 if (isPressed()) {
3519 setPressed(false);
3520 }
3521 if (imm != null && mAttachInfo != null
3522 && mAttachInfo.mHasWindowFocus) {
3523 imm.focusOut(this);
3524 }
Romain Guya2431d02009-04-30 16:30:00 -07003525 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 } else if (imm != null && mAttachInfo != null
3527 && mAttachInfo.mHasWindowFocus) {
3528 imm.focusIn(this);
3529 }
Romain Guy8506ab42009-06-11 17:35:47 -07003530
Romain Guy0fd89bf2011-01-26 15:41:30 -08003531 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 if (mOnFocusChangeListener != null) {
3533 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3534 }
Joe Malin32736f02011-01-19 16:14:20 -08003535
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003536 if (mAttachInfo != null) {
3537 mAttachInfo.mKeyDispatchState.reset(this);
3538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 }
3540
3541 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003542 * Sends an accessibility event of the given type. If accessiiblity is
3543 * not enabled this method has no effect. The default implementation calls
3544 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3545 * to populate information about the event source (this View), then calls
3546 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3547 * populate the text content of the event source including its descendants,
3548 * and last calls
3549 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3550 * on its parent to resuest sending of the event to interested parties.
3551 *
3552 * @param eventType The type of the event to send.
3553 *
3554 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3555 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3556 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
svetoslavganov75986cf2009-05-14 22:28:01 -07003557 */
3558 public void sendAccessibilityEvent(int eventType) {
3559 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3560 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3561 }
3562 }
3563
3564 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003565 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3566 * takes as an argument an empty {@link AccessibilityEvent} and does not
3567 * perfrom a check whether accessibility is enabled.
3568 *
3569 * @param event The event to send.
3570 *
3571 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003572 */
3573 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003574 if (!isShown()) {
3575 return;
3576 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003577 onInitializeAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003578 dispatchPopulateAccessibilityEvent(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003579 // In the beginning we called #isShown(), so we know that getParent() is not null.
3580 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003581 }
3582
3583 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003584 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3585 * to its children for adding their text content to the event. Note that the
3586 * event text is populated in a separate dispatch path since we add to the
3587 * event not only the text of the source but also the text of all its descendants.
3588 * </p>
3589 * A typical implementation will call
3590 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3591 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3592 * on each child. Override this method if custom population of the event text
3593 * content is required.
svetoslavganov75986cf2009-05-14 22:28:01 -07003594 *
3595 * @param event The event.
3596 *
3597 * @return True if the event population was completed.
3598 */
3599 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003600 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003601 return false;
3602 }
3603
3604 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003605 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003606 * giving a chance to this View to populate the accessibility event with its
3607 * text content. While the implementation is free to modify other event
3608 * attributes this should be performed in
3609 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
3610 * <p>
3611 * Example: Adding formatted date string to an accessibility event in addition
3612 * to the text added by the super implementation.
3613 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003614 * public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3615 * super.onPopulateAccessibilityEvent(event);
3616 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
3617 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
3618 * mCurrentDate.getTimeInMillis(), flags);
3619 * event.getText().add(selectedDateUtterance);
3620 * }
3621 * </code></pre></p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003622 *
3623 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003624 *
3625 * @see #sendAccessibilityEvent(int)
3626 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003627 */
3628 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3629
3630 }
3631
3632 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003633 * Initializes an {@link AccessibilityEvent} with information about the
3634 * the type of the event and this View which is the event source. In other
3635 * words, the source of an accessibility event is the view whose state
3636 * change triggered firing the event.
3637 * <p>
3638 * Example: Setting the password property of an event in addition
3639 * to properties set by the super implementation.
3640 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003641 * public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3642 * super.onInitializeAccessibilityEvent(event);
3643 * event.setPassword(true);
3644 * }
3645 * </code></pre></p>
3646 * @param event The event to initialeze.
3647 *
3648 * @see #sendAccessibilityEvent(int)
3649 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3650 */
3651 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3652 event.setClassName(getClass().getName());
3653 event.setPackageName(getContext().getPackageName());
3654 event.setEnabled(isEnabled());
3655 event.setContentDescription(mContentDescription);
3656
3657 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
3658 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3659 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
3660 event.setItemCount(focusablesTempList.size());
3661 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3662 focusablesTempList.clear();
3663 }
3664 }
3665
3666 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003667 * Gets the {@link View} description. It briefly describes the view and is
3668 * primarily used for accessibility support. Set this property to enable
3669 * better accessibility support for your application. This is especially
3670 * true for views that do not have textual representation (For example,
3671 * ImageButton).
3672 *
3673 * @return The content descriptiopn.
3674 *
3675 * @attr ref android.R.styleable#View_contentDescription
3676 */
3677 public CharSequence getContentDescription() {
3678 return mContentDescription;
3679 }
3680
3681 /**
3682 * Sets the {@link View} description. It briefly describes the view and is
3683 * primarily used for accessibility support. Set this property to enable
3684 * better accessibility support for your application. This is especially
3685 * true for views that do not have textual representation (For example,
3686 * ImageButton).
3687 *
3688 * @param contentDescription The content description.
3689 *
3690 * @attr ref android.R.styleable#View_contentDescription
3691 */
3692 public void setContentDescription(CharSequence contentDescription) {
3693 mContentDescription = contentDescription;
3694 }
3695
3696 /**
Romain Guya2431d02009-04-30 16:30:00 -07003697 * Invoked whenever this view loses focus, either by losing window focus or by losing
3698 * focus within its window. This method can be used to clear any state tied to the
3699 * focus. For instance, if a button is held pressed with the trackball and the window
3700 * loses focus, this method can be used to cancel the press.
3701 *
3702 * Subclasses of View overriding this method should always call super.onFocusLost().
3703 *
3704 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003705 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003706 *
3707 * @hide pending API council approval
3708 */
3709 protected void onFocusLost() {
3710 resetPressedState();
3711 }
3712
3713 private void resetPressedState() {
3714 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3715 return;
3716 }
3717
3718 if (isPressed()) {
3719 setPressed(false);
3720
3721 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05003722 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003723 }
3724 }
3725 }
3726
3727 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728 * Returns true if this view has focus
3729 *
3730 * @return True if this view has focus, false otherwise.
3731 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003732 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003733 public boolean isFocused() {
3734 return (mPrivateFlags & FOCUSED) != 0;
3735 }
3736
3737 /**
3738 * Find the view in the hierarchy rooted at this view that currently has
3739 * focus.
3740 *
3741 * @return The view that currently has focus, or null if no focused view can
3742 * be found.
3743 */
3744 public View findFocus() {
3745 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
3746 }
3747
3748 /**
3749 * Change whether this view is one of the set of scrollable containers in
3750 * its window. This will be used to determine whether the window can
3751 * resize or must pan when a soft input area is open -- scrollable
3752 * containers allow the window to use resize mode since the container
3753 * will appropriately shrink.
3754 */
3755 public void setScrollContainer(boolean isScrollContainer) {
3756 if (isScrollContainer) {
3757 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
3758 mAttachInfo.mScrollContainers.add(this);
3759 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
3760 }
3761 mPrivateFlags |= SCROLL_CONTAINER;
3762 } else {
3763 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
3764 mAttachInfo.mScrollContainers.remove(this);
3765 }
3766 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
3767 }
3768 }
3769
3770 /**
3771 * Returns the quality of the drawing cache.
3772 *
3773 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3774 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3775 *
3776 * @see #setDrawingCacheQuality(int)
3777 * @see #setDrawingCacheEnabled(boolean)
3778 * @see #isDrawingCacheEnabled()
3779 *
3780 * @attr ref android.R.styleable#View_drawingCacheQuality
3781 */
3782 public int getDrawingCacheQuality() {
3783 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
3784 }
3785
3786 /**
3787 * Set the drawing cache quality of this view. This value is used only when the
3788 * drawing cache is enabled
3789 *
3790 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3791 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3792 *
3793 * @see #getDrawingCacheQuality()
3794 * @see #setDrawingCacheEnabled(boolean)
3795 * @see #isDrawingCacheEnabled()
3796 *
3797 * @attr ref android.R.styleable#View_drawingCacheQuality
3798 */
3799 public void setDrawingCacheQuality(int quality) {
3800 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
3801 }
3802
3803 /**
3804 * Returns whether the screen should remain on, corresponding to the current
3805 * value of {@link #KEEP_SCREEN_ON}.
3806 *
3807 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
3808 *
3809 * @see #setKeepScreenOn(boolean)
3810 *
3811 * @attr ref android.R.styleable#View_keepScreenOn
3812 */
3813 public boolean getKeepScreenOn() {
3814 return (mViewFlags & KEEP_SCREEN_ON) != 0;
3815 }
3816
3817 /**
3818 * Controls whether the screen should remain on, modifying the
3819 * value of {@link #KEEP_SCREEN_ON}.
3820 *
3821 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
3822 *
3823 * @see #getKeepScreenOn()
3824 *
3825 * @attr ref android.R.styleable#View_keepScreenOn
3826 */
3827 public void setKeepScreenOn(boolean keepScreenOn) {
3828 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
3829 }
3830
3831 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003832 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3833 * @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 -08003834 *
3835 * @attr ref android.R.styleable#View_nextFocusLeft
3836 */
3837 public int getNextFocusLeftId() {
3838 return mNextFocusLeftId;
3839 }
3840
3841 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003842 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3843 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
3844 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003845 *
3846 * @attr ref android.R.styleable#View_nextFocusLeft
3847 */
3848 public void setNextFocusLeftId(int nextFocusLeftId) {
3849 mNextFocusLeftId = nextFocusLeftId;
3850 }
3851
3852 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003853 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3854 * @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 -08003855 *
3856 * @attr ref android.R.styleable#View_nextFocusRight
3857 */
3858 public int getNextFocusRightId() {
3859 return mNextFocusRightId;
3860 }
3861
3862 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003863 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3864 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
3865 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003866 *
3867 * @attr ref android.R.styleable#View_nextFocusRight
3868 */
3869 public void setNextFocusRightId(int nextFocusRightId) {
3870 mNextFocusRightId = nextFocusRightId;
3871 }
3872
3873 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003874 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3875 * @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 -08003876 *
3877 * @attr ref android.R.styleable#View_nextFocusUp
3878 */
3879 public int getNextFocusUpId() {
3880 return mNextFocusUpId;
3881 }
3882
3883 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003884 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3885 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
3886 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003887 *
3888 * @attr ref android.R.styleable#View_nextFocusUp
3889 */
3890 public void setNextFocusUpId(int nextFocusUpId) {
3891 mNextFocusUpId = nextFocusUpId;
3892 }
3893
3894 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003895 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3896 * @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 -08003897 *
3898 * @attr ref android.R.styleable#View_nextFocusDown
3899 */
3900 public int getNextFocusDownId() {
3901 return mNextFocusDownId;
3902 }
3903
3904 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003905 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3906 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
3907 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003908 *
3909 * @attr ref android.R.styleable#View_nextFocusDown
3910 */
3911 public void setNextFocusDownId(int nextFocusDownId) {
3912 mNextFocusDownId = nextFocusDownId;
3913 }
3914
3915 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003916 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3917 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
3918 *
3919 * @attr ref android.R.styleable#View_nextFocusForward
3920 */
3921 public int getNextFocusForwardId() {
3922 return mNextFocusForwardId;
3923 }
3924
3925 /**
3926 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3927 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
3928 * decide automatically.
3929 *
3930 * @attr ref android.R.styleable#View_nextFocusForward
3931 */
3932 public void setNextFocusForwardId(int nextFocusForwardId) {
3933 mNextFocusForwardId = nextFocusForwardId;
3934 }
3935
3936 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003937 * Returns the visibility of this view and all of its ancestors
3938 *
3939 * @return True if this view and all of its ancestors are {@link #VISIBLE}
3940 */
3941 public boolean isShown() {
3942 View current = this;
3943 //noinspection ConstantConditions
3944 do {
3945 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
3946 return false;
3947 }
3948 ViewParent parent = current.mParent;
3949 if (parent == null) {
3950 return false; // We are not attached to the view root
3951 }
3952 if (!(parent instanceof View)) {
3953 return true;
3954 }
3955 current = (View) parent;
3956 } while (current != null);
3957
3958 return false;
3959 }
3960
3961 /**
3962 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
3963 * is set
3964 *
3965 * @param insets Insets for system windows
3966 *
3967 * @return True if this view applied the insets, false otherwise
3968 */
3969 protected boolean fitSystemWindows(Rect insets) {
3970 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
3971 mPaddingLeft = insets.left;
3972 mPaddingTop = insets.top;
3973 mPaddingRight = insets.right;
3974 mPaddingBottom = insets.bottom;
3975 requestLayout();
3976 return true;
3977 }
3978 return false;
3979 }
3980
3981 /**
3982 * Returns the visibility status for this view.
3983 *
3984 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3985 * @attr ref android.R.styleable#View_visibility
3986 */
3987 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003988 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
3989 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
3990 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003991 })
3992 public int getVisibility() {
3993 return mViewFlags & VISIBILITY_MASK;
3994 }
3995
3996 /**
3997 * Set the enabled state of this view.
3998 *
3999 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4000 * @attr ref android.R.styleable#View_visibility
4001 */
4002 @RemotableViewMethod
4003 public void setVisibility(int visibility) {
4004 setFlags(visibility, VISIBILITY_MASK);
4005 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4006 }
4007
4008 /**
4009 * Returns the enabled status for this view. The interpretation of the
4010 * enabled state varies by subclass.
4011 *
4012 * @return True if this view is enabled, false otherwise.
4013 */
4014 @ViewDebug.ExportedProperty
4015 public boolean isEnabled() {
4016 return (mViewFlags & ENABLED_MASK) == ENABLED;
4017 }
4018
4019 /**
4020 * Set the enabled state of this view. The interpretation of the enabled
4021 * state varies by subclass.
4022 *
4023 * @param enabled True if this view is enabled, false otherwise.
4024 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004025 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004026 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004027 if (enabled == isEnabled()) return;
4028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004029 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4030
4031 /*
4032 * The View most likely has to change its appearance, so refresh
4033 * the drawable state.
4034 */
4035 refreshDrawableState();
4036
4037 // Invalidate too, since the default behavior for views is to be
4038 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004039 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 }
4041
4042 /**
4043 * Set whether this view can receive the focus.
4044 *
4045 * Setting this to false will also ensure that this view is not focusable
4046 * in touch mode.
4047 *
4048 * @param focusable If true, this view can receive the focus.
4049 *
4050 * @see #setFocusableInTouchMode(boolean)
4051 * @attr ref android.R.styleable#View_focusable
4052 */
4053 public void setFocusable(boolean focusable) {
4054 if (!focusable) {
4055 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4056 }
4057 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4058 }
4059
4060 /**
4061 * Set whether this view can receive focus while in touch mode.
4062 *
4063 * Setting this to true will also ensure that this view is focusable.
4064 *
4065 * @param focusableInTouchMode If true, this view can receive the focus while
4066 * in touch mode.
4067 *
4068 * @see #setFocusable(boolean)
4069 * @attr ref android.R.styleable#View_focusableInTouchMode
4070 */
4071 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4072 // Focusable in touch mode should always be set before the focusable flag
4073 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4074 // which, in touch mode, will not successfully request focus on this view
4075 // because the focusable in touch mode flag is not set
4076 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4077 if (focusableInTouchMode) {
4078 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4079 }
4080 }
4081
4082 /**
4083 * Set whether this view should have sound effects enabled for events such as
4084 * clicking and touching.
4085 *
4086 * <p>You may wish to disable sound effects for a view if you already play sounds,
4087 * for instance, a dial key that plays dtmf tones.
4088 *
4089 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4090 * @see #isSoundEffectsEnabled()
4091 * @see #playSoundEffect(int)
4092 * @attr ref android.R.styleable#View_soundEffectsEnabled
4093 */
4094 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4095 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4096 }
4097
4098 /**
4099 * @return whether this view should have sound effects enabled for events such as
4100 * clicking and touching.
4101 *
4102 * @see #setSoundEffectsEnabled(boolean)
4103 * @see #playSoundEffect(int)
4104 * @attr ref android.R.styleable#View_soundEffectsEnabled
4105 */
4106 @ViewDebug.ExportedProperty
4107 public boolean isSoundEffectsEnabled() {
4108 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4109 }
4110
4111 /**
4112 * Set whether this view should have haptic feedback for events such as
4113 * long presses.
4114 *
4115 * <p>You may wish to disable haptic feedback if your view already controls
4116 * its own haptic feedback.
4117 *
4118 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4119 * @see #isHapticFeedbackEnabled()
4120 * @see #performHapticFeedback(int)
4121 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4122 */
4123 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4124 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4125 }
4126
4127 /**
4128 * @return whether this view should have haptic feedback enabled for events
4129 * long presses.
4130 *
4131 * @see #setHapticFeedbackEnabled(boolean)
4132 * @see #performHapticFeedback(int)
4133 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4134 */
4135 @ViewDebug.ExportedProperty
4136 public boolean isHapticFeedbackEnabled() {
4137 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4138 }
4139
4140 /**
Cibu Johny7632cb92010-02-22 13:01:02 -08004141 * Returns the horizontal direction for this view.
4142 *
4143 * @return One of {@link #HORIZONTAL_DIRECTION_LTR},
4144 * {@link #HORIZONTAL_DIRECTION_RTL},
4145 * {@link #HORIZONTAL_DIRECTION_INHERIT} or
4146 * {@link #HORIZONTAL_DIRECTION_LOCALE}.
4147 * @attr ref android.R.styleable#View_horizontalDirection
4148 * @hide
4149 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004150 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Cibu Johny7632cb92010-02-22 13:01:02 -08004151 @ViewDebug.IntToString(from = HORIZONTAL_DIRECTION_LTR, to = "LTR"),
4152 @ViewDebug.IntToString(from = HORIZONTAL_DIRECTION_RTL, to = "RTL"),
4153 @ViewDebug.IntToString(from = HORIZONTAL_DIRECTION_INHERIT, to = "INHERIT"),
4154 @ViewDebug.IntToString(from = HORIZONTAL_DIRECTION_LOCALE, to = "LOCALE")
4155 })
4156 public int getHorizontalDirection() {
4157 return mViewFlags & HORIZONTAL_DIRECTION_MASK;
4158 }
4159
4160 /**
4161 * Set the horizontal direction for this view.
4162 *
4163 * @param horizontalDirection One of {@link #HORIZONTAL_DIRECTION_LTR},
4164 * {@link #HORIZONTAL_DIRECTION_RTL},
4165 * {@link #HORIZONTAL_DIRECTION_INHERIT} or
4166 * {@link #HORIZONTAL_DIRECTION_LOCALE}.
4167 * @attr ref android.R.styleable#View_horizontalDirection
4168 * @hide
4169 */
4170 @RemotableViewMethod
4171 public void setHorizontalDirection(int horizontalDirection) {
4172 setFlags(horizontalDirection, HORIZONTAL_DIRECTION_MASK);
4173 }
4174
4175 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004176 * If this view doesn't do any drawing on its own, set this flag to
4177 * allow further optimizations. By default, this flag is not set on
4178 * View, but could be set on some View subclasses such as ViewGroup.
4179 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004180 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4181 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004182 *
4183 * @param willNotDraw whether or not this View draw on its own
4184 */
4185 public void setWillNotDraw(boolean willNotDraw) {
4186 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4187 }
4188
4189 /**
4190 * Returns whether or not this View draws on its own.
4191 *
4192 * @return true if this view has nothing to draw, false otherwise
4193 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004194 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 public boolean willNotDraw() {
4196 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4197 }
4198
4199 /**
4200 * When a View's drawing cache is enabled, drawing is redirected to an
4201 * offscreen bitmap. Some views, like an ImageView, must be able to
4202 * bypass this mechanism if they already draw a single bitmap, to avoid
4203 * unnecessary usage of the memory.
4204 *
4205 * @param willNotCacheDrawing true if this view does not cache its
4206 * drawing, false otherwise
4207 */
4208 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4209 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4210 }
4211
4212 /**
4213 * Returns whether or not this View can cache its drawing or not.
4214 *
4215 * @return true if this view does not cache its drawing, false otherwise
4216 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004217 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004218 public boolean willNotCacheDrawing() {
4219 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4220 }
4221
4222 /**
4223 * Indicates whether this view reacts to click events or not.
4224 *
4225 * @return true if the view is clickable, false otherwise
4226 *
4227 * @see #setClickable(boolean)
4228 * @attr ref android.R.styleable#View_clickable
4229 */
4230 @ViewDebug.ExportedProperty
4231 public boolean isClickable() {
4232 return (mViewFlags & CLICKABLE) == CLICKABLE;
4233 }
4234
4235 /**
4236 * Enables or disables click events for this view. When a view
4237 * is clickable it will change its state to "pressed" on every click.
4238 * Subclasses should set the view clickable to visually react to
4239 * user's clicks.
4240 *
4241 * @param clickable true to make the view clickable, false otherwise
4242 *
4243 * @see #isClickable()
4244 * @attr ref android.R.styleable#View_clickable
4245 */
4246 public void setClickable(boolean clickable) {
4247 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4248 }
4249
4250 /**
4251 * Indicates whether this view reacts to long click events or not.
4252 *
4253 * @return true if the view is long clickable, false otherwise
4254 *
4255 * @see #setLongClickable(boolean)
4256 * @attr ref android.R.styleable#View_longClickable
4257 */
4258 public boolean isLongClickable() {
4259 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4260 }
4261
4262 /**
4263 * Enables or disables long click events for this view. When a view is long
4264 * clickable it reacts to the user holding down the button for a longer
4265 * duration than a tap. This event can either launch the listener or a
4266 * context menu.
4267 *
4268 * @param longClickable true to make the view long clickable, false otherwise
4269 * @see #isLongClickable()
4270 * @attr ref android.R.styleable#View_longClickable
4271 */
4272 public void setLongClickable(boolean longClickable) {
4273 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4274 }
4275
4276 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004277 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004278 *
4279 * @see #isClickable()
4280 * @see #setClickable(boolean)
4281 *
4282 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4283 * the View's internal state from a previously set "pressed" state.
4284 */
4285 public void setPressed(boolean pressed) {
4286 if (pressed) {
4287 mPrivateFlags |= PRESSED;
4288 } else {
4289 mPrivateFlags &= ~PRESSED;
4290 }
4291 refreshDrawableState();
4292 dispatchSetPressed(pressed);
4293 }
4294
4295 /**
4296 * Dispatch setPressed to all of this View's children.
4297 *
4298 * @see #setPressed(boolean)
4299 *
4300 * @param pressed The new pressed state
4301 */
4302 protected void dispatchSetPressed(boolean pressed) {
4303 }
4304
4305 /**
4306 * Indicates whether the view is currently in pressed state. Unless
4307 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4308 * the pressed state.
4309 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004310 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004311 * @see #isClickable()
4312 * @see #setClickable(boolean)
4313 *
4314 * @return true if the view is currently pressed, false otherwise
4315 */
4316 public boolean isPressed() {
4317 return (mPrivateFlags & PRESSED) == PRESSED;
4318 }
4319
4320 /**
4321 * Indicates whether this view will save its state (that is,
4322 * whether its {@link #onSaveInstanceState} method will be called).
4323 *
4324 * @return Returns true if the view state saving is enabled, else false.
4325 *
4326 * @see #setSaveEnabled(boolean)
4327 * @attr ref android.R.styleable#View_saveEnabled
4328 */
4329 public boolean isSaveEnabled() {
4330 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4331 }
4332
4333 /**
4334 * Controls whether the saving of this view's state is
4335 * enabled (that is, whether its {@link #onSaveInstanceState} method
4336 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004337 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004338 * for its state to be saved. This flag can only disable the
4339 * saving of this view; any child views may still have their state saved.
4340 *
4341 * @param enabled Set to false to <em>disable</em> state saving, or true
4342 * (the default) to allow it.
4343 *
4344 * @see #isSaveEnabled()
4345 * @see #setId(int)
4346 * @see #onSaveInstanceState()
4347 * @attr ref android.R.styleable#View_saveEnabled
4348 */
4349 public void setSaveEnabled(boolean enabled) {
4350 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4351 }
4352
Jeff Brown85a31762010-09-01 17:01:00 -07004353 /**
4354 * Gets whether the framework should discard touches when the view's
4355 * window is obscured by another visible window.
4356 * Refer to the {@link View} security documentation for more details.
4357 *
4358 * @return True if touch filtering is enabled.
4359 *
4360 * @see #setFilterTouchesWhenObscured(boolean)
4361 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4362 */
4363 @ViewDebug.ExportedProperty
4364 public boolean getFilterTouchesWhenObscured() {
4365 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4366 }
4367
4368 /**
4369 * Sets whether the framework should discard touches when the view's
4370 * window is obscured by another visible window.
4371 * Refer to the {@link View} security documentation for more details.
4372 *
4373 * @param enabled True if touch filtering should be enabled.
4374 *
4375 * @see #getFilterTouchesWhenObscured
4376 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4377 */
4378 public void setFilterTouchesWhenObscured(boolean enabled) {
4379 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4380 FILTER_TOUCHES_WHEN_OBSCURED);
4381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004382
4383 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004384 * Indicates whether the entire hierarchy under this view will save its
4385 * state when a state saving traversal occurs from its parent. The default
4386 * is true; if false, these views will not be saved unless
4387 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4388 *
4389 * @return Returns true if the view state saving from parent is enabled, else false.
4390 *
4391 * @see #setSaveFromParentEnabled(boolean)
4392 */
4393 public boolean isSaveFromParentEnabled() {
4394 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4395 }
4396
4397 /**
4398 * Controls whether the entire hierarchy under this view will save its
4399 * state when a state saving traversal occurs from its parent. The default
4400 * is true; if false, these views will not be saved unless
4401 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4402 *
4403 * @param enabled Set to false to <em>disable</em> state saving, or true
4404 * (the default) to allow it.
4405 *
4406 * @see #isSaveFromParentEnabled()
4407 * @see #setId(int)
4408 * @see #onSaveInstanceState()
4409 */
4410 public void setSaveFromParentEnabled(boolean enabled) {
4411 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4412 }
4413
4414
4415 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004416 * Returns whether this View is able to take focus.
4417 *
4418 * @return True if this view can take focus, or false otherwise.
4419 * @attr ref android.R.styleable#View_focusable
4420 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004421 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004422 public final boolean isFocusable() {
4423 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4424 }
4425
4426 /**
4427 * When a view is focusable, it may not want to take focus when in touch mode.
4428 * For example, a button would like focus when the user is navigating via a D-pad
4429 * so that the user can click on it, but once the user starts touching the screen,
4430 * the button shouldn't take focus
4431 * @return Whether the view is focusable in touch mode.
4432 * @attr ref android.R.styleable#View_focusableInTouchMode
4433 */
4434 @ViewDebug.ExportedProperty
4435 public final boolean isFocusableInTouchMode() {
4436 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4437 }
4438
4439 /**
4440 * Find the nearest view in the specified direction that can take focus.
4441 * This does not actually give focus to that view.
4442 *
4443 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4444 *
4445 * @return The nearest focusable in the specified direction, or null if none
4446 * can be found.
4447 */
4448 public View focusSearch(int direction) {
4449 if (mParent != null) {
4450 return mParent.focusSearch(this, direction);
4451 } else {
4452 return null;
4453 }
4454 }
4455
4456 /**
4457 * This method is the last chance for the focused view and its ancestors to
4458 * respond to an arrow key. This is called when the focused view did not
4459 * consume the key internally, nor could the view system find a new view in
4460 * the requested direction to give focus to.
4461 *
4462 * @param focused The currently focused view.
4463 * @param direction The direction focus wants to move. One of FOCUS_UP,
4464 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4465 * @return True if the this view consumed this unhandled move.
4466 */
4467 public boolean dispatchUnhandledMove(View focused, int direction) {
4468 return false;
4469 }
4470
4471 /**
4472 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004473 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004474 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004475 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4476 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004477 * @return The user specified next view, or null if there is none.
4478 */
4479 View findUserSetNextFocus(View root, int direction) {
4480 switch (direction) {
4481 case FOCUS_LEFT:
4482 if (mNextFocusLeftId == View.NO_ID) return null;
4483 return findViewShouldExist(root, mNextFocusLeftId);
4484 case FOCUS_RIGHT:
4485 if (mNextFocusRightId == View.NO_ID) return null;
4486 return findViewShouldExist(root, mNextFocusRightId);
4487 case FOCUS_UP:
4488 if (mNextFocusUpId == View.NO_ID) return null;
4489 return findViewShouldExist(root, mNextFocusUpId);
4490 case FOCUS_DOWN:
4491 if (mNextFocusDownId == View.NO_ID) return null;
4492 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004493 case FOCUS_FORWARD:
4494 if (mNextFocusForwardId == View.NO_ID) return null;
4495 return findViewShouldExist(root, mNextFocusForwardId);
4496 case FOCUS_BACKWARD: {
4497 final int id = mID;
4498 return root.findViewByPredicate(new Predicate<View>() {
4499 @Override
4500 public boolean apply(View t) {
4501 return t.mNextFocusForwardId == id;
4502 }
4503 });
4504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 }
4506 return null;
4507 }
4508
4509 private static View findViewShouldExist(View root, int childViewId) {
4510 View result = root.findViewById(childViewId);
4511 if (result == null) {
4512 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4513 + "by user for id " + childViewId);
4514 }
4515 return result;
4516 }
4517
4518 /**
4519 * Find and return all focusable views that are descendants of this view,
4520 * possibly including this view if it is focusable itself.
4521 *
4522 * @param direction The direction of the focus
4523 * @return A list of focusable views
4524 */
4525 public ArrayList<View> getFocusables(int direction) {
4526 ArrayList<View> result = new ArrayList<View>(24);
4527 addFocusables(result, direction);
4528 return result;
4529 }
4530
4531 /**
4532 * Add any focusable views that are descendants of this view (possibly
4533 * including this view if it is focusable itself) to views. If we are in touch mode,
4534 * only add views that are also focusable in touch mode.
4535 *
4536 * @param views Focusable views found so far
4537 * @param direction The direction of the focus
4538 */
4539 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004540 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4541 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004542
svetoslavganov75986cf2009-05-14 22:28:01 -07004543 /**
4544 * Adds any focusable views that are descendants of this view (possibly
4545 * including this view if it is focusable itself) to views. This method
4546 * adds all focusable views regardless if we are in touch mode or
4547 * only views focusable in touch mode if we are in touch mode depending on
4548 * the focusable mode paramater.
4549 *
4550 * @param views Focusable views found so far or null if all we are interested is
4551 * the number of focusables.
4552 * @param direction The direction of the focus.
4553 * @param focusableMode The type of focusables to be added.
4554 *
4555 * @see #FOCUSABLES_ALL
4556 * @see #FOCUSABLES_TOUCH_MODE
4557 */
4558 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4559 if (!isFocusable()) {
4560 return;
4561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004562
svetoslavganov75986cf2009-05-14 22:28:01 -07004563 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4564 isInTouchMode() && !isFocusableInTouchMode()) {
4565 return;
4566 }
4567
4568 if (views != null) {
4569 views.add(this);
4570 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004571 }
4572
4573 /**
4574 * Find and return all touchable views that are descendants of this view,
4575 * possibly including this view if it is touchable itself.
4576 *
4577 * @return A list of touchable views
4578 */
4579 public ArrayList<View> getTouchables() {
4580 ArrayList<View> result = new ArrayList<View>();
4581 addTouchables(result);
4582 return result;
4583 }
4584
4585 /**
4586 * Add any touchable views that are descendants of this view (possibly
4587 * including this view if it is touchable itself) to views.
4588 *
4589 * @param views Touchable views found so far
4590 */
4591 public void addTouchables(ArrayList<View> views) {
4592 final int viewFlags = mViewFlags;
4593
4594 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4595 && (viewFlags & ENABLED_MASK) == ENABLED) {
4596 views.add(this);
4597 }
4598 }
4599
4600 /**
4601 * Call this to try to give focus to a specific view or to one of its
4602 * descendants.
4603 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004604 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4605 * false), or if it is focusable and it is not focusable in touch mode
4606 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004607 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004608 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004609 * have focus, and you want your parent to look for the next one.
4610 *
4611 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4612 * {@link #FOCUS_DOWN} and <code>null</code>.
4613 *
4614 * @return Whether this view or one of its descendants actually took focus.
4615 */
4616 public final boolean requestFocus() {
4617 return requestFocus(View.FOCUS_DOWN);
4618 }
4619
4620
4621 /**
4622 * Call this to try to give focus to a specific view or to one of its
4623 * descendants and give it a hint about what direction focus is heading.
4624 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004625 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4626 * false), or if it is focusable and it is not focusable in touch mode
4627 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004628 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004629 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004630 * have focus, and you want your parent to look for the next one.
4631 *
4632 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4633 * <code>null</code> set for the previously focused rectangle.
4634 *
4635 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4636 * @return Whether this view or one of its descendants actually took focus.
4637 */
4638 public final boolean requestFocus(int direction) {
4639 return requestFocus(direction, null);
4640 }
4641
4642 /**
4643 * Call this to try to give focus to a specific view or to one of its descendants
4644 * and give it hints about the direction and a specific rectangle that the focus
4645 * is coming from. The rectangle can help give larger views a finer grained hint
4646 * about where focus is coming from, and therefore, where to show selection, or
4647 * forward focus change internally.
4648 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004649 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4650 * false), or if it is focusable and it is not focusable in touch mode
4651 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004652 *
4653 * A View will not take focus if it is not visible.
4654 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004655 * A View will not take focus if one of its parents has
4656 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
4657 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004658 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004659 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004660 * have focus, and you want your parent to look for the next one.
4661 *
4662 * You may wish to override this method if your custom {@link View} has an internal
4663 * {@link View} that it wishes to forward the request to.
4664 *
4665 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4666 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
4667 * to give a finer grained hint about where focus is coming from. May be null
4668 * if there is no hint.
4669 * @return Whether this view or one of its descendants actually took focus.
4670 */
4671 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
4672 // need to be focusable
4673 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
4674 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4675 return false;
4676 }
4677
4678 // need to be focusable in touch mode if in touch mode
4679 if (isInTouchMode() &&
4680 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
4681 return false;
4682 }
4683
4684 // need to not have any parents blocking us
4685 if (hasAncestorThatBlocksDescendantFocus()) {
4686 return false;
4687 }
4688
4689 handleFocusGainInternal(direction, previouslyFocusedRect);
4690 return true;
4691 }
4692
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004693 /** Gets the ViewAncestor, or null if not attached. */
4694 /*package*/ ViewAncestor getViewAncestor() {
Christopher Tate2c095f32010-10-04 14:13:40 -07004695 View root = getRootView();
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004696 return root != null ? (ViewAncestor)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07004697 }
4698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004699 /**
4700 * Call this to try to give focus to a specific view or to one of its descendants. This is a
4701 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
4702 * touch mode to request focus when they are touched.
4703 *
4704 * @return Whether this view or one of its descendants actually took focus.
4705 *
4706 * @see #isInTouchMode()
4707 *
4708 */
4709 public final boolean requestFocusFromTouch() {
4710 // Leave touch mode if we need to
4711 if (isInTouchMode()) {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004712 ViewAncestor viewRoot = getViewAncestor();
Christopher Tate2c095f32010-10-04 14:13:40 -07004713 if (viewRoot != null) {
4714 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004715 }
4716 }
4717 return requestFocus(View.FOCUS_DOWN);
4718 }
4719
4720 /**
4721 * @return Whether any ancestor of this view blocks descendant focus.
4722 */
4723 private boolean hasAncestorThatBlocksDescendantFocus() {
4724 ViewParent ancestor = mParent;
4725 while (ancestor instanceof ViewGroup) {
4726 final ViewGroup vgAncestor = (ViewGroup) ancestor;
4727 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
4728 return true;
4729 } else {
4730 ancestor = vgAncestor.getParent();
4731 }
4732 }
4733 return false;
4734 }
4735
4736 /**
Romain Guya440b002010-02-24 15:57:54 -08004737 * @hide
4738 */
4739 public void dispatchStartTemporaryDetach() {
4740 onStartTemporaryDetach();
4741 }
4742
4743 /**
4744 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004745 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
4746 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08004747 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004748 */
4749 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08004750 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08004751 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08004752 }
4753
4754 /**
4755 * @hide
4756 */
4757 public void dispatchFinishTemporaryDetach() {
4758 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004759 }
Romain Guy8506ab42009-06-11 17:35:47 -07004760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004761 /**
4762 * Called after {@link #onStartTemporaryDetach} when the container is done
4763 * changing the view.
4764 */
4765 public void onFinishTemporaryDetach() {
4766 }
Romain Guy8506ab42009-06-11 17:35:47 -07004767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004768 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004769 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
4770 * for this view's window. Returns null if the view is not currently attached
4771 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07004772 * just use the standard high-level event callbacks like
4773 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004774 */
4775 public KeyEvent.DispatcherState getKeyDispatcherState() {
4776 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
4777 }
Joe Malin32736f02011-01-19 16:14:20 -08004778
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004779 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004780 * Dispatch a key event before it is processed by any input method
4781 * associated with the view hierarchy. This can be used to intercept
4782 * key events in special situations before the IME consumes them; a
4783 * typical example would be handling the BACK key to update the application's
4784 * UI instead of allowing the IME to see it and close itself.
4785 *
4786 * @param event The key event to be dispatched.
4787 * @return True if the event was handled, false otherwise.
4788 */
4789 public boolean dispatchKeyEventPreIme(KeyEvent event) {
4790 return onKeyPreIme(event.getKeyCode(), event);
4791 }
4792
4793 /**
4794 * Dispatch a key event to the next view on the focus path. This path runs
4795 * from the top of the view tree down to the currently focused view. If this
4796 * view has focus, it will dispatch to itself. Otherwise it will dispatch
4797 * the next node down the focus path. This method also fires any key
4798 * listeners.
4799 *
4800 * @param event The key event to be dispatched.
4801 * @return True if the event was handled, false otherwise.
4802 */
4803 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004804 if (mInputEventConsistencyVerifier != null) {
4805 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
4806 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004807
Jeff Brown21bc5c92011-02-28 18:27:14 -08004808 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07004809 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004810 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4811 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
4812 return true;
4813 }
4814
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004815 if (event.dispatch(this, mAttachInfo != null
4816 ? mAttachInfo.mKeyDispatchState : null, this)) {
4817 return true;
4818 }
4819
4820 if (mInputEventConsistencyVerifier != null) {
4821 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4822 }
4823 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004824 }
4825
4826 /**
4827 * Dispatches a key shortcut event.
4828 *
4829 * @param event The key event to be dispatched.
4830 * @return True if the event was handled by the view, false otherwise.
4831 */
4832 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
4833 return onKeyShortcut(event.getKeyCode(), event);
4834 }
4835
4836 /**
4837 * Pass the touch screen motion event down to the target view, or this
4838 * view if it is the target.
4839 *
4840 * @param event The motion event to be dispatched.
4841 * @return True if the event was handled by the view, false otherwise.
4842 */
4843 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004844 if (mInputEventConsistencyVerifier != null) {
4845 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
4846 }
4847
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004848 if (onFilterTouchEventForSecurity(event)) {
4849 //noinspection SimplifiableIfStatement
4850 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
4851 mOnTouchListener.onTouch(this, event)) {
4852 return true;
4853 }
4854
4855 if (onTouchEvent(event)) {
4856 return true;
4857 }
Jeff Brown85a31762010-09-01 17:01:00 -07004858 }
4859
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004860 if (mInputEventConsistencyVerifier != null) {
4861 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004862 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004863 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004864 }
4865
4866 /**
Jeff Brown85a31762010-09-01 17:01:00 -07004867 * Filter the touch event to apply security policies.
4868 *
4869 * @param event The motion event to be filtered.
4870 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08004871 *
Jeff Brown85a31762010-09-01 17:01:00 -07004872 * @see #getFilterTouchesWhenObscured
4873 */
4874 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07004875 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07004876 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
4877 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
4878 // Window is obscured, drop this touch.
4879 return false;
4880 }
4881 return true;
4882 }
4883
4884 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004885 * Pass a trackball motion event down to the focused view.
4886 *
4887 * @param event The motion event to be dispatched.
4888 * @return True if the event was handled by the view, false otherwise.
4889 */
4890 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004891 if (mInputEventConsistencyVerifier != null) {
4892 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
4893 }
4894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004895 //Log.i("view", "view=" + this + ", " + event.toString());
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004896 if (onTrackballEvent(event)) {
4897 return true;
4898 }
4899
4900 if (mInputEventConsistencyVerifier != null) {
4901 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4902 }
4903 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004904 }
4905
4906 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004907 * Dispatch a generic motion event.
4908 * <p>
4909 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
4910 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08004911 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07004912 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08004913 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08004914 *
4915 * @param event The motion event to be dispatched.
4916 * @return True if the event was handled by the view, false otherwise.
4917 */
4918 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004919 if (mInputEventConsistencyVerifier != null) {
4920 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
4921 }
4922
Jeff Browna032cc02011-03-07 16:56:21 -08004923 final int source = event.getSource();
4924 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
4925 final int action = event.getAction();
4926 if (action == MotionEvent.ACTION_HOVER_ENTER
4927 || action == MotionEvent.ACTION_HOVER_MOVE
4928 || action == MotionEvent.ACTION_HOVER_EXIT) {
4929 if (dispatchHoverEvent(event)) {
4930 return true;
4931 }
4932 } else if (dispatchGenericPointerEvent(event)) {
4933 return true;
4934 }
4935 } else if (dispatchGenericFocusedEvent(event)) {
4936 return true;
4937 }
4938
Romain Guy7b5b6ab2011-03-14 18:05:08 -07004939 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08004940 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4941 && mOnGenericMotionListener.onGenericMotion(this, event)) {
4942 return true;
4943 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004944
4945 if (onGenericMotionEvent(event)) {
4946 return true;
4947 }
4948
4949 if (mInputEventConsistencyVerifier != null) {
4950 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4951 }
4952 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08004953 }
4954
4955 /**
Jeff Browna032cc02011-03-07 16:56:21 -08004956 * Dispatch a hover event.
4957 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07004958 * Do not call this method directly.
4959 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08004960 * </p>
4961 *
4962 * @param event The motion event to be dispatched.
4963 * @return True if the event was handled by the view, false otherwise.
4964 * @hide
4965 */
4966 protected boolean dispatchHoverEvent(MotionEvent event) {
4967 return onHoverEvent(event);
4968 }
4969
4970 /**
4971 * Dispatch a generic motion event to the view under the first pointer.
4972 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07004973 * Do not call this method directly.
4974 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08004975 * </p>
4976 *
4977 * @param event The motion event to be dispatched.
4978 * @return True if the event was handled by the view, false otherwise.
4979 * @hide
4980 */
4981 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
4982 return false;
4983 }
4984
4985 /**
4986 * Dispatch a generic motion event to the currently focused view.
4987 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07004988 * Do not call this method directly.
4989 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08004990 * </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 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
4997 return false;
4998 }
4999
5000 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005001 * Dispatch a pointer event.
5002 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005003 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5004 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5005 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005006 * and should not be expected to handle other pointing device features.
5007 * </p>
5008 *
5009 * @param event The motion event to be dispatched.
5010 * @return True if the event was handled by the view, false otherwise.
5011 * @hide
5012 */
5013 public final boolean dispatchPointerEvent(MotionEvent event) {
5014 if (event.isTouchEvent()) {
5015 return dispatchTouchEvent(event);
5016 } else {
5017 return dispatchGenericMotionEvent(event);
5018 }
5019 }
5020
5021 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005022 * Called when the window containing this view gains or loses window focus.
5023 * ViewGroups should override to route to their children.
5024 *
5025 * @param hasFocus True if the window containing this view now has focus,
5026 * false otherwise.
5027 */
5028 public void dispatchWindowFocusChanged(boolean hasFocus) {
5029 onWindowFocusChanged(hasFocus);
5030 }
5031
5032 /**
5033 * Called when the window containing this view gains or loses focus. Note
5034 * that this is separate from view focus: to receive key events, both
5035 * your view and its window must have focus. If a window is displayed
5036 * on top of yours that takes input focus, then your own window will lose
5037 * focus but the view focus will remain unchanged.
5038 *
5039 * @param hasWindowFocus True if the window containing this view now has
5040 * focus, false otherwise.
5041 */
5042 public void onWindowFocusChanged(boolean hasWindowFocus) {
5043 InputMethodManager imm = InputMethodManager.peekInstance();
5044 if (!hasWindowFocus) {
5045 if (isPressed()) {
5046 setPressed(false);
5047 }
5048 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5049 imm.focusOut(this);
5050 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005051 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005052 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005053 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005054 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5055 imm.focusIn(this);
5056 }
5057 refreshDrawableState();
5058 }
5059
5060 /**
5061 * Returns true if this view is in a window that currently has window focus.
5062 * Note that this is not the same as the view itself having focus.
5063 *
5064 * @return True if this view is in a window that currently has window focus.
5065 */
5066 public boolean hasWindowFocus() {
5067 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5068 }
5069
5070 /**
Adam Powell326d8082009-12-09 15:10:07 -08005071 * Dispatch a view visibility change down the view hierarchy.
5072 * ViewGroups should override to route to their children.
5073 * @param changedView The view whose visibility changed. Could be 'this' or
5074 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005075 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5076 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005077 */
5078 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5079 onVisibilityChanged(changedView, visibility);
5080 }
5081
5082 /**
5083 * Called when the visibility of the view or an ancestor of the view is changed.
5084 * @param changedView The view whose visibility changed. Could be 'this' or
5085 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005086 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5087 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005088 */
5089 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005090 if (visibility == VISIBLE) {
5091 if (mAttachInfo != null) {
5092 initialAwakenScrollBars();
5093 } else {
5094 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5095 }
5096 }
Adam Powell326d8082009-12-09 15:10:07 -08005097 }
5098
5099 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005100 * Dispatch a hint about whether this view is displayed. For instance, when
5101 * a View moves out of the screen, it might receives a display hint indicating
5102 * the view is not displayed. Applications should not <em>rely</em> on this hint
5103 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005104 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005105 * @param hint A hint about whether or not this view is displayed:
5106 * {@link #VISIBLE} or {@link #INVISIBLE}.
5107 */
5108 public void dispatchDisplayHint(int hint) {
5109 onDisplayHint(hint);
5110 }
5111
5112 /**
5113 * Gives this view a hint about whether is displayed or not. For instance, when
5114 * a View moves out of the screen, it might receives a display hint indicating
5115 * the view is not displayed. Applications should not <em>rely</em> on this hint
5116 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005117 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005118 * @param hint A hint about whether or not this view is displayed:
5119 * {@link #VISIBLE} or {@link #INVISIBLE}.
5120 */
5121 protected void onDisplayHint(int hint) {
5122 }
5123
5124 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005125 * Dispatch a window visibility change down the view hierarchy.
5126 * ViewGroups should override to route to their children.
5127 *
5128 * @param visibility The new visibility of the window.
5129 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005130 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005131 */
5132 public void dispatchWindowVisibilityChanged(int visibility) {
5133 onWindowVisibilityChanged(visibility);
5134 }
5135
5136 /**
5137 * Called when the window containing has change its visibility
5138 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5139 * that this tells you whether or not your window is being made visible
5140 * to the window manager; this does <em>not</em> tell you whether or not
5141 * your window is obscured by other windows on the screen, even if it
5142 * is itself visible.
5143 *
5144 * @param visibility The new visibility of the window.
5145 */
5146 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005147 if (visibility == VISIBLE) {
5148 initialAwakenScrollBars();
5149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005150 }
5151
5152 /**
5153 * Returns the current visibility of the window this view is attached to
5154 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5155 *
5156 * @return Returns the current visibility of the view's window.
5157 */
5158 public int getWindowVisibility() {
5159 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5160 }
5161
5162 /**
5163 * Retrieve the overall visible display size in which the window this view is
5164 * attached to has been positioned in. This takes into account screen
5165 * decorations above the window, for both cases where the window itself
5166 * is being position inside of them or the window is being placed under
5167 * then and covered insets are used for the window to position its content
5168 * inside. In effect, this tells you the available area where content can
5169 * be placed and remain visible to users.
5170 *
5171 * <p>This function requires an IPC back to the window manager to retrieve
5172 * the requested information, so should not be used in performance critical
5173 * code like drawing.
5174 *
5175 * @param outRect Filled in with the visible display frame. If the view
5176 * is not attached to a window, this is simply the raw display size.
5177 */
5178 public void getWindowVisibleDisplayFrame(Rect outRect) {
5179 if (mAttachInfo != null) {
5180 try {
5181 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5182 } catch (RemoteException e) {
5183 return;
5184 }
5185 // XXX This is really broken, and probably all needs to be done
5186 // in the window manager, and we need to know more about whether
5187 // we want the area behind or in front of the IME.
5188 final Rect insets = mAttachInfo.mVisibleInsets;
5189 outRect.left += insets.left;
5190 outRect.top += insets.top;
5191 outRect.right -= insets.right;
5192 outRect.bottom -= insets.bottom;
5193 return;
5194 }
5195 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005196 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005197 }
5198
5199 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005200 * Dispatch a notification about a resource configuration change down
5201 * the view hierarchy.
5202 * ViewGroups should override to route to their children.
5203 *
5204 * @param newConfig The new resource configuration.
5205 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005206 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005207 */
5208 public void dispatchConfigurationChanged(Configuration newConfig) {
5209 onConfigurationChanged(newConfig);
5210 }
5211
5212 /**
5213 * Called when the current configuration of the resources being used
5214 * by the application have changed. You can use this to decide when
5215 * to reload resources that can changed based on orientation and other
5216 * configuration characterstics. You only need to use this if you are
5217 * not relying on the normal {@link android.app.Activity} mechanism of
5218 * recreating the activity instance upon a configuration change.
5219 *
5220 * @param newConfig The new resource configuration.
5221 */
5222 protected void onConfigurationChanged(Configuration newConfig) {
5223 }
5224
5225 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005226 * Private function to aggregate all per-view attributes in to the view
5227 * root.
5228 */
5229 void dispatchCollectViewAttributes(int visibility) {
5230 performCollectViewAttributes(visibility);
5231 }
5232
5233 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005234 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005235 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5236 mAttachInfo.mKeepScreenOn = true;
5237 }
5238 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5239 if (mOnSystemUiVisibilityChangeListener != null) {
5240 mAttachInfo.mHasSystemUiListeners = true;
5241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005242 }
5243 }
5244
5245 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005246 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005247 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005248 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5249 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005250 ai.mRecomputeGlobalAttributes = true;
5251 }
5252 }
5253 }
5254
5255 /**
5256 * Returns whether the device is currently in touch mode. Touch mode is entered
5257 * once the user begins interacting with the device by touch, and affects various
5258 * things like whether focus is always visible to the user.
5259 *
5260 * @return Whether the device is in touch mode.
5261 */
5262 @ViewDebug.ExportedProperty
5263 public boolean isInTouchMode() {
5264 if (mAttachInfo != null) {
5265 return mAttachInfo.mInTouchMode;
5266 } else {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005267 return ViewAncestor.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005268 }
5269 }
5270
5271 /**
5272 * Returns the context the view is running in, through which it can
5273 * access the current theme, resources, etc.
5274 *
5275 * @return The view's Context.
5276 */
5277 @ViewDebug.CapturedViewProperty
5278 public final Context getContext() {
5279 return mContext;
5280 }
5281
5282 /**
5283 * Handle a key event before it is processed by any input method
5284 * associated with the view hierarchy. This can be used to intercept
5285 * key events in special situations before the IME consumes them; a
5286 * typical example would be handling the BACK key to update the application's
5287 * UI instead of allowing the IME to see it and close itself.
5288 *
5289 * @param keyCode The value in event.getKeyCode().
5290 * @param event Description of the key event.
5291 * @return If you handled the event, return true. If you want to allow the
5292 * event to be handled by the next receiver, return false.
5293 */
5294 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5295 return false;
5296 }
5297
5298 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005299 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5300 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005301 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5302 * is released, if the view is enabled and clickable.
5303 *
5304 * @param keyCode A key code that represents the button pressed, from
5305 * {@link android.view.KeyEvent}.
5306 * @param event The KeyEvent object that defines the button action.
5307 */
5308 public boolean onKeyDown(int keyCode, KeyEvent event) {
5309 boolean result = false;
5310
5311 switch (keyCode) {
5312 case KeyEvent.KEYCODE_DPAD_CENTER:
5313 case KeyEvent.KEYCODE_ENTER: {
5314 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5315 return true;
5316 }
5317 // Long clickable items don't necessarily have to be clickable
5318 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5319 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5320 (event.getRepeatCount() == 0)) {
5321 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005322 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005323 return true;
5324 }
5325 break;
5326 }
5327 }
5328 return result;
5329 }
5330
5331 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005332 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5333 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5334 * the event).
5335 */
5336 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5337 return false;
5338 }
5339
5340 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005341 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5342 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005343 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5344 * {@link KeyEvent#KEYCODE_ENTER} is released.
5345 *
5346 * @param keyCode A key code that represents the button pressed, from
5347 * {@link android.view.KeyEvent}.
5348 * @param event The KeyEvent object that defines the button action.
5349 */
5350 public boolean onKeyUp(int keyCode, KeyEvent event) {
5351 boolean result = false;
5352
5353 switch (keyCode) {
5354 case KeyEvent.KEYCODE_DPAD_CENTER:
5355 case KeyEvent.KEYCODE_ENTER: {
5356 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5357 return true;
5358 }
5359 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5360 setPressed(false);
5361
5362 if (!mHasPerformedLongPress) {
5363 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005364 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005365
5366 result = performClick();
5367 }
5368 }
5369 break;
5370 }
5371 }
5372 return result;
5373 }
5374
5375 /**
5376 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5377 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5378 * the event).
5379 *
5380 * @param keyCode A key code that represents the button pressed, from
5381 * {@link android.view.KeyEvent}.
5382 * @param repeatCount The number of times the action was made.
5383 * @param event The KeyEvent object that defines the button action.
5384 */
5385 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5386 return false;
5387 }
5388
5389 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005390 * Called on the focused view when a key shortcut event is not handled.
5391 * Override this method to implement local key shortcuts for the View.
5392 * Key shortcuts can also be implemented by setting the
5393 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005394 *
5395 * @param keyCode The value in event.getKeyCode().
5396 * @param event Description of the key event.
5397 * @return If you handled the event, return true. If you want to allow the
5398 * event to be handled by the next receiver, return false.
5399 */
5400 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5401 return false;
5402 }
5403
5404 /**
5405 * Check whether the called view is a text editor, in which case it
5406 * would make sense to automatically display a soft input window for
5407 * it. Subclasses should override this if they implement
5408 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005409 * a call on that method would return a non-null InputConnection, and
5410 * they are really a first-class editor that the user would normally
5411 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005412 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005413 * <p>The default implementation always returns false. This does
5414 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5415 * will not be called or the user can not otherwise perform edits on your
5416 * view; it is just a hint to the system that this is not the primary
5417 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005418 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005419 * @return Returns true if this view is a text editor, else false.
5420 */
5421 public boolean onCheckIsTextEditor() {
5422 return false;
5423 }
Romain Guy8506ab42009-06-11 17:35:47 -07005424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005425 /**
5426 * Create a new InputConnection for an InputMethod to interact
5427 * with the view. The default implementation returns null, since it doesn't
5428 * support input methods. You can override this to implement such support.
5429 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005430 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005431 * <p>When implementing this, you probably also want to implement
5432 * {@link #onCheckIsTextEditor()} to indicate you will return a
5433 * non-null InputConnection.
5434 *
5435 * @param outAttrs Fill in with attribute information about the connection.
5436 */
5437 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5438 return null;
5439 }
5440
5441 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005442 * Called by the {@link android.view.inputmethod.InputMethodManager}
5443 * when a view who is not the current
5444 * input connection target is trying to make a call on the manager. The
5445 * default implementation returns false; you can override this to return
5446 * true for certain views if you are performing InputConnection proxying
5447 * to them.
5448 * @param view The View that is making the InputMethodManager call.
5449 * @return Return true to allow the call, false to reject.
5450 */
5451 public boolean checkInputConnectionProxy(View view) {
5452 return false;
5453 }
Romain Guy8506ab42009-06-11 17:35:47 -07005454
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005455 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005456 * Show the context menu for this view. It is not safe to hold on to the
5457 * menu after returning from this method.
5458 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005459 * You should normally not overload this method. Overload
5460 * {@link #onCreateContextMenu(ContextMenu)} or define an
5461 * {@link OnCreateContextMenuListener} to add items to the context menu.
5462 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005463 * @param menu The context menu to populate
5464 */
5465 public void createContextMenu(ContextMenu menu) {
5466 ContextMenuInfo menuInfo = getContextMenuInfo();
5467
5468 // Sets the current menu info so all items added to menu will have
5469 // my extra info set.
5470 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5471
5472 onCreateContextMenu(menu);
5473 if (mOnCreateContextMenuListener != null) {
5474 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5475 }
5476
5477 // Clear the extra information so subsequent items that aren't mine don't
5478 // have my extra info.
5479 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5480
5481 if (mParent != null) {
5482 mParent.createContextMenu(menu);
5483 }
5484 }
5485
5486 /**
5487 * Views should implement this if they have extra information to associate
5488 * with the context menu. The return result is supplied as a parameter to
5489 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5490 * callback.
5491 *
5492 * @return Extra information about the item for which the context menu
5493 * should be shown. This information will vary across different
5494 * subclasses of View.
5495 */
5496 protected ContextMenuInfo getContextMenuInfo() {
5497 return null;
5498 }
5499
5500 /**
5501 * Views should implement this if the view itself is going to add items to
5502 * the context menu.
5503 *
5504 * @param menu the context menu to populate
5505 */
5506 protected void onCreateContextMenu(ContextMenu menu) {
5507 }
5508
5509 /**
5510 * Implement this method to handle trackball motion events. The
5511 * <em>relative</em> movement of the trackball since the last event
5512 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5513 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5514 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5515 * they will often be fractional values, representing the more fine-grained
5516 * movement information available from a trackball).
5517 *
5518 * @param event The motion event.
5519 * @return True if the event was handled, false otherwise.
5520 */
5521 public boolean onTrackballEvent(MotionEvent event) {
5522 return false;
5523 }
5524
5525 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005526 * Implement this method to handle generic motion events.
5527 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005528 * Generic motion events describe joystick movements, mouse hovers, track pad
5529 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005530 * {@link MotionEvent#getSource() source} of the motion event specifies
5531 * the class of input that was received. Implementations of this method
5532 * must examine the bits in the source before processing the event.
5533 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005534 * </p><p>
5535 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5536 * are delivered to the view under the pointer. All other generic motion events are
5537 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005538 * </p>
5539 * <code>
5540 * public boolean onGenericMotionEvent(MotionEvent event) {
5541 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005542 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5543 * // process the joystick movement...
5544 * return true;
5545 * }
5546 * }
5547 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5548 * switch (event.getAction()) {
5549 * case MotionEvent.ACTION_HOVER_MOVE:
5550 * // process the mouse hover movement...
5551 * return true;
5552 * case MotionEvent.ACTION_SCROLL:
5553 * // process the scroll wheel movement...
5554 * return true;
5555 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005556 * }
5557 * return super.onGenericMotionEvent(event);
5558 * }
5559 * </code>
5560 *
5561 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08005562 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08005563 */
5564 public boolean onGenericMotionEvent(MotionEvent event) {
5565 return false;
5566 }
5567
5568 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005569 * Implement this method to handle hover events.
5570 * <p>
5571 * Hover events are pointer events with action {@link MotionEvent#ACTION_HOVER_ENTER},
5572 * {@link MotionEvent#ACTION_HOVER_MOVE}, or {@link MotionEvent#ACTION_HOVER_EXIT}.
5573 * </p><p>
5574 * The view receives hover enter as the pointer enters the bounds of the view and hover
5575 * exit as the pointer exits the bound of the view or just before the pointer goes down
Romain Guy5c22a8c2011-05-13 11:48:45 -07005576 * (which implies that {@link #onTouchEvent(MotionEvent)} will be called soon).
Jeff Browna032cc02011-03-07 16:56:21 -08005577 * </p><p>
5578 * If the view would like to handle the hover event itself and prevent its children
5579 * from receiving hover, it should return true from this method. If this method returns
5580 * true and a child has already received a hover enter event, the child will
5581 * automatically receive a hover exit event.
5582 * </p><p>
5583 * The default implementation sets the hovered state of the view if the view is
5584 * clickable.
5585 * </p>
5586 *
5587 * @param event The motion event that describes the hover.
5588 * @return True if this view handled the hover event and does not want its children
5589 * to receive the hover event.
5590 */
5591 public boolean onHoverEvent(MotionEvent event) {
Jeff Browna032cc02011-03-07 16:56:21 -08005592 switch (event.getAction()) {
5593 case MotionEvent.ACTION_HOVER_ENTER:
5594 setHovered(true);
5595 break;
5596
5597 case MotionEvent.ACTION_HOVER_EXIT:
5598 setHovered(false);
5599 break;
5600 }
5601
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005602 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08005603 }
5604
5605 /**
5606 * Returns true if the view is currently hovered.
5607 *
5608 * @return True if the view is currently hovered.
5609 */
5610 public boolean isHovered() {
5611 return (mPrivateFlags & HOVERED) != 0;
5612 }
5613
5614 /**
5615 * Sets whether the view is currently hovered.
5616 *
5617 * @param hovered True if the view is hovered.
5618 */
5619 public void setHovered(boolean hovered) {
5620 if (hovered) {
5621 if ((mPrivateFlags & HOVERED) == 0) {
5622 mPrivateFlags |= HOVERED;
5623 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005624 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Jeff Browna032cc02011-03-07 16:56:21 -08005625 }
5626 } else {
5627 if ((mPrivateFlags & HOVERED) != 0) {
5628 mPrivateFlags &= ~HOVERED;
5629 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005630 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Jeff Browna032cc02011-03-07 16:56:21 -08005631 }
5632 }
5633 }
5634
5635 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005636 * Implement this method to handle touch screen motion events.
5637 *
5638 * @param event The motion event.
5639 * @return True if the event was handled, false otherwise.
5640 */
5641 public boolean onTouchEvent(MotionEvent event) {
5642 final int viewFlags = mViewFlags;
5643
5644 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07005645 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
5646 mPrivateFlags &= ~PRESSED;
5647 refreshDrawableState();
5648 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005649 // A disabled view that is clickable still consumes the touch
5650 // events, it just doesn't respond to them.
5651 return (((viewFlags & CLICKABLE) == CLICKABLE ||
5652 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
5653 }
5654
5655 if (mTouchDelegate != null) {
5656 if (mTouchDelegate.onTouchEvent(event)) {
5657 return true;
5658 }
5659 }
5660
5661 if (((viewFlags & CLICKABLE) == CLICKABLE ||
5662 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
5663 switch (event.getAction()) {
5664 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08005665 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
5666 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005667 // take focus if we don't have it already and we should in
5668 // touch mode.
5669 boolean focusTaken = false;
5670 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
5671 focusTaken = requestFocus();
5672 }
5673
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08005674 if (prepressed) {
5675 // The button is being released before we actually
5676 // showed it as pressed. Make it show the pressed
5677 // state now (before scheduling the click) to ensure
5678 // the user sees it.
5679 mPrivateFlags |= PRESSED;
5680 refreshDrawableState();
5681 }
Joe Malin32736f02011-01-19 16:14:20 -08005682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005683 if (!mHasPerformedLongPress) {
5684 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005685 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005686
5687 // Only perform take click actions if we were in the pressed state
5688 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08005689 // Use a Runnable and post this rather than calling
5690 // performClick directly. This lets other visual state
5691 // of the view update before click actions start.
5692 if (mPerformClick == null) {
5693 mPerformClick = new PerformClick();
5694 }
5695 if (!post(mPerformClick)) {
5696 performClick();
5697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005698 }
5699 }
5700
5701 if (mUnsetPressedState == null) {
5702 mUnsetPressedState = new UnsetPressedState();
5703 }
5704
Adam Powelle14579b2009-12-16 18:39:52 -08005705 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08005706 postDelayed(mUnsetPressedState,
5707 ViewConfiguration.getPressedStateDuration());
5708 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005709 // If the post failed, unpress right now
5710 mUnsetPressedState.run();
5711 }
Adam Powelle14579b2009-12-16 18:39:52 -08005712 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005713 }
5714 break;
5715
5716 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08005717 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005718
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005719 if (performButtonActionOnTouchDown(event)) {
5720 break;
5721 }
5722
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005723 // Walk up the hierarchy to determine if we're inside a scrolling container.
5724 boolean isInScrollingContainer = false;
5725 ViewParent p = getParent();
5726 while (p != null && p instanceof ViewGroup) {
5727 if (((ViewGroup) p).shouldDelayChildPressedState()) {
5728 isInScrollingContainer = true;
5729 break;
5730 }
5731 p = p.getParent();
5732 }
5733
5734 // For views inside a scrolling container, delay the pressed feedback for
5735 // a short period in case this is a scroll.
5736 if (isInScrollingContainer) {
5737 mPrivateFlags |= PREPRESSED;
5738 if (mPendingCheckForTap == null) {
5739 mPendingCheckForTap = new CheckForTap();
5740 }
5741 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
5742 } else {
5743 // Not inside a scrolling container, so show the feedback right away
5744 mPrivateFlags |= PRESSED;
5745 refreshDrawableState();
5746 checkForLongClick(0);
5747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005748 break;
5749
5750 case MotionEvent.ACTION_CANCEL:
5751 mPrivateFlags &= ~PRESSED;
5752 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08005753 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005754 break;
5755
5756 case MotionEvent.ACTION_MOVE:
5757 final int x = (int) event.getX();
5758 final int y = (int) event.getY();
5759
5760 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07005761 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005762 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08005763 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005764 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08005765 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05005766 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005767
5768 // Need to switch from pressed to not pressed
5769 mPrivateFlags &= ~PRESSED;
5770 refreshDrawableState();
5771 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005772 }
5773 break;
5774 }
5775 return true;
5776 }
5777
5778 return false;
5779 }
5780
5781 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05005782 * Remove the longpress detection timer.
5783 */
5784 private void removeLongPressCallback() {
5785 if (mPendingCheckForLongPress != null) {
5786 removeCallbacks(mPendingCheckForLongPress);
5787 }
5788 }
Adam Powell3cb8b632011-01-21 15:34:14 -08005789
5790 /**
5791 * Remove the pending click action
5792 */
5793 private void removePerformClickCallback() {
5794 if (mPerformClick != null) {
5795 removeCallbacks(mPerformClick);
5796 }
5797 }
5798
Adam Powelle14579b2009-12-16 18:39:52 -08005799 /**
Romain Guya440b002010-02-24 15:57:54 -08005800 * Remove the prepress detection timer.
5801 */
5802 private void removeUnsetPressCallback() {
5803 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
5804 setPressed(false);
5805 removeCallbacks(mUnsetPressedState);
5806 }
5807 }
5808
5809 /**
Adam Powelle14579b2009-12-16 18:39:52 -08005810 * Remove the tap detection timer.
5811 */
5812 private void removeTapCallback() {
5813 if (mPendingCheckForTap != null) {
5814 mPrivateFlags &= ~PREPRESSED;
5815 removeCallbacks(mPendingCheckForTap);
5816 }
5817 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005818
5819 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005820 * Cancels a pending long press. Your subclass can use this if you
5821 * want the context menu to come up if the user presses and holds
5822 * at the same place, but you don't want it to come up if they press
5823 * and then move around enough to cause scrolling.
5824 */
5825 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005826 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08005827
5828 /*
5829 * The prepressed state handled by the tap callback is a display
5830 * construct, but the tap callback will post a long press callback
5831 * less its own timeout. Remove it here.
5832 */
5833 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005834 }
5835
5836 /**
5837 * Sets the TouchDelegate for this View.
5838 */
5839 public void setTouchDelegate(TouchDelegate delegate) {
5840 mTouchDelegate = delegate;
5841 }
5842
5843 /**
5844 * Gets the TouchDelegate for this View.
5845 */
5846 public TouchDelegate getTouchDelegate() {
5847 return mTouchDelegate;
5848 }
5849
5850 /**
5851 * Set flags controlling behavior of this view.
5852 *
5853 * @param flags Constant indicating the value which should be set
5854 * @param mask Constant indicating the bit range that should be changed
5855 */
5856 void setFlags(int flags, int mask) {
5857 int old = mViewFlags;
5858 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
5859
5860 int changed = mViewFlags ^ old;
5861 if (changed == 0) {
5862 return;
5863 }
5864 int privateFlags = mPrivateFlags;
5865
5866 /* Check if the FOCUSABLE bit has changed */
5867 if (((changed & FOCUSABLE_MASK) != 0) &&
5868 ((privateFlags & HAS_BOUNDS) !=0)) {
5869 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
5870 && ((privateFlags & FOCUSED) != 0)) {
5871 /* Give up focus if we are no longer focusable */
5872 clearFocus();
5873 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
5874 && ((privateFlags & FOCUSED) == 0)) {
5875 /*
5876 * Tell the view system that we are now available to take focus
5877 * if no one else already has it.
5878 */
5879 if (mParent != null) mParent.focusableViewAvailable(this);
5880 }
5881 }
5882
5883 if ((flags & VISIBILITY_MASK) == VISIBLE) {
5884 if ((changed & VISIBILITY_MASK) != 0) {
5885 /*
5886 * If this view is becoming visible, set the DRAWN flag so that
5887 * the next invalidate() will not be skipped.
5888 */
5889 mPrivateFlags |= DRAWN;
5890
5891 needGlobalAttributesUpdate(true);
5892
5893 // a view becoming visible is worth notifying the parent
5894 // about in case nothing has focus. even if this specific view
5895 // isn't focusable, it may contain something that is, so let
5896 // the root view try to give this focus if nothing else does.
5897 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
5898 mParent.focusableViewAvailable(this);
5899 }
5900 }
5901 }
5902
5903 /* Check if the GONE bit has changed */
5904 if ((changed & GONE) != 0) {
5905 needGlobalAttributesUpdate(false);
5906 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005907 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005908
Romain Guyecd80ee2009-12-03 17:13:02 -08005909 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
5910 if (hasFocus()) clearFocus();
5911 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005912 }
5913 if (mAttachInfo != null) {
5914 mAttachInfo.mViewVisibilityChanged = true;
5915 }
5916 }
5917
5918 /* Check if the VISIBLE bit has changed */
5919 if ((changed & INVISIBLE) != 0) {
5920 needGlobalAttributesUpdate(false);
Romain Guy0fd89bf2011-01-26 15:41:30 -08005921 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005922
5923 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
5924 // root view becoming invisible shouldn't clear focus
5925 if (getRootView() != this) {
5926 clearFocus();
5927 }
5928 }
5929 if (mAttachInfo != null) {
5930 mAttachInfo.mViewVisibilityChanged = true;
5931 }
5932 }
5933
Adam Powell326d8082009-12-09 15:10:07 -08005934 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07005935 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005936 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
5937 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07005938 }
Adam Powell326d8082009-12-09 15:10:07 -08005939 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
5940 }
5941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005942 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
5943 destroyDrawingCache();
5944 }
5945
5946 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
5947 destroyDrawingCache();
5948 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08005949 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005950 }
5951
5952 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
5953 destroyDrawingCache();
5954 mPrivateFlags &= ~DRAWING_CACHE_VALID;
5955 }
5956
5957 if ((changed & DRAW_MASK) != 0) {
5958 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
5959 if (mBGDrawable != null) {
5960 mPrivateFlags &= ~SKIP_DRAW;
5961 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
5962 } else {
5963 mPrivateFlags |= SKIP_DRAW;
5964 }
5965 } else {
5966 mPrivateFlags &= ~SKIP_DRAW;
5967 }
5968 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005969 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005970 }
5971
5972 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08005973 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005974 mParent.recomputeViewAttributes(this);
5975 }
5976 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005977
5978 if ((changed & HORIZONTAL_DIRECTION_MASK) != 0) {
5979 requestLayout();
5980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005981 }
5982
5983 /**
5984 * Change the view's z order in the tree, so it's on top of other sibling
5985 * views
5986 */
5987 public void bringToFront() {
5988 if (mParent != null) {
5989 mParent.bringChildToFront(this);
5990 }
5991 }
5992
5993 /**
5994 * This is called in response to an internal scroll in this view (i.e., the
5995 * view scrolled its own contents). This is typically as a result of
5996 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
5997 * called.
5998 *
5999 * @param l Current horizontal scroll origin.
6000 * @param t Current vertical scroll origin.
6001 * @param oldl Previous horizontal scroll origin.
6002 * @param oldt Previous vertical scroll origin.
6003 */
6004 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
6005 mBackgroundSizeChanged = true;
6006
6007 final AttachInfo ai = mAttachInfo;
6008 if (ai != null) {
6009 ai.mViewScrollChanged = true;
6010 }
6011 }
6012
6013 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006014 * Interface definition for a callback to be invoked when the layout bounds of a view
6015 * changes due to layout processing.
6016 */
6017 public interface OnLayoutChangeListener {
6018 /**
6019 * Called when the focus state of a view has changed.
6020 *
6021 * @param v The view whose state has changed.
6022 * @param left The new value of the view's left property.
6023 * @param top The new value of the view's top property.
6024 * @param right The new value of the view's right property.
6025 * @param bottom The new value of the view's bottom property.
6026 * @param oldLeft The previous value of the view's left property.
6027 * @param oldTop The previous value of the view's top property.
6028 * @param oldRight The previous value of the view's right property.
6029 * @param oldBottom The previous value of the view's bottom property.
6030 */
6031 void onLayoutChange(View v, int left, int top, int right, int bottom,
6032 int oldLeft, int oldTop, int oldRight, int oldBottom);
6033 }
6034
6035 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006036 * This is called during layout when the size of this view has changed. If
6037 * you were just added to the view hierarchy, you're called with the old
6038 * values of 0.
6039 *
6040 * @param w Current width of this view.
6041 * @param h Current height of this view.
6042 * @param oldw Old width of this view.
6043 * @param oldh Old height of this view.
6044 */
6045 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6046 }
6047
6048 /**
6049 * Called by draw to draw the child views. This may be overridden
6050 * by derived classes to gain control just before its children are drawn
6051 * (but after its own view has been drawn).
6052 * @param canvas the canvas on which to draw the view
6053 */
6054 protected void dispatchDraw(Canvas canvas) {
6055 }
6056
6057 /**
6058 * Gets the parent of this view. Note that the parent is a
6059 * ViewParent and not necessarily a View.
6060 *
6061 * @return Parent of this view.
6062 */
6063 public final ViewParent getParent() {
6064 return mParent;
6065 }
6066
6067 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006068 * Set the horizontal scrolled position of your view. This will cause a call to
6069 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6070 * invalidated.
6071 * @param value the x position to scroll to
6072 */
6073 public void setScrollX(int value) {
6074 scrollTo(value, mScrollY);
6075 }
6076
6077 /**
6078 * Set the vertical scrolled position of your view. This will cause a call to
6079 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6080 * invalidated.
6081 * @param value the y position to scroll to
6082 */
6083 public void setScrollY(int value) {
6084 scrollTo(mScrollX, value);
6085 }
6086
6087 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006088 * Return the scrolled left position of this view. This is the left edge of
6089 * the displayed part of your view. You do not need to draw any pixels
6090 * farther left, since those are outside of the frame of your view on
6091 * screen.
6092 *
6093 * @return The left edge of the displayed part of your view, in pixels.
6094 */
6095 public final int getScrollX() {
6096 return mScrollX;
6097 }
6098
6099 /**
6100 * Return the scrolled top position of this view. This is the top edge of
6101 * the displayed part of your view. You do not need to draw any pixels above
6102 * it, since those are outside of the frame of your view on screen.
6103 *
6104 * @return The top edge of the displayed part of your view, in pixels.
6105 */
6106 public final int getScrollY() {
6107 return mScrollY;
6108 }
6109
6110 /**
6111 * Return the width of the your view.
6112 *
6113 * @return The width of your view, in pixels.
6114 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006115 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006116 public final int getWidth() {
6117 return mRight - mLeft;
6118 }
6119
6120 /**
6121 * Return the height of your view.
6122 *
6123 * @return The height of your view, in pixels.
6124 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006125 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006126 public final int getHeight() {
6127 return mBottom - mTop;
6128 }
6129
6130 /**
6131 * Return the visible drawing bounds of your view. Fills in the output
6132 * rectangle with the values from getScrollX(), getScrollY(),
6133 * getWidth(), and getHeight().
6134 *
6135 * @param outRect The (scrolled) drawing bounds of the view.
6136 */
6137 public void getDrawingRect(Rect outRect) {
6138 outRect.left = mScrollX;
6139 outRect.top = mScrollY;
6140 outRect.right = mScrollX + (mRight - mLeft);
6141 outRect.bottom = mScrollY + (mBottom - mTop);
6142 }
6143
6144 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006145 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6146 * raw width component (that is the result is masked by
6147 * {@link #MEASURED_SIZE_MASK}).
6148 *
6149 * @return The raw measured width of this view.
6150 */
6151 public final int getMeasuredWidth() {
6152 return mMeasuredWidth & MEASURED_SIZE_MASK;
6153 }
6154
6155 /**
6156 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006157 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006158 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006159 * This should be used during measurement and layout calculations only. Use
6160 * {@link #getWidth()} to see how wide a view is after layout.
6161 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006162 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006163 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006164 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006165 return mMeasuredWidth;
6166 }
6167
6168 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006169 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6170 * raw width component (that is the result is masked by
6171 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006172 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006173 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006174 */
6175 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006176 return mMeasuredHeight & MEASURED_SIZE_MASK;
6177 }
6178
6179 /**
6180 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006181 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006182 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6183 * This should be used during measurement and layout calculations only. Use
6184 * {@link #getHeight()} to see how wide a view is after layout.
6185 *
6186 * @return The measured width of this view as a bit mask.
6187 */
6188 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006189 return mMeasuredHeight;
6190 }
6191
6192 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006193 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6194 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6195 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6196 * and the height component is at the shifted bits
6197 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6198 */
6199 public final int getMeasuredState() {
6200 return (mMeasuredWidth&MEASURED_STATE_MASK)
6201 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6202 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6203 }
6204
6205 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006206 * The transform matrix of this view, which is calculated based on the current
6207 * roation, scale, and pivot properties.
6208 *
6209 * @see #getRotation()
6210 * @see #getScaleX()
6211 * @see #getScaleY()
6212 * @see #getPivotX()
6213 * @see #getPivotY()
6214 * @return The current transform matrix for the view
6215 */
6216 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07006217 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07006218 return mMatrix;
6219 }
6220
6221 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006222 * Utility function to determine if the value is far enough away from zero to be
6223 * considered non-zero.
6224 * @param value A floating point value to check for zero-ness
6225 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6226 */
6227 private static boolean nonzero(float value) {
6228 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6229 }
6230
6231 /**
Jeff Brown86671742010-09-30 20:00:15 -07006232 * Returns true if the transform matrix is the identity matrix.
6233 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006234 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006235 * @return True if the transform matrix is the identity matrix, false otherwise.
6236 */
Jeff Brown86671742010-09-30 20:00:15 -07006237 final boolean hasIdentityMatrix() {
6238 updateMatrix();
6239 return mMatrixIsIdentity;
6240 }
6241
6242 /**
6243 * Recomputes the transform matrix if necessary.
6244 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006245 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07006246 if (mMatrixDirty) {
6247 // transform-related properties have changed since the last time someone
6248 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006249
6250 // Figure out if we need to update the pivot point
6251 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006252 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006253 mPrevWidth = mRight - mLeft;
6254 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08006255 mPivotX = mPrevWidth / 2f;
6256 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07006257 }
6258 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006259 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07006260 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
6261 mMatrix.setTranslate(mTranslationX, mTranslationY);
6262 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
6263 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
6264 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07006265 if (mCamera == null) {
6266 mCamera = new Camera();
6267 matrix3D = new Matrix();
6268 }
6269 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07006270 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08006271 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07006272 mCamera.getMatrix(matrix3D);
6273 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006274 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07006275 mMatrix.postConcat(matrix3D);
6276 mCamera.restore();
6277 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006278 mMatrixDirty = false;
6279 mMatrixIsIdentity = mMatrix.isIdentity();
6280 mInverseMatrixDirty = true;
6281 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006282 }
6283
6284 /**
6285 * Utility method to retrieve the inverse of the current mMatrix property.
6286 * We cache the matrix to avoid recalculating it when transform properties
6287 * have not changed.
6288 *
6289 * @return The inverse of the current matrix of this view.
6290 */
Jeff Brown86671742010-09-30 20:00:15 -07006291 final Matrix getInverseMatrix() {
6292 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07006293 if (mInverseMatrixDirty) {
6294 if (mInverseMatrix == null) {
6295 mInverseMatrix = new Matrix();
6296 }
6297 mMatrix.invert(mInverseMatrix);
6298 mInverseMatrixDirty = false;
6299 }
6300 return mInverseMatrix;
6301 }
6302
6303 /**
Romain Guya5364ee2011-02-24 14:46:04 -08006304 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
6305 * views are drawn) from the camera to this view. The camera's distance
6306 * affects 3D transformations, for instance rotations around the X and Y
6307 * axis. If the rotationX or rotationY properties are changed and this view is
6308 * large (more than half the size of the screen), it is recommended to always
6309 * use a camera distance that's greater than the height (X axis rotation) or
6310 * the width (Y axis rotation) of this view.</p>
6311 *
6312 * <p>The distance of the camera from the view plane can have an affect on the
6313 * perspective distortion of the view when it is rotated around the x or y axis.
6314 * For example, a large distance will result in a large viewing angle, and there
6315 * will not be much perspective distortion of the view as it rotates. A short
6316 * distance may cause much more perspective distortion upon rotation, and can
6317 * also result in some drawing artifacts if the rotated view ends up partially
6318 * behind the camera (which is why the recommendation is to use a distance at
6319 * least as far as the size of the view, if the view is to be rotated.)</p>
6320 *
6321 * <p>The distance is expressed in "depth pixels." The default distance depends
6322 * on the screen density. For instance, on a medium density display, the
6323 * default distance is 1280. On a high density display, the default distance
6324 * is 1920.</p>
6325 *
6326 * <p>If you want to specify a distance that leads to visually consistent
6327 * results across various densities, use the following formula:</p>
6328 * <pre>
6329 * float scale = context.getResources().getDisplayMetrics().density;
6330 * view.setCameraDistance(distance * scale);
6331 * </pre>
6332 *
6333 * <p>The density scale factor of a high density display is 1.5,
6334 * and 1920 = 1280 * 1.5.</p>
6335 *
6336 * @param distance The distance in "depth pixels", if negative the opposite
6337 * value is used
6338 *
6339 * @see #setRotationX(float)
6340 * @see #setRotationY(float)
6341 */
6342 public void setCameraDistance(float distance) {
6343 invalidateParentCaches();
6344 invalidate(false);
6345
6346 final float dpi = mResources.getDisplayMetrics().densityDpi;
6347 if (mCamera == null) {
6348 mCamera = new Camera();
6349 matrix3D = new Matrix();
6350 }
6351
6352 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6353 mMatrixDirty = true;
6354
6355 invalidate(false);
6356 }
6357
6358 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006359 * The degrees that the view is rotated around the pivot point.
6360 *
Romain Guya5364ee2011-02-24 14:46:04 -08006361 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006362 * @see #getPivotX()
6363 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006364 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006365 * @return The degrees of rotation.
6366 */
6367 public float getRotation() {
6368 return mRotation;
6369 }
6370
6371 /**
Chet Haase897247b2010-09-09 14:54:47 -07006372 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6373 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006374 *
6375 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006376 *
6377 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006378 * @see #getPivotX()
6379 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006380 * @see #setRotationX(float)
6381 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006382 *
6383 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006384 */
6385 public void setRotation(float rotation) {
6386 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006387 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006388 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006389 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006390 mRotation = rotation;
6391 mMatrixDirty = true;
6392 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006393 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006394 }
6395 }
6396
6397 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006398 * The degrees that the view is rotated around the vertical axis through the pivot point.
6399 *
6400 * @see #getPivotX()
6401 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006402 * @see #setRotationY(float)
6403 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006404 * @return The degrees of Y rotation.
6405 */
6406 public float getRotationY() {
6407 return mRotationY;
6408 }
6409
6410 /**
Chet Haase897247b2010-09-09 14:54:47 -07006411 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6412 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6413 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006414 *
6415 * When rotating large views, it is recommended to adjust the camera distance
6416 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006417 *
6418 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006419 *
6420 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006421 * @see #getPivotX()
6422 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006423 * @see #setRotation(float)
6424 * @see #setRotationX(float)
6425 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006426 *
6427 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006428 */
6429 public void setRotationY(float rotationY) {
6430 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006431 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006432 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006433 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006434 mRotationY = rotationY;
6435 mMatrixDirty = true;
6436 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006437 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006438 }
6439 }
6440
6441 /**
6442 * The degrees that the view is rotated around the horizontal axis through the pivot point.
6443 *
6444 * @see #getPivotX()
6445 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006446 * @see #setRotationX(float)
6447 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006448 * @return The degrees of X rotation.
6449 */
6450 public float getRotationX() {
6451 return mRotationX;
6452 }
6453
6454 /**
Chet Haase897247b2010-09-09 14:54:47 -07006455 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6456 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6457 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006458 *
6459 * When rotating large views, it is recommended to adjust the camera distance
6460 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006461 *
6462 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006463 *
6464 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006465 * @see #getPivotX()
6466 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006467 * @see #setRotation(float)
6468 * @see #setRotationY(float)
6469 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006470 *
6471 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006472 */
6473 public void setRotationX(float rotationX) {
6474 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006475 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006476 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006477 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006478 mRotationX = rotationX;
6479 mMatrixDirty = true;
6480 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006481 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006482 }
6483 }
6484
6485 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006486 * The amount that the view is scaled in x around the pivot point, as a proportion of
6487 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6488 *
Joe Onorato93162322010-09-16 15:42:01 -04006489 * <p>By default, this is 1.0f.
6490 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006491 * @see #getPivotX()
6492 * @see #getPivotY()
6493 * @return The scaling factor.
6494 */
6495 public float getScaleX() {
6496 return mScaleX;
6497 }
6498
6499 /**
6500 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6501 * the view's unscaled width. A value of 1 means that no scaling is applied.
6502 *
6503 * @param scaleX The scaling factor.
6504 * @see #getPivotX()
6505 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006506 *
6507 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006508 */
6509 public void setScaleX(float scaleX) {
6510 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006511 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006512 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006513 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006514 mScaleX = scaleX;
6515 mMatrixDirty = true;
6516 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006517 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006518 }
6519 }
6520
6521 /**
6522 * The amount that the view is scaled in y around the pivot point, as a proportion of
6523 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6524 *
Joe Onorato93162322010-09-16 15:42:01 -04006525 * <p>By default, this is 1.0f.
6526 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006527 * @see #getPivotX()
6528 * @see #getPivotY()
6529 * @return The scaling factor.
6530 */
6531 public float getScaleY() {
6532 return mScaleY;
6533 }
6534
6535 /**
6536 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
6537 * the view's unscaled width. A value of 1 means that no scaling is applied.
6538 *
6539 * @param scaleY The scaling factor.
6540 * @see #getPivotX()
6541 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006542 *
6543 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07006544 */
6545 public void setScaleY(float scaleY) {
6546 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006547 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006548 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006549 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006550 mScaleY = scaleY;
6551 mMatrixDirty = true;
6552 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006553 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006554 }
6555 }
6556
6557 /**
6558 * The x location of the point around which the view is {@link #setRotation(float) rotated}
6559 * and {@link #setScaleX(float) scaled}.
6560 *
6561 * @see #getRotation()
6562 * @see #getScaleX()
6563 * @see #getScaleY()
6564 * @see #getPivotY()
6565 * @return The x location of the pivot point.
6566 */
6567 public float getPivotX() {
6568 return mPivotX;
6569 }
6570
6571 /**
6572 * Sets the x location of the point around which the view is
6573 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07006574 * By default, the pivot point is centered on the object.
6575 * Setting this property disables this behavior and causes the view to use only the
6576 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006577 *
6578 * @param pivotX The x location of the pivot point.
6579 * @see #getRotation()
6580 * @see #getScaleX()
6581 * @see #getScaleY()
6582 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006583 *
6584 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07006585 */
6586 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006587 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006588 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006589 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006590 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006591 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006592 mPivotX = pivotX;
6593 mMatrixDirty = true;
6594 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006595 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006596 }
6597 }
6598
6599 /**
6600 * The y location of the point around which the view is {@link #setRotation(float) rotated}
6601 * and {@link #setScaleY(float) scaled}.
6602 *
6603 * @see #getRotation()
6604 * @see #getScaleX()
6605 * @see #getScaleY()
6606 * @see #getPivotY()
6607 * @return The y location of the pivot point.
6608 */
6609 public float getPivotY() {
6610 return mPivotY;
6611 }
6612
6613 /**
6614 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07006615 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
6616 * Setting this property disables this behavior and causes the view to use only the
6617 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006618 *
6619 * @param pivotY The y location of the pivot point.
6620 * @see #getRotation()
6621 * @see #getScaleX()
6622 * @see #getScaleY()
6623 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006624 *
6625 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07006626 */
6627 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006628 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006629 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006630 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006631 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006632 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006633 mPivotY = pivotY;
6634 mMatrixDirty = true;
6635 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006636 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006637 }
6638 }
6639
6640 /**
6641 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
6642 * completely transparent and 1 means the view is completely opaque.
6643 *
Joe Onorato93162322010-09-16 15:42:01 -04006644 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07006645 * @return The opacity of the view.
6646 */
6647 public float getAlpha() {
6648 return mAlpha;
6649 }
6650
6651 /**
Romain Guy171c5922011-01-06 10:04:23 -08006652 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
6653 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08006654 *
Romain Guy171c5922011-01-06 10:04:23 -08006655 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
6656 * responsible for applying the opacity itself. Otherwise, calling this method is
6657 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08006658 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07006659 *
6660 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08006661 *
Joe Malin32736f02011-01-19 16:14:20 -08006662 * @see #setLayerType(int, android.graphics.Paint)
6663 *
Chet Haase73066682010-11-29 15:55:32 -08006664 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07006665 */
6666 public void setAlpha(float alpha) {
6667 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006668 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07006669 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07006670 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006671 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006672 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07006673 } else {
Romain Guya3496a92010-10-12 11:53:24 -07006674 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006675 invalidate(false);
6676 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006677 }
6678
6679 /**
Chet Haasea00f3862011-02-22 06:34:40 -08006680 * Faster version of setAlpha() which performs the same steps except there are
6681 * no calls to invalidate(). The caller of this function should perform proper invalidation
6682 * on the parent and this object. The return value indicates whether the subclass handles
6683 * alpha (the return value for onSetAlpha()).
6684 *
6685 * @param alpha The new value for the alpha property
6686 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
6687 */
6688 boolean setAlphaNoInvalidation(float alpha) {
6689 mAlpha = alpha;
6690 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
6691 if (subclassHandlesAlpha) {
6692 mPrivateFlags |= ALPHA_SET;
6693 } else {
6694 mPrivateFlags &= ~ALPHA_SET;
6695 }
6696 return subclassHandlesAlpha;
6697 }
6698
6699 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006700 * Top position of this view relative to its parent.
6701 *
6702 * @return The top of this view, in pixels.
6703 */
6704 @ViewDebug.CapturedViewProperty
6705 public final int getTop() {
6706 return mTop;
6707 }
6708
6709 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006710 * Sets the top position of this view relative to its parent. This method is meant to be called
6711 * by the layout system and should not generally be called otherwise, because the property
6712 * may be changed at any time by the layout.
6713 *
6714 * @param top The top of this view, in pixels.
6715 */
6716 public final void setTop(int top) {
6717 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07006718 updateMatrix();
6719 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006720 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006721 int minTop;
6722 int yLoc;
6723 if (top < mTop) {
6724 minTop = top;
6725 yLoc = top - mTop;
6726 } else {
6727 minTop = mTop;
6728 yLoc = 0;
6729 }
Chet Haasee9140a72011-02-16 16:23:29 -08006730 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006731 }
6732 } else {
6733 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006734 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006735 }
6736
Chet Haaseed032702010-10-01 14:05:54 -07006737 int width = mRight - mLeft;
6738 int oldHeight = mBottom - mTop;
6739
Chet Haase21cd1382010-09-01 17:42:29 -07006740 mTop = top;
6741
Chet Haaseed032702010-10-01 14:05:54 -07006742 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6743
Chet Haase21cd1382010-09-01 17:42:29 -07006744 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006745 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6746 // A change in dimension means an auto-centered pivot point changes, too
6747 mMatrixDirty = true;
6748 }
Chet Haase21cd1382010-09-01 17:42:29 -07006749 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006750 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006751 }
Chet Haase55dbb652010-12-21 20:15:08 -08006752 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006753 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006754 }
6755 }
6756
6757 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006758 * Bottom position of this view relative to its parent.
6759 *
6760 * @return The bottom of this view, in pixels.
6761 */
6762 @ViewDebug.CapturedViewProperty
6763 public final int getBottom() {
6764 return mBottom;
6765 }
6766
6767 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08006768 * True if this view has changed since the last time being drawn.
6769 *
6770 * @return The dirty state of this view.
6771 */
6772 public boolean isDirty() {
6773 return (mPrivateFlags & DIRTY_MASK) != 0;
6774 }
6775
6776 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006777 * Sets the bottom position of this view relative to its parent. This method is meant to be
6778 * called by the layout system and should not generally be called otherwise, because the
6779 * property may be changed at any time by the layout.
6780 *
6781 * @param bottom The bottom of this view, in pixels.
6782 */
6783 public final void setBottom(int bottom) {
6784 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07006785 updateMatrix();
6786 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006787 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006788 int maxBottom;
6789 if (bottom < mBottom) {
6790 maxBottom = mBottom;
6791 } else {
6792 maxBottom = bottom;
6793 }
Chet Haasee9140a72011-02-16 16:23:29 -08006794 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006795 }
6796 } else {
6797 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006798 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006799 }
6800
Chet Haaseed032702010-10-01 14:05:54 -07006801 int width = mRight - mLeft;
6802 int oldHeight = mBottom - mTop;
6803
Chet Haase21cd1382010-09-01 17:42:29 -07006804 mBottom = bottom;
6805
Chet Haaseed032702010-10-01 14:05:54 -07006806 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6807
Chet Haase21cd1382010-09-01 17:42:29 -07006808 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006809 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6810 // A change in dimension means an auto-centered pivot point changes, too
6811 mMatrixDirty = true;
6812 }
Chet Haase21cd1382010-09-01 17:42:29 -07006813 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006814 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006815 }
Chet Haase55dbb652010-12-21 20:15:08 -08006816 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006817 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006818 }
6819 }
6820
6821 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006822 * Left position of this view relative to its parent.
6823 *
6824 * @return The left edge of this view, in pixels.
6825 */
6826 @ViewDebug.CapturedViewProperty
6827 public final int getLeft() {
6828 return mLeft;
6829 }
6830
6831 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006832 * Sets the left position of this view relative to its parent. This method is meant to be called
6833 * by the layout system and should not generally be called otherwise, because the property
6834 * may be changed at any time by the layout.
6835 *
6836 * @param left The bottom of this view, in pixels.
6837 */
6838 public final void setLeft(int left) {
6839 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07006840 updateMatrix();
6841 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006842 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006843 int minLeft;
6844 int xLoc;
6845 if (left < mLeft) {
6846 minLeft = left;
6847 xLoc = left - mLeft;
6848 } else {
6849 minLeft = mLeft;
6850 xLoc = 0;
6851 }
Chet Haasee9140a72011-02-16 16:23:29 -08006852 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006853 }
6854 } else {
6855 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006856 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006857 }
6858
Chet Haaseed032702010-10-01 14:05:54 -07006859 int oldWidth = mRight - mLeft;
6860 int height = mBottom - mTop;
6861
Chet Haase21cd1382010-09-01 17:42:29 -07006862 mLeft = left;
6863
Chet Haaseed032702010-10-01 14:05:54 -07006864 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6865
Chet Haase21cd1382010-09-01 17:42:29 -07006866 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006867 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6868 // A change in dimension means an auto-centered pivot point changes, too
6869 mMatrixDirty = true;
6870 }
Chet Haase21cd1382010-09-01 17:42:29 -07006871 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006872 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006873 }
Chet Haase55dbb652010-12-21 20:15:08 -08006874 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006875 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006876 }
6877 }
6878
6879 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006880 * Right position of this view relative to its parent.
6881 *
6882 * @return The right edge of this view, in pixels.
6883 */
6884 @ViewDebug.CapturedViewProperty
6885 public final int getRight() {
6886 return mRight;
6887 }
6888
6889 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006890 * Sets the right position of this view relative to its parent. This method is meant to be called
6891 * by the layout system and should not generally be called otherwise, because the property
6892 * may be changed at any time by the layout.
6893 *
6894 * @param right The bottom of this view, in pixels.
6895 */
6896 public final void setRight(int right) {
6897 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07006898 updateMatrix();
6899 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006900 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006901 int maxRight;
6902 if (right < mRight) {
6903 maxRight = mRight;
6904 } else {
6905 maxRight = right;
6906 }
Chet Haasee9140a72011-02-16 16:23:29 -08006907 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006908 }
6909 } else {
6910 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006911 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006912 }
6913
Chet Haaseed032702010-10-01 14:05:54 -07006914 int oldWidth = mRight - mLeft;
6915 int height = mBottom - mTop;
6916
Chet Haase21cd1382010-09-01 17:42:29 -07006917 mRight = right;
6918
Chet Haaseed032702010-10-01 14:05:54 -07006919 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6920
Chet Haase21cd1382010-09-01 17:42:29 -07006921 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006922 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6923 // A change in dimension means an auto-centered pivot point changes, too
6924 mMatrixDirty = true;
6925 }
Chet Haase21cd1382010-09-01 17:42:29 -07006926 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006927 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006928 }
Chet Haase55dbb652010-12-21 20:15:08 -08006929 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006930 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006931 }
6932 }
6933
6934 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006935 * The visual x position of this view, in pixels. This is equivalent to the
6936 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08006937 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07006938 *
Chet Haasedf030d22010-07-30 17:22:38 -07006939 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006940 */
Chet Haasedf030d22010-07-30 17:22:38 -07006941 public float getX() {
6942 return mLeft + mTranslationX;
6943 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006944
Chet Haasedf030d22010-07-30 17:22:38 -07006945 /**
6946 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
6947 * {@link #setTranslationX(float) translationX} property to be the difference between
6948 * the x value passed in and the current {@link #getLeft() left} property.
6949 *
6950 * @param x The visual x position of this view, in pixels.
6951 */
6952 public void setX(float x) {
6953 setTranslationX(x - mLeft);
6954 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006955
Chet Haasedf030d22010-07-30 17:22:38 -07006956 /**
6957 * The visual y position of this view, in pixels. This is equivalent to the
6958 * {@link #setTranslationY(float) translationY} property plus the current
6959 * {@link #getTop() top} property.
6960 *
6961 * @return The visual y position of this view, in pixels.
6962 */
6963 public float getY() {
6964 return mTop + mTranslationY;
6965 }
6966
6967 /**
6968 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
6969 * {@link #setTranslationY(float) translationY} property to be the difference between
6970 * the y value passed in and the current {@link #getTop() top} property.
6971 *
6972 * @param y The visual y position of this view, in pixels.
6973 */
6974 public void setY(float y) {
6975 setTranslationY(y - mTop);
6976 }
6977
6978
6979 /**
6980 * The horizontal location of this view relative to its {@link #getLeft() left} position.
6981 * This position is post-layout, in addition to wherever the object's
6982 * layout placed it.
6983 *
6984 * @return The horizontal position of this view relative to its left position, in pixels.
6985 */
6986 public float getTranslationX() {
6987 return mTranslationX;
6988 }
6989
6990 /**
6991 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
6992 * This effectively positions the object post-layout, in addition to wherever the object's
6993 * layout placed it.
6994 *
6995 * @param translationX The horizontal position of this view relative to its left position,
6996 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006997 *
6998 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07006999 */
7000 public void setTranslationX(float translationX) {
7001 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007002 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007003 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007004 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007005 mTranslationX = translationX;
7006 mMatrixDirty = true;
7007 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007008 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007009 }
7010 }
7011
7012 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007013 * The horizontal location of this view relative to its {@link #getTop() top} position.
7014 * This position is post-layout, in addition to wherever the object's
7015 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007016 *
Chet Haasedf030d22010-07-30 17:22:38 -07007017 * @return The vertical position of this view relative to its top position,
7018 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007019 */
Chet Haasedf030d22010-07-30 17:22:38 -07007020 public float getTranslationY() {
7021 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07007022 }
7023
7024 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007025 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7026 * This effectively positions the object post-layout, in addition to wherever the object's
7027 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007028 *
Chet Haasedf030d22010-07-30 17:22:38 -07007029 * @param translationY The vertical position of this view relative to its top position,
7030 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007031 *
7032 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007033 */
Chet Haasedf030d22010-07-30 17:22:38 -07007034 public void setTranslationY(float translationY) {
7035 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007036 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007037 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007038 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007039 mTranslationY = translationY;
7040 mMatrixDirty = true;
7041 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007042 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007043 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007044 }
7045
7046 /**
Romain Guyda489792011-02-03 01:05:15 -08007047 * @hide
7048 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007049 public void setFastTranslationX(float x) {
7050 mTranslationX = x;
7051 mMatrixDirty = true;
7052 }
7053
7054 /**
7055 * @hide
7056 */
7057 public void setFastTranslationY(float y) {
7058 mTranslationY = y;
7059 mMatrixDirty = true;
7060 }
7061
7062 /**
7063 * @hide
7064 */
Romain Guyda489792011-02-03 01:05:15 -08007065 public void setFastX(float x) {
7066 mTranslationX = x - mLeft;
7067 mMatrixDirty = true;
7068 }
7069
7070 /**
7071 * @hide
7072 */
7073 public void setFastY(float y) {
7074 mTranslationY = y - mTop;
7075 mMatrixDirty = true;
7076 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007077
Romain Guyda489792011-02-03 01:05:15 -08007078 /**
7079 * @hide
7080 */
7081 public void setFastScaleX(float x) {
7082 mScaleX = x;
7083 mMatrixDirty = true;
7084 }
7085
7086 /**
7087 * @hide
7088 */
7089 public void setFastScaleY(float y) {
7090 mScaleY = y;
7091 mMatrixDirty = true;
7092 }
7093
7094 /**
7095 * @hide
7096 */
7097 public void setFastAlpha(float alpha) {
7098 mAlpha = alpha;
7099 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007100
Romain Guyda489792011-02-03 01:05:15 -08007101 /**
7102 * @hide
7103 */
7104 public void setFastRotationY(float y) {
7105 mRotationY = y;
7106 mMatrixDirty = true;
7107 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007108
Romain Guyda489792011-02-03 01:05:15 -08007109 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007110 * Hit rectangle in parent's coordinates
7111 *
7112 * @param outRect The hit rectangle of the view.
7113 */
7114 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007115 updateMatrix();
7116 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007117 outRect.set(mLeft, mTop, mRight, mBottom);
7118 } else {
7119 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07007120 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07007121 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007122 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7123 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007124 }
7125 }
7126
7127 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007128 * Determines whether the given point, in local coordinates is inside the view.
7129 */
7130 /*package*/ final boolean pointInView(float localX, float localY) {
7131 return localX >= 0 && localX < (mRight - mLeft)
7132 && localY >= 0 && localY < (mBottom - mTop);
7133 }
7134
7135 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007136 * Utility method to determine whether the given point, in local coordinates,
7137 * is inside the view, where the area of the view is expanded by the slop factor.
7138 * This method is called while processing touch-move events to determine if the event
7139 * is still within the view.
7140 */
7141 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007142 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007143 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007144 }
7145
7146 /**
7147 * When a view has focus and the user navigates away from it, the next view is searched for
7148 * starting from the rectangle filled in by this method.
7149 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007150 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7151 * of the view. However, if your view maintains some idea of internal selection,
7152 * such as a cursor, or a selected row or column, you should override this method and
7153 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007154 *
7155 * @param r The rectangle to fill in, in this view's coordinates.
7156 */
7157 public void getFocusedRect(Rect r) {
7158 getDrawingRect(r);
7159 }
7160
7161 /**
7162 * If some part of this view is not clipped by any of its parents, then
7163 * return that area in r in global (root) coordinates. To convert r to local
7164 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7165 * -globalOffset.y)) If the view is completely clipped or translated out,
7166 * return false.
7167 *
7168 * @param r If true is returned, r holds the global coordinates of the
7169 * visible portion of this view.
7170 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7171 * between this view and its root. globalOffet may be null.
7172 * @return true if r is non-empty (i.e. part of the view is visible at the
7173 * root level.
7174 */
7175 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7176 int width = mRight - mLeft;
7177 int height = mBottom - mTop;
7178 if (width > 0 && height > 0) {
7179 r.set(0, 0, width, height);
7180 if (globalOffset != null) {
7181 globalOffset.set(-mScrollX, -mScrollY);
7182 }
7183 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7184 }
7185 return false;
7186 }
7187
7188 public final boolean getGlobalVisibleRect(Rect r) {
7189 return getGlobalVisibleRect(r, null);
7190 }
7191
7192 public final boolean getLocalVisibleRect(Rect r) {
7193 Point offset = new Point();
7194 if (getGlobalVisibleRect(r, offset)) {
7195 r.offset(-offset.x, -offset.y); // make r local
7196 return true;
7197 }
7198 return false;
7199 }
7200
7201 /**
7202 * Offset this view's vertical location by the specified number of pixels.
7203 *
7204 * @param offset the number of pixels to offset the view by
7205 */
7206 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007207 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007208 updateMatrix();
7209 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007210 final ViewParent p = mParent;
7211 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007212 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007213 int minTop;
7214 int maxBottom;
7215 int yLoc;
7216 if (offset < 0) {
7217 minTop = mTop + offset;
7218 maxBottom = mBottom;
7219 yLoc = offset;
7220 } else {
7221 minTop = mTop;
7222 maxBottom = mBottom + offset;
7223 yLoc = 0;
7224 }
7225 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
7226 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007227 }
7228 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007229 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007230 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007231
Chet Haasec3aa3612010-06-17 08:50:37 -07007232 mTop += offset;
7233 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007234
Chet Haasec3aa3612010-06-17 08:50:37 -07007235 if (!mMatrixIsIdentity) {
7236 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007237 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007238 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007239 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007241 }
7242
7243 /**
7244 * Offset this view's horizontal location by the specified amount of pixels.
7245 *
7246 * @param offset the numer of pixels to offset the view by
7247 */
7248 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007249 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007250 updateMatrix();
7251 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007252 final ViewParent p = mParent;
7253 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007254 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007255 int minLeft;
7256 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007257 if (offset < 0) {
7258 minLeft = mLeft + offset;
7259 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007260 } else {
7261 minLeft = mLeft;
7262 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007263 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007264 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07007265 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007266 }
7267 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007268 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007269 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007270
Chet Haasec3aa3612010-06-17 08:50:37 -07007271 mLeft += offset;
7272 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007273
Chet Haasec3aa3612010-06-17 08:50:37 -07007274 if (!mMatrixIsIdentity) {
7275 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007276 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007277 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007278 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007279 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007280 }
7281
7282 /**
7283 * Get the LayoutParams associated with this view. All views should have
7284 * layout parameters. These supply parameters to the <i>parent</i> of this
7285 * view specifying how it should be arranged. There are many subclasses of
7286 * ViewGroup.LayoutParams, and these correspond to the different subclasses
7287 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08007288 *
7289 * This method may return null if this View is not attached to a parent
7290 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
7291 * was not invoked successfully. When a View is attached to a parent
7292 * ViewGroup, this method must not return null.
7293 *
7294 * @return The LayoutParams associated with this view, or null if no
7295 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007296 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07007297 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007298 public ViewGroup.LayoutParams getLayoutParams() {
7299 return mLayoutParams;
7300 }
7301
7302 /**
7303 * Set the layout parameters associated with this view. These supply
7304 * parameters to the <i>parent</i> of this view specifying how it should be
7305 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
7306 * correspond to the different subclasses of ViewGroup that are responsible
7307 * for arranging their children.
7308 *
Romain Guy01c174b2011-02-22 11:51:06 -08007309 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007310 */
7311 public void setLayoutParams(ViewGroup.LayoutParams params) {
7312 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007313 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007314 }
7315 mLayoutParams = params;
7316 requestLayout();
7317 }
7318
7319 /**
7320 * Set the scrolled position of your view. This will cause a call to
7321 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7322 * invalidated.
7323 * @param x the x position to scroll to
7324 * @param y the y position to scroll to
7325 */
7326 public void scrollTo(int x, int y) {
7327 if (mScrollX != x || mScrollY != y) {
7328 int oldX = mScrollX;
7329 int oldY = mScrollY;
7330 mScrollX = x;
7331 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007332 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007333 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007334 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007335 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007337 }
7338 }
7339
7340 /**
7341 * Move the scrolled position of your view. This will cause a call to
7342 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7343 * invalidated.
7344 * @param x the amount of pixels to scroll by horizontally
7345 * @param y the amount of pixels to scroll by vertically
7346 */
7347 public void scrollBy(int x, int y) {
7348 scrollTo(mScrollX + x, mScrollY + y);
7349 }
7350
7351 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007352 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7353 * animation to fade the scrollbars out after a default delay. If a subclass
7354 * provides animated scrolling, the start delay should equal the duration
7355 * of the scrolling animation.</p>
7356 *
7357 * <p>The animation starts only if at least one of the scrollbars is
7358 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7359 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7360 * this method returns true, and false otherwise. If the animation is
7361 * started, this method calls {@link #invalidate()}; in that case the
7362 * caller should not call {@link #invalidate()}.</p>
7363 *
7364 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007365 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007366 *
7367 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7368 * and {@link #scrollTo(int, int)}.</p>
7369 *
7370 * @return true if the animation is played, false otherwise
7371 *
7372 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007373 * @see #scrollBy(int, int)
7374 * @see #scrollTo(int, int)
7375 * @see #isHorizontalScrollBarEnabled()
7376 * @see #isVerticalScrollBarEnabled()
7377 * @see #setHorizontalScrollBarEnabled(boolean)
7378 * @see #setVerticalScrollBarEnabled(boolean)
7379 */
7380 protected boolean awakenScrollBars() {
7381 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007382 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007383 }
7384
7385 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007386 * Trigger the scrollbars to draw.
7387 * This method differs from awakenScrollBars() only in its default duration.
7388 * initialAwakenScrollBars() will show the scroll bars for longer than
7389 * usual to give the user more of a chance to notice them.
7390 *
7391 * @return true if the animation is played, false otherwise.
7392 */
7393 private boolean initialAwakenScrollBars() {
7394 return mScrollCache != null &&
7395 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7396 }
7397
7398 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007399 * <p>
7400 * Trigger the scrollbars to draw. When invoked this method starts an
7401 * animation to fade the scrollbars out after a fixed delay. If a subclass
7402 * provides animated scrolling, the start delay should equal the duration of
7403 * the scrolling animation.
7404 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007405 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007406 * <p>
7407 * The animation starts only if at least one of the scrollbars is enabled,
7408 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7409 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7410 * this method returns true, and false otherwise. If the animation is
7411 * started, this method calls {@link #invalidate()}; in that case the caller
7412 * should not call {@link #invalidate()}.
7413 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007414 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007415 * <p>
7416 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007417 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007418 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007419 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007420 * @param startDelay the delay, in milliseconds, after which the animation
7421 * should start; when the delay is 0, the animation starts
7422 * immediately
7423 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007424 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007425 * @see #scrollBy(int, int)
7426 * @see #scrollTo(int, int)
7427 * @see #isHorizontalScrollBarEnabled()
7428 * @see #isVerticalScrollBarEnabled()
7429 * @see #setHorizontalScrollBarEnabled(boolean)
7430 * @see #setVerticalScrollBarEnabled(boolean)
7431 */
7432 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007433 return awakenScrollBars(startDelay, true);
7434 }
Joe Malin32736f02011-01-19 16:14:20 -08007435
Mike Cleron290947b2009-09-29 18:34:32 -07007436 /**
7437 * <p>
7438 * Trigger the scrollbars to draw. When invoked this method starts an
7439 * animation to fade the scrollbars out after a fixed delay. If a subclass
7440 * provides animated scrolling, the start delay should equal the duration of
7441 * the scrolling animation.
7442 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007443 *
Mike Cleron290947b2009-09-29 18:34:32 -07007444 * <p>
7445 * The animation starts only if at least one of the scrollbars is enabled,
7446 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7447 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7448 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08007449 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07007450 * is set to true; in that case the caller
7451 * should not call {@link #invalidate()}.
7452 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007453 *
Mike Cleron290947b2009-09-29 18:34:32 -07007454 * <p>
7455 * This method should be invoked everytime a subclass directly updates the
7456 * scroll parameters.
7457 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007458 *
Mike Cleron290947b2009-09-29 18:34:32 -07007459 * @param startDelay the delay, in milliseconds, after which the animation
7460 * should start; when the delay is 0, the animation starts
7461 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007462 *
Mike Cleron290947b2009-09-29 18:34:32 -07007463 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007464 *
Mike Cleron290947b2009-09-29 18:34:32 -07007465 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007466 *
Mike Cleron290947b2009-09-29 18:34:32 -07007467 * @see #scrollBy(int, int)
7468 * @see #scrollTo(int, int)
7469 * @see #isHorizontalScrollBarEnabled()
7470 * @see #isVerticalScrollBarEnabled()
7471 * @see #setHorizontalScrollBarEnabled(boolean)
7472 * @see #setVerticalScrollBarEnabled(boolean)
7473 */
7474 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007475 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007476
Mike Cleronf116bf82009-09-27 19:14:12 -07007477 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7478 return false;
7479 }
7480
7481 if (scrollCache.scrollBar == null) {
7482 scrollCache.scrollBar = new ScrollBarDrawable();
7483 }
7484
7485 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7486
Mike Cleron290947b2009-09-29 18:34:32 -07007487 if (invalidate) {
7488 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007489 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007490 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007491
7492 if (scrollCache.state == ScrollabilityCache.OFF) {
7493 // FIXME: this is copied from WindowManagerService.
7494 // We should get this value from the system when it
7495 // is possible to do so.
7496 final int KEY_REPEAT_FIRST_DELAY = 750;
7497 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7498 }
7499
7500 // Tell mScrollCache when we should start fading. This may
7501 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007502 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007503 scrollCache.fadeStartTime = fadeStartTime;
7504 scrollCache.state = ScrollabilityCache.ON;
7505
7506 // Schedule our fader to run, unscheduling any old ones first
7507 if (mAttachInfo != null) {
7508 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7509 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7510 }
7511
7512 return true;
7513 }
7514
7515 return false;
7516 }
7517
7518 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007519 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07007520 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
7521 * in the future. This must be called from a UI thread. To call from a non-UI
7522 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007523 *
7524 * WARNING: This method is destructive to dirty.
7525 * @param dirty the rectangle representing the bounds of the dirty region
7526 */
7527 public void invalidate(Rect dirty) {
7528 if (ViewDebug.TRACE_HIERARCHY) {
7529 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7530 }
7531
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007532 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007533 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7534 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007535 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007536 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007537 final ViewParent p = mParent;
7538 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007539 //noinspection PointlessBooleanExpression,ConstantConditions
7540 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7541 if (p != null && ai != null && ai.mHardwareAccelerated) {
7542 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007543 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007544 p.invalidateChild(this, null);
7545 return;
7546 }
Romain Guyaf636eb2010-12-09 17:47:21 -08007547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007548 if (p != null && ai != null) {
7549 final int scrollX = mScrollX;
7550 final int scrollY = mScrollY;
7551 final Rect r = ai.mTmpInvalRect;
7552 r.set(dirty.left - scrollX, dirty.top - scrollY,
7553 dirty.right - scrollX, dirty.bottom - scrollY);
7554 mParent.invalidateChild(this, r);
7555 }
7556 }
7557 }
7558
7559 /**
7560 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
7561 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007562 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
7563 * will be called at some point in the future. This must be called from
7564 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007565 * @param l the left position of the dirty region
7566 * @param t the top position of the dirty region
7567 * @param r the right position of the dirty region
7568 * @param b the bottom position of the dirty region
7569 */
7570 public void invalidate(int l, int t, int r, int b) {
7571 if (ViewDebug.TRACE_HIERARCHY) {
7572 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7573 }
7574
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007575 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007576 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7577 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007578 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007579 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007580 final ViewParent p = mParent;
7581 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007582 //noinspection PointlessBooleanExpression,ConstantConditions
7583 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7584 if (p != null && ai != null && ai.mHardwareAccelerated) {
7585 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007586 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007587 p.invalidateChild(this, null);
7588 return;
7589 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007591 if (p != null && ai != null && l < r && t < b) {
7592 final int scrollX = mScrollX;
7593 final int scrollY = mScrollY;
7594 final Rect tmpr = ai.mTmpInvalRect;
7595 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
7596 p.invalidateChild(this, tmpr);
7597 }
7598 }
7599 }
7600
7601 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07007602 * Invalidate the whole view. If the view is visible,
7603 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
7604 * the future. This must be called from a UI thread. To call from a non-UI thread,
7605 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007606 */
7607 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07007608 invalidate(true);
7609 }
Joe Malin32736f02011-01-19 16:14:20 -08007610
Chet Haaseed032702010-10-01 14:05:54 -07007611 /**
7612 * This is where the invalidate() work actually happens. A full invalidate()
7613 * causes the drawing cache to be invalidated, but this function can be called with
7614 * invalidateCache set to false to skip that invalidation step for cases that do not
7615 * need it (for example, a component that remains at the same dimensions with the same
7616 * content).
7617 *
7618 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
7619 * well. This is usually true for a full invalidate, but may be set to false if the
7620 * View's contents or dimensions have not changed.
7621 */
Romain Guy849d0a32011-02-01 17:20:48 -08007622 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007623 if (ViewDebug.TRACE_HIERARCHY) {
7624 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7625 }
7626
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007627 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08007628 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08007629 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
7630 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07007631 mPrivateFlags &= ~DRAWN;
7632 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08007633 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07007634 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007636 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07007637 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08007638 //noinspection PointlessBooleanExpression,ConstantConditions
7639 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7640 if (p != null && ai != null && ai.mHardwareAccelerated) {
7641 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007642 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007643 p.invalidateChild(this, null);
7644 return;
7645 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007646 }
Michael Jurkaebefea42010-11-15 16:04:01 -08007647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007648 if (p != null && ai != null) {
7649 final Rect r = ai.mTmpInvalRect;
7650 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7651 // Don't call invalidate -- we don't want to internally scroll
7652 // our own bounds
7653 p.invalidateChild(this, r);
7654 }
7655 }
7656 }
7657
7658 /**
Romain Guyda489792011-02-03 01:05:15 -08007659 * @hide
7660 */
7661 public void fastInvalidate() {
7662 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
7663 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7664 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
7665 if (mParent instanceof View) {
7666 ((View) mParent).mPrivateFlags |= INVALIDATED;
7667 }
7668 mPrivateFlags &= ~DRAWN;
7669 mPrivateFlags |= INVALIDATED;
7670 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07007671 if (mParent != null && mAttachInfo != null) {
7672 if (mAttachInfo.mHardwareAccelerated) {
7673 mParent.invalidateChild(this, null);
7674 } else {
7675 final Rect r = mAttachInfo.mTmpInvalRect;
7676 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7677 // Don't call invalidate -- we don't want to internally scroll
7678 // our own bounds
7679 mParent.invalidateChild(this, r);
7680 }
Romain Guyda489792011-02-03 01:05:15 -08007681 }
7682 }
7683 }
7684
7685 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08007686 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08007687 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7688 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08007689 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
7690 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08007691 *
7692 * @hide
7693 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08007694 protected void invalidateParentCaches() {
7695 if (mParent instanceof View) {
7696 ((View) mParent).mPrivateFlags |= INVALIDATED;
7697 }
7698 }
Joe Malin32736f02011-01-19 16:14:20 -08007699
Romain Guy0fd89bf2011-01-26 15:41:30 -08007700 /**
7701 * Used to indicate that the parent of this view should be invalidated. This functionality
7702 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7703 * which is necessary when various parent-managed properties of the view change, such as
7704 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
7705 * an invalidation event to the parent.
7706 *
7707 * @hide
7708 */
7709 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08007710 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007711 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08007712 }
7713 }
7714
7715 /**
Romain Guy24443ea2009-05-11 11:56:30 -07007716 * Indicates whether this View is opaque. An opaque View guarantees that it will
7717 * draw all the pixels overlapping its bounds using a fully opaque color.
7718 *
7719 * Subclasses of View should override this method whenever possible to indicate
7720 * whether an instance is opaque. Opaque Views are treated in a special way by
7721 * the View hierarchy, possibly allowing it to perform optimizations during
7722 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07007723 *
Romain Guy24443ea2009-05-11 11:56:30 -07007724 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07007725 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007726 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07007727 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07007728 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
7729 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07007730 }
7731
Adam Powell20232d02010-12-08 21:08:53 -08007732 /**
7733 * @hide
7734 */
7735 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07007736 // Opaque if:
7737 // - Has a background
7738 // - Background is opaque
7739 // - Doesn't have scrollbars or scrollbars are inside overlay
7740
7741 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
7742 mPrivateFlags |= OPAQUE_BACKGROUND;
7743 } else {
7744 mPrivateFlags &= ~OPAQUE_BACKGROUND;
7745 }
7746
7747 final int flags = mViewFlags;
7748 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
7749 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
7750 mPrivateFlags |= OPAQUE_SCROLLBARS;
7751 } else {
7752 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
7753 }
7754 }
7755
7756 /**
7757 * @hide
7758 */
7759 protected boolean hasOpaqueScrollbars() {
7760 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07007761 }
7762
7763 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007764 * @return A handler associated with the thread running the View. This
7765 * handler can be used to pump events in the UI events queue.
7766 */
7767 public Handler getHandler() {
7768 if (mAttachInfo != null) {
7769 return mAttachInfo.mHandler;
7770 }
7771 return null;
7772 }
7773
7774 /**
7775 * Causes the Runnable to be added to the message queue.
7776 * The runnable will be run on the user interface thread.
7777 *
7778 * @param action The Runnable that will be executed.
7779 *
7780 * @return Returns true if the Runnable was successfully placed in to the
7781 * message queue. Returns false on failure, usually because the
7782 * looper processing the message queue is exiting.
7783 */
7784 public boolean post(Runnable action) {
7785 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007786 AttachInfo attachInfo = mAttachInfo;
7787 if (attachInfo != null) {
7788 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007789 } else {
7790 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007791 ViewAncestor.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007792 return true;
7793 }
7794
7795 return handler.post(action);
7796 }
7797
7798 /**
7799 * Causes the Runnable to be added to the message queue, to be run
7800 * after the specified amount of time elapses.
7801 * The runnable will be run on the user interface thread.
7802 *
7803 * @param action The Runnable that will be executed.
7804 * @param delayMillis The delay (in milliseconds) until the Runnable
7805 * will be executed.
7806 *
7807 * @return true if the Runnable was successfully placed in to the
7808 * message queue. Returns false on failure, usually because the
7809 * looper processing the message queue is exiting. Note that a
7810 * result of true does not mean the Runnable will be processed --
7811 * if the looper is quit before the delivery time of the message
7812 * occurs then the message will be dropped.
7813 */
7814 public boolean postDelayed(Runnable action, long delayMillis) {
7815 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007816 AttachInfo attachInfo = mAttachInfo;
7817 if (attachInfo != null) {
7818 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007819 } else {
7820 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007821 ViewAncestor.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007822 return true;
7823 }
7824
7825 return handler.postDelayed(action, delayMillis);
7826 }
7827
7828 /**
7829 * Removes the specified Runnable from the message queue.
7830 *
7831 * @param action The Runnable to remove from the message handling queue
7832 *
7833 * @return true if this view could ask the Handler to remove the Runnable,
7834 * false otherwise. When the returned value is true, the Runnable
7835 * may or may not have been actually removed from the message queue
7836 * (for instance, if the Runnable was not in the queue already.)
7837 */
7838 public boolean removeCallbacks(Runnable action) {
7839 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007840 AttachInfo attachInfo = mAttachInfo;
7841 if (attachInfo != null) {
7842 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007843 } else {
7844 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007845 ViewAncestor.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007846 return true;
7847 }
7848
7849 handler.removeCallbacks(action);
7850 return true;
7851 }
7852
7853 /**
7854 * Cause an invalidate to happen on a subsequent cycle through the event loop.
7855 * Use this to invalidate the View from a non-UI thread.
7856 *
7857 * @see #invalidate()
7858 */
7859 public void postInvalidate() {
7860 postInvalidateDelayed(0);
7861 }
7862
7863 /**
7864 * Cause an invalidate of the specified area to happen on a subsequent cycle
7865 * through the event loop. Use this to invalidate the View from a non-UI thread.
7866 *
7867 * @param left The left coordinate of the rectangle to invalidate.
7868 * @param top The top coordinate of the rectangle to invalidate.
7869 * @param right The right coordinate of the rectangle to invalidate.
7870 * @param bottom The bottom coordinate of the rectangle to invalidate.
7871 *
7872 * @see #invalidate(int, int, int, int)
7873 * @see #invalidate(Rect)
7874 */
7875 public void postInvalidate(int left, int top, int right, int bottom) {
7876 postInvalidateDelayed(0, left, top, right, bottom);
7877 }
7878
7879 /**
7880 * Cause an invalidate to happen on a subsequent cycle through the event
7881 * loop. Waits for the specified amount of time.
7882 *
7883 * @param delayMilliseconds the duration in milliseconds to delay the
7884 * invalidation by
7885 */
7886 public void postInvalidateDelayed(long delayMilliseconds) {
7887 // We try only with the AttachInfo because there's no point in invalidating
7888 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07007889 AttachInfo attachInfo = mAttachInfo;
7890 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007891 Message msg = Message.obtain();
7892 msg.what = AttachInfo.INVALIDATE_MSG;
7893 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07007894 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007895 }
7896 }
7897
7898 /**
7899 * Cause an invalidate of the specified area to happen on a subsequent cycle
7900 * through the event loop. Waits for the specified amount of time.
7901 *
7902 * @param delayMilliseconds the duration in milliseconds to delay the
7903 * invalidation by
7904 * @param left The left coordinate of the rectangle to invalidate.
7905 * @param top The top coordinate of the rectangle to invalidate.
7906 * @param right The right coordinate of the rectangle to invalidate.
7907 * @param bottom The bottom coordinate of the rectangle to invalidate.
7908 */
7909 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
7910 int right, int bottom) {
7911
7912 // We try only with the AttachInfo because there's no point in invalidating
7913 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07007914 AttachInfo attachInfo = mAttachInfo;
7915 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007916 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
7917 info.target = this;
7918 info.left = left;
7919 info.top = top;
7920 info.right = right;
7921 info.bottom = bottom;
7922
7923 final Message msg = Message.obtain();
7924 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
7925 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07007926 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007927 }
7928 }
7929
7930 /**
7931 * Called by a parent to request that a child update its values for mScrollX
7932 * and mScrollY if necessary. This will typically be done if the child is
7933 * animating a scroll using a {@link android.widget.Scroller Scroller}
7934 * object.
7935 */
7936 public void computeScroll() {
7937 }
7938
7939 /**
7940 * <p>Indicate whether the horizontal edges are faded when the view is
7941 * scrolled horizontally.</p>
7942 *
7943 * @return true if the horizontal edges should are faded on scroll, false
7944 * otherwise
7945 *
7946 * @see #setHorizontalFadingEdgeEnabled(boolean)
7947 * @attr ref android.R.styleable#View_fadingEdge
7948 */
7949 public boolean isHorizontalFadingEdgeEnabled() {
7950 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
7951 }
7952
7953 /**
7954 * <p>Define whether the horizontal edges should be faded when this view
7955 * is scrolled horizontally.</p>
7956 *
7957 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
7958 * be faded when the view is scrolled
7959 * horizontally
7960 *
7961 * @see #isHorizontalFadingEdgeEnabled()
7962 * @attr ref android.R.styleable#View_fadingEdge
7963 */
7964 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
7965 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
7966 if (horizontalFadingEdgeEnabled) {
7967 initScrollCache();
7968 }
7969
7970 mViewFlags ^= FADING_EDGE_HORIZONTAL;
7971 }
7972 }
7973
7974 /**
7975 * <p>Indicate whether the vertical edges are faded when the view is
7976 * scrolled horizontally.</p>
7977 *
7978 * @return true if the vertical edges should are faded on scroll, false
7979 * otherwise
7980 *
7981 * @see #setVerticalFadingEdgeEnabled(boolean)
7982 * @attr ref android.R.styleable#View_fadingEdge
7983 */
7984 public boolean isVerticalFadingEdgeEnabled() {
7985 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
7986 }
7987
7988 /**
7989 * <p>Define whether the vertical edges should be faded when this view
7990 * is scrolled vertically.</p>
7991 *
7992 * @param verticalFadingEdgeEnabled true if the vertical edges should
7993 * be faded when the view is scrolled
7994 * vertically
7995 *
7996 * @see #isVerticalFadingEdgeEnabled()
7997 * @attr ref android.R.styleable#View_fadingEdge
7998 */
7999 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8000 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8001 if (verticalFadingEdgeEnabled) {
8002 initScrollCache();
8003 }
8004
8005 mViewFlags ^= FADING_EDGE_VERTICAL;
8006 }
8007 }
8008
8009 /**
8010 * Returns the strength, or intensity, of the top faded edge. The strength is
8011 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8012 * returns 0.0 or 1.0 but no value in between.
8013 *
8014 * Subclasses should override this method to provide a smoother fade transition
8015 * when scrolling occurs.
8016 *
8017 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8018 */
8019 protected float getTopFadingEdgeStrength() {
8020 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8021 }
8022
8023 /**
8024 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8025 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8026 * returns 0.0 or 1.0 but no value in between.
8027 *
8028 * Subclasses should override this method to provide a smoother fade transition
8029 * when scrolling occurs.
8030 *
8031 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8032 */
8033 protected float getBottomFadingEdgeStrength() {
8034 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8035 computeVerticalScrollRange() ? 1.0f : 0.0f;
8036 }
8037
8038 /**
8039 * Returns the strength, or intensity, of the left faded edge. The strength is
8040 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8041 * returns 0.0 or 1.0 but no value in between.
8042 *
8043 * Subclasses should override this method to provide a smoother fade transition
8044 * when scrolling occurs.
8045 *
8046 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8047 */
8048 protected float getLeftFadingEdgeStrength() {
8049 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8050 }
8051
8052 /**
8053 * Returns the strength, or intensity, of the right faded edge. The strength is
8054 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8055 * returns 0.0 or 1.0 but no value in between.
8056 *
8057 * Subclasses should override this method to provide a smoother fade transition
8058 * when scrolling occurs.
8059 *
8060 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8061 */
8062 protected float getRightFadingEdgeStrength() {
8063 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8064 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8065 }
8066
8067 /**
8068 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8069 * scrollbar is not drawn by default.</p>
8070 *
8071 * @return true if the horizontal scrollbar should be painted, false
8072 * otherwise
8073 *
8074 * @see #setHorizontalScrollBarEnabled(boolean)
8075 */
8076 public boolean isHorizontalScrollBarEnabled() {
8077 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8078 }
8079
8080 /**
8081 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8082 * scrollbar is not drawn by default.</p>
8083 *
8084 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8085 * be painted
8086 *
8087 * @see #isHorizontalScrollBarEnabled()
8088 */
8089 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8090 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8091 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008092 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008093 recomputePadding();
8094 }
8095 }
8096
8097 /**
8098 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
8099 * scrollbar is not drawn by default.</p>
8100 *
8101 * @return true if the vertical scrollbar should be painted, false
8102 * otherwise
8103 *
8104 * @see #setVerticalScrollBarEnabled(boolean)
8105 */
8106 public boolean isVerticalScrollBarEnabled() {
8107 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
8108 }
8109
8110 /**
8111 * <p>Define whether the vertical scrollbar should be drawn or not. The
8112 * scrollbar is not drawn by default.</p>
8113 *
8114 * @param verticalScrollBarEnabled true if the vertical scrollbar should
8115 * be painted
8116 *
8117 * @see #isVerticalScrollBarEnabled()
8118 */
8119 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
8120 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
8121 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008122 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008123 recomputePadding();
8124 }
8125 }
8126
Adam Powell20232d02010-12-08 21:08:53 -08008127 /**
8128 * @hide
8129 */
8130 protected void recomputePadding() {
8131 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008132 }
Joe Malin32736f02011-01-19 16:14:20 -08008133
Mike Cleronfe81d382009-09-28 14:22:16 -07008134 /**
8135 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08008136 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008137 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08008138 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008139 */
8140 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
8141 initScrollCache();
8142 final ScrollabilityCache scrollabilityCache = mScrollCache;
8143 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008144 if (fadeScrollbars) {
8145 scrollabilityCache.state = ScrollabilityCache.OFF;
8146 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07008147 scrollabilityCache.state = ScrollabilityCache.ON;
8148 }
8149 }
Joe Malin32736f02011-01-19 16:14:20 -08008150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008151 /**
Joe Malin32736f02011-01-19 16:14:20 -08008152 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008153 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08008154 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008155 * @return true if scrollbar fading is enabled
8156 */
8157 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08008158 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008159 }
Joe Malin32736f02011-01-19 16:14:20 -08008160
Mike Cleron52f0a642009-09-28 18:21:37 -07008161 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008162 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
8163 * inset. When inset, they add to the padding of the view. And the scrollbars
8164 * can be drawn inside the padding area or on the edge of the view. For example,
8165 * if a view has a background drawable and you want to draw the scrollbars
8166 * inside the padding specified by the drawable, you can use
8167 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
8168 * appear at the edge of the view, ignoring the padding, then you can use
8169 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
8170 * @param style the style of the scrollbars. Should be one of
8171 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
8172 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
8173 * @see #SCROLLBARS_INSIDE_OVERLAY
8174 * @see #SCROLLBARS_INSIDE_INSET
8175 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8176 * @see #SCROLLBARS_OUTSIDE_INSET
8177 */
8178 public void setScrollBarStyle(int style) {
8179 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
8180 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07008181 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008182 recomputePadding();
8183 }
8184 }
8185
8186 /**
8187 * <p>Returns the current scrollbar style.</p>
8188 * @return the current scrollbar style
8189 * @see #SCROLLBARS_INSIDE_OVERLAY
8190 * @see #SCROLLBARS_INSIDE_INSET
8191 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8192 * @see #SCROLLBARS_OUTSIDE_INSET
8193 */
8194 public int getScrollBarStyle() {
8195 return mViewFlags & SCROLLBARS_STYLE_MASK;
8196 }
8197
8198 /**
8199 * <p>Compute the horizontal range that the horizontal scrollbar
8200 * represents.</p>
8201 *
8202 * <p>The range is expressed in arbitrary units that must be the same as the
8203 * units used by {@link #computeHorizontalScrollExtent()} and
8204 * {@link #computeHorizontalScrollOffset()}.</p>
8205 *
8206 * <p>The default range is the drawing width of this view.</p>
8207 *
8208 * @return the total horizontal range represented by the horizontal
8209 * scrollbar
8210 *
8211 * @see #computeHorizontalScrollExtent()
8212 * @see #computeHorizontalScrollOffset()
8213 * @see android.widget.ScrollBarDrawable
8214 */
8215 protected int computeHorizontalScrollRange() {
8216 return getWidth();
8217 }
8218
8219 /**
8220 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
8221 * within the horizontal range. This value is used to compute the position
8222 * of the thumb within the scrollbar's track.</p>
8223 *
8224 * <p>The range is expressed in arbitrary units that must be the same as the
8225 * units used by {@link #computeHorizontalScrollRange()} and
8226 * {@link #computeHorizontalScrollExtent()}.</p>
8227 *
8228 * <p>The default offset is the scroll offset of this view.</p>
8229 *
8230 * @return the horizontal offset of the scrollbar's thumb
8231 *
8232 * @see #computeHorizontalScrollRange()
8233 * @see #computeHorizontalScrollExtent()
8234 * @see android.widget.ScrollBarDrawable
8235 */
8236 protected int computeHorizontalScrollOffset() {
8237 return mScrollX;
8238 }
8239
8240 /**
8241 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
8242 * within the horizontal range. This value is used to compute the length
8243 * of the thumb within the scrollbar's track.</p>
8244 *
8245 * <p>The range is expressed in arbitrary units that must be the same as the
8246 * units used by {@link #computeHorizontalScrollRange()} and
8247 * {@link #computeHorizontalScrollOffset()}.</p>
8248 *
8249 * <p>The default extent is the drawing width of this view.</p>
8250 *
8251 * @return the horizontal extent of the scrollbar's thumb
8252 *
8253 * @see #computeHorizontalScrollRange()
8254 * @see #computeHorizontalScrollOffset()
8255 * @see android.widget.ScrollBarDrawable
8256 */
8257 protected int computeHorizontalScrollExtent() {
8258 return getWidth();
8259 }
8260
8261 /**
8262 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
8263 *
8264 * <p>The range is expressed in arbitrary units that must be the same as the
8265 * units used by {@link #computeVerticalScrollExtent()} and
8266 * {@link #computeVerticalScrollOffset()}.</p>
8267 *
8268 * @return the total vertical range represented by the vertical scrollbar
8269 *
8270 * <p>The default range is the drawing height of this view.</p>
8271 *
8272 * @see #computeVerticalScrollExtent()
8273 * @see #computeVerticalScrollOffset()
8274 * @see android.widget.ScrollBarDrawable
8275 */
8276 protected int computeVerticalScrollRange() {
8277 return getHeight();
8278 }
8279
8280 /**
8281 * <p>Compute the vertical offset of the vertical scrollbar's thumb
8282 * within the horizontal range. This value is used to compute the position
8283 * of the thumb within the scrollbar's track.</p>
8284 *
8285 * <p>The range is expressed in arbitrary units that must be the same as the
8286 * units used by {@link #computeVerticalScrollRange()} and
8287 * {@link #computeVerticalScrollExtent()}.</p>
8288 *
8289 * <p>The default offset is the scroll offset of this view.</p>
8290 *
8291 * @return the vertical offset of the scrollbar's thumb
8292 *
8293 * @see #computeVerticalScrollRange()
8294 * @see #computeVerticalScrollExtent()
8295 * @see android.widget.ScrollBarDrawable
8296 */
8297 protected int computeVerticalScrollOffset() {
8298 return mScrollY;
8299 }
8300
8301 /**
8302 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
8303 * within the vertical range. This value is used to compute the length
8304 * of the thumb within the scrollbar's track.</p>
8305 *
8306 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08008307 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008308 * {@link #computeVerticalScrollOffset()}.</p>
8309 *
8310 * <p>The default extent is the drawing height of this view.</p>
8311 *
8312 * @return the vertical extent of the scrollbar's thumb
8313 *
8314 * @see #computeVerticalScrollRange()
8315 * @see #computeVerticalScrollOffset()
8316 * @see android.widget.ScrollBarDrawable
8317 */
8318 protected int computeVerticalScrollExtent() {
8319 return getHeight();
8320 }
8321
8322 /**
8323 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8324 * scrollbars are painted only if they have been awakened first.</p>
8325 *
8326 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008327 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008328 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008329 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008330 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008331 // scrollbars are drawn only when the animation is running
8332 final ScrollabilityCache cache = mScrollCache;
8333 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008334
Mike Cleronf116bf82009-09-27 19:14:12 -07008335 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008336
Mike Cleronf116bf82009-09-27 19:14:12 -07008337 if (state == ScrollabilityCache.OFF) {
8338 return;
8339 }
Joe Malin32736f02011-01-19 16:14:20 -08008340
Mike Cleronf116bf82009-09-27 19:14:12 -07008341 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008342
Mike Cleronf116bf82009-09-27 19:14:12 -07008343 if (state == ScrollabilityCache.FADING) {
8344 // We're fading -- get our fade interpolation
8345 if (cache.interpolatorValues == null) {
8346 cache.interpolatorValues = new float[1];
8347 }
Joe Malin32736f02011-01-19 16:14:20 -08008348
Mike Cleronf116bf82009-09-27 19:14:12 -07008349 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008350
Mike Cleronf116bf82009-09-27 19:14:12 -07008351 // Stops the animation if we're done
8352 if (cache.scrollBarInterpolator.timeToValues(values) ==
8353 Interpolator.Result.FREEZE_END) {
8354 cache.state = ScrollabilityCache.OFF;
8355 } else {
8356 cache.scrollBar.setAlpha(Math.round(values[0]));
8357 }
Joe Malin32736f02011-01-19 16:14:20 -08008358
8359 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008360 // drawing. We only want this when we're fading so that
8361 // we prevent excessive redraws
8362 invalidate = true;
8363 } else {
8364 // We're just on -- but we may have been fading before so
8365 // reset alpha
8366 cache.scrollBar.setAlpha(255);
8367 }
8368
Joe Malin32736f02011-01-19 16:14:20 -08008369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008370 final int viewFlags = mViewFlags;
8371
8372 final boolean drawHorizontalScrollBar =
8373 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8374 final boolean drawVerticalScrollBar =
8375 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8376 && !isVerticalScrollBarHidden();
8377
8378 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8379 final int width = mRight - mLeft;
8380 final int height = mBottom - mTop;
8381
8382 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008383
Mike Reede8853fc2009-09-04 14:01:48 -04008384 final int scrollX = mScrollX;
8385 final int scrollY = mScrollY;
8386 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8387
Mike Cleronf116bf82009-09-27 19:14:12 -07008388 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008390 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008391 int size = scrollBar.getSize(false);
8392 if (size <= 0) {
8393 size = cache.scrollBarSize;
8394 }
8395
Mike Cleronf116bf82009-09-27 19:14:12 -07008396 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008397 computeHorizontalScrollOffset(),
8398 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008399 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008400 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008401 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008402 left = scrollX + (mPaddingLeft & inside);
8403 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8404 bottom = top + size;
8405 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8406 if (invalidate) {
8407 invalidate(left, top, right, bottom);
8408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008409 }
8410
8411 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008412 int size = scrollBar.getSize(true);
8413 if (size <= 0) {
8414 size = cache.scrollBarSize;
8415 }
8416
Mike Reede8853fc2009-09-04 14:01:48 -04008417 scrollBar.setParameters(computeVerticalScrollRange(),
8418 computeVerticalScrollOffset(),
8419 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008420 switch (mVerticalScrollbarPosition) {
8421 default:
8422 case SCROLLBAR_POSITION_DEFAULT:
8423 case SCROLLBAR_POSITION_RIGHT:
8424 left = scrollX + width - size - (mUserPaddingRight & inside);
8425 break;
8426 case SCROLLBAR_POSITION_LEFT:
8427 left = scrollX + (mUserPaddingLeft & inside);
8428 break;
8429 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008430 top = scrollY + (mPaddingTop & inside);
8431 right = left + size;
8432 bottom = scrollY + height - (mUserPaddingBottom & inside);
8433 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8434 if (invalidate) {
8435 invalidate(left, top, right, bottom);
8436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008437 }
8438 }
8439 }
8440 }
Romain Guy8506ab42009-06-11 17:35:47 -07008441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008442 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008443 * 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 -08008444 * FastScroller is visible.
8445 * @return whether to temporarily hide the vertical scrollbar
8446 * @hide
8447 */
8448 protected boolean isVerticalScrollBarHidden() {
8449 return false;
8450 }
8451
8452 /**
8453 * <p>Draw the horizontal scrollbar if
8454 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
8455 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008456 * @param canvas the canvas on which to draw the scrollbar
8457 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008458 *
8459 * @see #isHorizontalScrollBarEnabled()
8460 * @see #computeHorizontalScrollRange()
8461 * @see #computeHorizontalScrollExtent()
8462 * @see #computeHorizontalScrollOffset()
8463 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008464 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008465 */
Romain Guy8fb95422010-08-17 18:38:51 -07008466 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8467 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008468 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008469 scrollBar.draw(canvas);
8470 }
Mike Reede8853fc2009-09-04 14:01:48 -04008471
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008472 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008473 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8474 * returns true.</p>
8475 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008476 * @param canvas the canvas on which to draw the scrollbar
8477 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008478 *
8479 * @see #isVerticalScrollBarEnabled()
8480 * @see #computeVerticalScrollRange()
8481 * @see #computeVerticalScrollExtent()
8482 * @see #computeVerticalScrollOffset()
8483 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008484 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008485 */
Romain Guy8fb95422010-08-17 18:38:51 -07008486 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
8487 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04008488 scrollBar.setBounds(l, t, r, b);
8489 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008490 }
8491
8492 /**
8493 * Implement this to do your drawing.
8494 *
8495 * @param canvas the canvas on which the background will be drawn
8496 */
8497 protected void onDraw(Canvas canvas) {
8498 }
8499
8500 /*
8501 * Caller is responsible for calling requestLayout if necessary.
8502 * (This allows addViewInLayout to not request a new layout.)
8503 */
8504 void assignParent(ViewParent parent) {
8505 if (mParent == null) {
8506 mParent = parent;
8507 } else if (parent == null) {
8508 mParent = null;
8509 } else {
8510 throw new RuntimeException("view " + this + " being added, but"
8511 + " it already has a parent");
8512 }
8513 }
8514
8515 /**
8516 * This is called when the view is attached to a window. At this point it
8517 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008518 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
8519 * however it may be called any time before the first onDraw -- including
8520 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008521 *
8522 * @see #onDetachedFromWindow()
8523 */
8524 protected void onAttachedToWindow() {
8525 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
8526 mParent.requestTransparentRegion(this);
8527 }
Adam Powell8568c3a2010-04-19 14:26:11 -07008528 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
8529 initialAwakenScrollBars();
8530 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
8531 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08008532 jumpDrawablesToCurrentState();
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008533 resolveHorizontalDirection();
8534 }
Cibu Johny86666632010-02-22 13:01:02 -08008535
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008536 /**
8537 * Resolving the layout direction. LTR is set initially.
8538 * We are supposing here that the parent directionality will be resolved before its children
8539 */
8540 private void resolveHorizontalDirection() {
Cibu Johny86666632010-02-22 13:01:02 -08008541 mPrivateFlags2 &= ~RESOLVED_LAYOUT_RTL;
8542 switch (getHorizontalDirection()) {
8543 case HORIZONTAL_DIRECTION_INHERIT:
8544 // If this is root view, no need to look at parent's layout dir.
8545 if (mParent != null && mParent instanceof ViewGroup &&
8546 ((ViewGroup) mParent).isLayoutRtl()) {
8547 mPrivateFlags2 |= RESOLVED_LAYOUT_RTL;
8548 }
8549 break;
8550 case HORIZONTAL_DIRECTION_RTL:
8551 mPrivateFlags2 |= RESOLVED_LAYOUT_RTL;
8552 break;
8553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008554 }
8555
8556 /**
8557 * This is called when the view is detached from a window. At this point it
8558 * no longer has a surface for drawing.
8559 *
8560 * @see #onAttachedToWindow()
8561 */
8562 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08008563 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08008564
Romain Guya440b002010-02-24 15:57:54 -08008565 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05008566 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08008567 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08008568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008569 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08008570
8571 if (mHardwareLayer != null) {
8572 mHardwareLayer.destroy();
8573 mHardwareLayer = null;
8574 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008575
Chet Haasedaf98e92011-01-10 14:10:36 -08008576 if (mDisplayList != null) {
8577 mDisplayList.invalidate();
8578 }
8579
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008580 if (mAttachInfo != null) {
8581 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
8582 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
8583 }
8584
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08008585 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008586 }
8587
8588 /**
8589 * @return The number of times this view has been attached to a window
8590 */
8591 protected int getWindowAttachCount() {
8592 return mWindowAttachCount;
8593 }
8594
8595 /**
8596 * Retrieve a unique token identifying the window this view is attached to.
8597 * @return Return the window's token for use in
8598 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
8599 */
8600 public IBinder getWindowToken() {
8601 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
8602 }
8603
8604 /**
8605 * Retrieve a unique token identifying the top-level "real" window of
8606 * the window that this view is attached to. That is, this is like
8607 * {@link #getWindowToken}, except if the window this view in is a panel
8608 * window (attached to another containing window), then the token of
8609 * the containing window is returned instead.
8610 *
8611 * @return Returns the associated window token, either
8612 * {@link #getWindowToken()} or the containing window's token.
8613 */
8614 public IBinder getApplicationWindowToken() {
8615 AttachInfo ai = mAttachInfo;
8616 if (ai != null) {
8617 IBinder appWindowToken = ai.mPanelParentWindowToken;
8618 if (appWindowToken == null) {
8619 appWindowToken = ai.mWindowToken;
8620 }
8621 return appWindowToken;
8622 }
8623 return null;
8624 }
8625
8626 /**
8627 * Retrieve private session object this view hierarchy is using to
8628 * communicate with the window manager.
8629 * @return the session object to communicate with the window manager
8630 */
8631 /*package*/ IWindowSession getWindowSession() {
8632 return mAttachInfo != null ? mAttachInfo.mSession : null;
8633 }
8634
8635 /**
8636 * @param info the {@link android.view.View.AttachInfo} to associated with
8637 * this view
8638 */
8639 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
8640 //System.out.println("Attached! " + this);
8641 mAttachInfo = info;
8642 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008643 // We will need to evaluate the drawable state at least once.
8644 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008645 if (mFloatingTreeObserver != null) {
8646 info.mTreeObserver.merge(mFloatingTreeObserver);
8647 mFloatingTreeObserver = null;
8648 }
8649 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
8650 mAttachInfo.mScrollContainers.add(this);
8651 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
8652 }
8653 performCollectViewAttributes(visibility);
8654 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08008655
8656 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8657 mOnAttachStateChangeListeners;
8658 if (listeners != null && listeners.size() > 0) {
8659 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8660 // perform the dispatching. The iterator is a safe guard against listeners that
8661 // could mutate the list by calling the various add/remove methods. This prevents
8662 // the array from being modified while we iterate it.
8663 for (OnAttachStateChangeListener listener : listeners) {
8664 listener.onViewAttachedToWindow(this);
8665 }
8666 }
8667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008668 int vis = info.mWindowVisibility;
8669 if (vis != GONE) {
8670 onWindowVisibilityChanged(vis);
8671 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008672 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
8673 // If nobody has evaluated the drawable state yet, then do it now.
8674 refreshDrawableState();
8675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008676 }
8677
8678 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008679 AttachInfo info = mAttachInfo;
8680 if (info != null) {
8681 int vis = info.mWindowVisibility;
8682 if (vis != GONE) {
8683 onWindowVisibilityChanged(GONE);
8684 }
8685 }
8686
8687 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08008688
Adam Powell4afd62b2011-02-18 15:02:18 -08008689 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8690 mOnAttachStateChangeListeners;
8691 if (listeners != null && listeners.size() > 0) {
8692 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8693 // perform the dispatching. The iterator is a safe guard against listeners that
8694 // could mutate the list by calling the various add/remove methods. This prevents
8695 // the array from being modified while we iterate it.
8696 for (OnAttachStateChangeListener listener : listeners) {
8697 listener.onViewDetachedFromWindow(this);
8698 }
8699 }
8700
Romain Guy01d5edc2011-01-28 11:28:53 -08008701 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008702 mAttachInfo.mScrollContainers.remove(this);
8703 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
8704 }
Romain Guy01d5edc2011-01-28 11:28:53 -08008705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008706 mAttachInfo = null;
8707 }
8708
8709 /**
8710 * Store this view hierarchy's frozen state into the given container.
8711 *
8712 * @param container The SparseArray in which to save the view's state.
8713 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008714 * @see #restoreHierarchyState(android.util.SparseArray)
8715 * @see #dispatchSaveInstanceState(android.util.SparseArray)
8716 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008717 */
8718 public void saveHierarchyState(SparseArray<Parcelable> container) {
8719 dispatchSaveInstanceState(container);
8720 }
8721
8722 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008723 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
8724 * this view and its children. May be overridden to modify how freezing happens to a
8725 * 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 -08008726 *
8727 * @param container The SparseArray in which to save the view's state.
8728 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008729 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
8730 * @see #saveHierarchyState(android.util.SparseArray)
8731 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008732 */
8733 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
8734 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
8735 mPrivateFlags &= ~SAVE_STATE_CALLED;
8736 Parcelable state = onSaveInstanceState();
8737 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8738 throw new IllegalStateException(
8739 "Derived class did not call super.onSaveInstanceState()");
8740 }
8741 if (state != null) {
8742 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
8743 // + ": " + state);
8744 container.put(mID, state);
8745 }
8746 }
8747 }
8748
8749 /**
8750 * Hook allowing a view to generate a representation of its internal state
8751 * that can later be used to create a new instance with that same state.
8752 * This state should only contain information that is not persistent or can
8753 * not be reconstructed later. For example, you will never store your
8754 * current position on screen because that will be computed again when a
8755 * new instance of the view is placed in its view hierarchy.
8756 * <p>
8757 * Some examples of things you may store here: the current cursor position
8758 * in a text view (but usually not the text itself since that is stored in a
8759 * content provider or other persistent storage), the currently selected
8760 * item in a list view.
8761 *
8762 * @return Returns a Parcelable object containing the view's current dynamic
8763 * state, or null if there is nothing interesting to save. The
8764 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008765 * @see #onRestoreInstanceState(android.os.Parcelable)
8766 * @see #saveHierarchyState(android.util.SparseArray)
8767 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008768 * @see #setSaveEnabled(boolean)
8769 */
8770 protected Parcelable onSaveInstanceState() {
8771 mPrivateFlags |= SAVE_STATE_CALLED;
8772 return BaseSavedState.EMPTY_STATE;
8773 }
8774
8775 /**
8776 * Restore this view hierarchy's frozen state from the given container.
8777 *
8778 * @param container The SparseArray which holds previously frozen states.
8779 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008780 * @see #saveHierarchyState(android.util.SparseArray)
8781 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
8782 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008783 */
8784 public void restoreHierarchyState(SparseArray<Parcelable> container) {
8785 dispatchRestoreInstanceState(container);
8786 }
8787
8788 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008789 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
8790 * state for this view and its children. May be overridden to modify how restoring
8791 * happens to a view's children; for example, some views may want to not store state
8792 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008793 *
8794 * @param container The SparseArray which holds previously saved state.
8795 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008796 * @see #dispatchSaveInstanceState(android.util.SparseArray)
8797 * @see #restoreHierarchyState(android.util.SparseArray)
8798 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008799 */
8800 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
8801 if (mID != NO_ID) {
8802 Parcelable state = container.get(mID);
8803 if (state != null) {
8804 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
8805 // + ": " + state);
8806 mPrivateFlags &= ~SAVE_STATE_CALLED;
8807 onRestoreInstanceState(state);
8808 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8809 throw new IllegalStateException(
8810 "Derived class did not call super.onRestoreInstanceState()");
8811 }
8812 }
8813 }
8814 }
8815
8816 /**
8817 * Hook allowing a view to re-apply a representation of its internal state that had previously
8818 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
8819 * null state.
8820 *
8821 * @param state The frozen state that had previously been returned by
8822 * {@link #onSaveInstanceState}.
8823 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008824 * @see #onSaveInstanceState()
8825 * @see #restoreHierarchyState(android.util.SparseArray)
8826 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008827 */
8828 protected void onRestoreInstanceState(Parcelable state) {
8829 mPrivateFlags |= SAVE_STATE_CALLED;
8830 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08008831 throw new IllegalArgumentException("Wrong state class, expecting View State but "
8832 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08008833 + "when two views of different type have the same id in the same hierarchy. "
8834 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08008835 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008836 }
8837 }
8838
8839 /**
8840 * <p>Return the time at which the drawing of the view hierarchy started.</p>
8841 *
8842 * @return the drawing start time in milliseconds
8843 */
8844 public long getDrawingTime() {
8845 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
8846 }
8847
8848 /**
8849 * <p>Enables or disables the duplication of the parent's state into this view. When
8850 * duplication is enabled, this view gets its drawable state from its parent rather
8851 * than from its own internal properties.</p>
8852 *
8853 * <p>Note: in the current implementation, setting this property to true after the
8854 * view was added to a ViewGroup might have no effect at all. This property should
8855 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
8856 *
8857 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
8858 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008859 *
Gilles Debunnefb817032011-01-13 13:52:49 -08008860 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
8861 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008862 *
8863 * @param enabled True to enable duplication of the parent's drawable state, false
8864 * to disable it.
8865 *
8866 * @see #getDrawableState()
8867 * @see #isDuplicateParentStateEnabled()
8868 */
8869 public void setDuplicateParentStateEnabled(boolean enabled) {
8870 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
8871 }
8872
8873 /**
8874 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
8875 *
8876 * @return True if this view's drawable state is duplicated from the parent,
8877 * false otherwise
8878 *
8879 * @see #getDrawableState()
8880 * @see #setDuplicateParentStateEnabled(boolean)
8881 */
8882 public boolean isDuplicateParentStateEnabled() {
8883 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
8884 }
8885
8886 /**
Romain Guy171c5922011-01-06 10:04:23 -08008887 * <p>Specifies the type of layer backing this view. The layer can be
8888 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
8889 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008890 *
Romain Guy171c5922011-01-06 10:04:23 -08008891 * <p>A layer is associated with an optional {@link android.graphics.Paint}
8892 * instance that controls how the layer is composed on screen. The following
8893 * properties of the paint are taken into account when composing the layer:</p>
8894 * <ul>
8895 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
8896 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
8897 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
8898 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08008899 *
Romain Guy171c5922011-01-06 10:04:23 -08008900 * <p>If this view has an alpha value set to < 1.0 by calling
8901 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
8902 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
8903 * equivalent to setting a hardware layer on this view and providing a paint with
8904 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08008905 *
Romain Guy171c5922011-01-06 10:04:23 -08008906 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
8907 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
8908 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008909 *
Romain Guy171c5922011-01-06 10:04:23 -08008910 * @param layerType The ype of layer to use with this view, must be one of
8911 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8912 * {@link #LAYER_TYPE_HARDWARE}
8913 * @param paint The paint used to compose the layer. This argument is optional
8914 * and can be null. It is ignored when the layer type is
8915 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08008916 *
8917 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08008918 * @see #LAYER_TYPE_NONE
8919 * @see #LAYER_TYPE_SOFTWARE
8920 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08008921 * @see #setAlpha(float)
8922 *
Romain Guy171c5922011-01-06 10:04:23 -08008923 * @attr ref android.R.styleable#View_layerType
8924 */
8925 public void setLayerType(int layerType, Paint paint) {
8926 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08008927 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08008928 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
8929 }
Chet Haasedaf98e92011-01-10 14:10:36 -08008930
Romain Guyd6cd5722011-01-17 14:42:41 -08008931 if (layerType == mLayerType) {
8932 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
8933 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008934 invalidateParentCaches();
8935 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08008936 }
8937 return;
8938 }
Romain Guy171c5922011-01-06 10:04:23 -08008939
8940 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08008941 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -07008942 case LAYER_TYPE_HARDWARE:
8943 if (mHardwareLayer != null) {
8944 mHardwareLayer.destroy();
8945 mHardwareLayer = null;
8946 }
8947 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -08008948 case LAYER_TYPE_SOFTWARE:
8949 if (mDrawingCache != null) {
8950 mDrawingCache.recycle();
8951 mDrawingCache = null;
8952 }
Joe Malin32736f02011-01-19 16:14:20 -08008953
Romain Guy6c319ca2011-01-11 14:29:25 -08008954 if (mUnscaledDrawingCache != null) {
8955 mUnscaledDrawingCache.recycle();
8956 mUnscaledDrawingCache = null;
8957 }
8958 break;
Romain Guy6c319ca2011-01-11 14:29:25 -08008959 default:
8960 break;
Romain Guy171c5922011-01-06 10:04:23 -08008961 }
8962
8963 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08008964 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
8965 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
8966 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08008967
Romain Guy0fd89bf2011-01-26 15:41:30 -08008968 invalidateParentCaches();
8969 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08008970 }
8971
8972 /**
8973 * Indicates what type of layer is currently associated with this view. By default
8974 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
8975 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
8976 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08008977 *
Romain Guy171c5922011-01-06 10:04:23 -08008978 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8979 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08008980 *
8981 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08008982 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08008983 * @see #LAYER_TYPE_NONE
8984 * @see #LAYER_TYPE_SOFTWARE
8985 * @see #LAYER_TYPE_HARDWARE
8986 */
8987 public int getLayerType() {
8988 return mLayerType;
8989 }
Joe Malin32736f02011-01-19 16:14:20 -08008990
Romain Guy6c319ca2011-01-11 14:29:25 -08008991 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08008992 * Forces this view's layer to be created and this view to be rendered
8993 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
8994 * invoking this method will have no effect.
8995 *
8996 * This method can for instance be used to render a view into its layer before
8997 * starting an animation. If this view is complex, rendering into the layer
8998 * before starting the animation will avoid skipping frames.
8999 *
9000 * @throws IllegalStateException If this view is not attached to a window
9001 *
9002 * @see #setLayerType(int, android.graphics.Paint)
9003 */
9004 public void buildLayer() {
9005 if (mLayerType == LAYER_TYPE_NONE) return;
9006
9007 if (mAttachInfo == null) {
9008 throw new IllegalStateException("This view must be attached to a window first");
9009 }
9010
9011 switch (mLayerType) {
9012 case LAYER_TYPE_HARDWARE:
9013 getHardwareLayer();
9014 break;
9015 case LAYER_TYPE_SOFTWARE:
9016 buildDrawingCache(true);
9017 break;
9018 }
9019 }
9020
9021 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08009022 * <p>Returns a hardware layer that can be used to draw this view again
9023 * without executing its draw method.</p>
9024 *
9025 * @return A HardwareLayer ready to render, or null if an error occurred.
9026 */
Romain Guy5e7f7662011-01-24 22:35:56 -08009027 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08009028 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
9029 return null;
9030 }
9031
9032 final int width = mRight - mLeft;
9033 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08009034
Romain Guy6c319ca2011-01-11 14:29:25 -08009035 if (width == 0 || height == 0) {
9036 return null;
9037 }
9038
9039 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
9040 if (mHardwareLayer == null) {
9041 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
9042 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08009043 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009044 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
9045 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08009046 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009047 }
9048
Romain Guy5e7f7662011-01-24 22:35:56 -08009049 Canvas currentCanvas = mAttachInfo.mHardwareCanvas;
9050 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
9051 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009052 try {
9053 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08009054 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08009055 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009056
Chet Haase88172fe2011-03-07 17:36:33 -08009057 final int restoreCount = canvas.save();
9058
Romain Guy6c319ca2011-01-11 14:29:25 -08009059 computeScroll();
9060 canvas.translate(-mScrollX, -mScrollY);
9061
Romain Guy6c319ca2011-01-11 14:29:25 -08009062 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009063
Romain Guy6c319ca2011-01-11 14:29:25 -08009064 // Fast path for layouts with no backgrounds
9065 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9066 mPrivateFlags &= ~DIRTY_MASK;
9067 dispatchDraw(canvas);
9068 } else {
9069 draw(canvas);
9070 }
Joe Malin32736f02011-01-19 16:14:20 -08009071
Chet Haase88172fe2011-03-07 17:36:33 -08009072 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08009073 } finally {
9074 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08009075 mHardwareLayer.end(currentCanvas);
9076 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009077 }
9078 }
9079
9080 return mHardwareLayer;
9081 }
Romain Guy171c5922011-01-06 10:04:23 -08009082
9083 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009084 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
9085 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
9086 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
9087 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
9088 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
9089 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009090 *
Romain Guy171c5922011-01-06 10:04:23 -08009091 * <p>Enabling the drawing cache is similar to
9092 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08009093 * acceleration is turned off. When hardware acceleration is turned on, enabling the
9094 * drawing cache has no effect on rendering because the system uses a different mechanism
9095 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
9096 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
9097 * for information on how to enable software and hardware layers.</p>
9098 *
9099 * <p>This API can be used to manually generate
9100 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
9101 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009102 *
9103 * @param enabled true to enable the drawing cache, false otherwise
9104 *
9105 * @see #isDrawingCacheEnabled()
9106 * @see #getDrawingCache()
9107 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08009108 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009109 */
9110 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009111 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009112 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
9113 }
9114
9115 /**
9116 * <p>Indicates whether the drawing cache is enabled for this view.</p>
9117 *
9118 * @return true if the drawing cache is enabled
9119 *
9120 * @see #setDrawingCacheEnabled(boolean)
9121 * @see #getDrawingCache()
9122 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009123 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009124 public boolean isDrawingCacheEnabled() {
9125 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
9126 }
9127
9128 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08009129 * Debugging utility which recursively outputs the dirty state of a view and its
9130 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08009131 *
Chet Haasedaf98e92011-01-10 14:10:36 -08009132 * @hide
9133 */
Romain Guy676b1732011-02-14 14:45:33 -08009134 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08009135 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
9136 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
9137 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
9138 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
9139 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
9140 if (clear) {
9141 mPrivateFlags &= clearMask;
9142 }
9143 if (this instanceof ViewGroup) {
9144 ViewGroup parent = (ViewGroup) this;
9145 final int count = parent.getChildCount();
9146 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08009147 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08009148 child.outputDirtyFlags(indent + " ", clear, clearMask);
9149 }
9150 }
9151 }
9152
9153 /**
9154 * This method is used by ViewGroup to cause its children to restore or recreate their
9155 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
9156 * to recreate its own display list, which would happen if it went through the normal
9157 * draw/dispatchDraw mechanisms.
9158 *
9159 * @hide
9160 */
9161 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08009162
9163 /**
9164 * A view that is not attached or hardware accelerated cannot create a display list.
9165 * This method checks these conditions and returns the appropriate result.
9166 *
9167 * @return true if view has the ability to create a display list, false otherwise.
9168 *
9169 * @hide
9170 */
9171 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08009172 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08009173 }
Joe Malin32736f02011-01-19 16:14:20 -08009174
Chet Haasedaf98e92011-01-10 14:10:36 -08009175 /**
Romain Guyb051e892010-09-28 19:09:36 -07009176 * <p>Returns a display list that can be used to draw this view again
9177 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009178 *
Romain Guyb051e892010-09-28 19:09:36 -07009179 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08009180 *
9181 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07009182 */
Chet Haasedaf98e92011-01-10 14:10:36 -08009183 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08009184 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07009185 return null;
9186 }
9187
Chet Haasedaf98e92011-01-10 14:10:36 -08009188 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
9189 mDisplayList == null || !mDisplayList.isValid() ||
9190 mRecreateDisplayList)) {
9191 // Don't need to recreate the display list, just need to tell our
9192 // children to restore/recreate theirs
9193 if (mDisplayList != null && mDisplayList.isValid() &&
9194 !mRecreateDisplayList) {
9195 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9196 mPrivateFlags &= ~DIRTY_MASK;
9197 dispatchGetDisplayList();
9198
9199 return mDisplayList;
9200 }
9201
9202 // If we got here, we're recreating it. Mark it as such to ensure that
9203 // we copy in child display lists into ours in drawChild()
9204 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08009205 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009206 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
9207 // If we're creating a new display list, make sure our parent gets invalidated
9208 // since they will need to recreate their display list to account for this
9209 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08009210 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08009211 }
Romain Guyb051e892010-09-28 19:09:36 -07009212
9213 final HardwareCanvas canvas = mDisplayList.start();
9214 try {
9215 int width = mRight - mLeft;
9216 int height = mBottom - mTop;
9217
9218 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08009219 // The dirty rect should always be null for a display list
9220 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07009221
Chet Haase88172fe2011-03-07 17:36:33 -08009222 final int restoreCount = canvas.save();
9223
Chet Haasedaf98e92011-01-10 14:10:36 -08009224 computeScroll();
9225 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009226 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009227
Romain Guyb051e892010-09-28 19:09:36 -07009228 // Fast path for layouts with no backgrounds
9229 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9230 mPrivateFlags &= ~DIRTY_MASK;
9231 dispatchDraw(canvas);
9232 } else {
9233 draw(canvas);
9234 }
Joe Malin32736f02011-01-19 16:14:20 -08009235
Chet Haase88172fe2011-03-07 17:36:33 -08009236 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07009237 } finally {
9238 canvas.onPostDraw();
9239
9240 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07009241 }
Chet Haase77785f92011-01-25 23:22:09 -08009242 } else {
9243 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9244 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07009245 }
9246
9247 return mDisplayList;
9248 }
9249
9250 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009251 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009252 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009253 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009254 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009255 * @see #getDrawingCache(boolean)
9256 */
9257 public Bitmap getDrawingCache() {
9258 return getDrawingCache(false);
9259 }
9260
9261 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009262 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
9263 * is null when caching is disabled. If caching is enabled and the cache is not ready,
9264 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
9265 * draw from the cache when the cache is enabled. To benefit from the cache, you must
9266 * request the drawing cache by calling this method and draw it on screen if the
9267 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009268 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009269 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9270 * this method will create a bitmap of the same size as this view. Because this bitmap
9271 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9272 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9273 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9274 * size than the view. This implies that your application must be able to handle this
9275 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009276 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009277 * @param autoScale Indicates whether the generated bitmap should be scaled based on
9278 * the current density of the screen when the application is in compatibility
9279 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009280 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009281 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009282 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009283 * @see #setDrawingCacheEnabled(boolean)
9284 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07009285 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009286 * @see #destroyDrawingCache()
9287 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009288 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009289 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
9290 return null;
9291 }
9292 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009293 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009294 }
Romain Guy02890fd2010-08-06 17:58:44 -07009295 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009296 }
9297
9298 /**
9299 * <p>Frees the resources used by the drawing cache. If you call
9300 * {@link #buildDrawingCache()} manually without calling
9301 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9302 * should cleanup the cache with this method afterwards.</p>
9303 *
9304 * @see #setDrawingCacheEnabled(boolean)
9305 * @see #buildDrawingCache()
9306 * @see #getDrawingCache()
9307 */
9308 public void destroyDrawingCache() {
9309 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009310 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009311 mDrawingCache = null;
9312 }
Romain Guyfbd8f692009-06-26 14:51:58 -07009313 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009314 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07009315 mUnscaledDrawingCache = null;
9316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009317 }
9318
9319 /**
9320 * Setting a solid background color for the drawing cache's bitmaps will improve
9321 * perfromance and memory usage. Note, though that this should only be used if this
9322 * view will always be drawn on top of a solid color.
9323 *
9324 * @param color The background color to use for the drawing cache's bitmap
9325 *
9326 * @see #setDrawingCacheEnabled(boolean)
9327 * @see #buildDrawingCache()
9328 * @see #getDrawingCache()
9329 */
9330 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08009331 if (color != mDrawingCacheBackgroundColor) {
9332 mDrawingCacheBackgroundColor = color;
9333 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9334 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009335 }
9336
9337 /**
9338 * @see #setDrawingCacheBackgroundColor(int)
9339 *
9340 * @return The background color to used for the drawing cache's bitmap
9341 */
9342 public int getDrawingCacheBackgroundColor() {
9343 return mDrawingCacheBackgroundColor;
9344 }
9345
9346 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009347 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009348 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009349 * @see #buildDrawingCache(boolean)
9350 */
9351 public void buildDrawingCache() {
9352 buildDrawingCache(false);
9353 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08009354
Romain Guyfbd8f692009-06-26 14:51:58 -07009355 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009356 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
9357 *
9358 * <p>If you call {@link #buildDrawingCache()} manually without calling
9359 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9360 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009361 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009362 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9363 * this method will create a bitmap of the same size as this view. Because this bitmap
9364 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9365 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9366 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9367 * size than the view. This implies that your application must be able to handle this
9368 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009369 *
Romain Guy0d9275e2010-10-26 14:22:30 -07009370 * <p>You should avoid calling this method when hardware acceleration is enabled. If
9371 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08009372 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07009373 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009374 *
9375 * @see #getDrawingCache()
9376 * @see #destroyDrawingCache()
9377 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009378 public void buildDrawingCache(boolean autoScale) {
9379 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07009380 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009381 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009382
9383 if (ViewDebug.TRACE_HIERARCHY) {
9384 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
9385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009386
Romain Guy8506ab42009-06-11 17:35:47 -07009387 int width = mRight - mLeft;
9388 int height = mBottom - mTop;
9389
9390 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07009391 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07009392
Romain Guye1123222009-06-29 14:24:56 -07009393 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009394 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
9395 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07009396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009397
9398 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07009399 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08009400 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009401
9402 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07009403 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08009404 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009405 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
9406 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08009407 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009408 return;
9409 }
9410
9411 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07009412 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009413
9414 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009415 Bitmap.Config quality;
9416 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08009417 // Never pick ARGB_4444 because it looks awful
9418 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009419 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
9420 case DRAWING_CACHE_QUALITY_AUTO:
9421 quality = Bitmap.Config.ARGB_8888;
9422 break;
9423 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08009424 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009425 break;
9426 case DRAWING_CACHE_QUALITY_HIGH:
9427 quality = Bitmap.Config.ARGB_8888;
9428 break;
9429 default:
9430 quality = Bitmap.Config.ARGB_8888;
9431 break;
9432 }
9433 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07009434 // Optimization for translucent windows
9435 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08009436 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009437 }
9438
9439 // Try to cleanup memory
9440 if (bitmap != null) bitmap.recycle();
9441
9442 try {
9443 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07009444 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07009445 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07009446 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009447 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07009448 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009449 }
Adam Powell26153a32010-11-08 15:22:27 -08009450 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009451 } catch (OutOfMemoryError e) {
9452 // If there is not enough memory to create the bitmap cache, just
9453 // ignore the issue as bitmap caches are not required to draw the
9454 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07009455 if (autoScale) {
9456 mDrawingCache = null;
9457 } else {
9458 mUnscaledDrawingCache = null;
9459 }
Romain Guy0211a0a2011-02-14 16:34:59 -08009460 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009461 return;
9462 }
9463
9464 clear = drawingCacheBackgroundColor != 0;
9465 }
9466
9467 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009468 if (attachInfo != null) {
9469 canvas = attachInfo.mCanvas;
9470 if (canvas == null) {
9471 canvas = new Canvas();
9472 }
9473 canvas.setBitmap(bitmap);
9474 // Temporarily clobber the cached Canvas in case one of our children
9475 // is also using a drawing cache. Without this, the children would
9476 // steal the canvas by attaching their own bitmap to it and bad, bad
9477 // thing would happen (invisible views, corrupted drawings, etc.)
9478 attachInfo.mCanvas = null;
9479 } else {
9480 // This case should hopefully never or seldom happen
9481 canvas = new Canvas(bitmap);
9482 }
9483
9484 if (clear) {
9485 bitmap.eraseColor(drawingCacheBackgroundColor);
9486 }
9487
9488 computeScroll();
9489 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08009490
Romain Guye1123222009-06-29 14:24:56 -07009491 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009492 final float scale = attachInfo.mApplicationScale;
9493 canvas.scale(scale, scale);
9494 }
Joe Malin32736f02011-01-19 16:14:20 -08009495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009496 canvas.translate(-mScrollX, -mScrollY);
9497
Romain Guy5bcdff42009-05-14 21:27:18 -07009498 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08009499 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
9500 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07009501 mPrivateFlags |= DRAWING_CACHE_VALID;
9502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009503
9504 // Fast path for layouts with no backgrounds
9505 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9506 if (ViewDebug.TRACE_HIERARCHY) {
9507 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9508 }
Romain Guy5bcdff42009-05-14 21:27:18 -07009509 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009510 dispatchDraw(canvas);
9511 } else {
9512 draw(canvas);
9513 }
9514
9515 canvas.restoreToCount(restoreCount);
9516
9517 if (attachInfo != null) {
9518 // Restore the cached Canvas for our siblings
9519 attachInfo.mCanvas = canvas;
9520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009521 }
9522 }
9523
9524 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009525 * Create a snapshot of the view into a bitmap. We should probably make
9526 * some form of this public, but should think about the API.
9527 */
Romain Guy223ff5c2010-03-02 17:07:47 -08009528 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009529 int width = mRight - mLeft;
9530 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009531
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009532 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07009533 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009534 width = (int) ((width * scale) + 0.5f);
9535 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08009536
Romain Guy8c11e312009-09-14 15:15:30 -07009537 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009538 if (bitmap == null) {
9539 throw new OutOfMemoryError();
9540 }
9541
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009542 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08009543
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009544 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009545 if (attachInfo != null) {
9546 canvas = attachInfo.mCanvas;
9547 if (canvas == null) {
9548 canvas = new Canvas();
9549 }
9550 canvas.setBitmap(bitmap);
9551 // Temporarily clobber the cached Canvas in case one of our children
9552 // is also using a drawing cache. Without this, the children would
9553 // steal the canvas by attaching their own bitmap to it and bad, bad
9554 // things would happen (invisible views, corrupted drawings, etc.)
9555 attachInfo.mCanvas = null;
9556 } else {
9557 // This case should hopefully never or seldom happen
9558 canvas = new Canvas(bitmap);
9559 }
9560
Romain Guy5bcdff42009-05-14 21:27:18 -07009561 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009562 bitmap.eraseColor(backgroundColor);
9563 }
9564
9565 computeScroll();
9566 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009567 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009568 canvas.translate(-mScrollX, -mScrollY);
9569
Romain Guy5bcdff42009-05-14 21:27:18 -07009570 // Temporarily remove the dirty mask
9571 int flags = mPrivateFlags;
9572 mPrivateFlags &= ~DIRTY_MASK;
9573
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009574 // Fast path for layouts with no backgrounds
9575 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9576 dispatchDraw(canvas);
9577 } else {
9578 draw(canvas);
9579 }
9580
Romain Guy5bcdff42009-05-14 21:27:18 -07009581 mPrivateFlags = flags;
9582
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009583 canvas.restoreToCount(restoreCount);
9584
9585 if (attachInfo != null) {
9586 // Restore the cached Canvas for our siblings
9587 attachInfo.mCanvas = canvas;
9588 }
Romain Guy8506ab42009-06-11 17:35:47 -07009589
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009590 return bitmap;
9591 }
9592
9593 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009594 * Indicates whether this View is currently in edit mode. A View is usually
9595 * in edit mode when displayed within a developer tool. For instance, if
9596 * this View is being drawn by a visual user interface builder, this method
9597 * should return true.
9598 *
9599 * Subclasses should check the return value of this method to provide
9600 * different behaviors if their normal behavior might interfere with the
9601 * host environment. For instance: the class spawns a thread in its
9602 * constructor, the drawing code relies on device-specific features, etc.
9603 *
9604 * This method is usually checked in the drawing code of custom widgets.
9605 *
9606 * @return True if this View is in edit mode, false otherwise.
9607 */
9608 public boolean isInEditMode() {
9609 return false;
9610 }
9611
9612 /**
9613 * If the View draws content inside its padding and enables fading edges,
9614 * it needs to support padding offsets. Padding offsets are added to the
9615 * fading edges to extend the length of the fade so that it covers pixels
9616 * drawn inside the padding.
9617 *
9618 * Subclasses of this class should override this method if they need
9619 * to draw content inside the padding.
9620 *
9621 * @return True if padding offset must be applied, false otherwise.
9622 *
9623 * @see #getLeftPaddingOffset()
9624 * @see #getRightPaddingOffset()
9625 * @see #getTopPaddingOffset()
9626 * @see #getBottomPaddingOffset()
9627 *
9628 * @since CURRENT
9629 */
9630 protected boolean isPaddingOffsetRequired() {
9631 return false;
9632 }
9633
9634 /**
9635 * Amount by which to extend the left fading region. Called only when
9636 * {@link #isPaddingOffsetRequired()} returns true.
9637 *
9638 * @return The left padding offset in pixels.
9639 *
9640 * @see #isPaddingOffsetRequired()
9641 *
9642 * @since CURRENT
9643 */
9644 protected int getLeftPaddingOffset() {
9645 return 0;
9646 }
9647
9648 /**
9649 * Amount by which to extend the right fading region. Called only when
9650 * {@link #isPaddingOffsetRequired()} returns true.
9651 *
9652 * @return The right padding offset in pixels.
9653 *
9654 * @see #isPaddingOffsetRequired()
9655 *
9656 * @since CURRENT
9657 */
9658 protected int getRightPaddingOffset() {
9659 return 0;
9660 }
9661
9662 /**
9663 * Amount by which to extend the top fading region. Called only when
9664 * {@link #isPaddingOffsetRequired()} returns true.
9665 *
9666 * @return The top padding offset in pixels.
9667 *
9668 * @see #isPaddingOffsetRequired()
9669 *
9670 * @since CURRENT
9671 */
9672 protected int getTopPaddingOffset() {
9673 return 0;
9674 }
9675
9676 /**
9677 * Amount by which to extend the bottom fading region. Called only when
9678 * {@link #isPaddingOffsetRequired()} returns true.
9679 *
9680 * @return The bottom padding offset in pixels.
9681 *
9682 * @see #isPaddingOffsetRequired()
9683 *
9684 * @since CURRENT
9685 */
9686 protected int getBottomPaddingOffset() {
9687 return 0;
9688 }
9689
9690 /**
Romain Guy2bffd262010-09-12 17:40:02 -07009691 * <p>Indicates whether this view is attached to an hardware accelerated
9692 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009693 *
Romain Guy2bffd262010-09-12 17:40:02 -07009694 * <p>Even if this method returns true, it does not mean that every call
9695 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
9696 * accelerated {@link android.graphics.Canvas}. For instance, if this view
9697 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
9698 * window is hardware accelerated,
9699 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
9700 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009701 *
Romain Guy2bffd262010-09-12 17:40:02 -07009702 * @return True if the view is attached to a window and the window is
9703 * hardware accelerated; false in any other case.
9704 */
9705 public boolean isHardwareAccelerated() {
9706 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
9707 }
Joe Malin32736f02011-01-19 16:14:20 -08009708
Romain Guy2bffd262010-09-12 17:40:02 -07009709 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009710 * Manually render this view (and all of its children) to the given Canvas.
9711 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009712 * called. When implementing a view, implement
9713 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
9714 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009715 *
9716 * @param canvas The Canvas to which the View is rendered.
9717 */
9718 public void draw(Canvas canvas) {
9719 if (ViewDebug.TRACE_HIERARCHY) {
9720 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9721 }
9722
Romain Guy5bcdff42009-05-14 21:27:18 -07009723 final int privateFlags = mPrivateFlags;
9724 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
9725 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
9726 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07009727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009728 /*
9729 * Draw traversal performs several drawing steps which must be executed
9730 * in the appropriate order:
9731 *
9732 * 1. Draw the background
9733 * 2. If necessary, save the canvas' layers to prepare for fading
9734 * 3. Draw view's content
9735 * 4. Draw children
9736 * 5. If necessary, draw the fading edges and restore layers
9737 * 6. Draw decorations (scrollbars for instance)
9738 */
9739
9740 // Step 1, draw the background, if needed
9741 int saveCount;
9742
Romain Guy24443ea2009-05-11 11:56:30 -07009743 if (!dirtyOpaque) {
9744 final Drawable background = mBGDrawable;
9745 if (background != null) {
9746 final int scrollX = mScrollX;
9747 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009748
Romain Guy24443ea2009-05-11 11:56:30 -07009749 if (mBackgroundSizeChanged) {
9750 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
9751 mBackgroundSizeChanged = false;
9752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009753
Romain Guy24443ea2009-05-11 11:56:30 -07009754 if ((scrollX | scrollY) == 0) {
9755 background.draw(canvas);
9756 } else {
9757 canvas.translate(scrollX, scrollY);
9758 background.draw(canvas);
9759 canvas.translate(-scrollX, -scrollY);
9760 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009761 }
9762 }
9763
9764 // skip step 2 & 5 if possible (common case)
9765 final int viewFlags = mViewFlags;
9766 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
9767 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
9768 if (!verticalEdges && !horizontalEdges) {
9769 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009770 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009771
9772 // Step 4, draw the children
9773 dispatchDraw(canvas);
9774
9775 // Step 6, draw decorations (scrollbars)
9776 onDrawScrollBars(canvas);
9777
9778 // we're done...
9779 return;
9780 }
9781
9782 /*
9783 * Here we do the full fledged routine...
9784 * (this is an uncommon case where speed matters less,
9785 * this is why we repeat some of the tests that have been
9786 * done above)
9787 */
9788
9789 boolean drawTop = false;
9790 boolean drawBottom = false;
9791 boolean drawLeft = false;
9792 boolean drawRight = false;
9793
9794 float topFadeStrength = 0.0f;
9795 float bottomFadeStrength = 0.0f;
9796 float leftFadeStrength = 0.0f;
9797 float rightFadeStrength = 0.0f;
9798
9799 // Step 2, save the canvas' layers
9800 int paddingLeft = mPaddingLeft;
9801 int paddingTop = mPaddingTop;
9802
9803 final boolean offsetRequired = isPaddingOffsetRequired();
9804 if (offsetRequired) {
9805 paddingLeft += getLeftPaddingOffset();
9806 paddingTop += getTopPaddingOffset();
9807 }
9808
9809 int left = mScrollX + paddingLeft;
9810 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
9811 int top = mScrollY + paddingTop;
9812 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
9813
9814 if (offsetRequired) {
9815 right += getRightPaddingOffset();
9816 bottom += getBottomPaddingOffset();
9817 }
9818
9819 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009820 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
9821 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009822
9823 // clip the fade length if top and bottom fades overlap
9824 // overlapping fades produce odd-looking artifacts
9825 if (verticalEdges && (top + length > bottom - length)) {
9826 length = (bottom - top) / 2;
9827 }
9828
9829 // also clip horizontal fades if necessary
9830 if (horizontalEdges && (left + length > right - length)) {
9831 length = (right - left) / 2;
9832 }
9833
9834 if (verticalEdges) {
9835 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009836 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009837 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009838 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009839 }
9840
9841 if (horizontalEdges) {
9842 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009843 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009844 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009845 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009846 }
9847
9848 saveCount = canvas.getSaveCount();
9849
9850 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -07009851 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009852 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
9853
9854 if (drawTop) {
9855 canvas.saveLayer(left, top, right, top + length, null, flags);
9856 }
9857
9858 if (drawBottom) {
9859 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
9860 }
9861
9862 if (drawLeft) {
9863 canvas.saveLayer(left, top, left + length, bottom, null, flags);
9864 }
9865
9866 if (drawRight) {
9867 canvas.saveLayer(right - length, top, right, bottom, null, flags);
9868 }
9869 } else {
9870 scrollabilityCache.setFadeColor(solidColor);
9871 }
9872
9873 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009874 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009875
9876 // Step 4, draw the children
9877 dispatchDraw(canvas);
9878
9879 // Step 5, draw the fade effect and restore layers
9880 final Paint p = scrollabilityCache.paint;
9881 final Matrix matrix = scrollabilityCache.matrix;
9882 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009883
9884 if (drawTop) {
9885 matrix.setScale(1, fadeHeight * topFadeStrength);
9886 matrix.postTranslate(left, top);
9887 fade.setLocalMatrix(matrix);
9888 canvas.drawRect(left, top, right, top + length, p);
9889 }
9890
9891 if (drawBottom) {
9892 matrix.setScale(1, fadeHeight * bottomFadeStrength);
9893 matrix.postRotate(180);
9894 matrix.postTranslate(left, bottom);
9895 fade.setLocalMatrix(matrix);
9896 canvas.drawRect(left, bottom - length, right, bottom, p);
9897 }
9898
9899 if (drawLeft) {
9900 matrix.setScale(1, fadeHeight * leftFadeStrength);
9901 matrix.postRotate(-90);
9902 matrix.postTranslate(left, top);
9903 fade.setLocalMatrix(matrix);
9904 canvas.drawRect(left, top, left + length, bottom, p);
9905 }
9906
9907 if (drawRight) {
9908 matrix.setScale(1, fadeHeight * rightFadeStrength);
9909 matrix.postRotate(90);
9910 matrix.postTranslate(right, top);
9911 fade.setLocalMatrix(matrix);
9912 canvas.drawRect(right - length, top, right, bottom, p);
9913 }
9914
9915 canvas.restoreToCount(saveCount);
9916
9917 // Step 6, draw decorations (scrollbars)
9918 onDrawScrollBars(canvas);
9919 }
9920
9921 /**
9922 * Override this if your view is known to always be drawn on top of a solid color background,
9923 * and needs to draw fading edges. Returning a non-zero color enables the view system to
9924 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
9925 * should be set to 0xFF.
9926 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009927 * @see #setVerticalFadingEdgeEnabled(boolean)
9928 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009929 *
9930 * @return The known solid color background for this view, or 0 if the color may vary
9931 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009932 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009933 public int getSolidColor() {
9934 return 0;
9935 }
9936
9937 /**
9938 * Build a human readable string representation of the specified view flags.
9939 *
9940 * @param flags the view flags to convert to a string
9941 * @return a String representing the supplied flags
9942 */
9943 private static String printFlags(int flags) {
9944 String output = "";
9945 int numFlags = 0;
9946 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
9947 output += "TAKES_FOCUS";
9948 numFlags++;
9949 }
9950
9951 switch (flags & VISIBILITY_MASK) {
9952 case INVISIBLE:
9953 if (numFlags > 0) {
9954 output += " ";
9955 }
9956 output += "INVISIBLE";
9957 // USELESS HERE numFlags++;
9958 break;
9959 case GONE:
9960 if (numFlags > 0) {
9961 output += " ";
9962 }
9963 output += "GONE";
9964 // USELESS HERE numFlags++;
9965 break;
9966 default:
9967 break;
9968 }
9969 return output;
9970 }
9971
9972 /**
9973 * Build a human readable string representation of the specified private
9974 * view flags.
9975 *
9976 * @param privateFlags the private view flags to convert to a string
9977 * @return a String representing the supplied flags
9978 */
9979 private static String printPrivateFlags(int privateFlags) {
9980 String output = "";
9981 int numFlags = 0;
9982
9983 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
9984 output += "WANTS_FOCUS";
9985 numFlags++;
9986 }
9987
9988 if ((privateFlags & FOCUSED) == FOCUSED) {
9989 if (numFlags > 0) {
9990 output += " ";
9991 }
9992 output += "FOCUSED";
9993 numFlags++;
9994 }
9995
9996 if ((privateFlags & SELECTED) == SELECTED) {
9997 if (numFlags > 0) {
9998 output += " ";
9999 }
10000 output += "SELECTED";
10001 numFlags++;
10002 }
10003
10004 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
10005 if (numFlags > 0) {
10006 output += " ";
10007 }
10008 output += "IS_ROOT_NAMESPACE";
10009 numFlags++;
10010 }
10011
10012 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
10013 if (numFlags > 0) {
10014 output += " ";
10015 }
10016 output += "HAS_BOUNDS";
10017 numFlags++;
10018 }
10019
10020 if ((privateFlags & DRAWN) == DRAWN) {
10021 if (numFlags > 0) {
10022 output += " ";
10023 }
10024 output += "DRAWN";
10025 // USELESS HERE numFlags++;
10026 }
10027 return output;
10028 }
10029
10030 /**
10031 * <p>Indicates whether or not this view's layout will be requested during
10032 * the next hierarchy layout pass.</p>
10033 *
10034 * @return true if the layout will be forced during next layout pass
10035 */
10036 public boolean isLayoutRequested() {
10037 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
10038 }
10039
10040 /**
Cibu Johny86666632010-02-22 13:01:02 -080010041 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
10042 * layout attribute and/or the inherited value from the parent.</p>
10043 *
10044 * @return true if the layout is right-to-left.
10045 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -070010046 @ViewDebug.ExportedProperty(category = "layout")
Cibu Johny86666632010-02-22 13:01:02 -080010047 public boolean isLayoutRtl() {
10048 return (mPrivateFlags2 & RESOLVED_LAYOUT_RTL) == RESOLVED_LAYOUT_RTL;
10049 }
10050
10051 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010052 * Assign a size and position to a view and all of its
10053 * descendants
10054 *
10055 * <p>This is the second phase of the layout mechanism.
10056 * (The first is measuring). In this phase, each parent calls
10057 * layout on all of its children to position them.
10058 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080010059 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010060 *
Chet Haase9c087442011-01-12 16:20:16 -080010061 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010062 * Derived classes with children should override
10063 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080010064 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010065 *
10066 * @param l Left position, relative to parent
10067 * @param t Top position, relative to parent
10068 * @param r Right position, relative to parent
10069 * @param b Bottom position, relative to parent
10070 */
Romain Guy5429e1d2010-09-07 12:38:00 -070010071 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080010072 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070010073 int oldL = mLeft;
10074 int oldT = mTop;
10075 int oldB = mBottom;
10076 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010077 boolean changed = setFrame(l, t, r, b);
10078 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
10079 if (ViewDebug.TRACE_HIERARCHY) {
10080 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
10081 }
10082
10083 onLayout(changed, l, t, r, b);
10084 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070010085
10086 if (mOnLayoutChangeListeners != null) {
10087 ArrayList<OnLayoutChangeListener> listenersCopy =
10088 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
10089 int numListeners = listenersCopy.size();
10090 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070010091 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070010092 }
10093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010094 }
10095 mPrivateFlags &= ~FORCE_LAYOUT;
10096 }
10097
10098 /**
10099 * Called from layout when this view should
10100 * assign a size and position to each of its children.
10101 *
10102 * Derived classes with children should override
10103 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070010104 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010105 * @param changed This is a new size or position for this view
10106 * @param left Left position, relative to parent
10107 * @param top Top position, relative to parent
10108 * @param right Right position, relative to parent
10109 * @param bottom Bottom position, relative to parent
10110 */
10111 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
10112 }
10113
10114 /**
10115 * Assign a size and position to this view.
10116 *
10117 * This is called from layout.
10118 *
10119 * @param left Left position, relative to parent
10120 * @param top Top position, relative to parent
10121 * @param right Right position, relative to parent
10122 * @param bottom Bottom position, relative to parent
10123 * @return true if the new size and position are different than the
10124 * previous ones
10125 * {@hide}
10126 */
10127 protected boolean setFrame(int left, int top, int right, int bottom) {
10128 boolean changed = false;
10129
10130 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070010131 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010132 + right + "," + bottom + ")");
10133 }
10134
10135 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
10136 changed = true;
10137
10138 // Remember our drawn bit
10139 int drawn = mPrivateFlags & DRAWN;
10140
10141 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -080010142 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010143
10144
10145 int oldWidth = mRight - mLeft;
10146 int oldHeight = mBottom - mTop;
10147
10148 mLeft = left;
10149 mTop = top;
10150 mRight = right;
10151 mBottom = bottom;
10152
10153 mPrivateFlags |= HAS_BOUNDS;
10154
10155 int newWidth = right - left;
10156 int newHeight = bottom - top;
10157
10158 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010159 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
10160 // A change in dimension means an auto-centered pivot point changes, too
10161 mMatrixDirty = true;
10162 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010163 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
10164 }
10165
10166 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
10167 // If we are visible, force the DRAWN bit to on so that
10168 // this invalidate will go through (at least to our parent).
10169 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010170 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010171 // the DRAWN bit.
10172 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010173 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -080010174 // parent display list may need to be recreated based on a change in the bounds
10175 // of any child
10176 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010177 }
10178
10179 // Reset drawn bit to original value (invalidate turns it off)
10180 mPrivateFlags |= drawn;
10181
10182 mBackgroundSizeChanged = true;
10183 }
10184 return changed;
10185 }
10186
10187 /**
10188 * Finalize inflating a view from XML. This is called as the last phase
10189 * of inflation, after all child views have been added.
10190 *
10191 * <p>Even if the subclass overrides onFinishInflate, they should always be
10192 * sure to call the super method, so that we get called.
10193 */
10194 protected void onFinishInflate() {
10195 }
10196
10197 /**
10198 * Returns the resources associated with this view.
10199 *
10200 * @return Resources object.
10201 */
10202 public Resources getResources() {
10203 return mResources;
10204 }
10205
10206 /**
10207 * Invalidates the specified Drawable.
10208 *
10209 * @param drawable the drawable to invalidate
10210 */
10211 public void invalidateDrawable(Drawable drawable) {
10212 if (verifyDrawable(drawable)) {
10213 final Rect dirty = drawable.getBounds();
10214 final int scrollX = mScrollX;
10215 final int scrollY = mScrollY;
10216
10217 invalidate(dirty.left + scrollX, dirty.top + scrollY,
10218 dirty.right + scrollX, dirty.bottom + scrollY);
10219 }
10220 }
10221
10222 /**
10223 * Schedules an action on a drawable to occur at a specified time.
10224 *
10225 * @param who the recipient of the action
10226 * @param what the action to run on the drawable
10227 * @param when the time at which the action must occur. Uses the
10228 * {@link SystemClock#uptimeMillis} timebase.
10229 */
10230 public void scheduleDrawable(Drawable who, Runnable what, long when) {
10231 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10232 mAttachInfo.mHandler.postAtTime(what, who, when);
10233 }
10234 }
10235
10236 /**
10237 * Cancels a scheduled action on a drawable.
10238 *
10239 * @param who the recipient of the action
10240 * @param what the action to cancel
10241 */
10242 public void unscheduleDrawable(Drawable who, Runnable what) {
10243 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10244 mAttachInfo.mHandler.removeCallbacks(what, who);
10245 }
10246 }
10247
10248 /**
10249 * Unschedule any events associated with the given Drawable. This can be
10250 * used when selecting a new Drawable into a view, so that the previous
10251 * one is completely unscheduled.
10252 *
10253 * @param who The Drawable to unschedule.
10254 *
10255 * @see #drawableStateChanged
10256 */
10257 public void unscheduleDrawable(Drawable who) {
10258 if (mAttachInfo != null) {
10259 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
10260 }
10261 }
10262
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070010263 /**
10264 * Check if a given Drawable is in RTL layout direction.
10265 *
10266 * @param who the recipient of the action
10267 */
10268 public boolean isLayoutRtl(Drawable who) {
10269 return (who == mBGDrawable) && isLayoutRtl();
10270 }
10271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010272 /**
10273 * If your view subclass is displaying its own Drawable objects, it should
10274 * override this function and return true for any Drawable it is
10275 * displaying. This allows animations for those drawables to be
10276 * scheduled.
10277 *
10278 * <p>Be sure to call through to the super class when overriding this
10279 * function.
10280 *
10281 * @param who The Drawable to verify. Return true if it is one you are
10282 * displaying, else return the result of calling through to the
10283 * super class.
10284 *
10285 * @return boolean If true than the Drawable is being displayed in the
10286 * view; else false and it is not allowed to animate.
10287 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010288 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
10289 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010290 */
10291 protected boolean verifyDrawable(Drawable who) {
10292 return who == mBGDrawable;
10293 }
10294
10295 /**
10296 * This function is called whenever the state of the view changes in such
10297 * a way that it impacts the state of drawables being shown.
10298 *
10299 * <p>Be sure to call through to the superclass when overriding this
10300 * function.
10301 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010302 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010303 */
10304 protected void drawableStateChanged() {
10305 Drawable d = mBGDrawable;
10306 if (d != null && d.isStateful()) {
10307 d.setState(getDrawableState());
10308 }
10309 }
10310
10311 /**
10312 * Call this to force a view to update its drawable state. This will cause
10313 * drawableStateChanged to be called on this view. Views that are interested
10314 * in the new state should call getDrawableState.
10315 *
10316 * @see #drawableStateChanged
10317 * @see #getDrawableState
10318 */
10319 public void refreshDrawableState() {
10320 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
10321 drawableStateChanged();
10322
10323 ViewParent parent = mParent;
10324 if (parent != null) {
10325 parent.childDrawableStateChanged(this);
10326 }
10327 }
10328
10329 /**
10330 * Return an array of resource IDs of the drawable states representing the
10331 * current state of the view.
10332 *
10333 * @return The current drawable state
10334 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010335 * @see Drawable#setState(int[])
10336 * @see #drawableStateChanged()
10337 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010338 */
10339 public final int[] getDrawableState() {
10340 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
10341 return mDrawableState;
10342 } else {
10343 mDrawableState = onCreateDrawableState(0);
10344 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
10345 return mDrawableState;
10346 }
10347 }
10348
10349 /**
10350 * Generate the new {@link android.graphics.drawable.Drawable} state for
10351 * this view. This is called by the view
10352 * system when the cached Drawable state is determined to be invalid. To
10353 * retrieve the current state, you should use {@link #getDrawableState}.
10354 *
10355 * @param extraSpace if non-zero, this is the number of extra entries you
10356 * would like in the returned array in which you can place your own
10357 * states.
10358 *
10359 * @return Returns an array holding the current {@link Drawable} state of
10360 * the view.
10361 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010362 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010363 */
10364 protected int[] onCreateDrawableState(int extraSpace) {
10365 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
10366 mParent instanceof View) {
10367 return ((View) mParent).onCreateDrawableState(extraSpace);
10368 }
10369
10370 int[] drawableState;
10371
10372 int privateFlags = mPrivateFlags;
10373
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010374 int viewStateIndex = 0;
10375 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
10376 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
10377 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070010378 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010379 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
10380 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070010381 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
10382 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010383 // This is set if HW acceleration is requested, even if the current
10384 // process doesn't allow it. This is just to allow app preview
10385 // windows to better match their app.
10386 viewStateIndex |= VIEW_STATE_ACCELERATED;
10387 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070010388 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010389
Christopher Tate3d4bf172011-03-28 16:16:46 -070010390 final int privateFlags2 = mPrivateFlags2;
10391 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
10392 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
10393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010394 drawableState = VIEW_STATE_SETS[viewStateIndex];
10395
10396 //noinspection ConstantIfStatement
10397 if (false) {
10398 Log.i("View", "drawableStateIndex=" + viewStateIndex);
10399 Log.i("View", toString()
10400 + " pressed=" + ((privateFlags & PRESSED) != 0)
10401 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
10402 + " fo=" + hasFocus()
10403 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010404 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010405 + ": " + Arrays.toString(drawableState));
10406 }
10407
10408 if (extraSpace == 0) {
10409 return drawableState;
10410 }
10411
10412 final int[] fullState;
10413 if (drawableState != null) {
10414 fullState = new int[drawableState.length + extraSpace];
10415 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
10416 } else {
10417 fullState = new int[extraSpace];
10418 }
10419
10420 return fullState;
10421 }
10422
10423 /**
10424 * Merge your own state values in <var>additionalState</var> into the base
10425 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070010426 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010427 *
10428 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070010429 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010430 * own additional state values.
10431 *
10432 * @param additionalState The additional state values you would like
10433 * added to <var>baseState</var>; this array is not modified.
10434 *
10435 * @return As a convenience, the <var>baseState</var> array you originally
10436 * passed into the function is returned.
10437 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010438 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010439 */
10440 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
10441 final int N = baseState.length;
10442 int i = N - 1;
10443 while (i >= 0 && baseState[i] == 0) {
10444 i--;
10445 }
10446 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
10447 return baseState;
10448 }
10449
10450 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070010451 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
10452 * on all Drawable objects associated with this view.
10453 */
10454 public void jumpDrawablesToCurrentState() {
10455 if (mBGDrawable != null) {
10456 mBGDrawable.jumpToCurrentState();
10457 }
10458 }
10459
10460 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010461 * Sets the background color for this view.
10462 * @param color the color of the background
10463 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010464 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010465 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070010466 if (mBGDrawable instanceof ColorDrawable) {
10467 ((ColorDrawable) mBGDrawable).setColor(color);
10468 } else {
10469 setBackgroundDrawable(new ColorDrawable(color));
10470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010471 }
10472
10473 /**
10474 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070010475 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010476 * @param resid The identifier of the resource.
10477 * @attr ref android.R.styleable#View_background
10478 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010479 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010480 public void setBackgroundResource(int resid) {
10481 if (resid != 0 && resid == mBackgroundResource) {
10482 return;
10483 }
10484
10485 Drawable d= null;
10486 if (resid != 0) {
10487 d = mResources.getDrawable(resid);
10488 }
10489 setBackgroundDrawable(d);
10490
10491 mBackgroundResource = resid;
10492 }
10493
10494 /**
10495 * Set the background to a given Drawable, or remove the background. If the
10496 * background has padding, this View's padding is set to the background's
10497 * padding. However, when a background is removed, this View's padding isn't
10498 * touched. If setting the padding is desired, please use
10499 * {@link #setPadding(int, int, int, int)}.
10500 *
10501 * @param d The Drawable to use as the background, or null to remove the
10502 * background
10503 */
10504 public void setBackgroundDrawable(Drawable d) {
10505 boolean requestLayout = false;
10506
10507 mBackgroundResource = 0;
10508
10509 /*
10510 * Regardless of whether we're setting a new background or not, we want
10511 * to clear the previous drawable.
10512 */
10513 if (mBGDrawable != null) {
10514 mBGDrawable.setCallback(null);
10515 unscheduleDrawable(mBGDrawable);
10516 }
10517
10518 if (d != null) {
10519 Rect padding = sThreadLocal.get();
10520 if (padding == null) {
10521 padding = new Rect();
10522 sThreadLocal.set(padding);
10523 }
10524 if (d.getPadding(padding)) {
10525 setPadding(padding.left, padding.top, padding.right, padding.bottom);
10526 }
10527
10528 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
10529 // if it has a different minimum size, we should layout again
10530 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
10531 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
10532 requestLayout = true;
10533 }
10534
10535 d.setCallback(this);
10536 if (d.isStateful()) {
10537 d.setState(getDrawableState());
10538 }
10539 d.setVisible(getVisibility() == VISIBLE, false);
10540 mBGDrawable = d;
10541
10542 if ((mPrivateFlags & SKIP_DRAW) != 0) {
10543 mPrivateFlags &= ~SKIP_DRAW;
10544 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
10545 requestLayout = true;
10546 }
10547 } else {
10548 /* Remove the background */
10549 mBGDrawable = null;
10550
10551 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
10552 /*
10553 * This view ONLY drew the background before and we're removing
10554 * the background, so now it won't draw anything
10555 * (hence we SKIP_DRAW)
10556 */
10557 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
10558 mPrivateFlags |= SKIP_DRAW;
10559 }
10560
10561 /*
10562 * When the background is set, we try to apply its padding to this
10563 * View. When the background is removed, we don't touch this View's
10564 * padding. This is noted in the Javadocs. Hence, we don't need to
10565 * requestLayout(), the invalidate() below is sufficient.
10566 */
10567
10568 // The old background's minimum size could have affected this
10569 // View's layout, so let's requestLayout
10570 requestLayout = true;
10571 }
10572
Romain Guy8f1344f52009-05-15 16:03:59 -070010573 computeOpaqueFlags();
10574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010575 if (requestLayout) {
10576 requestLayout();
10577 }
10578
10579 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010580 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010581 }
10582
10583 /**
10584 * Gets the background drawable
10585 * @return The drawable used as the background for this view, if any.
10586 */
10587 public Drawable getBackground() {
10588 return mBGDrawable;
10589 }
10590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010591 /**
10592 * Sets the padding. The view may add on the space required to display
10593 * the scrollbars, depending on the style and visibility of the scrollbars.
10594 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
10595 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
10596 * from the values set in this call.
10597 *
10598 * @attr ref android.R.styleable#View_padding
10599 * @attr ref android.R.styleable#View_paddingBottom
10600 * @attr ref android.R.styleable#View_paddingLeft
10601 * @attr ref android.R.styleable#View_paddingRight
10602 * @attr ref android.R.styleable#View_paddingTop
10603 * @param left the left padding in pixels
10604 * @param top the top padding in pixels
10605 * @param right the right padding in pixels
10606 * @param bottom the bottom padding in pixels
10607 */
10608 public void setPadding(int left, int top, int right, int bottom) {
10609 boolean changed = false;
10610
Adam Powell20232d02010-12-08 21:08:53 -080010611 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010612 mUserPaddingRight = right;
10613 mUserPaddingBottom = bottom;
10614
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010615 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070010616
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010617 // Common case is there are no scroll bars.
10618 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010619 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080010620 // TODO Determine what to do with SCROLLBAR_POSITION_DEFAULT based on RTL settings.
10621 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010622 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080010623 switch (mVerticalScrollbarPosition) {
10624 case SCROLLBAR_POSITION_DEFAULT:
10625 case SCROLLBAR_POSITION_RIGHT:
10626 right += offset;
10627 break;
10628 case SCROLLBAR_POSITION_LEFT:
10629 left += offset;
10630 break;
10631 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010632 }
Adam Powell20232d02010-12-08 21:08:53 -080010633 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010634 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
10635 ? 0 : getHorizontalScrollbarHeight();
10636 }
10637 }
Romain Guy8506ab42009-06-11 17:35:47 -070010638
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010639 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010640 changed = true;
10641 mPaddingLeft = left;
10642 }
10643 if (mPaddingTop != top) {
10644 changed = true;
10645 mPaddingTop = top;
10646 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010647 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010648 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010649 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010650 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010651 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010652 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010653 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010654 }
10655
10656 if (changed) {
10657 requestLayout();
10658 }
10659 }
10660
10661 /**
10662 * Returns the top padding of this view.
10663 *
10664 * @return the top padding in pixels
10665 */
10666 public int getPaddingTop() {
10667 return mPaddingTop;
10668 }
10669
10670 /**
10671 * Returns the bottom padding of this view. If there are inset and enabled
10672 * scrollbars, this value may include the space required to display the
10673 * scrollbars as well.
10674 *
10675 * @return the bottom padding in pixels
10676 */
10677 public int getPaddingBottom() {
10678 return mPaddingBottom;
10679 }
10680
10681 /**
10682 * Returns the left padding of this view. If there are inset and enabled
10683 * scrollbars, this value may include the space required to display the
10684 * scrollbars as well.
10685 *
10686 * @return the left padding in pixels
10687 */
10688 public int getPaddingLeft() {
10689 return mPaddingLeft;
10690 }
10691
10692 /**
10693 * Returns the right padding of this view. If there are inset and enabled
10694 * scrollbars, this value may include the space required to display the
10695 * scrollbars as well.
10696 *
10697 * @return the right padding in pixels
10698 */
10699 public int getPaddingRight() {
10700 return mPaddingRight;
10701 }
10702
10703 /**
10704 * Changes the selection state of this view. A view can be selected or not.
10705 * Note that selection is not the same as focus. Views are typically
10706 * selected in the context of an AdapterView like ListView or GridView;
10707 * the selected view is the view that is highlighted.
10708 *
10709 * @param selected true if the view must be selected, false otherwise
10710 */
10711 public void setSelected(boolean selected) {
10712 if (((mPrivateFlags & SELECTED) != 0) != selected) {
10713 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070010714 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080010715 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010716 refreshDrawableState();
10717 dispatchSetSelected(selected);
10718 }
10719 }
10720
10721 /**
10722 * Dispatch setSelected to all of this View's children.
10723 *
10724 * @see #setSelected(boolean)
10725 *
10726 * @param selected The new selected state
10727 */
10728 protected void dispatchSetSelected(boolean selected) {
10729 }
10730
10731 /**
10732 * Indicates the selection state of this view.
10733 *
10734 * @return true if the view is selected, false otherwise
10735 */
10736 @ViewDebug.ExportedProperty
10737 public boolean isSelected() {
10738 return (mPrivateFlags & SELECTED) != 0;
10739 }
10740
10741 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010742 * Changes the activated state of this view. A view can be activated or not.
10743 * Note that activation is not the same as selection. Selection is
10744 * a transient property, representing the view (hierarchy) the user is
10745 * currently interacting with. Activation is a longer-term state that the
10746 * user can move views in and out of. For example, in a list view with
10747 * single or multiple selection enabled, the views in the current selection
10748 * set are activated. (Um, yeah, we are deeply sorry about the terminology
10749 * here.) The activated state is propagated down to children of the view it
10750 * is set on.
10751 *
10752 * @param activated true if the view must be activated, false otherwise
10753 */
10754 public void setActivated(boolean activated) {
10755 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
10756 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010757 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010758 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070010759 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010760 }
10761 }
10762
10763 /**
10764 * Dispatch setActivated to all of this View's children.
10765 *
10766 * @see #setActivated(boolean)
10767 *
10768 * @param activated The new activated state
10769 */
10770 protected void dispatchSetActivated(boolean activated) {
10771 }
10772
10773 /**
10774 * Indicates the activation state of this view.
10775 *
10776 * @return true if the view is activated, false otherwise
10777 */
10778 @ViewDebug.ExportedProperty
10779 public boolean isActivated() {
10780 return (mPrivateFlags & ACTIVATED) != 0;
10781 }
10782
10783 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010784 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
10785 * observer can be used to get notifications when global events, like
10786 * layout, happen.
10787 *
10788 * The returned ViewTreeObserver observer is not guaranteed to remain
10789 * valid for the lifetime of this View. If the caller of this method keeps
10790 * a long-lived reference to ViewTreeObserver, it should always check for
10791 * the return value of {@link ViewTreeObserver#isAlive()}.
10792 *
10793 * @return The ViewTreeObserver for this view's hierarchy.
10794 */
10795 public ViewTreeObserver getViewTreeObserver() {
10796 if (mAttachInfo != null) {
10797 return mAttachInfo.mTreeObserver;
10798 }
10799 if (mFloatingTreeObserver == null) {
10800 mFloatingTreeObserver = new ViewTreeObserver();
10801 }
10802 return mFloatingTreeObserver;
10803 }
10804
10805 /**
10806 * <p>Finds the topmost view in the current view hierarchy.</p>
10807 *
10808 * @return the topmost view containing this view
10809 */
10810 public View getRootView() {
10811 if (mAttachInfo != null) {
10812 final View v = mAttachInfo.mRootView;
10813 if (v != null) {
10814 return v;
10815 }
10816 }
Romain Guy8506ab42009-06-11 17:35:47 -070010817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010818 View parent = this;
10819
10820 while (parent.mParent != null && parent.mParent instanceof View) {
10821 parent = (View) parent.mParent;
10822 }
10823
10824 return parent;
10825 }
10826
10827 /**
10828 * <p>Computes the coordinates of this view on the screen. The argument
10829 * must be an array of two integers. After the method returns, the array
10830 * contains the x and y location in that order.</p>
10831 *
10832 * @param location an array of two integers in which to hold the coordinates
10833 */
10834 public void getLocationOnScreen(int[] location) {
10835 getLocationInWindow(location);
10836
10837 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070010838 if (info != null) {
10839 location[0] += info.mWindowLeft;
10840 location[1] += info.mWindowTop;
10841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010842 }
10843
10844 /**
10845 * <p>Computes the coordinates of this view in its window. The argument
10846 * must be an array of two integers. After the method returns, the array
10847 * contains the x and y location in that order.</p>
10848 *
10849 * @param location an array of two integers in which to hold the coordinates
10850 */
10851 public void getLocationInWindow(int[] location) {
10852 if (location == null || location.length < 2) {
10853 throw new IllegalArgumentException("location must be an array of "
10854 + "two integers");
10855 }
10856
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010857 location[0] = mLeft + (int) (mTranslationX + 0.5f);
10858 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010859
10860 ViewParent viewParent = mParent;
10861 while (viewParent instanceof View) {
10862 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010863 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
10864 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010865 viewParent = view.mParent;
10866 }
Romain Guy8506ab42009-06-11 17:35:47 -070010867
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010868 if (viewParent instanceof ViewAncestor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010869 // *cough*
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010870 final ViewAncestor vr = (ViewAncestor)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010871 location[1] -= vr.mCurScrollY;
10872 }
10873 }
10874
10875 /**
10876 * {@hide}
10877 * @param id the id of the view to be found
10878 * @return the view of the specified id, null if cannot be found
10879 */
10880 protected View findViewTraversal(int id) {
10881 if (id == mID) {
10882 return this;
10883 }
10884 return null;
10885 }
10886
10887 /**
10888 * {@hide}
10889 * @param tag the tag of the view to be found
10890 * @return the view of specified tag, null if cannot be found
10891 */
10892 protected View findViewWithTagTraversal(Object tag) {
10893 if (tag != null && tag.equals(mTag)) {
10894 return this;
10895 }
10896 return null;
10897 }
10898
10899 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010900 * {@hide}
10901 * @param predicate The predicate to evaluate.
10902 * @return The first view that matches the predicate or null.
10903 */
10904 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
10905 if (predicate.apply(this)) {
10906 return this;
10907 }
10908 return null;
10909 }
10910
10911 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010912 * Look for a child view with the given id. If this view has the given
10913 * id, return this view.
10914 *
10915 * @param id The id to search for.
10916 * @return The view that has the given id in the hierarchy or null
10917 */
10918 public final View findViewById(int id) {
10919 if (id < 0) {
10920 return null;
10921 }
10922 return findViewTraversal(id);
10923 }
10924
10925 /**
10926 * Look for a child view with the given tag. If this view has the given
10927 * tag, return this view.
10928 *
10929 * @param tag The tag to search for, using "tag.equals(getTag())".
10930 * @return The View that has the given tag in the hierarchy or null
10931 */
10932 public final View findViewWithTag(Object tag) {
10933 if (tag == null) {
10934 return null;
10935 }
10936 return findViewWithTagTraversal(tag);
10937 }
10938
10939 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010940 * {@hide}
10941 * Look for a child view that matches the specified predicate.
10942 * If this view matches the predicate, return this view.
10943 *
10944 * @param predicate The predicate to evaluate.
10945 * @return The first view that matches the predicate or null.
10946 */
10947 public final View findViewByPredicate(Predicate<View> predicate) {
10948 return findViewByPredicateTraversal(predicate);
10949 }
10950
10951 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010952 * Sets the identifier for this view. The identifier does not have to be
10953 * unique in this view's hierarchy. The identifier should be a positive
10954 * number.
10955 *
10956 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070010957 * @see #getId()
10958 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010959 *
10960 * @param id a number used to identify the view
10961 *
10962 * @attr ref android.R.styleable#View_id
10963 */
10964 public void setId(int id) {
10965 mID = id;
10966 }
10967
10968 /**
10969 * {@hide}
10970 *
10971 * @param isRoot true if the view belongs to the root namespace, false
10972 * otherwise
10973 */
10974 public void setIsRootNamespace(boolean isRoot) {
10975 if (isRoot) {
10976 mPrivateFlags |= IS_ROOT_NAMESPACE;
10977 } else {
10978 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
10979 }
10980 }
10981
10982 /**
10983 * {@hide}
10984 *
10985 * @return true if the view belongs to the root namespace, false otherwise
10986 */
10987 public boolean isRootNamespace() {
10988 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
10989 }
10990
10991 /**
10992 * Returns this view's identifier.
10993 *
10994 * @return a positive integer used to identify the view or {@link #NO_ID}
10995 * if the view has no ID
10996 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010997 * @see #setId(int)
10998 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010999 * @attr ref android.R.styleable#View_id
11000 */
11001 @ViewDebug.CapturedViewProperty
11002 public int getId() {
11003 return mID;
11004 }
11005
11006 /**
11007 * Returns this view's tag.
11008 *
11009 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070011010 *
11011 * @see #setTag(Object)
11012 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011013 */
11014 @ViewDebug.ExportedProperty
11015 public Object getTag() {
11016 return mTag;
11017 }
11018
11019 /**
11020 * Sets the tag associated with this view. A tag can be used to mark
11021 * a view in its hierarchy and does not have to be unique within the
11022 * hierarchy. Tags can also be used to store data within a view without
11023 * resorting to another data structure.
11024 *
11025 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070011026 *
11027 * @see #getTag()
11028 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011029 */
11030 public void setTag(final Object tag) {
11031 mTag = tag;
11032 }
11033
11034 /**
Romain Guyd90a3312009-05-06 14:54:28 -070011035 * Returns the tag associated with this view and the specified key.
11036 *
11037 * @param key The key identifying the tag
11038 *
11039 * @return the Object stored in this view as a tag
11040 *
11041 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070011042 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070011043 */
11044 public Object getTag(int key) {
11045 SparseArray<Object> tags = null;
11046 synchronized (sTagsLock) {
11047 if (sTags != null) {
11048 tags = sTags.get(this);
11049 }
11050 }
11051
11052 if (tags != null) return tags.get(key);
11053 return null;
11054 }
11055
11056 /**
11057 * Sets a tag associated with this view and a key. A tag can be used
11058 * to mark a view in its hierarchy and does not have to be unique within
11059 * the hierarchy. Tags can also be used to store data within a view
11060 * without resorting to another data structure.
11061 *
11062 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070011063 * application to ensure it is unique (see the <a
11064 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
11065 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070011066 * the Android framework or not associated with any package will cause
11067 * an {@link IllegalArgumentException} to be thrown.
11068 *
11069 * @param key The key identifying the tag
11070 * @param tag An Object to tag the view with
11071 *
11072 * @throws IllegalArgumentException If they specified key is not valid
11073 *
11074 * @see #setTag(Object)
11075 * @see #getTag(int)
11076 */
11077 public void setTag(int key, final Object tag) {
11078 // If the package id is 0x00 or 0x01, it's either an undefined package
11079 // or a framework id
11080 if ((key >>> 24) < 2) {
11081 throw new IllegalArgumentException("The key must be an application-specific "
11082 + "resource id.");
11083 }
11084
11085 setTagInternal(this, key, tag);
11086 }
11087
11088 /**
11089 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
11090 * framework id.
11091 *
11092 * @hide
11093 */
11094 public void setTagInternal(int key, Object tag) {
11095 if ((key >>> 24) != 0x1) {
11096 throw new IllegalArgumentException("The key must be a framework-specific "
11097 + "resource id.");
11098 }
11099
Romain Guy8506ab42009-06-11 17:35:47 -070011100 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070011101 }
11102
11103 private static void setTagInternal(View view, int key, Object tag) {
11104 SparseArray<Object> tags = null;
11105 synchronized (sTagsLock) {
11106 if (sTags == null) {
11107 sTags = new WeakHashMap<View, SparseArray<Object>>();
11108 } else {
11109 tags = sTags.get(view);
11110 }
11111 }
11112
11113 if (tags == null) {
11114 tags = new SparseArray<Object>(2);
11115 synchronized (sTagsLock) {
11116 sTags.put(view, tags);
11117 }
11118 }
11119
11120 tags.put(key, tag);
11121 }
11122
11123 /**
Romain Guy13922e02009-05-12 17:56:14 -070011124 * @param consistency The type of consistency. See ViewDebug for more information.
11125 *
11126 * @hide
11127 */
11128 protected boolean dispatchConsistencyCheck(int consistency) {
11129 return onConsistencyCheck(consistency);
11130 }
11131
11132 /**
11133 * Method that subclasses should implement to check their consistency. The type of
11134 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070011135 *
Romain Guy13922e02009-05-12 17:56:14 -070011136 * @param consistency The type of consistency. See ViewDebug for more information.
11137 *
11138 * @throws IllegalStateException if the view is in an inconsistent state.
11139 *
11140 * @hide
11141 */
11142 protected boolean onConsistencyCheck(int consistency) {
11143 boolean result = true;
11144
11145 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
11146 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
11147
11148 if (checkLayout) {
11149 if (getParent() == null) {
11150 result = false;
11151 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11152 "View " + this + " does not have a parent.");
11153 }
11154
11155 if (mAttachInfo == null) {
11156 result = false;
11157 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11158 "View " + this + " is not attached to a window.");
11159 }
11160 }
11161
11162 if (checkDrawing) {
11163 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
11164 // from their draw() method
11165
11166 if ((mPrivateFlags & DRAWN) != DRAWN &&
11167 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
11168 result = false;
11169 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11170 "View " + this + " was invalidated but its drawing cache is valid.");
11171 }
11172 }
11173
11174 return result;
11175 }
11176
11177 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011178 * Prints information about this view in the log output, with the tag
11179 * {@link #VIEW_LOG_TAG}.
11180 *
11181 * @hide
11182 */
11183 public void debug() {
11184 debug(0);
11185 }
11186
11187 /**
11188 * Prints information about this view in the log output, with the tag
11189 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
11190 * indentation defined by the <code>depth</code>.
11191 *
11192 * @param depth the indentation level
11193 *
11194 * @hide
11195 */
11196 protected void debug(int depth) {
11197 String output = debugIndent(depth - 1);
11198
11199 output += "+ " + this;
11200 int id = getId();
11201 if (id != -1) {
11202 output += " (id=" + id + ")";
11203 }
11204 Object tag = getTag();
11205 if (tag != null) {
11206 output += " (tag=" + tag + ")";
11207 }
11208 Log.d(VIEW_LOG_TAG, output);
11209
11210 if ((mPrivateFlags & FOCUSED) != 0) {
11211 output = debugIndent(depth) + " FOCUSED";
11212 Log.d(VIEW_LOG_TAG, output);
11213 }
11214
11215 output = debugIndent(depth);
11216 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
11217 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
11218 + "} ";
11219 Log.d(VIEW_LOG_TAG, output);
11220
11221 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
11222 || mPaddingBottom != 0) {
11223 output = debugIndent(depth);
11224 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
11225 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
11226 Log.d(VIEW_LOG_TAG, output);
11227 }
11228
11229 output = debugIndent(depth);
11230 output += "mMeasureWidth=" + mMeasuredWidth +
11231 " mMeasureHeight=" + mMeasuredHeight;
11232 Log.d(VIEW_LOG_TAG, output);
11233
11234 output = debugIndent(depth);
11235 if (mLayoutParams == null) {
11236 output += "BAD! no layout params";
11237 } else {
11238 output = mLayoutParams.debug(output);
11239 }
11240 Log.d(VIEW_LOG_TAG, output);
11241
11242 output = debugIndent(depth);
11243 output += "flags={";
11244 output += View.printFlags(mViewFlags);
11245 output += "}";
11246 Log.d(VIEW_LOG_TAG, output);
11247
11248 output = debugIndent(depth);
11249 output += "privateFlags={";
11250 output += View.printPrivateFlags(mPrivateFlags);
11251 output += "}";
11252 Log.d(VIEW_LOG_TAG, output);
11253 }
11254
11255 /**
11256 * Creates an string of whitespaces used for indentation.
11257 *
11258 * @param depth the indentation level
11259 * @return a String containing (depth * 2 + 3) * 2 white spaces
11260 *
11261 * @hide
11262 */
11263 protected static String debugIndent(int depth) {
11264 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
11265 for (int i = 0; i < (depth * 2) + 3; i++) {
11266 spaces.append(' ').append(' ');
11267 }
11268 return spaces.toString();
11269 }
11270
11271 /**
11272 * <p>Return the offset of the widget's text baseline from the widget's top
11273 * boundary. If this widget does not support baseline alignment, this
11274 * method returns -1. </p>
11275 *
11276 * @return the offset of the baseline within the widget's bounds or -1
11277 * if baseline alignment is not supported
11278 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011279 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011280 public int getBaseline() {
11281 return -1;
11282 }
11283
11284 /**
11285 * Call this when something has changed which has invalidated the
11286 * layout of this view. This will schedule a layout pass of the view
11287 * tree.
11288 */
11289 public void requestLayout() {
11290 if (ViewDebug.TRACE_HIERARCHY) {
11291 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
11292 }
11293
11294 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011295 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011296
11297 if (mParent != null && !mParent.isLayoutRequested()) {
11298 mParent.requestLayout();
11299 }
11300 }
11301
11302 /**
11303 * Forces this view to be laid out during the next layout pass.
11304 * This method does not call requestLayout() or forceLayout()
11305 * on the parent.
11306 */
11307 public void forceLayout() {
11308 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011309 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011310 }
11311
11312 /**
11313 * <p>
11314 * This is called to find out how big a view should be. The parent
11315 * supplies constraint information in the width and height parameters.
11316 * </p>
11317 *
11318 * <p>
11319 * The actual mesurement work of a view is performed in
11320 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
11321 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
11322 * </p>
11323 *
11324 *
11325 * @param widthMeasureSpec Horizontal space requirements as imposed by the
11326 * parent
11327 * @param heightMeasureSpec Vertical space requirements as imposed by the
11328 * parent
11329 *
11330 * @see #onMeasure(int, int)
11331 */
11332 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
11333 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
11334 widthMeasureSpec != mOldWidthMeasureSpec ||
11335 heightMeasureSpec != mOldHeightMeasureSpec) {
11336
11337 // first clears the measured dimension flag
11338 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
11339
11340 if (ViewDebug.TRACE_HIERARCHY) {
11341 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
11342 }
11343
11344 // measure ourselves, this should set the measured dimension flag back
11345 onMeasure(widthMeasureSpec, heightMeasureSpec);
11346
11347 // flag not set, setMeasuredDimension() was not invoked, we raise
11348 // an exception to warn the developer
11349 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
11350 throw new IllegalStateException("onMeasure() did not set the"
11351 + " measured dimension by calling"
11352 + " setMeasuredDimension()");
11353 }
11354
11355 mPrivateFlags |= LAYOUT_REQUIRED;
11356 }
11357
11358 mOldWidthMeasureSpec = widthMeasureSpec;
11359 mOldHeightMeasureSpec = heightMeasureSpec;
11360 }
11361
11362 /**
11363 * <p>
11364 * Measure the view and its content to determine the measured width and the
11365 * measured height. This method is invoked by {@link #measure(int, int)} and
11366 * should be overriden by subclasses to provide accurate and efficient
11367 * measurement of their contents.
11368 * </p>
11369 *
11370 * <p>
11371 * <strong>CONTRACT:</strong> When overriding this method, you
11372 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
11373 * measured width and height of this view. Failure to do so will trigger an
11374 * <code>IllegalStateException</code>, thrown by
11375 * {@link #measure(int, int)}. Calling the superclass'
11376 * {@link #onMeasure(int, int)} is a valid use.
11377 * </p>
11378 *
11379 * <p>
11380 * The base class implementation of measure defaults to the background size,
11381 * unless a larger size is allowed by the MeasureSpec. Subclasses should
11382 * override {@link #onMeasure(int, int)} to provide better measurements of
11383 * their content.
11384 * </p>
11385 *
11386 * <p>
11387 * If this method is overridden, it is the subclass's responsibility to make
11388 * sure the measured height and width are at least the view's minimum height
11389 * and width ({@link #getSuggestedMinimumHeight()} and
11390 * {@link #getSuggestedMinimumWidth()}).
11391 * </p>
11392 *
11393 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
11394 * The requirements are encoded with
11395 * {@link android.view.View.MeasureSpec}.
11396 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
11397 * The requirements are encoded with
11398 * {@link android.view.View.MeasureSpec}.
11399 *
11400 * @see #getMeasuredWidth()
11401 * @see #getMeasuredHeight()
11402 * @see #setMeasuredDimension(int, int)
11403 * @see #getSuggestedMinimumHeight()
11404 * @see #getSuggestedMinimumWidth()
11405 * @see android.view.View.MeasureSpec#getMode(int)
11406 * @see android.view.View.MeasureSpec#getSize(int)
11407 */
11408 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
11409 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
11410 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
11411 }
11412
11413 /**
11414 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
11415 * measured width and measured height. Failing to do so will trigger an
11416 * exception at measurement time.</p>
11417 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080011418 * @param measuredWidth The measured width of this view. May be a complex
11419 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11420 * {@link #MEASURED_STATE_TOO_SMALL}.
11421 * @param measuredHeight The measured height of this view. May be a complex
11422 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11423 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011424 */
11425 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
11426 mMeasuredWidth = measuredWidth;
11427 mMeasuredHeight = measuredHeight;
11428
11429 mPrivateFlags |= MEASURED_DIMENSION_SET;
11430 }
11431
11432 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080011433 * Merge two states as returned by {@link #getMeasuredState()}.
11434 * @param curState The current state as returned from a view or the result
11435 * of combining multiple views.
11436 * @param newState The new view state to combine.
11437 * @return Returns a new integer reflecting the combination of the two
11438 * states.
11439 */
11440 public static int combineMeasuredStates(int curState, int newState) {
11441 return curState | newState;
11442 }
11443
11444 /**
11445 * Version of {@link #resolveSizeAndState(int, int, int)}
11446 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
11447 */
11448 public static int resolveSize(int size, int measureSpec) {
11449 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
11450 }
11451
11452 /**
11453 * Utility to reconcile a desired size and state, with constraints imposed
11454 * by a MeasureSpec. Will take the desired size, unless a different size
11455 * is imposed by the constraints. The returned value is a compound integer,
11456 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
11457 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
11458 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011459 *
11460 * @param size How big the view wants to be
11461 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080011462 * @return Size information bit mask as defined by
11463 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011464 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080011465 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011466 int result = size;
11467 int specMode = MeasureSpec.getMode(measureSpec);
11468 int specSize = MeasureSpec.getSize(measureSpec);
11469 switch (specMode) {
11470 case MeasureSpec.UNSPECIFIED:
11471 result = size;
11472 break;
11473 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080011474 if (specSize < size) {
11475 result = specSize | MEASURED_STATE_TOO_SMALL;
11476 } else {
11477 result = size;
11478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011479 break;
11480 case MeasureSpec.EXACTLY:
11481 result = specSize;
11482 break;
11483 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080011484 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011485 }
11486
11487 /**
11488 * Utility to return a default size. Uses the supplied size if the
11489 * MeasureSpec imposed no contraints. Will get larger if allowed
11490 * by the MeasureSpec.
11491 *
11492 * @param size Default size for this view
11493 * @param measureSpec Constraints imposed by the parent
11494 * @return The size this view should be.
11495 */
11496 public static int getDefaultSize(int size, int measureSpec) {
11497 int result = size;
11498 int specMode = MeasureSpec.getMode(measureSpec);
11499 int specSize = MeasureSpec.getSize(measureSpec);
11500
11501 switch (specMode) {
11502 case MeasureSpec.UNSPECIFIED:
11503 result = size;
11504 break;
11505 case MeasureSpec.AT_MOST:
11506 case MeasureSpec.EXACTLY:
11507 result = specSize;
11508 break;
11509 }
11510 return result;
11511 }
11512
11513 /**
11514 * Returns the suggested minimum height that the view should use. This
11515 * returns the maximum of the view's minimum height
11516 * and the background's minimum height
11517 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
11518 * <p>
11519 * When being used in {@link #onMeasure(int, int)}, the caller should still
11520 * ensure the returned height is within the requirements of the parent.
11521 *
11522 * @return The suggested minimum height of the view.
11523 */
11524 protected int getSuggestedMinimumHeight() {
11525 int suggestedMinHeight = mMinHeight;
11526
11527 if (mBGDrawable != null) {
11528 final int bgMinHeight = mBGDrawable.getMinimumHeight();
11529 if (suggestedMinHeight < bgMinHeight) {
11530 suggestedMinHeight = bgMinHeight;
11531 }
11532 }
11533
11534 return suggestedMinHeight;
11535 }
11536
11537 /**
11538 * Returns the suggested minimum width that the view should use. This
11539 * returns the maximum of the view's minimum width)
11540 * and the background's minimum width
11541 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
11542 * <p>
11543 * When being used in {@link #onMeasure(int, int)}, the caller should still
11544 * ensure the returned width is within the requirements of the parent.
11545 *
11546 * @return The suggested minimum width of the view.
11547 */
11548 protected int getSuggestedMinimumWidth() {
11549 int suggestedMinWidth = mMinWidth;
11550
11551 if (mBGDrawable != null) {
11552 final int bgMinWidth = mBGDrawable.getMinimumWidth();
11553 if (suggestedMinWidth < bgMinWidth) {
11554 suggestedMinWidth = bgMinWidth;
11555 }
11556 }
11557
11558 return suggestedMinWidth;
11559 }
11560
11561 /**
11562 * Sets the minimum height of the view. It is not guaranteed the view will
11563 * be able to achieve this minimum height (for example, if its parent layout
11564 * constrains it with less available height).
11565 *
11566 * @param minHeight The minimum height the view will try to be.
11567 */
11568 public void setMinimumHeight(int minHeight) {
11569 mMinHeight = minHeight;
11570 }
11571
11572 /**
11573 * Sets the minimum width of the view. It is not guaranteed the view will
11574 * be able to achieve this minimum width (for example, if its parent layout
11575 * constrains it with less available width).
11576 *
11577 * @param minWidth The minimum width the view will try to be.
11578 */
11579 public void setMinimumWidth(int minWidth) {
11580 mMinWidth = minWidth;
11581 }
11582
11583 /**
11584 * Get the animation currently associated with this view.
11585 *
11586 * @return The animation that is currently playing or
11587 * scheduled to play for this view.
11588 */
11589 public Animation getAnimation() {
11590 return mCurrentAnimation;
11591 }
11592
11593 /**
11594 * Start the specified animation now.
11595 *
11596 * @param animation the animation to start now
11597 */
11598 public void startAnimation(Animation animation) {
11599 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
11600 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011601 invalidateParentCaches();
11602 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011603 }
11604
11605 /**
11606 * Cancels any animations for this view.
11607 */
11608 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080011609 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080011610 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080011611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011612 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011613 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011614 }
11615
11616 /**
11617 * Sets the next animation to play for this view.
11618 * If you want the animation to play immediately, use
11619 * startAnimation. This method provides allows fine-grained
11620 * control over the start time and invalidation, but you
11621 * must make sure that 1) the animation has a start time set, and
11622 * 2) the view will be invalidated when the animation is supposed to
11623 * start.
11624 *
11625 * @param animation The next animation, or null.
11626 */
11627 public void setAnimation(Animation animation) {
11628 mCurrentAnimation = animation;
11629 if (animation != null) {
11630 animation.reset();
11631 }
11632 }
11633
11634 /**
11635 * Invoked by a parent ViewGroup to notify the start of the animation
11636 * currently associated with this view. If you override this method,
11637 * always call super.onAnimationStart();
11638 *
11639 * @see #setAnimation(android.view.animation.Animation)
11640 * @see #getAnimation()
11641 */
11642 protected void onAnimationStart() {
11643 mPrivateFlags |= ANIMATION_STARTED;
11644 }
11645
11646 /**
11647 * Invoked by a parent ViewGroup to notify the end of the animation
11648 * currently associated with this view. If you override this method,
11649 * always call super.onAnimationEnd();
11650 *
11651 * @see #setAnimation(android.view.animation.Animation)
11652 * @see #getAnimation()
11653 */
11654 protected void onAnimationEnd() {
11655 mPrivateFlags &= ~ANIMATION_STARTED;
11656 }
11657
11658 /**
11659 * Invoked if there is a Transform that involves alpha. Subclass that can
11660 * draw themselves with the specified alpha should return true, and then
11661 * respect that alpha when their onDraw() is called. If this returns false
11662 * then the view may be redirected to draw into an offscreen buffer to
11663 * fulfill the request, which will look fine, but may be slower than if the
11664 * subclass handles it internally. The default implementation returns false.
11665 *
11666 * @param alpha The alpha (0..255) to apply to the view's drawing
11667 * @return true if the view can draw with the specified alpha.
11668 */
11669 protected boolean onSetAlpha(int alpha) {
11670 return false;
11671 }
11672
11673 /**
11674 * This is used by the RootView to perform an optimization when
11675 * the view hierarchy contains one or several SurfaceView.
11676 * SurfaceView is always considered transparent, but its children are not,
11677 * therefore all View objects remove themselves from the global transparent
11678 * region (passed as a parameter to this function).
11679 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011680 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011681 *
11682 * @return Returns true if the effective visibility of the view at this
11683 * point is opaque, regardless of the transparent region; returns false
11684 * if it is possible for underlying windows to be seen behind the view.
11685 *
11686 * {@hide}
11687 */
11688 public boolean gatherTransparentRegion(Region region) {
11689 final AttachInfo attachInfo = mAttachInfo;
11690 if (region != null && attachInfo != null) {
11691 final int pflags = mPrivateFlags;
11692 if ((pflags & SKIP_DRAW) == 0) {
11693 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
11694 // remove it from the transparent region.
11695 final int[] location = attachInfo.mTransparentLocation;
11696 getLocationInWindow(location);
11697 region.op(location[0], location[1], location[0] + mRight - mLeft,
11698 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
11699 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
11700 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
11701 // exists, so we remove the background drawable's non-transparent
11702 // parts from this transparent region.
11703 applyDrawableToTransparentRegion(mBGDrawable, region);
11704 }
11705 }
11706 return true;
11707 }
11708
11709 /**
11710 * Play a sound effect for this view.
11711 *
11712 * <p>The framework will play sound effects for some built in actions, such as
11713 * clicking, but you may wish to play these effects in your widget,
11714 * for instance, for internal navigation.
11715 *
11716 * <p>The sound effect will only be played if sound effects are enabled by the user, and
11717 * {@link #isSoundEffectsEnabled()} is true.
11718 *
11719 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
11720 */
11721 public void playSoundEffect(int soundConstant) {
11722 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
11723 return;
11724 }
11725 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
11726 }
11727
11728 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011729 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011730 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011731 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011732 *
11733 * <p>The framework will provide haptic feedback for some built in actions,
11734 * such as long presses, but you may wish to provide feedback for your
11735 * own widget.
11736 *
11737 * <p>The feedback will only be performed if
11738 * {@link #isHapticFeedbackEnabled()} is true.
11739 *
11740 * @param feedbackConstant One of the constants defined in
11741 * {@link HapticFeedbackConstants}
11742 */
11743 public boolean performHapticFeedback(int feedbackConstant) {
11744 return performHapticFeedback(feedbackConstant, 0);
11745 }
11746
11747 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011748 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011749 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011750 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011751 *
11752 * @param feedbackConstant One of the constants defined in
11753 * {@link HapticFeedbackConstants}
11754 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
11755 */
11756 public boolean performHapticFeedback(int feedbackConstant, int flags) {
11757 if (mAttachInfo == null) {
11758 return false;
11759 }
Romain Guyf607bdc2010-09-10 19:20:06 -070011760 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070011761 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011762 && !isHapticFeedbackEnabled()) {
11763 return false;
11764 }
Romain Guy812ccbe2010-06-01 14:07:24 -070011765 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
11766 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011767 }
11768
11769 /**
Joe Onorato664644d2011-01-23 17:53:23 -080011770 * Request that the visibility of the status bar be changed.
Scott Mainec6331b2011-05-24 16:55:56 -070011771 * @param visibility Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080011772 */
11773 public void setSystemUiVisibility(int visibility) {
11774 if (visibility != mSystemUiVisibility) {
11775 mSystemUiVisibility = visibility;
11776 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11777 mParent.recomputeViewAttributes(this);
11778 }
11779 }
11780 }
11781
11782 /**
11783 * Returns the status bar visibility that this view has requested.
Scott Mainec6331b2011-05-24 16:55:56 -070011784 * @return Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080011785 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080011786 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080011787 return mSystemUiVisibility;
11788 }
11789
Scott Mainec6331b2011-05-24 16:55:56 -070011790 /**
11791 * Set a listener to receive callbacks when the visibility of the system bar changes.
11792 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
11793 */
Joe Onorato664644d2011-01-23 17:53:23 -080011794 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
11795 mOnSystemUiVisibilityChangeListener = l;
11796 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11797 mParent.recomputeViewAttributes(this);
11798 }
11799 }
11800
11801 /**
11802 */
11803 public void dispatchSystemUiVisibilityChanged(int visibility) {
11804 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080011805 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080011806 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080011807 }
11808 }
11809
11810 /**
Joe Malin32736f02011-01-19 16:14:20 -080011811 * Creates an image that the system displays during the drag and drop
11812 * operation. This is called a &quot;drag shadow&quot;. The default implementation
11813 * for a DragShadowBuilder based on a View returns an image that has exactly the same
11814 * appearance as the given View. The default also positions the center of the drag shadow
11815 * directly under the touch point. If no View is provided (the constructor with no parameters
11816 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
11817 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
11818 * default is an invisible drag shadow.
11819 * <p>
11820 * You are not required to use the View you provide to the constructor as the basis of the
11821 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
11822 * anything you want as the drag shadow.
11823 * </p>
11824 * <p>
11825 * You pass a DragShadowBuilder object to the system when you start the drag. The system
11826 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
11827 * size and position of the drag shadow. It uses this data to construct a
11828 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
11829 * so that your application can draw the shadow image in the Canvas.
11830 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011831 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011832 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070011833 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070011834
11835 /**
Joe Malin32736f02011-01-19 16:14:20 -080011836 * Constructs a shadow image builder based on a View. By default, the resulting drag
11837 * shadow will have the same appearance and dimensions as the View, with the touch point
11838 * over the center of the View.
11839 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070011840 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011841 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070011842 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070011843 }
11844
Christopher Tate17ed60c2011-01-18 12:50:26 -080011845 /**
11846 * Construct a shadow builder object with no associated View. This
11847 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
11848 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
11849 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080011850 * reference to any View object. If they are not overridden, then the result is an
11851 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080011852 */
11853 public DragShadowBuilder() {
11854 mView = new WeakReference<View>(null);
11855 }
11856
11857 /**
11858 * Returns the View object that had been passed to the
11859 * {@link #View.DragShadowBuilder(View)}
11860 * constructor. If that View parameter was {@code null} or if the
11861 * {@link #View.DragShadowBuilder()}
11862 * constructor was used to instantiate the builder object, this method will return
11863 * null.
11864 *
11865 * @return The View object associate with this builder object.
11866 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070011867 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070011868 final public View getView() {
11869 return mView.get();
11870 }
11871
Christopher Tate2c095f32010-10-04 14:13:40 -070011872 /**
Joe Malin32736f02011-01-19 16:14:20 -080011873 * Provides the metrics for the shadow image. These include the dimensions of
11874 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070011875 * be centered under the touch location while dragging.
11876 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011877 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080011878 * same as the dimensions of the View itself and centers the shadow under
11879 * the touch point.
11880 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011881 *
Joe Malin32736f02011-01-19 16:14:20 -080011882 * @param shadowSize A {@link android.graphics.Point} containing the width and height
11883 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
11884 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
11885 * image.
11886 *
11887 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
11888 * shadow image that should be underneath the touch point during the drag and drop
11889 * operation. Your application must set {@link android.graphics.Point#x} to the
11890 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070011891 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011892 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070011893 final View view = mView.get();
11894 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011895 shadowSize.set(view.getWidth(), view.getHeight());
11896 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070011897 } else {
11898 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
11899 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011900 }
11901
11902 /**
Joe Malin32736f02011-01-19 16:14:20 -080011903 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
11904 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011905 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070011906 *
Joe Malin32736f02011-01-19 16:14:20 -080011907 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070011908 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011909 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070011910 final View view = mView.get();
11911 if (view != null) {
11912 view.draw(canvas);
11913 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011914 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070011915 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011916 }
11917 }
11918
11919 /**
Joe Malin32736f02011-01-19 16:14:20 -080011920 * Starts a drag and drop operation. When your application calls this method, it passes a
11921 * {@link android.view.View.DragShadowBuilder} object to the system. The
11922 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
11923 * to get metrics for the drag shadow, and then calls the object's
11924 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
11925 * <p>
11926 * Once the system has the drag shadow, it begins the drag and drop operation by sending
11927 * drag events to all the View objects in your application that are currently visible. It does
11928 * this either by calling the View object's drag listener (an implementation of
11929 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
11930 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
11931 * Both are passed a {@link android.view.DragEvent} object that has a
11932 * {@link android.view.DragEvent#getAction()} value of
11933 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
11934 * </p>
11935 * <p>
11936 * Your application can invoke startDrag() on any attached View object. The View object does not
11937 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
11938 * be related to the View the user selected for dragging.
11939 * </p>
11940 * @param data A {@link android.content.ClipData} object pointing to the data to be
11941 * transferred by the drag and drop operation.
11942 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
11943 * drag shadow.
11944 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
11945 * drop operation. This Object is put into every DragEvent object sent by the system during the
11946 * current drag.
11947 * <p>
11948 * myLocalState is a lightweight mechanism for the sending information from the dragged View
11949 * to the target Views. For example, it can contain flags that differentiate between a
11950 * a copy operation and a move operation.
11951 * </p>
11952 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
11953 * so the parameter should be set to 0.
11954 * @return {@code true} if the method completes successfully, or
11955 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
11956 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070011957 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011958 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011959 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070011960 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011961 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070011962 }
11963 boolean okay = false;
11964
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011965 Point shadowSize = new Point();
11966 Point shadowTouchPoint = new Point();
11967 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070011968
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011969 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
11970 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
11971 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070011972 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011973
Chris Tatea32dcf72010-10-14 12:13:50 -070011974 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011975 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
11976 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070011977 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011978 Surface surface = new Surface();
11979 try {
11980 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011981 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070011982 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070011983 + " surface=" + surface);
11984 if (token != null) {
11985 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070011986 try {
Chris Tate6b391282010-10-14 15:48:59 -070011987 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011988 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070011989 } finally {
11990 surface.unlockCanvasAndPost(canvas);
11991 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011992
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011993 final ViewAncestor root = getViewAncestor();
Christopher Tate407b4e92010-11-30 17:14:08 -080011994
11995 // Cache the local state object for delivery with DragEvents
11996 root.setLocalDragState(myLocalState);
11997
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011998 // repurpose 'shadowSize' for the last touch point
11999 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070012000
Christopher Tatea53146c2010-09-07 11:57:52 -070012001 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012002 shadowSize.x, shadowSize.y,
12003 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070012004 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070012005 }
12006 } catch (Exception e) {
12007 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
12008 surface.destroy();
12009 }
12010
12011 return okay;
12012 }
12013
Christopher Tatea53146c2010-09-07 11:57:52 -070012014 /**
Joe Malin32736f02011-01-19 16:14:20 -080012015 * Handles drag events sent by the system following a call to
12016 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
12017 *<p>
12018 * When the system calls this method, it passes a
12019 * {@link android.view.DragEvent} object. A call to
12020 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
12021 * in DragEvent. The method uses these to determine what is happening in the drag and drop
12022 * operation.
12023 * @param event The {@link android.view.DragEvent} sent by the system.
12024 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
12025 * in DragEvent, indicating the type of drag event represented by this object.
12026 * @return {@code true} if the method was successful, otherwise {@code false}.
12027 * <p>
12028 * The method should return {@code true} in response to an action type of
12029 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
12030 * operation.
12031 * </p>
12032 * <p>
12033 * The method should also return {@code true} in response to an action type of
12034 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
12035 * {@code false} if it didn't.
12036 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012037 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070012038 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070012039 return false;
12040 }
12041
12042 /**
Joe Malin32736f02011-01-19 16:14:20 -080012043 * Detects if this View is enabled and has a drag event listener.
12044 * If both are true, then it calls the drag event listener with the
12045 * {@link android.view.DragEvent} it received. If the drag event listener returns
12046 * {@code true}, then dispatchDragEvent() returns {@code true}.
12047 * <p>
12048 * For all other cases, the method calls the
12049 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
12050 * method and returns its result.
12051 * </p>
12052 * <p>
12053 * This ensures that a drag event is always consumed, even if the View does not have a drag
12054 * event listener. However, if the View has a listener and the listener returns true, then
12055 * onDragEvent() is not called.
12056 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012057 */
12058 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080012059 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070012060 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
12061 && mOnDragListener.onDrag(this, event)) {
12062 return true;
12063 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012064 return onDragEvent(event);
12065 }
12066
Christopher Tate3d4bf172011-03-28 16:16:46 -070012067 boolean canAcceptDrag() {
12068 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
12069 }
12070
Christopher Tatea53146c2010-09-07 11:57:52 -070012071 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070012072 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
12073 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070012074 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070012075 */
12076 public void onCloseSystemDialogs(String reason) {
12077 }
Joe Malin32736f02011-01-19 16:14:20 -080012078
Dianne Hackbornffa42482009-09-23 22:20:11 -070012079 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012080 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070012081 * update a Region being computed for
12082 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012083 * that any non-transparent parts of the Drawable are removed from the
12084 * given transparent region.
12085 *
12086 * @param dr The Drawable whose transparency is to be applied to the region.
12087 * @param region A Region holding the current transparency information,
12088 * where any parts of the region that are set are considered to be
12089 * transparent. On return, this region will be modified to have the
12090 * transparency information reduced by the corresponding parts of the
12091 * Drawable that are not transparent.
12092 * {@hide}
12093 */
12094 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
12095 if (DBG) {
12096 Log.i("View", "Getting transparent region for: " + this);
12097 }
12098 final Region r = dr.getTransparentRegion();
12099 final Rect db = dr.getBounds();
12100 final AttachInfo attachInfo = mAttachInfo;
12101 if (r != null && attachInfo != null) {
12102 final int w = getRight()-getLeft();
12103 final int h = getBottom()-getTop();
12104 if (db.left > 0) {
12105 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
12106 r.op(0, 0, db.left, h, Region.Op.UNION);
12107 }
12108 if (db.right < w) {
12109 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
12110 r.op(db.right, 0, w, h, Region.Op.UNION);
12111 }
12112 if (db.top > 0) {
12113 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
12114 r.op(0, 0, w, db.top, Region.Op.UNION);
12115 }
12116 if (db.bottom < h) {
12117 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
12118 r.op(0, db.bottom, w, h, Region.Op.UNION);
12119 }
12120 final int[] location = attachInfo.mTransparentLocation;
12121 getLocationInWindow(location);
12122 r.translate(location[0], location[1]);
12123 region.op(r, Region.Op.INTERSECT);
12124 } else {
12125 region.op(db, Region.Op.DIFFERENCE);
12126 }
12127 }
12128
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012129 private void checkForLongClick(int delayOffset) {
12130 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
12131 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012132
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012133 if (mPendingCheckForLongPress == null) {
12134 mPendingCheckForLongPress = new CheckForLongPress();
12135 }
12136 mPendingCheckForLongPress.rememberWindowAttachCount();
12137 postDelayed(mPendingCheckForLongPress,
12138 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012140 }
12141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012142 /**
12143 * Inflate a view from an XML resource. This convenience method wraps the {@link
12144 * LayoutInflater} class, which provides a full range of options for view inflation.
12145 *
12146 * @param context The Context object for your activity or application.
12147 * @param resource The resource ID to inflate
12148 * @param root A view group that will be the parent. Used to properly inflate the
12149 * layout_* parameters.
12150 * @see LayoutInflater
12151 */
12152 public static View inflate(Context context, int resource, ViewGroup root) {
12153 LayoutInflater factory = LayoutInflater.from(context);
12154 return factory.inflate(resource, root);
12155 }
Romain Guy33e72ae2010-07-17 12:40:29 -070012156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012157 /**
Adam Powell637d3372010-08-25 14:37:03 -070012158 * Scroll the view with standard behavior for scrolling beyond the normal
12159 * content boundaries. Views that call this method should override
12160 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
12161 * results of an over-scroll operation.
12162 *
12163 * Views can use this method to handle any touch or fling-based scrolling.
12164 *
12165 * @param deltaX Change in X in pixels
12166 * @param deltaY Change in Y in pixels
12167 * @param scrollX Current X scroll value in pixels before applying deltaX
12168 * @param scrollY Current Y scroll value in pixels before applying deltaY
12169 * @param scrollRangeX Maximum content scroll range along the X axis
12170 * @param scrollRangeY Maximum content scroll range along the Y axis
12171 * @param maxOverScrollX Number of pixels to overscroll by in either direction
12172 * along the X axis.
12173 * @param maxOverScrollY Number of pixels to overscroll by in either direction
12174 * along the Y axis.
12175 * @param isTouchEvent true if this scroll operation is the result of a touch event.
12176 * @return true if scrolling was clamped to an over-scroll boundary along either
12177 * axis, false otherwise.
12178 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012179 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070012180 protected boolean overScrollBy(int deltaX, int deltaY,
12181 int scrollX, int scrollY,
12182 int scrollRangeX, int scrollRangeY,
12183 int maxOverScrollX, int maxOverScrollY,
12184 boolean isTouchEvent) {
12185 final int overScrollMode = mOverScrollMode;
12186 final boolean canScrollHorizontal =
12187 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
12188 final boolean canScrollVertical =
12189 computeVerticalScrollRange() > computeVerticalScrollExtent();
12190 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
12191 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
12192 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
12193 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
12194
12195 int newScrollX = scrollX + deltaX;
12196 if (!overScrollHorizontal) {
12197 maxOverScrollX = 0;
12198 }
12199
12200 int newScrollY = scrollY + deltaY;
12201 if (!overScrollVertical) {
12202 maxOverScrollY = 0;
12203 }
12204
12205 // Clamp values if at the limits and record
12206 final int left = -maxOverScrollX;
12207 final int right = maxOverScrollX + scrollRangeX;
12208 final int top = -maxOverScrollY;
12209 final int bottom = maxOverScrollY + scrollRangeY;
12210
12211 boolean clampedX = false;
12212 if (newScrollX > right) {
12213 newScrollX = right;
12214 clampedX = true;
12215 } else if (newScrollX < left) {
12216 newScrollX = left;
12217 clampedX = true;
12218 }
12219
12220 boolean clampedY = false;
12221 if (newScrollY > bottom) {
12222 newScrollY = bottom;
12223 clampedY = true;
12224 } else if (newScrollY < top) {
12225 newScrollY = top;
12226 clampedY = true;
12227 }
12228
12229 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
12230
12231 return clampedX || clampedY;
12232 }
12233
12234 /**
12235 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
12236 * respond to the results of an over-scroll operation.
12237 *
12238 * @param scrollX New X scroll value in pixels
12239 * @param scrollY New Y scroll value in pixels
12240 * @param clampedX True if scrollX was clamped to an over-scroll boundary
12241 * @param clampedY True if scrollY was clamped to an over-scroll boundary
12242 */
12243 protected void onOverScrolled(int scrollX, int scrollY,
12244 boolean clampedX, boolean clampedY) {
12245 // Intentionally empty.
12246 }
12247
12248 /**
12249 * Returns the over-scroll mode for this view. The result will be
12250 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12251 * (allow over-scrolling only if the view content is larger than the container),
12252 * or {@link #OVER_SCROLL_NEVER}.
12253 *
12254 * @return This view's over-scroll mode.
12255 */
12256 public int getOverScrollMode() {
12257 return mOverScrollMode;
12258 }
12259
12260 /**
12261 * Set the over-scroll mode for this view. Valid over-scroll modes are
12262 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12263 * (allow over-scrolling only if the view content is larger than the container),
12264 * or {@link #OVER_SCROLL_NEVER}.
12265 *
12266 * Setting the over-scroll mode of a view will have an effect only if the
12267 * view is capable of scrolling.
12268 *
12269 * @param overScrollMode The new over-scroll mode for this view.
12270 */
12271 public void setOverScrollMode(int overScrollMode) {
12272 if (overScrollMode != OVER_SCROLL_ALWAYS &&
12273 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
12274 overScrollMode != OVER_SCROLL_NEVER) {
12275 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
12276 }
12277 mOverScrollMode = overScrollMode;
12278 }
12279
12280 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012281 * Gets a scale factor that determines the distance the view should scroll
12282 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
12283 * @return The vertical scroll scale factor.
12284 * @hide
12285 */
12286 protected float getVerticalScrollFactor() {
12287 if (mVerticalScrollFactor == 0) {
12288 TypedValue outValue = new TypedValue();
12289 if (!mContext.getTheme().resolveAttribute(
12290 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
12291 throw new IllegalStateException(
12292 "Expected theme to define listPreferredItemHeight.");
12293 }
12294 mVerticalScrollFactor = outValue.getDimension(
12295 mContext.getResources().getDisplayMetrics());
12296 }
12297 return mVerticalScrollFactor;
12298 }
12299
12300 /**
12301 * Gets a scale factor that determines the distance the view should scroll
12302 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
12303 * @return The horizontal scroll scale factor.
12304 * @hide
12305 */
12306 protected float getHorizontalScrollFactor() {
12307 // TODO: Should use something else.
12308 return getVerticalScrollFactor();
12309 }
12310
12311 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012312 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
12313 * Each MeasureSpec represents a requirement for either the width or the height.
12314 * A MeasureSpec is comprised of a size and a mode. There are three possible
12315 * modes:
12316 * <dl>
12317 * <dt>UNSPECIFIED</dt>
12318 * <dd>
12319 * The parent has not imposed any constraint on the child. It can be whatever size
12320 * it wants.
12321 * </dd>
12322 *
12323 * <dt>EXACTLY</dt>
12324 * <dd>
12325 * The parent has determined an exact size for the child. The child is going to be
12326 * given those bounds regardless of how big it wants to be.
12327 * </dd>
12328 *
12329 * <dt>AT_MOST</dt>
12330 * <dd>
12331 * The child can be as large as it wants up to the specified size.
12332 * </dd>
12333 * </dl>
12334 *
12335 * MeasureSpecs are implemented as ints to reduce object allocation. This class
12336 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
12337 */
12338 public static class MeasureSpec {
12339 private static final int MODE_SHIFT = 30;
12340 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
12341
12342 /**
12343 * Measure specification mode: The parent has not imposed any constraint
12344 * on the child. It can be whatever size it wants.
12345 */
12346 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
12347
12348 /**
12349 * Measure specification mode: The parent has determined an exact size
12350 * for the child. The child is going to be given those bounds regardless
12351 * of how big it wants to be.
12352 */
12353 public static final int EXACTLY = 1 << MODE_SHIFT;
12354
12355 /**
12356 * Measure specification mode: The child can be as large as it wants up
12357 * to the specified size.
12358 */
12359 public static final int AT_MOST = 2 << MODE_SHIFT;
12360
12361 /**
12362 * Creates a measure specification based on the supplied size and mode.
12363 *
12364 * The mode must always be one of the following:
12365 * <ul>
12366 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
12367 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
12368 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
12369 * </ul>
12370 *
12371 * @param size the size of the measure specification
12372 * @param mode the mode of the measure specification
12373 * @return the measure specification based on size and mode
12374 */
12375 public static int makeMeasureSpec(int size, int mode) {
12376 return size + mode;
12377 }
12378
12379 /**
12380 * Extracts the mode from the supplied measure specification.
12381 *
12382 * @param measureSpec the measure specification to extract the mode from
12383 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
12384 * {@link android.view.View.MeasureSpec#AT_MOST} or
12385 * {@link android.view.View.MeasureSpec#EXACTLY}
12386 */
12387 public static int getMode(int measureSpec) {
12388 return (measureSpec & MODE_MASK);
12389 }
12390
12391 /**
12392 * Extracts the size from the supplied measure specification.
12393 *
12394 * @param measureSpec the measure specification to extract the size from
12395 * @return the size in pixels defined in the supplied measure specification
12396 */
12397 public static int getSize(int measureSpec) {
12398 return (measureSpec & ~MODE_MASK);
12399 }
12400
12401 /**
12402 * Returns a String representation of the specified measure
12403 * specification.
12404 *
12405 * @param measureSpec the measure specification to convert to a String
12406 * @return a String with the following format: "MeasureSpec: MODE SIZE"
12407 */
12408 public static String toString(int measureSpec) {
12409 int mode = getMode(measureSpec);
12410 int size = getSize(measureSpec);
12411
12412 StringBuilder sb = new StringBuilder("MeasureSpec: ");
12413
12414 if (mode == UNSPECIFIED)
12415 sb.append("UNSPECIFIED ");
12416 else if (mode == EXACTLY)
12417 sb.append("EXACTLY ");
12418 else if (mode == AT_MOST)
12419 sb.append("AT_MOST ");
12420 else
12421 sb.append(mode).append(" ");
12422
12423 sb.append(size);
12424 return sb.toString();
12425 }
12426 }
12427
12428 class CheckForLongPress implements Runnable {
12429
12430 private int mOriginalWindowAttachCount;
12431
12432 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070012433 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012434 && mOriginalWindowAttachCount == mWindowAttachCount) {
12435 if (performLongClick()) {
12436 mHasPerformedLongPress = true;
12437 }
12438 }
12439 }
12440
12441 public void rememberWindowAttachCount() {
12442 mOriginalWindowAttachCount = mWindowAttachCount;
12443 }
12444 }
Joe Malin32736f02011-01-19 16:14:20 -080012445
Adam Powelle14579b2009-12-16 18:39:52 -080012446 private final class CheckForTap implements Runnable {
12447 public void run() {
12448 mPrivateFlags &= ~PREPRESSED;
12449 mPrivateFlags |= PRESSED;
12450 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012451 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080012452 }
12453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012454
Adam Powella35d7682010-03-12 14:48:13 -080012455 private final class PerformClick implements Runnable {
12456 public void run() {
12457 performClick();
12458 }
12459 }
12460
Dianne Hackborn63042d62011-01-26 18:56:29 -080012461 /** @hide */
12462 public void hackTurnOffWindowResizeAnim(boolean off) {
12463 mAttachInfo.mTurnOffWindowResizeAnim = off;
12464 }
Joe Malin32736f02011-01-19 16:14:20 -080012465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012466 /**
Chet Haasea00f3862011-02-22 06:34:40 -080012467 * This method returns a ViewPropertyAnimator object, which can be used to animate
12468 * specific properties on this View.
12469 *
12470 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
12471 */
12472 public ViewPropertyAnimator animate() {
12473 if (mAnimator == null) {
12474 mAnimator = new ViewPropertyAnimator(this);
12475 }
12476 return mAnimator;
12477 }
12478
12479 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012480 * Interface definition for a callback to be invoked when a key event is
12481 * dispatched to this view. The callback will be invoked before the key
12482 * event is given to the view.
12483 */
12484 public interface OnKeyListener {
12485 /**
12486 * Called when a key 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 key has been dispatched to.
12490 * @param keyCode The code for the physical key that was pressed
12491 * @param event The KeyEvent object containing full information about
12492 * the event.
12493 * @return True if the listener has consumed the event, false otherwise.
12494 */
12495 boolean onKey(View v, int keyCode, KeyEvent event);
12496 }
12497
12498 /**
12499 * Interface definition for a callback to be invoked when a touch event is
12500 * dispatched to this view. The callback will be invoked before the touch
12501 * event is given to the view.
12502 */
12503 public interface OnTouchListener {
12504 /**
12505 * Called when a touch event is dispatched to a view. This allows listeners to
12506 * get a chance to respond before the target view.
12507 *
12508 * @param v The view the touch event has been dispatched to.
12509 * @param event The MotionEvent object containing full information about
12510 * the event.
12511 * @return True if the listener has consumed the event, false otherwise.
12512 */
12513 boolean onTouch(View v, MotionEvent event);
12514 }
12515
12516 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012517 * Interface definition for a callback to be invoked when a generic motion event is
12518 * dispatched to this view. The callback will be invoked before the generic motion
12519 * event is given to the view.
12520 */
12521 public interface OnGenericMotionListener {
12522 /**
12523 * Called when a generic motion event is dispatched to a view. This allows listeners to
12524 * get a chance to respond before the target view.
12525 *
12526 * @param v The view the generic motion event has been dispatched to.
12527 * @param event The MotionEvent object containing full information about
12528 * the event.
12529 * @return True if the listener has consumed the event, false otherwise.
12530 */
12531 boolean onGenericMotion(View v, MotionEvent event);
12532 }
12533
12534 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012535 * Interface definition for a callback to be invoked when a view has been clicked and held.
12536 */
12537 public interface OnLongClickListener {
12538 /**
12539 * Called when a view has been clicked and held.
12540 *
12541 * @param v The view that was clicked and held.
12542 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080012543 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012544 */
12545 boolean onLongClick(View v);
12546 }
12547
12548 /**
Chris Tate32affef2010-10-18 15:29:21 -070012549 * Interface definition for a callback to be invoked when a drag is being dispatched
12550 * to this view. The callback will be invoked before the hosting view's own
12551 * onDrag(event) method. If the listener wants to fall back to the hosting view's
12552 * onDrag(event) behavior, it should return 'false' from this callback.
12553 */
12554 public interface OnDragListener {
12555 /**
12556 * Called when a drag event is dispatched to a view. This allows listeners
12557 * to get a chance to override base View behavior.
12558 *
Joe Malin32736f02011-01-19 16:14:20 -080012559 * @param v The View that received the drag event.
12560 * @param event The {@link android.view.DragEvent} object for the drag event.
12561 * @return {@code true} if the drag event was handled successfully, or {@code false}
12562 * if the drag event was not handled. Note that {@code false} will trigger the View
12563 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070012564 */
12565 boolean onDrag(View v, DragEvent event);
12566 }
12567
12568 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012569 * Interface definition for a callback to be invoked when the focus state of
12570 * a view changed.
12571 */
12572 public interface OnFocusChangeListener {
12573 /**
12574 * Called when the focus state of a view has changed.
12575 *
12576 * @param v The view whose state has changed.
12577 * @param hasFocus The new focus state of v.
12578 */
12579 void onFocusChange(View v, boolean hasFocus);
12580 }
12581
12582 /**
12583 * Interface definition for a callback to be invoked when a view is clicked.
12584 */
12585 public interface OnClickListener {
12586 /**
12587 * Called when a view has been clicked.
12588 *
12589 * @param v The view that was clicked.
12590 */
12591 void onClick(View v);
12592 }
12593
12594 /**
12595 * Interface definition for a callback to be invoked when the context menu
12596 * for this view is being built.
12597 */
12598 public interface OnCreateContextMenuListener {
12599 /**
12600 * Called when the context menu for this view is being built. It is not
12601 * safe to hold onto the menu after this method returns.
12602 *
12603 * @param menu The context menu that is being built
12604 * @param v The view for which the context menu is being built
12605 * @param menuInfo Extra information about the item for which the
12606 * context menu should be shown. This information will vary
12607 * depending on the class of v.
12608 */
12609 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
12610 }
12611
Joe Onorato664644d2011-01-23 17:53:23 -080012612 /**
12613 * Interface definition for a callback to be invoked when the status bar changes
12614 * visibility.
12615 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012616 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080012617 */
12618 public interface OnSystemUiVisibilityChangeListener {
12619 /**
12620 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070012621 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080012622 *
12623 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
12624 */
12625 public void onSystemUiVisibilityChange(int visibility);
12626 }
12627
Adam Powell4afd62b2011-02-18 15:02:18 -080012628 /**
12629 * Interface definition for a callback to be invoked when this view is attached
12630 * or detached from its window.
12631 */
12632 public interface OnAttachStateChangeListener {
12633 /**
12634 * Called when the view is attached to a window.
12635 * @param v The view that was attached
12636 */
12637 public void onViewAttachedToWindow(View v);
12638 /**
12639 * Called when the view is detached from a window.
12640 * @param v The view that was detached
12641 */
12642 public void onViewDetachedFromWindow(View v);
12643 }
12644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012645 private final class UnsetPressedState implements Runnable {
12646 public void run() {
12647 setPressed(false);
12648 }
12649 }
12650
12651 /**
12652 * Base class for derived classes that want to save and restore their own
12653 * state in {@link android.view.View#onSaveInstanceState()}.
12654 */
12655 public static class BaseSavedState extends AbsSavedState {
12656 /**
12657 * Constructor used when reading from a parcel. Reads the state of the superclass.
12658 *
12659 * @param source
12660 */
12661 public BaseSavedState(Parcel source) {
12662 super(source);
12663 }
12664
12665 /**
12666 * Constructor called by derived classes when creating their SavedState objects
12667 *
12668 * @param superState The state of the superclass of this view
12669 */
12670 public BaseSavedState(Parcelable superState) {
12671 super(superState);
12672 }
12673
12674 public static final Parcelable.Creator<BaseSavedState> CREATOR =
12675 new Parcelable.Creator<BaseSavedState>() {
12676 public BaseSavedState createFromParcel(Parcel in) {
12677 return new BaseSavedState(in);
12678 }
12679
12680 public BaseSavedState[] newArray(int size) {
12681 return new BaseSavedState[size];
12682 }
12683 };
12684 }
12685
12686 /**
12687 * A set of information given to a view when it is attached to its parent
12688 * window.
12689 */
12690 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012691 interface Callbacks {
12692 void playSoundEffect(int effectId);
12693 boolean performHapticFeedback(int effectId, boolean always);
12694 }
12695
12696 /**
12697 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
12698 * to a Handler. This class contains the target (View) to invalidate and
12699 * the coordinates of the dirty rectangle.
12700 *
12701 * For performance purposes, this class also implements a pool of up to
12702 * POOL_LIMIT objects that get reused. This reduces memory allocations
12703 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012704 */
Romain Guyd928d682009-03-31 17:52:16 -070012705 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012706 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070012707 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
12708 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070012709 public InvalidateInfo newInstance() {
12710 return new InvalidateInfo();
12711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012712
Romain Guyd928d682009-03-31 17:52:16 -070012713 public void onAcquired(InvalidateInfo element) {
12714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012715
Romain Guyd928d682009-03-31 17:52:16 -070012716 public void onReleased(InvalidateInfo element) {
12717 }
12718 }, POOL_LIMIT)
12719 );
12720
12721 private InvalidateInfo mNext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012722
12723 View target;
12724
12725 int left;
12726 int top;
12727 int right;
12728 int bottom;
12729
Romain Guyd928d682009-03-31 17:52:16 -070012730 public void setNextPoolable(InvalidateInfo element) {
12731 mNext = element;
12732 }
12733
12734 public InvalidateInfo getNextPoolable() {
12735 return mNext;
12736 }
12737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012738 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070012739 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012740 }
12741
12742 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070012743 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012744 }
12745 }
12746
12747 final IWindowSession mSession;
12748
12749 final IWindow mWindow;
12750
12751 final IBinder mWindowToken;
12752
12753 final Callbacks mRootCallbacks;
12754
Chet Haasedaf98e92011-01-10 14:10:36 -080012755 Canvas mHardwareCanvas;
12756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012757 /**
12758 * The top view of the hierarchy.
12759 */
12760 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070012761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012762 IBinder mPanelParentWindowToken;
12763 Surface mSurface;
12764
Romain Guyb051e892010-09-28 19:09:36 -070012765 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012766 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070012767 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080012768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012769 /**
Romain Guy8506ab42009-06-11 17:35:47 -070012770 * Scale factor used by the compatibility mode
12771 */
12772 float mApplicationScale;
12773
12774 /**
12775 * Indicates whether the application is in compatibility mode
12776 */
12777 boolean mScalingRequired;
12778
12779 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012780 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080012781 */
12782 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080012783
Dianne Hackborn63042d62011-01-26 18:56:29 -080012784 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012785 * Left position of this view's window
12786 */
12787 int mWindowLeft;
12788
12789 /**
12790 * Top position of this view's window
12791 */
12792 int mWindowTop;
12793
12794 /**
Adam Powell26153a32010-11-08 15:22:27 -080012795 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070012796 */
Adam Powell26153a32010-11-08 15:22:27 -080012797 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070012798
12799 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012800 * For windows that are full-screen but using insets to layout inside
12801 * of the screen decorations, these are the current insets for the
12802 * content of the window.
12803 */
12804 final Rect mContentInsets = new Rect();
12805
12806 /**
12807 * For windows that are full-screen but using insets to layout inside
12808 * of the screen decorations, these are the current insets for the
12809 * actual visible parts of the window.
12810 */
12811 final Rect mVisibleInsets = new Rect();
12812
12813 /**
12814 * The internal insets given by this window. This value is
12815 * supplied by the client (through
12816 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
12817 * be given to the window manager when changed to be used in laying
12818 * out windows behind it.
12819 */
12820 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
12821 = new ViewTreeObserver.InternalInsetsInfo();
12822
12823 /**
12824 * All views in the window's hierarchy that serve as scroll containers,
12825 * used to determine if the window can be resized or must be panned
12826 * to adjust for a soft input area.
12827 */
12828 final ArrayList<View> mScrollContainers = new ArrayList<View>();
12829
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070012830 final KeyEvent.DispatcherState mKeyDispatchState
12831 = new KeyEvent.DispatcherState();
12832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012833 /**
12834 * Indicates whether the view's window currently has the focus.
12835 */
12836 boolean mHasWindowFocus;
12837
12838 /**
12839 * The current visibility of the window.
12840 */
12841 int mWindowVisibility;
12842
12843 /**
12844 * Indicates the time at which drawing started to occur.
12845 */
12846 long mDrawingTime;
12847
12848 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070012849 * Indicates whether or not ignoring the DIRTY_MASK flags.
12850 */
12851 boolean mIgnoreDirtyState;
12852
12853 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012854 * Indicates whether the view's window is currently in touch mode.
12855 */
12856 boolean mInTouchMode;
12857
12858 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012859 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012860 * the next time it performs a traversal
12861 */
12862 boolean mRecomputeGlobalAttributes;
12863
12864 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012865 * Set during a traveral if any views want to keep the screen on.
12866 */
12867 boolean mKeepScreenOn;
12868
12869 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012870 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
12871 */
12872 int mSystemUiVisibility;
12873
12874 /**
12875 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
12876 * attached.
12877 */
12878 boolean mHasSystemUiListeners;
12879
12880 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012881 * Set if the visibility of any views has changed.
12882 */
12883 boolean mViewVisibilityChanged;
12884
12885 /**
12886 * Set to true if a view has been scrolled.
12887 */
12888 boolean mViewScrollChanged;
12889
12890 /**
12891 * Global to the view hierarchy used as a temporary for dealing with
12892 * x/y points in the transparent region computations.
12893 */
12894 final int[] mTransparentLocation = new int[2];
12895
12896 /**
12897 * Global to the view hierarchy used as a temporary for dealing with
12898 * x/y points in the ViewGroup.invalidateChild implementation.
12899 */
12900 final int[] mInvalidateChildLocation = new int[2];
12901
Chet Haasec3aa3612010-06-17 08:50:37 -070012902
12903 /**
12904 * Global to the view hierarchy used as a temporary for dealing with
12905 * x/y location when view is transformed.
12906 */
12907 final float[] mTmpTransformLocation = new float[2];
12908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012909 /**
12910 * The view tree observer used to dispatch global events like
12911 * layout, pre-draw, touch mode change, etc.
12912 */
12913 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
12914
12915 /**
12916 * A Canvas used by the view hierarchy to perform bitmap caching.
12917 */
12918 Canvas mCanvas;
12919
12920 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012921 * A Handler supplied by a view's {@link android.view.ViewAncestor}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012922 * handler can be used to pump events in the UI events queue.
12923 */
12924 final Handler mHandler;
12925
12926 /**
12927 * Identifier for messages requesting the view to be invalidated.
12928 * Such messages should be sent to {@link #mHandler}.
12929 */
12930 static final int INVALIDATE_MSG = 0x1;
12931
12932 /**
12933 * Identifier for messages requesting the view to invalidate a region.
12934 * Such messages should be sent to {@link #mHandler}.
12935 */
12936 static final int INVALIDATE_RECT_MSG = 0x2;
12937
12938 /**
12939 * Temporary for use in computing invalidate rectangles while
12940 * calling up the hierarchy.
12941 */
12942 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070012943
12944 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070012945 * Temporary for use in computing hit areas with transformed views
12946 */
12947 final RectF mTmpTransformRect = new RectF();
12948
12949 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070012950 * Temporary list for use in collecting focusable descendents of a view.
12951 */
12952 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
12953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012954 /**
12955 * Creates a new set of attachment information with the specified
12956 * events handler and thread.
12957 *
12958 * @param handler the events handler the view must use
12959 */
12960 AttachInfo(IWindowSession session, IWindow window,
12961 Handler handler, Callbacks effectPlayer) {
12962 mSession = session;
12963 mWindow = window;
12964 mWindowToken = window.asBinder();
12965 mHandler = handler;
12966 mRootCallbacks = effectPlayer;
12967 }
12968 }
12969
12970 /**
12971 * <p>ScrollabilityCache holds various fields used by a View when scrolling
12972 * is supported. This avoids keeping too many unused fields in most
12973 * instances of View.</p>
12974 */
Mike Cleronf116bf82009-09-27 19:14:12 -070012975 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080012976
Mike Cleronf116bf82009-09-27 19:14:12 -070012977 /**
12978 * Scrollbars are not visible
12979 */
12980 public static final int OFF = 0;
12981
12982 /**
12983 * Scrollbars are visible
12984 */
12985 public static final int ON = 1;
12986
12987 /**
12988 * Scrollbars are fading away
12989 */
12990 public static final int FADING = 2;
12991
12992 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080012993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012994 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070012995 public int scrollBarDefaultDelayBeforeFade;
12996 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012997
12998 public int scrollBarSize;
12999 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070013000 public float[] interpolatorValues;
13001 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013002
13003 public final Paint paint;
13004 public final Matrix matrix;
13005 public Shader shader;
13006
Mike Cleronf116bf82009-09-27 19:14:12 -070013007 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
13008
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013009 private static final float[] OPAQUE = { 255 };
13010 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080013011
Mike Cleronf116bf82009-09-27 19:14:12 -070013012 /**
13013 * When fading should start. This time moves into the future every time
13014 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
13015 */
13016 public long fadeStartTime;
13017
13018
13019 /**
13020 * The current state of the scrollbars: ON, OFF, or FADING
13021 */
13022 public int state = OFF;
13023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013024 private int mLastColor;
13025
Mike Cleronf116bf82009-09-27 19:14:12 -070013026 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013027 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
13028 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070013029 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
13030 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013031
13032 paint = new Paint();
13033 matrix = new Matrix();
13034 // use use a height of 1, and then wack the matrix each time we
13035 // actually use it.
13036 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070013037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013038 paint.setShader(shader);
13039 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070013040 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013041 }
Romain Guy8506ab42009-06-11 17:35:47 -070013042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013043 public void setFadeColor(int color) {
13044 if (color != 0 && color != mLastColor) {
13045 mLastColor = color;
13046 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070013047
Romain Guye55e1a72009-08-27 10:42:26 -070013048 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
13049 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070013050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013051 paint.setShader(shader);
13052 // Restore the default transfer mode (src_over)
13053 paint.setXfermode(null);
13054 }
13055 }
Joe Malin32736f02011-01-19 16:14:20 -080013056
Mike Cleronf116bf82009-09-27 19:14:12 -070013057 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070013058 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070013059 if (now >= fadeStartTime) {
13060
13061 // the animation fades the scrollbars out by changing
13062 // the opacity (alpha) from fully opaque to fully
13063 // transparent
13064 int nextFrame = (int) now;
13065 int framesCount = 0;
13066
13067 Interpolator interpolator = scrollBarInterpolator;
13068
13069 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013070 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070013071
13072 // End transparent
13073 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013074 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070013075
13076 state = FADING;
13077
13078 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080013079 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070013080 }
13081 }
13082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013083 }
13084}