blob: 7d09fb2871460ab6e9a8b9c90a2d697ac27d348c [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
Christopher Tatea53146c2010-09-07 11:57:52 -070019import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070025import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Canvas;
Mike Cleronf116bf82009-09-27 19:14:12 -070027import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.graphics.LinearGradient;
29import android.graphics.Matrix;
30import android.graphics.Paint;
31import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070032import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.graphics.PorterDuff;
34import android.graphics.PorterDuffXfermode;
35import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070036import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.graphics.Region;
38import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.graphics.drawable.ColorDrawable;
40import android.graphics.drawable.Drawable;
41import android.os.Handler;
42import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Parcel;
44import android.os.Parcelable;
45import android.os.RemoteException;
46import android.os.SystemClock;
Svetoslav Ganovea515ae2011-09-14 18:15:32 -070047import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070049import android.util.FloatProperty;
50import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070052import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070053import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070056import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080058import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.view.accessibility.AccessibilityEvent;
61import android.view.accessibility.AccessibilityEventSource;
62import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070063import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070064import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070066import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080067import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070068import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.view.inputmethod.InputConnection;
70import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.widget.ScrollBarDrawable;
72
Romain Guy1ef3fdb2011-09-09 15:30:30 -070073import static android.os.Build.VERSION_CODES.*;
74
Doug Feltcb3791202011-07-07 11:57:48 -070075import com.android.internal.R;
76import com.android.internal.util.Predicate;
77import com.android.internal.view.menu.MenuBuilder;
78
Christopher Tatea0374192010-10-05 13:06:41 -070079import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070080import java.lang.reflect.InvocationTargetException;
81import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import java.util.ArrayList;
83import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070084import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080085import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
87/**
88 * <p>
89 * This class represents the basic building block for user interface components. A View
90 * occupies a rectangular area on the screen and is responsible for drawing and
91 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070092 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
94 * are invisible containers that hold other Views (or other ViewGroups) and define
95 * their layout properties.
96 * </p>
97 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -070098 * <div class="special reference">
99 * <h3>Developer Guides</h3>
100 * <p>For information about using this class to develop your application's user interface,
101 * read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700103 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 * <a name="Using"></a>
105 * <h3>Using Views</h3>
106 * <p>
107 * All of the views in a window are arranged in a single tree. You can add views
108 * either from code or by specifying a tree of views in one or more XML layout
109 * files. There are many specialized subclasses of views that act as controls or
110 * are capable of displaying text, images, or other content.
111 * </p>
112 * <p>
113 * Once you have created a tree of views, there are typically a few types of
114 * common operations you may wish to perform:
115 * <ul>
116 * <li><strong>Set properties:</strong> for example setting the text of a
117 * {@link android.widget.TextView}. The available properties and the methods
118 * that set them will vary among the different subclasses of views. Note that
119 * properties that are known at build time can be set in the XML layout
120 * files.</li>
121 * <li><strong>Set focus:</strong> The framework will handled moving focus in
122 * response to user input. To force focus to a specific view, call
123 * {@link #requestFocus}.</li>
124 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
125 * that will be notified when something interesting happens to the view. For
126 * example, all views will let you set a listener to be notified when the view
127 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700128 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
129 * Other view subclasses offer more specialized listeners. For example, a Button
130 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700132 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 * </ul>
134 * </p>
135 * <p><em>
136 * Note: The Android framework is responsible for measuring, laying out and
137 * drawing views. You should not call methods that perform these actions on
138 * views yourself unless you are actually implementing a
139 * {@link android.view.ViewGroup}.
140 * </em></p>
141 *
142 * <a name="Lifecycle"></a>
143 * <h3>Implementing a Custom View</h3>
144 *
145 * <p>
146 * To implement a custom view, you will usually begin by providing overrides for
147 * some of the standard methods that the framework calls on all views. You do
148 * not need to override all of these methods. In fact, you can start by just
149 * overriding {@link #onDraw(android.graphics.Canvas)}.
150 * <table border="2" width="85%" align="center" cellpadding="5">
151 * <thead>
152 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
153 * </thead>
154 *
155 * <tbody>
156 * <tr>
157 * <td rowspan="2">Creation</td>
158 * <td>Constructors</td>
159 * <td>There is a form of the constructor that are called when the view
160 * is created from code and a form that is called when the view is
161 * inflated from a layout file. The second form should parse and apply
162 * any attributes defined in the layout file.
163 * </td>
164 * </tr>
165 * <tr>
166 * <td><code>{@link #onFinishInflate()}</code></td>
167 * <td>Called after a view and all of its children has been inflated
168 * from XML.</td>
169 * </tr>
170 *
171 * <tr>
172 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700173 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 * <td>Called to determine the size requirements for this view and all
175 * of its children.
176 * </td>
177 * </tr>
178 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700179 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 * <td>Called when this view should assign a size and position to all
181 * of its children.
182 * </td>
183 * </tr>
184 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700185 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 * <td>Called when the size of this view has changed.
187 * </td>
188 * </tr>
189 *
190 * <tr>
191 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700192 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * <td>Called when the view should render its content.
194 * </td>
195 * </tr>
196 *
197 * <tr>
198 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when a new key event occurs.
201 * </td>
202 * </tr>
203 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700204 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 * <td>Called when a key up event occurs.
206 * </td>
207 * </tr>
208 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700209 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 * <td>Called when a trackball motion event occurs.
211 * </td>
212 * </tr>
213 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700214 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 * <td>Called when a touch screen motion event occurs.
216 * </td>
217 * </tr>
218 *
219 * <tr>
220 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700221 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <td>Called when the view gains or loses focus.
223 * </td>
224 * </tr>
225 *
226 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700227 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 * <td>Called when the window containing the view gains or loses focus.
229 * </td>
230 * </tr>
231 *
232 * <tr>
233 * <td rowspan="3">Attaching</td>
234 * <td><code>{@link #onAttachedToWindow()}</code></td>
235 * <td>Called when the view is attached to a window.
236 * </td>
237 * </tr>
238 *
239 * <tr>
240 * <td><code>{@link #onDetachedFromWindow}</code></td>
241 * <td>Called when the view is detached from its window.
242 * </td>
243 * </tr>
244 *
245 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700246 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 * <td>Called when the visibility of the window containing the view
248 * has changed.
249 * </td>
250 * </tr>
251 * </tbody>
252 *
253 * </table>
254 * </p>
255 *
256 * <a name="IDs"></a>
257 * <h3>IDs</h3>
258 * Views may have an integer id associated with them. These ids are typically
259 * assigned in the layout XML files, and are used to find specific views within
260 * the view tree. A common pattern is to:
261 * <ul>
262 * <li>Define a Button in the layout file and assign it a unique ID.
263 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700264 * &lt;Button
265 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 * android:layout_width="wrap_content"
267 * android:layout_height="wrap_content"
268 * android:text="@string/my_button_text"/&gt;
269 * </pre></li>
270 * <li>From the onCreate method of an Activity, find the Button
271 * <pre class="prettyprint">
272 * Button myButton = (Button) findViewById(R.id.my_button);
273 * </pre></li>
274 * </ul>
275 * <p>
276 * View IDs need not be unique throughout the tree, but it is good practice to
277 * ensure that they are at least unique within the part of the tree you are
278 * searching.
279 * </p>
280 *
281 * <a name="Position"></a>
282 * <h3>Position</h3>
283 * <p>
284 * The geometry of a view is that of a rectangle. A view has a location,
285 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
286 * two dimensions, expressed as a width and a height. The unit for location
287 * and dimensions is the pixel.
288 * </p>
289 *
290 * <p>
291 * It is possible to retrieve the location of a view by invoking the methods
292 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
293 * coordinate of the rectangle representing the view. The latter returns the
294 * top, or Y, coordinate of the rectangle representing the view. These methods
295 * both return the location of the view relative to its parent. For instance,
296 * when getLeft() returns 20, that means the view is located 20 pixels to the
297 * right of the left edge of its direct parent.
298 * </p>
299 *
300 * <p>
301 * In addition, several convenience methods are offered to avoid unnecessary
302 * computations, namely {@link #getRight()} and {@link #getBottom()}.
303 * These methods return the coordinates of the right and bottom edges of the
304 * rectangle representing the view. For instance, calling {@link #getRight()}
305 * is similar to the following computation: <code>getLeft() + getWidth()</code>
306 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
307 * </p>
308 *
309 * <a name="SizePaddingMargins"></a>
310 * <h3>Size, padding and margins</h3>
311 * <p>
312 * The size of a view is expressed with a width and a height. A view actually
313 * possess two pairs of width and height values.
314 * </p>
315 *
316 * <p>
317 * The first pair is known as <em>measured width</em> and
318 * <em>measured height</em>. These dimensions define how big a view wants to be
319 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
320 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
321 * and {@link #getMeasuredHeight()}.
322 * </p>
323 *
324 * <p>
325 * The second pair is simply known as <em>width</em> and <em>height</em>, or
326 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
327 * dimensions define the actual size of the view on screen, at drawing time and
328 * after layout. These values may, but do not have to, be different from the
329 * measured width and height. The width and height can be obtained by calling
330 * {@link #getWidth()} and {@link #getHeight()}.
331 * </p>
332 *
333 * <p>
334 * To measure its dimensions, a view takes into account its padding. The padding
335 * is expressed in pixels for the left, top, right and bottom parts of the view.
336 * Padding can be used to offset the content of the view by a specific amount of
337 * pixels. For instance, a left padding of 2 will push the view's content by
338 * 2 pixels to the right of the left edge. Padding can be set using the
339 * {@link #setPadding(int, int, int, int)} method and queried by calling
340 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
Fabrice Di Megliod8703a92011-06-16 18:54:08 -0700341 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 * </p>
343 *
344 * <p>
345 * Even though a view can define a padding, it does not provide any support for
346 * margins. However, view groups provide such a support. Refer to
347 * {@link android.view.ViewGroup} and
348 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
349 * </p>
350 *
351 * <a name="Layout"></a>
352 * <h3>Layout</h3>
353 * <p>
354 * Layout is a two pass process: a measure pass and a layout pass. The measuring
355 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
356 * of the view tree. Each view pushes dimension specifications down the tree
357 * during the recursion. At the end of the measure pass, every view has stored
358 * its measurements. The second pass happens in
359 * {@link #layout(int,int,int,int)} and is also top-down. During
360 * this pass each parent is responsible for positioning all of its children
361 * using the sizes computed in the measure pass.
362 * </p>
363 *
364 * <p>
365 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
366 * {@link #getMeasuredHeight()} values must be set, along with those for all of
367 * that view's descendants. A view's measured width and measured height values
368 * must respect the constraints imposed by the view's parents. This guarantees
369 * that at the end of the measure pass, all parents accept all of their
370 * children's measurements. A parent view may call measure() more than once on
371 * its children. For example, the parent may measure each child once with
372 * unspecified dimensions to find out how big they want to be, then call
373 * measure() on them again with actual numbers if the sum of all the children's
374 * unconstrained sizes is too big or too small.
375 * </p>
376 *
377 * <p>
378 * The measure pass uses two classes to communicate dimensions. The
379 * {@link MeasureSpec} class is used by views to tell their parents how they
380 * want to be measured and positioned. The base LayoutParams class just
381 * describes how big the view wants to be for both width and height. For each
382 * dimension, it can specify one of:
383 * <ul>
384 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800385 * <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 -0800386 * (minus padding)
387 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
388 * enclose its content (plus padding).
389 * </ul>
390 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
391 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
392 * an X and Y value.
393 * </p>
394 *
395 * <p>
396 * MeasureSpecs are used to push requirements down the tree from parent to
397 * child. A MeasureSpec can be in one of three modes:
398 * <ul>
399 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
400 * of a child view. For example, a LinearLayout may call measure() on its child
401 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
402 * tall the child view wants to be given a width of 240 pixels.
403 * <li>EXACTLY: This is used by the parent to impose an exact size on the
404 * child. The child must use this size, and guarantee that all of its
405 * descendants will fit within this size.
406 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
407 * child. The child must gurantee that it and all of its descendants will fit
408 * within this size.
409 * </ul>
410 * </p>
411 *
412 * <p>
413 * To intiate a layout, call {@link #requestLayout}. This method is typically
414 * called by a view on itself when it believes that is can no longer fit within
415 * its current bounds.
416 * </p>
417 *
418 * <a name="Drawing"></a>
419 * <h3>Drawing</h3>
420 * <p>
421 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700422 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 * this means that parents will draw before (i.e., behind) their children, with
424 * siblings drawn in the order they appear in the tree.
425 * If you set a background drawable for a View, then the View will draw it for you
426 * before calling back to its <code>onDraw()</code> method.
427 * </p>
428 *
429 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700430 * 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 -0800431 * </p>
432 *
433 * <p>
434 * To force a view to draw, call {@link #invalidate()}.
435 * </p>
436 *
437 * <a name="EventHandlingThreading"></a>
438 * <h3>Event Handling and Threading</h3>
439 * <p>
440 * The basic cycle of a view is as follows:
441 * <ol>
442 * <li>An event comes in and is dispatched to the appropriate view. The view
443 * handles the event and notifies any listeners.</li>
444 * <li>If in the course of processing the event, the view's bounds may need
445 * to be changed, the view will call {@link #requestLayout()}.</li>
446 * <li>Similarly, if in the course of processing the event the view's appearance
447 * may need to be changed, the view will call {@link #invalidate()}.</li>
448 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
449 * the framework will take care of measuring, laying out, and drawing the tree
450 * as appropriate.</li>
451 * </ol>
452 * </p>
453 *
454 * <p><em>Note: The entire view tree is single threaded. You must always be on
455 * the UI thread when calling any method on any view.</em>
456 * If you are doing work on other threads and want to update the state of a view
457 * from that thread, you should use a {@link Handler}.
458 * </p>
459 *
460 * <a name="FocusHandling"></a>
461 * <h3>Focus Handling</h3>
462 * <p>
463 * The framework will handle routine focus movement in response to user input.
464 * This includes changing the focus as views are removed or hidden, or as new
465 * views become available. Views indicate their willingness to take focus
466 * through the {@link #isFocusable} method. To change whether a view can take
467 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
468 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
469 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
470 * </p>
471 * <p>
472 * Focus movement is based on an algorithm which finds the nearest neighbor in a
473 * given direction. In rare cases, the default algorithm may not match the
474 * intended behavior of the developer. In these situations, you can provide
475 * explicit overrides by using these XML attributes in the layout file:
476 * <pre>
477 * nextFocusDown
478 * nextFocusLeft
479 * nextFocusRight
480 * nextFocusUp
481 * </pre>
482 * </p>
483 *
484 *
485 * <p>
486 * To get a particular view to take focus, call {@link #requestFocus()}.
487 * </p>
488 *
489 * <a name="TouchMode"></a>
490 * <h3>Touch Mode</h3>
491 * <p>
492 * When a user is navigating a user interface via directional keys such as a D-pad, it is
493 * necessary to give focus to actionable items such as buttons so the user can see
494 * what will take input. If the device has touch capabilities, however, and the user
495 * begins interacting with the interface by touching it, it is no longer necessary to
496 * always highlight, or give focus to, a particular view. This motivates a mode
497 * for interaction named 'touch mode'.
498 * </p>
499 * <p>
500 * For a touch capable device, once the user touches the screen, the device
501 * will enter touch mode. From this point onward, only views for which
502 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
503 * Other views that are touchable, like buttons, will not take focus when touched; they will
504 * only fire the on click listeners.
505 * </p>
506 * <p>
507 * Any time a user hits a directional key, such as a D-pad direction, the view device will
508 * exit touch mode, and find a view to take focus, so that the user may resume interacting
509 * with the user interface without touching the screen again.
510 * </p>
511 * <p>
512 * The touch mode state is maintained across {@link android.app.Activity}s. Call
513 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
514 * </p>
515 *
516 * <a name="Scrolling"></a>
517 * <h3>Scrolling</h3>
518 * <p>
519 * The framework provides basic support for views that wish to internally
520 * scroll their content. This includes keeping track of the X and Y scroll
521 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800522 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700523 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 * </p>
525 *
526 * <a name="Tags"></a>
527 * <h3>Tags</h3>
528 * <p>
529 * Unlike IDs, tags are not used to identify views. Tags are essentially an
530 * extra piece of information that can be associated with a view. They are most
531 * often used as a convenience to store data related to views in the views
532 * themselves rather than by putting them in a separate structure.
533 * </p>
534 *
535 * <a name="Animation"></a>
536 * <h3>Animation</h3>
537 * <p>
538 * You can attach an {@link Animation} object to a view using
539 * {@link #setAnimation(Animation)} or
540 * {@link #startAnimation(Animation)}. The animation can alter the scale,
541 * rotation, translation and alpha of a view over time. If the animation is
542 * attached to a view that has children, the animation will affect the entire
543 * subtree rooted by that node. When an animation is started, the framework will
544 * take care of redrawing the appropriate views until the animation completes.
545 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800546 * <p>
547 * Starting with Android 3.0, the preferred way of animating views is to use the
548 * {@link android.animation} package APIs.
549 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 *
Jeff Brown85a31762010-09-01 17:01:00 -0700551 * <a name="Security"></a>
552 * <h3>Security</h3>
553 * <p>
554 * Sometimes it is essential that an application be able to verify that an action
555 * is being performed with the full knowledge and consent of the user, such as
556 * granting a permission request, making a purchase or clicking on an advertisement.
557 * Unfortunately, a malicious application could try to spoof the user into
558 * performing these actions, unaware, by concealing the intended purpose of the view.
559 * As a remedy, the framework offers a touch filtering mechanism that can be used to
560 * improve the security of views that provide access to sensitive functionality.
561 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700562 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800563 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700564 * will discard touches that are received whenever the view's window is obscured by
565 * another visible window. As a result, the view will not receive touches whenever a
566 * toast, dialog or other window appears above the view's window.
567 * </p><p>
568 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700569 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
570 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700571 * </p>
572 *
Romain Guy171c5922011-01-06 10:04:23 -0800573 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700574 * @attr ref android.R.styleable#View_background
575 * @attr ref android.R.styleable#View_clickable
576 * @attr ref android.R.styleable#View_contentDescription
577 * @attr ref android.R.styleable#View_drawingCacheQuality
578 * @attr ref android.R.styleable#View_duplicateParentState
579 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700580 * @attr ref android.R.styleable#View_requiresFadingEdge
Romain Guyd6a463a2009-05-21 23:10:10 -0700581 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700582 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700584 * @attr ref android.R.styleable#View_isScrollContainer
585 * @attr ref android.R.styleable#View_focusable
586 * @attr ref android.R.styleable#View_focusableInTouchMode
587 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
588 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800589 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700590 * @attr ref android.R.styleable#View_longClickable
591 * @attr ref android.R.styleable#View_minHeight
592 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 * @attr ref android.R.styleable#View_nextFocusDown
594 * @attr ref android.R.styleable#View_nextFocusLeft
595 * @attr ref android.R.styleable#View_nextFocusRight
596 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700597 * @attr ref android.R.styleable#View_onClick
598 * @attr ref android.R.styleable#View_padding
599 * @attr ref android.R.styleable#View_paddingBottom
600 * @attr ref android.R.styleable#View_paddingLeft
601 * @attr ref android.R.styleable#View_paddingRight
602 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800603 * @attr ref android.R.styleable#View_paddingStart
604 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700605 * @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 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700634public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
635 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 private static final boolean DBG = false;
637
638 /**
639 * The logging tag used by this class with android.util.Log.
640 */
641 protected static final String VIEW_LOG_TAG = "View";
642
643 /**
644 * Used to mark a View that has no ID.
645 */
646 public static final int NO_ID = -1;
647
648 /**
649 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
650 * calling setFlags.
651 */
652 private static final int NOT_FOCUSABLE = 0x00000000;
653
654 /**
655 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
656 * setFlags.
657 */
658 private static final int FOCUSABLE = 0x00000001;
659
660 /**
661 * Mask for use with setFlags indicating bits used for focus.
662 */
663 private static final int FOCUSABLE_MASK = 0x00000001;
664
665 /**
666 * This view will adjust its padding to fit sytem windows (e.g. status bar)
667 */
668 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
669
670 /**
Scott Main812634c22011-07-27 13:22:35 -0700671 * This view is visible.
672 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
673 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 */
675 public static final int VISIBLE = 0x00000000;
676
677 /**
678 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700679 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
680 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 */
682 public static final int INVISIBLE = 0x00000004;
683
684 /**
685 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700686 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
687 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 */
689 public static final int GONE = 0x00000008;
690
691 /**
692 * Mask for use with setFlags indicating bits used for visibility.
693 * {@hide}
694 */
695 static final int VISIBILITY_MASK = 0x0000000C;
696
697 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
698
699 /**
700 * This view is enabled. Intrepretation varies by subclass.
701 * Use with ENABLED_MASK when calling setFlags.
702 * {@hide}
703 */
704 static final int ENABLED = 0x00000000;
705
706 /**
707 * This view is disabled. Intrepretation varies by subclass.
708 * Use with ENABLED_MASK when calling setFlags.
709 * {@hide}
710 */
711 static final int DISABLED = 0x00000020;
712
713 /**
714 * Mask for use with setFlags indicating bits used for indicating whether
715 * this view is enabled
716 * {@hide}
717 */
718 static final int ENABLED_MASK = 0x00000020;
719
720 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700721 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
722 * called and further optimizations will be performed. It is okay to have
723 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 * {@hide}
725 */
726 static final int WILL_NOT_DRAW = 0x00000080;
727
728 /**
729 * Mask for use with setFlags indicating bits used for indicating whether
730 * this view is will draw
731 * {@hide}
732 */
733 static final int DRAW_MASK = 0x00000080;
734
735 /**
736 * <p>This view doesn't show scrollbars.</p>
737 * {@hide}
738 */
739 static final int SCROLLBARS_NONE = 0x00000000;
740
741 /**
742 * <p>This view shows horizontal scrollbars.</p>
743 * {@hide}
744 */
745 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
746
747 /**
748 * <p>This view shows vertical scrollbars.</p>
749 * {@hide}
750 */
751 static final int SCROLLBARS_VERTICAL = 0x00000200;
752
753 /**
754 * <p>Mask for use with setFlags indicating bits used for indicating which
755 * scrollbars are enabled.</p>
756 * {@hide}
757 */
758 static final int SCROLLBARS_MASK = 0x00000300;
759
Jeff Brown85a31762010-09-01 17:01:00 -0700760 /**
761 * Indicates that the view should filter touches when its window is obscured.
762 * Refer to the class comments for more information about this security feature.
763 * {@hide}
764 */
765 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
766
767 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768
769 /**
770 * <p>This view doesn't show fading edges.</p>
771 * {@hide}
772 */
773 static final int FADING_EDGE_NONE = 0x00000000;
774
775 /**
776 * <p>This view shows horizontal fading edges.</p>
777 * {@hide}
778 */
779 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
780
781 /**
782 * <p>This view shows vertical fading edges.</p>
783 * {@hide}
784 */
785 static final int FADING_EDGE_VERTICAL = 0x00002000;
786
787 /**
788 * <p>Mask for use with setFlags indicating bits used for indicating which
789 * fading edges are enabled.</p>
790 * {@hide}
791 */
792 static final int FADING_EDGE_MASK = 0x00003000;
793
794 /**
795 * <p>Indicates this view can be clicked. When clickable, a View reacts
796 * to clicks by notifying the OnClickListener.<p>
797 * {@hide}
798 */
799 static final int CLICKABLE = 0x00004000;
800
801 /**
802 * <p>Indicates this view is caching its drawing into a bitmap.</p>
803 * {@hide}
804 */
805 static final int DRAWING_CACHE_ENABLED = 0x00008000;
806
807 /**
808 * <p>Indicates that no icicle should be saved for this view.<p>
809 * {@hide}
810 */
811 static final int SAVE_DISABLED = 0x000010000;
812
813 /**
814 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
815 * property.</p>
816 * {@hide}
817 */
818 static final int SAVE_DISABLED_MASK = 0x000010000;
819
820 /**
821 * <p>Indicates that no drawing cache should ever be created for this view.<p>
822 * {@hide}
823 */
824 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
825
826 /**
827 * <p>Indicates this view can take / keep focus when int touch mode.</p>
828 * {@hide}
829 */
830 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
831
832 /**
833 * <p>Enables low quality mode for the drawing cache.</p>
834 */
835 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
836
837 /**
838 * <p>Enables high quality mode for the drawing cache.</p>
839 */
840 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
841
842 /**
843 * <p>Enables automatic quality mode for the drawing cache.</p>
844 */
845 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
846
847 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
848 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
849 };
850
851 /**
852 * <p>Mask for use with setFlags indicating bits used for the cache
853 * quality property.</p>
854 * {@hide}
855 */
856 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
857
858 /**
859 * <p>
860 * Indicates this view can be long clicked. When long clickable, a View
861 * reacts to long clicks by notifying the OnLongClickListener or showing a
862 * context menu.
863 * </p>
864 * {@hide}
865 */
866 static final int LONG_CLICKABLE = 0x00200000;
867
868 /**
869 * <p>Indicates that this view gets its drawable states from its direct parent
870 * and ignores its original internal states.</p>
871 *
872 * @hide
873 */
874 static final int DUPLICATE_PARENT_STATE = 0x00400000;
875
876 /**
877 * The scrollbar style to display the scrollbars inside the content area,
878 * without increasing the padding. The scrollbars will be overlaid with
879 * translucency on the view's content.
880 */
881 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
882
883 /**
884 * The scrollbar style to display the scrollbars inside the padded area,
885 * increasing the padding of the view. The scrollbars will not overlap the
886 * content area of the view.
887 */
888 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
889
890 /**
891 * The scrollbar style to display the scrollbars at the edge of the view,
892 * without increasing the padding. The scrollbars will be overlaid with
893 * translucency.
894 */
895 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
896
897 /**
898 * The scrollbar style to display the scrollbars at the edge of the view,
899 * increasing the padding of the view. The scrollbars will only overlap the
900 * background, if any.
901 */
902 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
903
904 /**
905 * Mask to check if the scrollbar style is overlay or inset.
906 * {@hide}
907 */
908 static final int SCROLLBARS_INSET_MASK = 0x01000000;
909
910 /**
911 * Mask to check if the scrollbar style is inside or outside.
912 * {@hide}
913 */
914 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
915
916 /**
917 * Mask for scrollbar style.
918 * {@hide}
919 */
920 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
921
922 /**
923 * View flag indicating that the screen should remain on while the
924 * window containing this view is visible to the user. This effectively
925 * takes care of automatically setting the WindowManager's
926 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
927 */
928 public static final int KEEP_SCREEN_ON = 0x04000000;
929
930 /**
931 * View flag indicating whether this view should have sound effects enabled
932 * for events such as clicking and touching.
933 */
934 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
935
936 /**
937 * View flag indicating whether this view should have haptic feedback
938 * enabled for events such as long presses.
939 */
940 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
941
942 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700943 * <p>Indicates that the view hierarchy should stop saving state when
944 * it reaches this view. If state saving is initiated immediately at
945 * the view, it will be allowed.
946 * {@hide}
947 */
948 static final int PARENT_SAVE_DISABLED = 0x20000000;
949
950 /**
951 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
952 * {@hide}
953 */
954 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
955
956 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800957 * Horizontal direction of this view is from Left to Right.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700958 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800959 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700960 public static final int LAYOUT_DIRECTION_LTR = 0x00000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800961
962 /**
963 * Horizontal direction of this view is from Right to Left.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700964 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800965 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700966 public static final int LAYOUT_DIRECTION_RTL = 0x40000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800967
968 /**
969 * Horizontal direction of this view is inherited from its parent.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700970 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800971 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700972 public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800973
974 /**
975 * Horizontal direction of this view is from deduced from the default language
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700976 * script for the locale. Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800977 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700978 public static final int LAYOUT_DIRECTION_LOCALE = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800979
980 /**
981 * Mask for use with setFlags indicating bits used for horizontalDirection.
982 * {@hide}
983 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700984 static final int LAYOUT_DIRECTION_MASK = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800985
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700986 /*
987 * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct
988 * flag value.
989 * {@hide}
990 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700991 private static final int[] LAYOUT_DIRECTION_FLAGS = {LAYOUT_DIRECTION_LTR,
992 LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE};
Cibu Johny7632cb92010-02-22 13:01:02 -0800993
994 /**
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700995 * Default horizontalDirection.
996 * {@hide}
997 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700998 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700999
1000 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001001 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1002 * should add all focusable Views regardless if they are focusable in touch mode.
1003 */
1004 public static final int FOCUSABLES_ALL = 0x00000000;
1005
1006 /**
1007 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1008 * should add only Views focusable in touch mode.
1009 */
1010 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1011
1012 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001013 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 * item.
1015 */
1016 public static final int FOCUS_BACKWARD = 0x00000001;
1017
1018 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001019 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 * item.
1021 */
1022 public static final int FOCUS_FORWARD = 0x00000002;
1023
1024 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001025 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 */
1027 public static final int FOCUS_LEFT = 0x00000011;
1028
1029 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001030 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 */
1032 public static final int FOCUS_UP = 0x00000021;
1033
1034 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001035 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 */
1037 public static final int FOCUS_RIGHT = 0x00000042;
1038
1039 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001040 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 */
1042 public static final int FOCUS_DOWN = 0x00000082;
1043
1044 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001045 * Bits of {@link #getMeasuredWidthAndState()} and
1046 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1047 */
1048 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1049
1050 /**
1051 * Bits of {@link #getMeasuredWidthAndState()} and
1052 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1053 */
1054 public static final int MEASURED_STATE_MASK = 0xff000000;
1055
1056 /**
1057 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1058 * for functions that combine both width and height into a single int,
1059 * such as {@link #getMeasuredState()} and the childState argument of
1060 * {@link #resolveSizeAndState(int, int, int)}.
1061 */
1062 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1063
1064 /**
1065 * Bit of {@link #getMeasuredWidthAndState()} and
1066 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1067 * is smaller that the space the view would like to have.
1068 */
1069 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1070
1071 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 * Base View state sets
1073 */
1074 // Singles
1075 /**
1076 * Indicates the view has no states set. States are used with
1077 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1078 * view depending on its state.
1079 *
1080 * @see android.graphics.drawable.Drawable
1081 * @see #getDrawableState()
1082 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001083 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 /**
1085 * Indicates the view is enabled. States are used with
1086 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1087 * view depending on its state.
1088 *
1089 * @see android.graphics.drawable.Drawable
1090 * @see #getDrawableState()
1091 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001092 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 /**
1094 * Indicates the view is focused. States are used with
1095 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1096 * view depending on its state.
1097 *
1098 * @see android.graphics.drawable.Drawable
1099 * @see #getDrawableState()
1100 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001101 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 /**
1103 * Indicates the view is selected. States are used with
1104 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1105 * view depending on its state.
1106 *
1107 * @see android.graphics.drawable.Drawable
1108 * @see #getDrawableState()
1109 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001110 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 /**
1112 * Indicates the view is pressed. States are used with
1113 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1114 * view depending on its state.
1115 *
1116 * @see android.graphics.drawable.Drawable
1117 * @see #getDrawableState()
1118 * @hide
1119 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001120 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 /**
1122 * Indicates the view's window has focus. States are used with
1123 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1124 * view depending on its state.
1125 *
1126 * @see android.graphics.drawable.Drawable
1127 * @see #getDrawableState()
1128 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001129 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 // Doubles
1131 /**
1132 * Indicates the view is enabled and has the focus.
1133 *
1134 * @see #ENABLED_STATE_SET
1135 * @see #FOCUSED_STATE_SET
1136 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001137 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 /**
1139 * Indicates the view is enabled and selected.
1140 *
1141 * @see #ENABLED_STATE_SET
1142 * @see #SELECTED_STATE_SET
1143 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001144 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 /**
1146 * Indicates the view is enabled and that its window has focus.
1147 *
1148 * @see #ENABLED_STATE_SET
1149 * @see #WINDOW_FOCUSED_STATE_SET
1150 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001151 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 /**
1153 * Indicates the view is focused and selected.
1154 *
1155 * @see #FOCUSED_STATE_SET
1156 * @see #SELECTED_STATE_SET
1157 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001158 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 /**
1160 * Indicates the view has the focus and that its window has the focus.
1161 *
1162 * @see #FOCUSED_STATE_SET
1163 * @see #WINDOW_FOCUSED_STATE_SET
1164 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001165 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 /**
1167 * Indicates the view is selected and that its window has the focus.
1168 *
1169 * @see #SELECTED_STATE_SET
1170 * @see #WINDOW_FOCUSED_STATE_SET
1171 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001172 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 // Triples
1174 /**
1175 * Indicates the view is enabled, focused and selected.
1176 *
1177 * @see #ENABLED_STATE_SET
1178 * @see #FOCUSED_STATE_SET
1179 * @see #SELECTED_STATE_SET
1180 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001181 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 /**
1183 * Indicates the view is enabled, focused and its window has the focus.
1184 *
1185 * @see #ENABLED_STATE_SET
1186 * @see #FOCUSED_STATE_SET
1187 * @see #WINDOW_FOCUSED_STATE_SET
1188 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001189 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 /**
1191 * Indicates the view is enabled, selected and its window has the focus.
1192 *
1193 * @see #ENABLED_STATE_SET
1194 * @see #SELECTED_STATE_SET
1195 * @see #WINDOW_FOCUSED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view is focused, selected and its window has the focus.
1200 *
1201 * @see #FOCUSED_STATE_SET
1202 * @see #SELECTED_STATE_SET
1203 * @see #WINDOW_FOCUSED_STATE_SET
1204 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001205 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 /**
1207 * Indicates the view is enabled, focused, selected and its window
1208 * has the focus.
1209 *
1210 * @see #ENABLED_STATE_SET
1211 * @see #FOCUSED_STATE_SET
1212 * @see #SELECTED_STATE_SET
1213 * @see #WINDOW_FOCUSED_STATE_SET
1214 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001215 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 /**
1217 * Indicates the view is pressed and its window has the focus.
1218 *
1219 * @see #PRESSED_STATE_SET
1220 * @see #WINDOW_FOCUSED_STATE_SET
1221 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001222 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 /**
1224 * Indicates the view is pressed and selected.
1225 *
1226 * @see #PRESSED_STATE_SET
1227 * @see #SELECTED_STATE_SET
1228 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001229 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 /**
1231 * Indicates the view is pressed, selected and its window has the focus.
1232 *
1233 * @see #PRESSED_STATE_SET
1234 * @see #SELECTED_STATE_SET
1235 * @see #WINDOW_FOCUSED_STATE_SET
1236 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001237 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 /**
1239 * Indicates the view is pressed and focused.
1240 *
1241 * @see #PRESSED_STATE_SET
1242 * @see #FOCUSED_STATE_SET
1243 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001244 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 /**
1246 * Indicates the view is pressed, focused and its window has the focus.
1247 *
1248 * @see #PRESSED_STATE_SET
1249 * @see #FOCUSED_STATE_SET
1250 * @see #WINDOW_FOCUSED_STATE_SET
1251 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001252 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 /**
1254 * Indicates the view is pressed, focused and selected.
1255 *
1256 * @see #PRESSED_STATE_SET
1257 * @see #SELECTED_STATE_SET
1258 * @see #FOCUSED_STATE_SET
1259 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001260 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 /**
1262 * Indicates the view is pressed, focused, selected and its window has the focus.
1263 *
1264 * @see #PRESSED_STATE_SET
1265 * @see #FOCUSED_STATE_SET
1266 * @see #SELECTED_STATE_SET
1267 * @see #WINDOW_FOCUSED_STATE_SET
1268 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001269 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 /**
1271 * Indicates the view is pressed and enabled.
1272 *
1273 * @see #PRESSED_STATE_SET
1274 * @see #ENABLED_STATE_SET
1275 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001276 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 /**
1278 * Indicates the view is pressed, enabled and its window has the focus.
1279 *
1280 * @see #PRESSED_STATE_SET
1281 * @see #ENABLED_STATE_SET
1282 * @see #WINDOW_FOCUSED_STATE_SET
1283 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001284 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 /**
1286 * Indicates the view is pressed, enabled and selected.
1287 *
1288 * @see #PRESSED_STATE_SET
1289 * @see #ENABLED_STATE_SET
1290 * @see #SELECTED_STATE_SET
1291 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001292 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 /**
1294 * Indicates the view is pressed, enabled, selected and its window has the
1295 * focus.
1296 *
1297 * @see #PRESSED_STATE_SET
1298 * @see #ENABLED_STATE_SET
1299 * @see #SELECTED_STATE_SET
1300 * @see #WINDOW_FOCUSED_STATE_SET
1301 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001302 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 /**
1304 * Indicates the view is pressed, enabled and focused.
1305 *
1306 * @see #PRESSED_STATE_SET
1307 * @see #ENABLED_STATE_SET
1308 * @see #FOCUSED_STATE_SET
1309 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001310 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 /**
1312 * Indicates the view is pressed, enabled, focused and its window has the
1313 * focus.
1314 *
1315 * @see #PRESSED_STATE_SET
1316 * @see #ENABLED_STATE_SET
1317 * @see #FOCUSED_STATE_SET
1318 * @see #WINDOW_FOCUSED_STATE_SET
1319 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001320 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 /**
1322 * Indicates the view is pressed, enabled, focused and selected.
1323 *
1324 * @see #PRESSED_STATE_SET
1325 * @see #ENABLED_STATE_SET
1326 * @see #SELECTED_STATE_SET
1327 * @see #FOCUSED_STATE_SET
1328 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001329 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 /**
1331 * Indicates the view is pressed, enabled, focused, selected and its window
1332 * has the focus.
1333 *
1334 * @see #PRESSED_STATE_SET
1335 * @see #ENABLED_STATE_SET
1336 * @see #SELECTED_STATE_SET
1337 * @see #FOCUSED_STATE_SET
1338 * @see #WINDOW_FOCUSED_STATE_SET
1339 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001340 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341
1342 /**
1343 * The order here is very important to {@link #getDrawableState()}
1344 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001345 private static final int[][] VIEW_STATE_SETS;
1346
Romain Guyb051e892010-09-28 19:09:36 -07001347 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1348 static final int VIEW_STATE_SELECTED = 1 << 1;
1349 static final int VIEW_STATE_FOCUSED = 1 << 2;
1350 static final int VIEW_STATE_ENABLED = 1 << 3;
1351 static final int VIEW_STATE_PRESSED = 1 << 4;
1352 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001353 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001354 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001355 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1356 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001357
1358 static final int[] VIEW_STATE_IDS = new int[] {
1359 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1360 R.attr.state_selected, VIEW_STATE_SELECTED,
1361 R.attr.state_focused, VIEW_STATE_FOCUSED,
1362 R.attr.state_enabled, VIEW_STATE_ENABLED,
1363 R.attr.state_pressed, VIEW_STATE_PRESSED,
1364 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001365 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001366 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001367 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1368 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 };
1370
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001371 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001372 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1373 throw new IllegalStateException(
1374 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1375 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001376 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001377 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001378 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001379 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001380 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001381 orderedIds[i * 2] = viewState;
1382 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001383 }
1384 }
1385 }
Romain Guyb051e892010-09-28 19:09:36 -07001386 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1387 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1388 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001389 int numBits = Integer.bitCount(i);
1390 int[] set = new int[numBits];
1391 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001392 for (int j = 0; j < orderedIds.length; j += 2) {
1393 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001394 set[pos++] = orderedIds[j];
1395 }
1396 }
1397 VIEW_STATE_SETS[i] = set;
1398 }
1399
1400 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1401 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1402 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1403 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1404 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1405 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1406 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1407 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1408 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1409 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1410 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1411 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1412 | VIEW_STATE_FOCUSED];
1413 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1414 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1416 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1418 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1419 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1420 | VIEW_STATE_ENABLED];
1421 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1422 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1423 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1424 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1425 | VIEW_STATE_ENABLED];
1426 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1427 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1428 | VIEW_STATE_ENABLED];
1429 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1430 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1431 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1432
1433 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1434 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1435 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1436 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1437 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1438 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1439 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1440 | VIEW_STATE_PRESSED];
1441 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1442 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1443 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1444 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1445 | VIEW_STATE_PRESSED];
1446 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1447 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1448 | VIEW_STATE_PRESSED];
1449 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1451 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1452 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1453 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1454 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1455 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1456 | VIEW_STATE_PRESSED];
1457 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1459 | VIEW_STATE_PRESSED];
1460 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1462 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1463 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1464 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1465 | VIEW_STATE_PRESSED];
1466 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1467 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1468 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1469 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1470 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1471 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1472 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1473 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1474 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1475 | VIEW_STATE_PRESSED];
1476 }
1477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001479 * Accessibility event types that are dispatched for text population.
1480 */
1481 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1482 AccessibilityEvent.TYPE_VIEW_CLICKED
1483 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1484 | AccessibilityEvent.TYPE_VIEW_SELECTED
1485 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1486 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1487 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001488 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001489 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
1490 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001491
1492 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 * Temporary Rect currently for use in setBackground(). This will probably
1494 * be extended in the future to hold our own class with more than just
1495 * a Rect. :)
1496 */
1497 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001498
1499 /**
1500 * Map used to store views' tags.
1501 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001502 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001505 * The next available accessiiblity id.
1506 */
1507 private static int sNextAccessibilityViewId;
1508
1509 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 * The animation currently associated with this view.
1511 * @hide
1512 */
1513 protected Animation mCurrentAnimation = null;
1514
1515 /**
1516 * Width as measured during measure pass.
1517 * {@hide}
1518 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001519 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001520 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521
1522 /**
1523 * Height as measured during measure pass.
1524 * {@hide}
1525 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001526 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001527 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528
1529 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001530 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1531 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1532 * its display list. This flag, used only when hw accelerated, allows us to clear the
1533 * flag while retaining this information until it's needed (at getDisplayList() time and
1534 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1535 *
1536 * {@hide}
1537 */
1538 boolean mRecreateDisplayList = false;
1539
1540 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 * The view's identifier.
1542 * {@hide}
1543 *
1544 * @see #setId(int)
1545 * @see #getId()
1546 */
1547 @ViewDebug.ExportedProperty(resolveId = true)
1548 int mID = NO_ID;
1549
1550 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001551 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001552 */
1553 int mAccessibilityViewId = NO_ID;
1554
1555 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 * The view's tag.
1557 * {@hide}
1558 *
1559 * @see #setTag(Object)
1560 * @see #getTag()
1561 */
1562 protected Object mTag;
1563
1564 // for mPrivateFlags:
1565 /** {@hide} */
1566 static final int WANTS_FOCUS = 0x00000001;
1567 /** {@hide} */
1568 static final int FOCUSED = 0x00000002;
1569 /** {@hide} */
1570 static final int SELECTED = 0x00000004;
1571 /** {@hide} */
1572 static final int IS_ROOT_NAMESPACE = 0x00000008;
1573 /** {@hide} */
1574 static final int HAS_BOUNDS = 0x00000010;
1575 /** {@hide} */
1576 static final int DRAWN = 0x00000020;
1577 /**
1578 * When this flag is set, this view is running an animation on behalf of its
1579 * children and should therefore not cancel invalidate requests, even if they
1580 * lie outside of this view's bounds.
1581 *
1582 * {@hide}
1583 */
1584 static final int DRAW_ANIMATION = 0x00000040;
1585 /** {@hide} */
1586 static final int SKIP_DRAW = 0x00000080;
1587 /** {@hide} */
1588 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1589 /** {@hide} */
1590 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1591 /** {@hide} */
1592 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1593 /** {@hide} */
1594 static final int MEASURED_DIMENSION_SET = 0x00000800;
1595 /** {@hide} */
1596 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001597 /** {@hide} */
1598 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599
1600 private static final int PRESSED = 0x00004000;
1601
1602 /** {@hide} */
1603 static final int DRAWING_CACHE_VALID = 0x00008000;
1604 /**
1605 * Flag used to indicate that this view should be drawn once more (and only once
1606 * more) after its animation has completed.
1607 * {@hide}
1608 */
1609 static final int ANIMATION_STARTED = 0x00010000;
1610
1611 private static final int SAVE_STATE_CALLED = 0x00020000;
1612
1613 /**
1614 * Indicates that the View returned true when onSetAlpha() was called and that
1615 * the alpha must be restored.
1616 * {@hide}
1617 */
1618 static final int ALPHA_SET = 0x00040000;
1619
1620 /**
1621 * Set by {@link #setScrollContainer(boolean)}.
1622 */
1623 static final int SCROLL_CONTAINER = 0x00080000;
1624
1625 /**
1626 * Set by {@link #setScrollContainer(boolean)}.
1627 */
1628 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1629
1630 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001631 * View flag indicating whether this view was invalidated (fully or partially.)
1632 *
1633 * @hide
1634 */
1635 static final int DIRTY = 0x00200000;
1636
1637 /**
1638 * View flag indicating whether this view was invalidated by an opaque
1639 * invalidate request.
1640 *
1641 * @hide
1642 */
1643 static final int DIRTY_OPAQUE = 0x00400000;
1644
1645 /**
1646 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1647 *
1648 * @hide
1649 */
1650 static final int DIRTY_MASK = 0x00600000;
1651
1652 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001653 * Indicates whether the background is opaque.
1654 *
1655 * @hide
1656 */
1657 static final int OPAQUE_BACKGROUND = 0x00800000;
1658
1659 /**
1660 * Indicates whether the scrollbars are opaque.
1661 *
1662 * @hide
1663 */
1664 static final int OPAQUE_SCROLLBARS = 0x01000000;
1665
1666 /**
1667 * Indicates whether the view is opaque.
1668 *
1669 * @hide
1670 */
1671 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001672
Adam Powelle14579b2009-12-16 18:39:52 -08001673 /**
1674 * Indicates a prepressed state;
1675 * the short time between ACTION_DOWN and recognizing
1676 * a 'real' press. Prepressed is used to recognize quick taps
1677 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001678 *
Adam Powelle14579b2009-12-16 18:39:52 -08001679 * @hide
1680 */
1681 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001682
Adam Powellc9fbaab2010-02-16 17:16:19 -08001683 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001684 * Indicates whether the view is temporarily detached.
1685 *
1686 * @hide
1687 */
1688 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001689
Adam Powell8568c3a2010-04-19 14:26:11 -07001690 /**
1691 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001692 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001693 * @hide
1694 */
1695 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001696
1697 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001698 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1699 * @hide
1700 */
1701 private static final int HOVERED = 0x10000000;
1702
1703 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001704 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1705 * for transform operations
1706 *
1707 * @hide
1708 */
Adam Powellf37df072010-09-17 16:22:49 -07001709 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001710
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001711 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001712 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001713
Chet Haasefd2b0022010-08-06 13:08:56 -07001714 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001715 * Indicates that this view was specifically invalidated, not just dirtied because some
1716 * child view was invalidated. The flag is used to determine when we need to recreate
1717 * a view's display list (as opposed to just returning a reference to its existing
1718 * display list).
1719 *
1720 * @hide
1721 */
1722 static final int INVALIDATED = 0x80000000;
1723
Christopher Tate3d4bf172011-03-28 16:16:46 -07001724 /* Masks for mPrivateFlags2 */
1725
1726 /**
1727 * Indicates that this view has reported that it can accept the current drag's content.
1728 * Cleared when the drag operation concludes.
1729 * @hide
1730 */
1731 static final int DRAG_CAN_ACCEPT = 0x00000001;
1732
1733 /**
1734 * Indicates that this view is currently directly under the drag location in a
1735 * drag-and-drop operation involving content that it can accept. Cleared when
1736 * the drag exits the view, or when the drag operation concludes.
1737 * @hide
1738 */
1739 static final int DRAG_HOVERED = 0x00000002;
1740
Cibu Johny86666632010-02-22 13:01:02 -08001741 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001742 * Indicates whether the view layout direction has been resolved and drawn to the
1743 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001744 *
1745 * @hide
1746 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001747 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1748
1749 /**
1750 * Indicates whether the view layout direction has been resolved.
1751 *
1752 * @hide
1753 */
1754 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1755
Cibu Johny86666632010-02-22 13:01:02 -08001756
Adam Powell539ee872012-02-03 19:00:49 -08001757 /**
1758 * Indicates that the view is tracking some sort of transient state
1759 * that the app should not need to be aware of, but that the framework
1760 * should take special care to preserve.
1761 *
1762 * @hide
1763 */
1764 static final int HAS_TRANSIENT_STATE = 0x00000010;
1765
1766
Christopher Tate3d4bf172011-03-28 16:16:46 -07001767 /* End of masks for mPrivateFlags2 */
1768
1769 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1770
Chet Haasedaf98e92011-01-10 14:10:36 -08001771 /**
Adam Powell637d3372010-08-25 14:37:03 -07001772 * Always allow a user to over-scroll this view, provided it is a
1773 * view that can scroll.
1774 *
1775 * @see #getOverScrollMode()
1776 * @see #setOverScrollMode(int)
1777 */
1778 public static final int OVER_SCROLL_ALWAYS = 0;
1779
1780 /**
1781 * Allow a user to over-scroll this view only if the content is large
1782 * enough to meaningfully scroll, provided it is a view that can scroll.
1783 *
1784 * @see #getOverScrollMode()
1785 * @see #setOverScrollMode(int)
1786 */
1787 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1788
1789 /**
1790 * Never allow a user to over-scroll this view.
1791 *
1792 * @see #getOverScrollMode()
1793 * @see #setOverScrollMode(int)
1794 */
1795 public static final int OVER_SCROLL_NEVER = 2;
1796
1797 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001798 * View has requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08001799 *
Joe Malin32736f02011-01-19 16:14:20 -08001800 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001801 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001802 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08001803
1804 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001805 * View has requested the system UI to enter an unobtrusive "low profile" mode.
1806 *
1807 * This is for use in games, book readers, video players, or any other "immersive" application
1808 * where the usual system chrome is deemed too distracting.
1809 *
1810 * In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08001811 *
Joe Malin32736f02011-01-19 16:14:20 -08001812 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001813 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001814 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
1815
1816 /**
1817 * View has requested that the system navigation be temporarily hidden.
1818 *
1819 * This is an even less obtrusive state than that called for by
1820 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
1821 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
1822 * those to disappear. This is useful (in conjunction with the
1823 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
1824 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
1825 * window flags) for displaying content using every last pixel on the display.
1826 *
1827 * There is a limitation: because navigation controls are so important, the least user
1828 * interaction will cause them to reappear immediately.
1829 *
1830 * @see #setSystemUiVisibility(int)
1831 */
1832 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
1833
1834 /**
1835 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
1836 */
1837 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
1838
1839 /**
1840 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
1841 */
1842 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08001843
1844 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001845 * @hide
1846 *
1847 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1848 * out of the public fields to keep the undefined bits out of the developer's way.
1849 *
1850 * Flag to make the status bar not expandable. Unless you also
1851 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1852 */
1853 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1854
1855 /**
1856 * @hide
1857 *
1858 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1859 * out of the public fields to keep the undefined bits out of the developer's way.
1860 *
1861 * Flag to hide notification icons and scrolling ticker text.
1862 */
1863 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1864
1865 /**
1866 * @hide
1867 *
1868 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1869 * out of the public fields to keep the undefined bits out of the developer's way.
1870 *
1871 * Flag to disable incoming notification alerts. This will not block
1872 * icons, but it will block sound, vibrating and other visual or aural notifications.
1873 */
1874 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1875
1876 /**
1877 * @hide
1878 *
1879 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1880 * out of the public fields to keep the undefined bits out of the developer's way.
1881 *
1882 * Flag to hide only the scrolling ticker. Note that
1883 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1884 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1885 */
1886 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1887
1888 /**
1889 * @hide
1890 *
1891 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1892 * out of the public fields to keep the undefined bits out of the developer's way.
1893 *
1894 * Flag to hide the center system info area.
1895 */
1896 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1897
1898 /**
1899 * @hide
1900 *
1901 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1902 * out of the public fields to keep the undefined bits out of the developer's way.
1903 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001904 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001905 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1906 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04001907 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001908
1909 /**
1910 * @hide
1911 *
1912 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1913 * out of the public fields to keep the undefined bits out of the developer's way.
1914 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001915 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08001916 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1917 */
1918 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1919
1920 /**
1921 * @hide
1922 *
1923 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1924 * out of the public fields to keep the undefined bits out of the developer's way.
1925 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001926 * Flag to hide only the clock. You might use this if your activity has
1927 * its own clock making the status bar's clock redundant.
1928 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001929 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1930
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001931 /**
1932 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04001933 *
1934 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1935 * out of the public fields to keep the undefined bits out of the developer's way.
1936 *
1937 * Flag to hide only the recent apps button. Don't use this
1938 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1939 */
1940 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
1941
1942 /**
1943 * @hide
1944 *
1945 * NOTE: This flag may only be used in subtreeSystemUiVisibility, etc. etc.
1946 *
1947 * This hides HOME and RECENT and is provided for compatibility with interim implementations.
1948 */
1949 @Deprecated
1950 public static final int STATUS_BAR_DISABLE_NAVIGATION =
1951 STATUS_BAR_DISABLE_HOME | STATUS_BAR_DISABLE_RECENT;
1952
1953 /**
1954 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001955 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001956 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001957
1958 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001959 * These are the system UI flags that can be cleared by events outside
1960 * of an application. Currently this is just the ability to tap on the
1961 * screen while hiding the navigation bar to have it return.
1962 * @hide
1963 */
1964 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
1965 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION;
1966
1967 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001968 * Find views that render the specified text.
1969 *
1970 * @see #findViewsWithText(ArrayList, CharSequence, int)
1971 */
1972 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
1973
1974 /**
1975 * Find find views that contain the specified content description.
1976 *
1977 * @see #findViewsWithText(ArrayList, CharSequence, int)
1978 */
1979 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
1980
1981 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07001982 * Find views that contain {@link AccessibilityNodeProvider}. Such
1983 * a View is a root of virtual view hierarchy and may contain the searched
1984 * text. If this flag is set Views with providers are automatically
1985 * added and it is a responsibility of the client to call the APIs of
1986 * the provider to determine whether the virtual tree rooted at this View
1987 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
1988 * represeting the virtual views with this text.
1989 *
1990 * @see #findViewsWithText(ArrayList, CharSequence, int)
1991 *
1992 * @hide
1993 */
1994 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
1995
1996 /**
Romain Guybb9908b2012-03-08 11:14:07 -08001997 * Indicates that the screen has changed state and is now off.
1998 *
1999 * @see #onScreenStateChanged(int)
2000 */
2001 public static final int SCREEN_STATE_OFF = 0x0;
2002
2003 /**
2004 * Indicates that the screen has changed state and is now on.
2005 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002006 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002007 */
2008 public static final int SCREEN_STATE_ON = 0x1;
2009
2010 /**
Adam Powell637d3372010-08-25 14:37:03 -07002011 * Controls the over-scroll mode for this view.
2012 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2013 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2014 * and {@link #OVER_SCROLL_NEVER}.
2015 */
2016 private int mOverScrollMode;
2017
2018 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 * The parent this view is attached to.
2020 * {@hide}
2021 *
2022 * @see #getParent()
2023 */
2024 protected ViewParent mParent;
2025
2026 /**
2027 * {@hide}
2028 */
2029 AttachInfo mAttachInfo;
2030
2031 /**
2032 * {@hide}
2033 */
Romain Guy809a7f62009-05-14 15:44:42 -07002034 @ViewDebug.ExportedProperty(flagMapping = {
2035 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2036 name = "FORCE_LAYOUT"),
2037 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2038 name = "LAYOUT_REQUIRED"),
2039 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002040 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002041 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2042 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2043 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2044 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2045 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002047 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048
2049 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002050 * This view's request for the visibility of the status bar.
2051 * @hide
2052 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002053 @ViewDebug.ExportedProperty(flagMapping = {
2054 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2055 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2056 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2057 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2058 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2059 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2060 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2061 equals = SYSTEM_UI_FLAG_VISIBLE,
2062 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2063 })
Joe Onorato664644d2011-01-23 17:53:23 -08002064 int mSystemUiVisibility;
2065
2066 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 * Count of how many windows this view has been attached to.
2068 */
2069 int mWindowAttachCount;
2070
2071 /**
2072 * The layout parameters associated with this view and used by the parent
2073 * {@link android.view.ViewGroup} to determine how this view should be
2074 * laid out.
2075 * {@hide}
2076 */
2077 protected ViewGroup.LayoutParams mLayoutParams;
2078
2079 /**
2080 * The view flags hold various views states.
2081 * {@hide}
2082 */
2083 @ViewDebug.ExportedProperty
2084 int mViewFlags;
2085
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002086 static class TransformationInfo {
2087 /**
2088 * The transform matrix for the View. This transform is calculated internally
2089 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2090 * is used by default. Do *not* use this variable directly; instead call
2091 * getMatrix(), which will automatically recalculate the matrix if necessary
2092 * to get the correct matrix based on the latest rotation and scale properties.
2093 */
2094 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002095
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002096 /**
2097 * The transform matrix for the View. This transform is calculated internally
2098 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2099 * is used by default. Do *not* use this variable directly; instead call
2100 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2101 * to get the correct matrix based on the latest rotation and scale properties.
2102 */
2103 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002104
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002105 /**
2106 * An internal variable that tracks whether we need to recalculate the
2107 * transform matrix, based on whether the rotation or scaleX/Y properties
2108 * have changed since the matrix was last calculated.
2109 */
2110 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002111
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002112 /**
2113 * An internal variable that tracks whether we need to recalculate the
2114 * transform matrix, based on whether the rotation or scaleX/Y properties
2115 * have changed since the matrix was last calculated.
2116 */
2117 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002118
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002119 /**
2120 * A variable that tracks whether we need to recalculate the
2121 * transform matrix, based on whether the rotation or scaleX/Y properties
2122 * have changed since the matrix was last calculated. This variable
2123 * is only valid after a call to updateMatrix() or to a function that
2124 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2125 */
2126 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002127
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002128 /**
2129 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2130 */
2131 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002132
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002133 /**
2134 * This matrix is used when computing the matrix for 3D rotations.
2135 */
2136 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002137
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002138 /**
2139 * These prev values are used to recalculate a centered pivot point when necessary. The
2140 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2141 * set), so thes values are only used then as well.
2142 */
2143 private int mPrevWidth = -1;
2144 private int mPrevHeight = -1;
2145
2146 /**
2147 * The degrees rotation around the vertical axis through the pivot point.
2148 */
2149 @ViewDebug.ExportedProperty
2150 float mRotationY = 0f;
2151
2152 /**
2153 * The degrees rotation around the horizontal axis through the pivot point.
2154 */
2155 @ViewDebug.ExportedProperty
2156 float mRotationX = 0f;
2157
2158 /**
2159 * The degrees rotation around the pivot point.
2160 */
2161 @ViewDebug.ExportedProperty
2162 float mRotation = 0f;
2163
2164 /**
2165 * The amount of translation of the object away from its left property (post-layout).
2166 */
2167 @ViewDebug.ExportedProperty
2168 float mTranslationX = 0f;
2169
2170 /**
2171 * The amount of translation of the object away from its top property (post-layout).
2172 */
2173 @ViewDebug.ExportedProperty
2174 float mTranslationY = 0f;
2175
2176 /**
2177 * The amount of scale in the x direction around the pivot point. A
2178 * value of 1 means no scaling is applied.
2179 */
2180 @ViewDebug.ExportedProperty
2181 float mScaleX = 1f;
2182
2183 /**
2184 * The amount of scale in the y direction around the pivot point. A
2185 * value of 1 means no scaling is applied.
2186 */
2187 @ViewDebug.ExportedProperty
2188 float mScaleY = 1f;
2189
2190 /**
Chet Haasea33de552012-02-03 16:28:24 -08002191 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002192 */
2193 @ViewDebug.ExportedProperty
2194 float mPivotX = 0f;
2195
2196 /**
Chet Haasea33de552012-02-03 16:28:24 -08002197 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002198 */
2199 @ViewDebug.ExportedProperty
2200 float mPivotY = 0f;
2201
2202 /**
2203 * The opacity of the View. This is a value from 0 to 1, where 0 means
2204 * completely transparent and 1 means completely opaque.
2205 */
2206 @ViewDebug.ExportedProperty
2207 float mAlpha = 1f;
2208 }
2209
2210 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002211
Joe Malin32736f02011-01-19 16:14:20 -08002212 private boolean mLastIsOpaque;
2213
Chet Haasefd2b0022010-08-06 13:08:56 -07002214 /**
2215 * Convenience value to check for float values that are close enough to zero to be considered
2216 * zero.
2217 */
Romain Guy2542d192010-08-18 11:47:12 -07002218 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002219
2220 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 * The distance in pixels from the left edge of this view's parent
2222 * to the left edge of this view.
2223 * {@hide}
2224 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002225 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 protected int mLeft;
2227 /**
2228 * The distance in pixels from the left edge of this view's parent
2229 * to the right edge of this view.
2230 * {@hide}
2231 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002232 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 protected int mRight;
2234 /**
2235 * The distance in pixels from the top edge of this view's parent
2236 * to the top edge of this view.
2237 * {@hide}
2238 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002239 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 protected int mTop;
2241 /**
2242 * The distance in pixels from the top edge of this view's parent
2243 * to the bottom edge of this view.
2244 * {@hide}
2245 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002246 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 protected int mBottom;
2248
2249 /**
2250 * The offset, in pixels, by which the content of this view is scrolled
2251 * horizontally.
2252 * {@hide}
2253 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002254 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 protected int mScrollX;
2256 /**
2257 * The offset, in pixels, by which the content of this view is scrolled
2258 * vertically.
2259 * {@hide}
2260 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002261 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 protected int mScrollY;
2263
2264 /**
2265 * The left padding in pixels, that is the distance in pixels between the
2266 * left edge of this view and the left edge of its content.
2267 * {@hide}
2268 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002269 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 protected int mPaddingLeft;
2271 /**
2272 * The right padding in pixels, that is the distance in pixels between the
2273 * right edge of this view and the right edge of its content.
2274 * {@hide}
2275 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002276 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 protected int mPaddingRight;
2278 /**
2279 * The top padding in pixels, that is the distance in pixels between the
2280 * top edge of this view and the top edge of its content.
2281 * {@hide}
2282 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002283 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 protected int mPaddingTop;
2285 /**
2286 * The bottom padding in pixels, that is the distance in pixels between the
2287 * bottom edge of this view and the bottom edge of its content.
2288 * {@hide}
2289 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002290 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 protected int mPaddingBottom;
2292
2293 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002294 * Briefly describes the view and is primarily used for accessibility support.
2295 */
2296 private CharSequence mContentDescription;
2297
2298 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002300 *
2301 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002302 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002303 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002304 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305
2306 /**
2307 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002308 *
2309 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002311 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002312 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002314 /**
Adam Powell20232d02010-12-08 21:08:53 -08002315 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002316 *
2317 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002318 */
2319 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002320 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002321
2322 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002323 * Cache if the user padding is relative.
2324 *
2325 */
2326 @ViewDebug.ExportedProperty(category = "padding")
2327 boolean mUserPaddingRelative;
2328
2329 /**
2330 * Cache the paddingStart set by the user to append to the scrollbar's size.
2331 *
2332 */
2333 @ViewDebug.ExportedProperty(category = "padding")
2334 int mUserPaddingStart;
2335
2336 /**
2337 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2338 *
2339 */
2340 @ViewDebug.ExportedProperty(category = "padding")
2341 int mUserPaddingEnd;
2342
2343 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002344 * @hide
2345 */
2346 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2347 /**
2348 * @hide
2349 */
2350 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 private Drawable mBGDrawable;
2353
2354 private int mBackgroundResource;
2355 private boolean mBackgroundSizeChanged;
2356
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002357 static class ListenerInfo {
2358 /**
2359 * Listener used to dispatch focus change events.
2360 * This field should be made private, so it is hidden from the SDK.
2361 * {@hide}
2362 */
2363 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002365 /**
2366 * Listeners for layout change events.
2367 */
2368 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002369
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002370 /**
2371 * Listeners for attach events.
2372 */
2373 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002374
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002375 /**
2376 * Listener used to dispatch click events.
2377 * This field should be made private, so it is hidden from the SDK.
2378 * {@hide}
2379 */
2380 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002382 /**
2383 * Listener used to dispatch long click events.
2384 * This field should be made private, so it is hidden from the SDK.
2385 * {@hide}
2386 */
2387 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002389 /**
2390 * Listener used to build the context menu.
2391 * This field should be made private, so it is hidden from the SDK.
2392 * {@hide}
2393 */
2394 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002396 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002398 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002400 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002401
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002402 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002403
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002404 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002405
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002406 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2407 }
2408
2409 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 /**
2412 * The application environment this view lives in.
2413 * This field should be made private, so it is hidden from the SDK.
2414 * {@hide}
2415 */
2416 protected Context mContext;
2417
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002418 private final Resources mResources;
2419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 private ScrollabilityCache mScrollCache;
2421
2422 private int[] mDrawableState = null;
2423
Romain Guy0211a0a2011-02-14 16:34:59 -08002424 /**
2425 * Set to true when drawing cache is enabled and cannot be created.
2426 *
2427 * @hide
2428 */
2429 public boolean mCachingFailed;
2430
Romain Guy02890fd2010-08-06 17:58:44 -07002431 private Bitmap mDrawingCache;
2432 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002433 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002434 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435
2436 /**
2437 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2438 * the user may specify which view to go to next.
2439 */
2440 private int mNextFocusLeftId = View.NO_ID;
2441
2442 /**
2443 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2444 * the user may specify which view to go to next.
2445 */
2446 private int mNextFocusRightId = View.NO_ID;
2447
2448 /**
2449 * When this view has focus and the next focus is {@link #FOCUS_UP},
2450 * the user may specify which view to go to next.
2451 */
2452 private int mNextFocusUpId = View.NO_ID;
2453
2454 /**
2455 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2456 * the user may specify which view to go to next.
2457 */
2458 private int mNextFocusDownId = View.NO_ID;
2459
Jeff Brown4e6319b2010-12-13 10:36:51 -08002460 /**
2461 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2462 * the user may specify which view to go to next.
2463 */
2464 int mNextFocusForwardId = View.NO_ID;
2465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002467 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002468 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002469 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 private UnsetPressedState mUnsetPressedState;
2472
2473 /**
2474 * Whether the long press's action has been invoked. The tap's action is invoked on the
2475 * up event while a long press is invoked as soon as the long press duration is reached, so
2476 * a long press could be performed before the tap is checked, in which case the tap's action
2477 * should not be invoked.
2478 */
2479 private boolean mHasPerformedLongPress;
2480
2481 /**
2482 * The minimum height of the view. We'll try our best to have the height
2483 * of this view to at least this amount.
2484 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002485 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 private int mMinHeight;
2487
2488 /**
2489 * The minimum width of the view. We'll try our best to have the width
2490 * of this view to at least this amount.
2491 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002492 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 private int mMinWidth;
2494
2495 /**
2496 * The delegate to handle touch events that are physically in this view
2497 * but should be handled by another view.
2498 */
2499 private TouchDelegate mTouchDelegate = null;
2500
2501 /**
2502 * Solid color to use as a background when creating the drawing cache. Enables
2503 * the cache to use 16 bit bitmaps instead of 32 bit.
2504 */
2505 private int mDrawingCacheBackgroundColor = 0;
2506
2507 /**
2508 * Special tree observer used when mAttachInfo is null.
2509 */
2510 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002511
Adam Powelle14579b2009-12-16 18:39:52 -08002512 /**
2513 * Cache the touch slop from the context that created the view.
2514 */
2515 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002518 * Object that handles automatic animation of view properties.
2519 */
2520 private ViewPropertyAnimator mAnimator = null;
2521
2522 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002523 * Flag indicating that a drag can cross window boundaries. When
2524 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2525 * with this flag set, all visible applications will be able to participate
2526 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002527 *
2528 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002529 */
2530 public static final int DRAG_FLAG_GLOBAL = 1;
2531
2532 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002533 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2534 */
2535 private float mVerticalScrollFactor;
2536
2537 /**
Adam Powell20232d02010-12-08 21:08:53 -08002538 * Position of the vertical scroll bar.
2539 */
2540 private int mVerticalScrollbarPosition;
2541
2542 /**
2543 * Position the scroll bar at the default position as determined by the system.
2544 */
2545 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2546
2547 /**
2548 * Position the scroll bar along the left edge.
2549 */
2550 public static final int SCROLLBAR_POSITION_LEFT = 1;
2551
2552 /**
2553 * Position the scroll bar along the right edge.
2554 */
2555 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2556
2557 /**
Romain Guy171c5922011-01-06 10:04:23 -08002558 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002559 *
2560 * @see #getLayerType()
2561 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002562 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002563 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002564 */
2565 public static final int LAYER_TYPE_NONE = 0;
2566
2567 /**
2568 * <p>Indicates that the view has a software layer. A software layer is backed
2569 * by a bitmap and causes the view to be rendered using Android's software
2570 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002571 *
Romain Guy171c5922011-01-06 10:04:23 -08002572 * <p>Software layers have various usages:</p>
2573 * <p>When the application is not using hardware acceleration, a software layer
2574 * is useful to apply a specific color filter and/or blending mode and/or
2575 * translucency to a view and all its children.</p>
2576 * <p>When the application is using hardware acceleration, a software layer
2577 * is useful to render drawing primitives not supported by the hardware
2578 * accelerated pipeline. It can also be used to cache a complex view tree
2579 * into a texture and reduce the complexity of drawing operations. For instance,
2580 * when animating a complex view tree with a translation, a software layer can
2581 * be used to render the view tree only once.</p>
2582 * <p>Software layers should be avoided when the affected view tree updates
2583 * often. Every update will require to re-render the software layer, which can
2584 * potentially be slow (particularly when hardware acceleration is turned on
2585 * since the layer will have to be uploaded into a hardware texture after every
2586 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002587 *
2588 * @see #getLayerType()
2589 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002590 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002591 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002592 */
2593 public static final int LAYER_TYPE_SOFTWARE = 1;
2594
2595 /**
2596 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2597 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2598 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2599 * rendering pipeline, but only if hardware acceleration is turned on for the
2600 * view hierarchy. When hardware acceleration is turned off, hardware layers
2601 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002602 *
Romain Guy171c5922011-01-06 10:04:23 -08002603 * <p>A hardware layer is useful to apply a specific color filter and/or
2604 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002605 * <p>A hardware layer can be used to cache a complex view tree into a
2606 * texture and reduce the complexity of drawing operations. For instance,
2607 * when animating a complex view tree with a translation, a hardware layer can
2608 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002609 * <p>A hardware layer can also be used to increase the rendering quality when
2610 * rotation transformations are applied on a view. It can also be used to
2611 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002612 *
2613 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002614 * @see #setLayerType(int, android.graphics.Paint)
2615 * @see #LAYER_TYPE_NONE
2616 * @see #LAYER_TYPE_SOFTWARE
2617 */
2618 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002619
Romain Guy3aaff3a2011-01-12 14:18:47 -08002620 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2621 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2622 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2623 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2624 })
Romain Guy171c5922011-01-06 10:04:23 -08002625 int mLayerType = LAYER_TYPE_NONE;
2626 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002627 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002628
2629 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002630 * Set to true when the view is sending hover accessibility events because it
2631 * is the innermost hovered view.
2632 */
2633 private boolean mSendingHoverAccessibilityEvents;
2634
2635 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002636 * Delegate for injecting accessiblity functionality.
2637 */
2638 AccessibilityDelegate mAccessibilityDelegate;
2639
2640 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002641 * Text direction is inherited thru {@link ViewGroup}
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002642 */
2643 public static final int TEXT_DIRECTION_INHERIT = 0;
2644
2645 /**
2646 * Text direction is using "first strong algorithm". The first strong directional character
2647 * determines the paragraph direction. If there is no strong directional character, the
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002648 * paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002649 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002650 */
2651 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2652
2653 /**
2654 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2655 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002656 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002657 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002658 */
2659 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2660
2661 /**
2662 * Text direction is forced to LTR.
2663 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002664 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002665 public static final int TEXT_DIRECTION_LTR = 3;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002666
2667 /**
2668 * Text direction is forced to RTL.
2669 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002670 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002671 public static final int TEXT_DIRECTION_RTL = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002672
2673 /**
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002674 * Text direction is coming from the system Locale.
2675 *
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002676 */
2677 public static final int TEXT_DIRECTION_LOCALE = 5;
2678
2679 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002680 * Default text direction is inherited
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07002681 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002682 */
2683 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2684
2685 /**
2686 * The text direction that has been defined by {@link #setTextDirection(int)}.
2687 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002688 */
2689 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002690 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2691 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2692 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2693 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002694 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
2695 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002696 })
Doug Feltcb3791202011-07-07 11:57:48 -07002697 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002698
2699 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002700 * The resolved text direction. This needs resolution if the value is
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002701 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if it is
Doug Feltcb3791202011-07-07 11:57:48 -07002702 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2703 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002704 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002705 */
2706 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002707 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2708 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2709 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002710 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002711 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
2712 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002713 })
Doug Feltcb3791202011-07-07 11:57:48 -07002714 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002715
2716 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002717 * Consistency verifier for debugging purposes.
2718 * @hide
2719 */
2720 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2721 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2722 new InputEventConsistencyVerifier(this, 0) : null;
2723
2724 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 * Simple constructor to use when creating a view from code.
2726 *
2727 * @param context The Context the view is running in, through which it can
2728 * access the current theme, resources, etc.
2729 */
2730 public View(Context context) {
2731 mContext = context;
2732 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002733 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002734 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002735 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002736 mUserPaddingStart = -1;
2737 mUserPaddingEnd = -1;
2738 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 }
2740
2741 /**
2742 * Constructor that is called when inflating a view from XML. This is called
2743 * when a view is being constructed from an XML file, supplying attributes
2744 * that were specified in the XML file. This version uses a default style of
2745 * 0, so the only attribute values applied are those in the Context's Theme
2746 * and the given AttributeSet.
2747 *
2748 * <p>
2749 * The method onFinishInflate() will be called after all children have been
2750 * added.
2751 *
2752 * @param context The Context the view is running in, through which it can
2753 * access the current theme, resources, etc.
2754 * @param attrs The attributes of the XML tag that is inflating the view.
2755 * @see #View(Context, AttributeSet, int)
2756 */
2757 public View(Context context, AttributeSet attrs) {
2758 this(context, attrs, 0);
2759 }
2760
2761 /**
2762 * Perform inflation from XML and apply a class-specific base style. This
2763 * constructor of View allows subclasses to use their own base style when
2764 * they are inflating. For example, a Button class's constructor would call
2765 * this version of the super class constructor and supply
2766 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2767 * the theme's button style to modify all of the base view attributes (in
2768 * particular its background) as well as the Button class's attributes.
2769 *
2770 * @param context The Context the view is running in, through which it can
2771 * access the current theme, resources, etc.
2772 * @param attrs The attributes of the XML tag that is inflating the view.
2773 * @param defStyle The default style to apply to this view. If 0, no style
2774 * will be applied (beyond what is included in the theme). This may
2775 * either be an attribute resource, whose value will be retrieved
2776 * from the current theme, or an explicit style resource.
2777 * @see #View(Context, AttributeSet)
2778 */
2779 public View(Context context, AttributeSet attrs, int defStyle) {
2780 this(context);
2781
2782 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2783 defStyle, 0);
2784
2785 Drawable background = null;
2786
2787 int leftPadding = -1;
2788 int topPadding = -1;
2789 int rightPadding = -1;
2790 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002791 int startPadding = -1;
2792 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793
2794 int padding = -1;
2795
2796 int viewFlagValues = 0;
2797 int viewFlagMasks = 0;
2798
2799 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 int x = 0;
2802 int y = 0;
2803
Chet Haase73066682010-11-29 15:55:32 -08002804 float tx = 0;
2805 float ty = 0;
2806 float rotation = 0;
2807 float rotationX = 0;
2808 float rotationY = 0;
2809 float sx = 1f;
2810 float sy = 1f;
2811 boolean transformSet = false;
2812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2814
Adam Powell637d3372010-08-25 14:37:03 -07002815 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 final int N = a.getIndexCount();
2817 for (int i = 0; i < N; i++) {
2818 int attr = a.getIndex(i);
2819 switch (attr) {
2820 case com.android.internal.R.styleable.View_background:
2821 background = a.getDrawable(attr);
2822 break;
2823 case com.android.internal.R.styleable.View_padding:
2824 padding = a.getDimensionPixelSize(attr, -1);
2825 break;
2826 case com.android.internal.R.styleable.View_paddingLeft:
2827 leftPadding = a.getDimensionPixelSize(attr, -1);
2828 break;
2829 case com.android.internal.R.styleable.View_paddingTop:
2830 topPadding = a.getDimensionPixelSize(attr, -1);
2831 break;
2832 case com.android.internal.R.styleable.View_paddingRight:
2833 rightPadding = a.getDimensionPixelSize(attr, -1);
2834 break;
2835 case com.android.internal.R.styleable.View_paddingBottom:
2836 bottomPadding = a.getDimensionPixelSize(attr, -1);
2837 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002838 case com.android.internal.R.styleable.View_paddingStart:
2839 startPadding = a.getDimensionPixelSize(attr, -1);
2840 break;
2841 case com.android.internal.R.styleable.View_paddingEnd:
2842 endPadding = a.getDimensionPixelSize(attr, -1);
2843 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 case com.android.internal.R.styleable.View_scrollX:
2845 x = a.getDimensionPixelOffset(attr, 0);
2846 break;
2847 case com.android.internal.R.styleable.View_scrollY:
2848 y = a.getDimensionPixelOffset(attr, 0);
2849 break;
Chet Haase73066682010-11-29 15:55:32 -08002850 case com.android.internal.R.styleable.View_alpha:
2851 setAlpha(a.getFloat(attr, 1f));
2852 break;
2853 case com.android.internal.R.styleable.View_transformPivotX:
2854 setPivotX(a.getDimensionPixelOffset(attr, 0));
2855 break;
2856 case com.android.internal.R.styleable.View_transformPivotY:
2857 setPivotY(a.getDimensionPixelOffset(attr, 0));
2858 break;
2859 case com.android.internal.R.styleable.View_translationX:
2860 tx = a.getDimensionPixelOffset(attr, 0);
2861 transformSet = true;
2862 break;
2863 case com.android.internal.R.styleable.View_translationY:
2864 ty = a.getDimensionPixelOffset(attr, 0);
2865 transformSet = true;
2866 break;
2867 case com.android.internal.R.styleable.View_rotation:
2868 rotation = a.getFloat(attr, 0);
2869 transformSet = true;
2870 break;
2871 case com.android.internal.R.styleable.View_rotationX:
2872 rotationX = a.getFloat(attr, 0);
2873 transformSet = true;
2874 break;
2875 case com.android.internal.R.styleable.View_rotationY:
2876 rotationY = a.getFloat(attr, 0);
2877 transformSet = true;
2878 break;
2879 case com.android.internal.R.styleable.View_scaleX:
2880 sx = a.getFloat(attr, 1f);
2881 transformSet = true;
2882 break;
2883 case com.android.internal.R.styleable.View_scaleY:
2884 sy = a.getFloat(attr, 1f);
2885 transformSet = true;
2886 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 case com.android.internal.R.styleable.View_id:
2888 mID = a.getResourceId(attr, NO_ID);
2889 break;
2890 case com.android.internal.R.styleable.View_tag:
2891 mTag = a.getText(attr);
2892 break;
2893 case com.android.internal.R.styleable.View_fitsSystemWindows:
2894 if (a.getBoolean(attr, false)) {
2895 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2896 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2897 }
2898 break;
2899 case com.android.internal.R.styleable.View_focusable:
2900 if (a.getBoolean(attr, false)) {
2901 viewFlagValues |= FOCUSABLE;
2902 viewFlagMasks |= FOCUSABLE_MASK;
2903 }
2904 break;
2905 case com.android.internal.R.styleable.View_focusableInTouchMode:
2906 if (a.getBoolean(attr, false)) {
2907 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2908 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2909 }
2910 break;
2911 case com.android.internal.R.styleable.View_clickable:
2912 if (a.getBoolean(attr, false)) {
2913 viewFlagValues |= CLICKABLE;
2914 viewFlagMasks |= CLICKABLE;
2915 }
2916 break;
2917 case com.android.internal.R.styleable.View_longClickable:
2918 if (a.getBoolean(attr, false)) {
2919 viewFlagValues |= LONG_CLICKABLE;
2920 viewFlagMasks |= LONG_CLICKABLE;
2921 }
2922 break;
2923 case com.android.internal.R.styleable.View_saveEnabled:
2924 if (!a.getBoolean(attr, true)) {
2925 viewFlagValues |= SAVE_DISABLED;
2926 viewFlagMasks |= SAVE_DISABLED_MASK;
2927 }
2928 break;
2929 case com.android.internal.R.styleable.View_duplicateParentState:
2930 if (a.getBoolean(attr, false)) {
2931 viewFlagValues |= DUPLICATE_PARENT_STATE;
2932 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2933 }
2934 break;
2935 case com.android.internal.R.styleable.View_visibility:
2936 final int visibility = a.getInt(attr, 0);
2937 if (visibility != 0) {
2938 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2939 viewFlagMasks |= VISIBILITY_MASK;
2940 }
2941 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002942 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002943 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002944 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002945 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002946 final int layoutDirection = a.getInt(attr, -1);
2947 if (layoutDirection != -1) {
2948 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002949 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002950 // Set to default (LAYOUT_DIRECTION_INHERIT)
2951 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002952 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002953 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002954 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955 case com.android.internal.R.styleable.View_drawingCacheQuality:
2956 final int cacheQuality = a.getInt(attr, 0);
2957 if (cacheQuality != 0) {
2958 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2959 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2960 }
2961 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002962 case com.android.internal.R.styleable.View_contentDescription:
2963 mContentDescription = a.getString(attr);
2964 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2966 if (!a.getBoolean(attr, true)) {
2967 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2968 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2969 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002970 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2972 if (!a.getBoolean(attr, true)) {
2973 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2974 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2975 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002976 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 case R.styleable.View_scrollbars:
2978 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2979 if (scrollbars != SCROLLBARS_NONE) {
2980 viewFlagValues |= scrollbars;
2981 viewFlagMasks |= SCROLLBARS_MASK;
2982 initializeScrollbars(a);
2983 }
2984 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07002985 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07002987 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
2988 // Ignore the attribute starting with ICS
2989 break;
2990 }
2991 // With builds < ICS, fall through and apply fading edges
2992 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2994 if (fadingEdge != FADING_EDGE_NONE) {
2995 viewFlagValues |= fadingEdge;
2996 viewFlagMasks |= FADING_EDGE_MASK;
2997 initializeFadingEdge(a);
2998 }
2999 break;
3000 case R.styleable.View_scrollbarStyle:
3001 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3002 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3003 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3004 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3005 }
3006 break;
3007 case R.styleable.View_isScrollContainer:
3008 setScrollContainer = true;
3009 if (a.getBoolean(attr, false)) {
3010 setScrollContainer(true);
3011 }
3012 break;
3013 case com.android.internal.R.styleable.View_keepScreenOn:
3014 if (a.getBoolean(attr, false)) {
3015 viewFlagValues |= KEEP_SCREEN_ON;
3016 viewFlagMasks |= KEEP_SCREEN_ON;
3017 }
3018 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003019 case R.styleable.View_filterTouchesWhenObscured:
3020 if (a.getBoolean(attr, false)) {
3021 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3022 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3023 }
3024 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 case R.styleable.View_nextFocusLeft:
3026 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3027 break;
3028 case R.styleable.View_nextFocusRight:
3029 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3030 break;
3031 case R.styleable.View_nextFocusUp:
3032 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3033 break;
3034 case R.styleable.View_nextFocusDown:
3035 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3036 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003037 case R.styleable.View_nextFocusForward:
3038 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3039 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 case R.styleable.View_minWidth:
3041 mMinWidth = a.getDimensionPixelSize(attr, 0);
3042 break;
3043 case R.styleable.View_minHeight:
3044 mMinHeight = a.getDimensionPixelSize(attr, 0);
3045 break;
Romain Guy9a817362009-05-01 10:57:14 -07003046 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003047 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003048 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003049 + "be used within a restricted context");
3050 }
3051
Romain Guy9a817362009-05-01 10:57:14 -07003052 final String handlerName = a.getString(attr);
3053 if (handlerName != null) {
3054 setOnClickListener(new OnClickListener() {
3055 private Method mHandler;
3056
3057 public void onClick(View v) {
3058 if (mHandler == null) {
3059 try {
3060 mHandler = getContext().getClass().getMethod(handlerName,
3061 View.class);
3062 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003063 int id = getId();
3064 String idText = id == NO_ID ? "" : " with id '"
3065 + getContext().getResources().getResourceEntryName(
3066 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003067 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003068 handlerName + "(View) in the activity "
3069 + getContext().getClass() + " for onClick handler"
3070 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003071 }
3072 }
3073
3074 try {
3075 mHandler.invoke(getContext(), View.this);
3076 } catch (IllegalAccessException e) {
3077 throw new IllegalStateException("Could not execute non "
3078 + "public method of the activity", e);
3079 } catch (InvocationTargetException e) {
3080 throw new IllegalStateException("Could not execute "
3081 + "method of the activity", e);
3082 }
3083 }
3084 });
3085 }
3086 break;
Adam Powell637d3372010-08-25 14:37:03 -07003087 case R.styleable.View_overScrollMode:
3088 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3089 break;
Adam Powell20232d02010-12-08 21:08:53 -08003090 case R.styleable.View_verticalScrollbarPosition:
3091 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3092 break;
Romain Guy171c5922011-01-06 10:04:23 -08003093 case R.styleable.View_layerType:
3094 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3095 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003096 case R.styleable.View_textDirection:
3097 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
3098 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 }
3100 }
3101
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003102 a.recycle();
3103
Adam Powell637d3372010-08-25 14:37:03 -07003104 setOverScrollMode(overScrollMode);
3105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106 if (background != null) {
3107 setBackgroundDrawable(background);
3108 }
3109
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003110 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3111 // layout direction). Those cached values will be used later during padding resolution.
3112 mUserPaddingStart = startPadding;
3113 mUserPaddingEnd = endPadding;
3114
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003115 updateUserPaddingRelative();
3116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117 if (padding >= 0) {
3118 leftPadding = padding;
3119 topPadding = padding;
3120 rightPadding = padding;
3121 bottomPadding = padding;
3122 }
3123
3124 // If the user specified the padding (either with android:padding or
3125 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3126 // use the default padding or the padding from the background drawable
3127 // (stored at this point in mPadding*)
3128 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3129 topPadding >= 0 ? topPadding : mPaddingTop,
3130 rightPadding >= 0 ? rightPadding : mPaddingRight,
3131 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3132
3133 if (viewFlagMasks != 0) {
3134 setFlags(viewFlagValues, viewFlagMasks);
3135 }
3136
3137 // Needs to be called after mViewFlags is set
3138 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3139 recomputePadding();
3140 }
3141
3142 if (x != 0 || y != 0) {
3143 scrollTo(x, y);
3144 }
3145
Chet Haase73066682010-11-29 15:55:32 -08003146 if (transformSet) {
3147 setTranslationX(tx);
3148 setTranslationY(ty);
3149 setRotation(rotation);
3150 setRotationX(rotationX);
3151 setRotationY(rotationY);
3152 setScaleX(sx);
3153 setScaleY(sy);
3154 }
3155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003156 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3157 setScrollContainer(true);
3158 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003159
3160 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 }
3162
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003163 private void updateUserPaddingRelative() {
3164 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3165 }
3166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 /**
3168 * Non-public constructor for use in testing
3169 */
3170 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003171 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 }
3173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 /**
3175 * <p>
3176 * Initializes the fading edges from a given set of styled attributes. This
3177 * method should be called by subclasses that need fading edges and when an
3178 * instance of these subclasses is created programmatically rather than
3179 * being inflated from XML. This method is automatically called when the XML
3180 * is inflated.
3181 * </p>
3182 *
3183 * @param a the styled attributes set to initialize the fading edges from
3184 */
3185 protected void initializeFadingEdge(TypedArray a) {
3186 initScrollCache();
3187
3188 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3189 R.styleable.View_fadingEdgeLength,
3190 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3191 }
3192
3193 /**
3194 * Returns the size of the vertical faded edges used to indicate that more
3195 * content in this view is visible.
3196 *
3197 * @return The size in pixels of the vertical faded edge or 0 if vertical
3198 * faded edges are not enabled for this view.
3199 * @attr ref android.R.styleable#View_fadingEdgeLength
3200 */
3201 public int getVerticalFadingEdgeLength() {
3202 if (isVerticalFadingEdgeEnabled()) {
3203 ScrollabilityCache cache = mScrollCache;
3204 if (cache != null) {
3205 return cache.fadingEdgeLength;
3206 }
3207 }
3208 return 0;
3209 }
3210
3211 /**
3212 * Set the size of the faded edge used to indicate that more content in this
3213 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003214 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3215 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3216 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 *
3218 * @param length The size in pixels of the faded edge used to indicate that more
3219 * content in this view is visible.
3220 */
3221 public void setFadingEdgeLength(int length) {
3222 initScrollCache();
3223 mScrollCache.fadingEdgeLength = length;
3224 }
3225
3226 /**
3227 * Returns the size of the horizontal faded edges used to indicate that more
3228 * content in this view is visible.
3229 *
3230 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3231 * faded edges are not enabled for this view.
3232 * @attr ref android.R.styleable#View_fadingEdgeLength
3233 */
3234 public int getHorizontalFadingEdgeLength() {
3235 if (isHorizontalFadingEdgeEnabled()) {
3236 ScrollabilityCache cache = mScrollCache;
3237 if (cache != null) {
3238 return cache.fadingEdgeLength;
3239 }
3240 }
3241 return 0;
3242 }
3243
3244 /**
3245 * Returns the width of the vertical scrollbar.
3246 *
3247 * @return The width in pixels of the vertical scrollbar or 0 if there
3248 * is no vertical scrollbar.
3249 */
3250 public int getVerticalScrollbarWidth() {
3251 ScrollabilityCache cache = mScrollCache;
3252 if (cache != null) {
3253 ScrollBarDrawable scrollBar = cache.scrollBar;
3254 if (scrollBar != null) {
3255 int size = scrollBar.getSize(true);
3256 if (size <= 0) {
3257 size = cache.scrollBarSize;
3258 }
3259 return size;
3260 }
3261 return 0;
3262 }
3263 return 0;
3264 }
3265
3266 /**
3267 * Returns the height of the horizontal scrollbar.
3268 *
3269 * @return The height in pixels of the horizontal scrollbar or 0 if
3270 * there is no horizontal scrollbar.
3271 */
3272 protected int getHorizontalScrollbarHeight() {
3273 ScrollabilityCache cache = mScrollCache;
3274 if (cache != null) {
3275 ScrollBarDrawable scrollBar = cache.scrollBar;
3276 if (scrollBar != null) {
3277 int size = scrollBar.getSize(false);
3278 if (size <= 0) {
3279 size = cache.scrollBarSize;
3280 }
3281 return size;
3282 }
3283 return 0;
3284 }
3285 return 0;
3286 }
3287
3288 /**
3289 * <p>
3290 * Initializes the scrollbars from a given set of styled attributes. This
3291 * method should be called by subclasses that need scrollbars and when an
3292 * instance of these subclasses is created programmatically rather than
3293 * being inflated from XML. This method is automatically called when the XML
3294 * is inflated.
3295 * </p>
3296 *
3297 * @param a the styled attributes set to initialize the scrollbars from
3298 */
3299 protected void initializeScrollbars(TypedArray a) {
3300 initScrollCache();
3301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003303
Mike Cleronf116bf82009-09-27 19:14:12 -07003304 if (scrollabilityCache.scrollBar == null) {
3305 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3306 }
Joe Malin32736f02011-01-19 16:14:20 -08003307
Romain Guy8bda2482010-03-02 11:42:11 -08003308 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309
Mike Cleronf116bf82009-09-27 19:14:12 -07003310 if (!fadeScrollbars) {
3311 scrollabilityCache.state = ScrollabilityCache.ON;
3312 }
3313 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003314
3315
Mike Cleronf116bf82009-09-27 19:14:12 -07003316 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3317 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3318 .getScrollBarFadeDuration());
3319 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3320 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3321 ViewConfiguration.getScrollDefaultDelay());
3322
Joe Malin32736f02011-01-19 16:14:20 -08003323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003324 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3325 com.android.internal.R.styleable.View_scrollbarSize,
3326 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3327
3328 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3329 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3330
3331 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3332 if (thumb != null) {
3333 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3334 }
3335
3336 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3337 false);
3338 if (alwaysDraw) {
3339 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3340 }
3341
3342 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3343 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3344
3345 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3346 if (thumb != null) {
3347 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3348 }
3349
3350 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3351 false);
3352 if (alwaysDraw) {
3353 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3354 }
3355
3356 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003357 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358 }
3359
3360 /**
3361 * <p>
3362 * Initalizes the scrollability cache if necessary.
3363 * </p>
3364 */
3365 private void initScrollCache() {
3366 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003367 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003368 }
3369 }
3370
3371 /**
Adam Powell20232d02010-12-08 21:08:53 -08003372 * Set the position of the vertical scroll bar. Should be one of
3373 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3374 * {@link #SCROLLBAR_POSITION_RIGHT}.
3375 *
3376 * @param position Where the vertical scroll bar should be positioned.
3377 */
3378 public void setVerticalScrollbarPosition(int position) {
3379 if (mVerticalScrollbarPosition != position) {
3380 mVerticalScrollbarPosition = position;
3381 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003382 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003383 }
3384 }
3385
3386 /**
3387 * @return The position where the vertical scroll bar will show, if applicable.
3388 * @see #setVerticalScrollbarPosition(int)
3389 */
3390 public int getVerticalScrollbarPosition() {
3391 return mVerticalScrollbarPosition;
3392 }
3393
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003394 ListenerInfo getListenerInfo() {
3395 if (mListenerInfo != null) {
3396 return mListenerInfo;
3397 }
3398 mListenerInfo = new ListenerInfo();
3399 return mListenerInfo;
3400 }
3401
Adam Powell20232d02010-12-08 21:08:53 -08003402 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003403 * Register a callback to be invoked when focus of this view changed.
3404 *
3405 * @param l The callback that will run.
3406 */
3407 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003408 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409 }
3410
3411 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003412 * Add a listener that will be called when the bounds of the view change due to
3413 * layout processing.
3414 *
3415 * @param listener The listener that will be called when layout bounds change.
3416 */
3417 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003418 ListenerInfo li = getListenerInfo();
3419 if (li.mOnLayoutChangeListeners == null) {
3420 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003421 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003422 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3423 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003424 }
Chet Haase21cd1382010-09-01 17:42:29 -07003425 }
3426
3427 /**
3428 * Remove a listener for layout changes.
3429 *
3430 * @param listener The listener for layout bounds change.
3431 */
3432 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003433 ListenerInfo li = mListenerInfo;
3434 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003435 return;
3436 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003437 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003438 }
3439
3440 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003441 * Add a listener for attach state changes.
3442 *
3443 * This listener will be called whenever this view is attached or detached
3444 * from a window. Remove the listener using
3445 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3446 *
3447 * @param listener Listener to attach
3448 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3449 */
3450 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003451 ListenerInfo li = getListenerInfo();
3452 if (li.mOnAttachStateChangeListeners == null) {
3453 li.mOnAttachStateChangeListeners
3454 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003455 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003456 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003457 }
3458
3459 /**
3460 * Remove a listener for attach state changes. The listener will receive no further
3461 * notification of window attach/detach events.
3462 *
3463 * @param listener Listener to remove
3464 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3465 */
3466 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003467 ListenerInfo li = mListenerInfo;
3468 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003469 return;
3470 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003471 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003472 }
3473
3474 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003475 * Returns the focus-change callback registered for this view.
3476 *
3477 * @return The callback, or null if one is not registered.
3478 */
3479 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003480 ListenerInfo li = mListenerInfo;
3481 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 }
3483
3484 /**
3485 * Register a callback to be invoked when this view is clicked. If this view is not
3486 * clickable, it becomes clickable.
3487 *
3488 * @param l The callback that will run
3489 *
3490 * @see #setClickable(boolean)
3491 */
3492 public void setOnClickListener(OnClickListener l) {
3493 if (!isClickable()) {
3494 setClickable(true);
3495 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003496 getListenerInfo().mOnClickListener = l;
3497 }
3498
3499 /**
3500 * Return whether this view has an attached OnClickListener. Returns
3501 * true if there is a listener, false if there is none.
3502 */
3503 public boolean hasOnClickListeners() {
3504 ListenerInfo li = mListenerInfo;
3505 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003506 }
3507
3508 /**
3509 * Register a callback to be invoked when this view is clicked and held. If this view is not
3510 * long clickable, it becomes long clickable.
3511 *
3512 * @param l The callback that will run
3513 *
3514 * @see #setLongClickable(boolean)
3515 */
3516 public void setOnLongClickListener(OnLongClickListener l) {
3517 if (!isLongClickable()) {
3518 setLongClickable(true);
3519 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003520 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003521 }
3522
3523 /**
3524 * Register a callback to be invoked when the context menu for this view is
3525 * being built. If this view is not long clickable, it becomes long clickable.
3526 *
3527 * @param l The callback that will run
3528 *
3529 */
3530 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3531 if (!isLongClickable()) {
3532 setLongClickable(true);
3533 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003534 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003535 }
3536
3537 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003538 * Call this view's OnClickListener, if it is defined. Performs all normal
3539 * actions associated with clicking: reporting accessibility event, playing
3540 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003541 *
3542 * @return True there was an assigned OnClickListener that was called, false
3543 * otherwise is returned.
3544 */
3545 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003546 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3547
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003548 ListenerInfo li = mListenerInfo;
3549 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003551 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003552 return true;
3553 }
3554
3555 return false;
3556 }
3557
3558 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003559 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3560 * this only calls the listener, and does not do any associated clicking
3561 * actions like reporting an accessibility event.
3562 *
3563 * @return True there was an assigned OnClickListener that was called, false
3564 * otherwise is returned.
3565 */
3566 public boolean callOnClick() {
3567 ListenerInfo li = mListenerInfo;
3568 if (li != null && li.mOnClickListener != null) {
3569 li.mOnClickListener.onClick(this);
3570 return true;
3571 }
3572 return false;
3573 }
3574
3575 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003576 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3577 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003578 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003579 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003580 */
3581 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003582 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003584 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003585 ListenerInfo li = mListenerInfo;
3586 if (li != null && li.mOnLongClickListener != null) {
3587 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003588 }
3589 if (!handled) {
3590 handled = showContextMenu();
3591 }
3592 if (handled) {
3593 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3594 }
3595 return handled;
3596 }
3597
3598 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003599 * Performs button-related actions during a touch down event.
3600 *
3601 * @param event The event.
3602 * @return True if the down was consumed.
3603 *
3604 * @hide
3605 */
3606 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3607 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3608 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3609 return true;
3610 }
3611 }
3612 return false;
3613 }
3614
3615 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003616 * Bring up the context menu for this view.
3617 *
3618 * @return Whether a context menu was displayed.
3619 */
3620 public boolean showContextMenu() {
3621 return getParent().showContextMenuForChild(this);
3622 }
3623
3624 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003625 * Bring up the context menu for this view, referring to the item under the specified point.
3626 *
3627 * @param x The referenced x coordinate.
3628 * @param y The referenced y coordinate.
3629 * @param metaState The keyboard modifiers that were pressed.
3630 * @return Whether a context menu was displayed.
3631 *
3632 * @hide
3633 */
3634 public boolean showContextMenu(float x, float y, int metaState) {
3635 return showContextMenu();
3636 }
3637
3638 /**
Adam Powell6e346362010-07-23 10:18:23 -07003639 * Start an action mode.
3640 *
3641 * @param callback Callback that will control the lifecycle of the action mode
3642 * @return The new action mode if it is started, null otherwise
3643 *
3644 * @see ActionMode
3645 */
3646 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08003647 ViewParent parent = getParent();
3648 if (parent == null) return null;
3649 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07003650 }
3651
3652 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003653 * Register a callback to be invoked when a key is pressed in this view.
3654 * @param l the key listener to attach to this view
3655 */
3656 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003657 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003658 }
3659
3660 /**
3661 * Register a callback to be invoked when a touch event is sent to this view.
3662 * @param l the touch listener to attach to this view
3663 */
3664 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003665 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003666 }
3667
3668 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003669 * Register a callback to be invoked when a generic motion event is sent to this view.
3670 * @param l the generic motion listener to attach to this view
3671 */
3672 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003673 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08003674 }
3675
3676 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003677 * Register a callback to be invoked when a hover event is sent to this view.
3678 * @param l the hover listener to attach to this view
3679 */
3680 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003681 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07003682 }
3683
3684 /**
Joe Malin32736f02011-01-19 16:14:20 -08003685 * Register a drag event listener callback object for this View. The parameter is
3686 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3687 * View, the system calls the
3688 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3689 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003690 */
3691 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003692 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07003693 }
3694
3695 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003696 * Give this view focus. This will cause
3697 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003698 *
3699 * Note: this does not check whether this {@link View} should get focus, it just
3700 * gives it focus no matter what. It should only be called internally by framework
3701 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3702 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003703 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3704 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003705 * focus moved when requestFocus() is called. It may not always
3706 * apply, in which case use the default View.FOCUS_DOWN.
3707 * @param previouslyFocusedRect The rectangle of the view that had focus
3708 * prior in this View's coordinate system.
3709 */
3710 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3711 if (DBG) {
3712 System.out.println(this + " requestFocus()");
3713 }
3714
3715 if ((mPrivateFlags & FOCUSED) == 0) {
3716 mPrivateFlags |= FOCUSED;
3717
3718 if (mParent != null) {
3719 mParent.requestChildFocus(this, this);
3720 }
3721
3722 onFocusChanged(true, direction, previouslyFocusedRect);
3723 refreshDrawableState();
3724 }
3725 }
3726
3727 /**
3728 * Request that a rectangle of this view be visible on the screen,
3729 * scrolling if necessary just enough.
3730 *
3731 * <p>A View should call this if it maintains some notion of which part
3732 * of its content is interesting. For example, a text editing view
3733 * should call this when its cursor moves.
3734 *
3735 * @param rectangle The rectangle.
3736 * @return Whether any parent scrolled.
3737 */
3738 public boolean requestRectangleOnScreen(Rect rectangle) {
3739 return requestRectangleOnScreen(rectangle, false);
3740 }
3741
3742 /**
3743 * Request that a rectangle of this view be visible on the screen,
3744 * scrolling if necessary just enough.
3745 *
3746 * <p>A View should call this if it maintains some notion of which part
3747 * of its content is interesting. For example, a text editing view
3748 * should call this when its cursor moves.
3749 *
3750 * <p>When <code>immediate</code> is set to true, scrolling will not be
3751 * animated.
3752 *
3753 * @param rectangle The rectangle.
3754 * @param immediate True to forbid animated scrolling, false otherwise
3755 * @return Whether any parent scrolled.
3756 */
3757 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3758 View child = this;
3759 ViewParent parent = mParent;
3760 boolean scrolled = false;
3761 while (parent != null) {
3762 scrolled |= parent.requestChildRectangleOnScreen(child,
3763 rectangle, immediate);
3764
3765 // offset rect so next call has the rectangle in the
3766 // coordinate system of its direct child.
3767 rectangle.offset(child.getLeft(), child.getTop());
3768 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3769
3770 if (!(parent instanceof View)) {
3771 break;
3772 }
Romain Guy8506ab42009-06-11 17:35:47 -07003773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003774 child = (View) parent;
3775 parent = child.getParent();
3776 }
3777 return scrolled;
3778 }
3779
3780 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08003781 * Called when this view wants to give up focus. If focus is cleared
3782 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
3783 * <p>
3784 * <strong>Note:</strong> When a View clears focus the framework is trying
3785 * to give focus to the first focusable View from the top. Hence, if this
3786 * View is the first from the top that can take focus, then its focus will
3787 * not be cleared nor will the focus change callback be invoked.
3788 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003789 */
3790 public void clearFocus() {
3791 if (DBG) {
3792 System.out.println(this + " clearFocus()");
3793 }
3794
3795 if ((mPrivateFlags & FOCUSED) != 0) {
3796 mPrivateFlags &= ~FOCUSED;
3797
3798 if (mParent != null) {
3799 mParent.clearChildFocus(this);
3800 }
3801
3802 onFocusChanged(false, 0, null);
3803 refreshDrawableState();
3804 }
3805 }
3806
3807 /**
3808 * Called to clear the focus of a view that is about to be removed.
3809 * Doesn't call clearChildFocus, which prevents this view from taking
3810 * focus again before it has been removed from the parent
3811 */
3812 void clearFocusForRemoval() {
3813 if ((mPrivateFlags & FOCUSED) != 0) {
3814 mPrivateFlags &= ~FOCUSED;
3815
3816 onFocusChanged(false, 0, null);
3817 refreshDrawableState();
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08003818
3819 // The view cleared focus and invoked the callbacks, so now is the
3820 // time to give focus to the the first focusable from the top to
3821 // ensure that the gain focus is announced after clear focus.
3822 getRootView().requestFocus(FOCUS_FORWARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823 }
3824 }
3825
3826 /**
3827 * Called internally by the view system when a new view is getting focus.
3828 * This is what clears the old focus.
3829 */
3830 void unFocus() {
3831 if (DBG) {
3832 System.out.println(this + " unFocus()");
3833 }
3834
3835 if ((mPrivateFlags & FOCUSED) != 0) {
3836 mPrivateFlags &= ~FOCUSED;
3837
3838 onFocusChanged(false, 0, null);
3839 refreshDrawableState();
3840 }
3841 }
3842
3843 /**
3844 * Returns true if this view has focus iteself, or is the ancestor of the
3845 * view that has focus.
3846 *
3847 * @return True if this view has or contains focus, false otherwise.
3848 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003849 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003850 public boolean hasFocus() {
3851 return (mPrivateFlags & FOCUSED) != 0;
3852 }
3853
3854 /**
3855 * Returns true if this view is focusable or if it contains a reachable View
3856 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3857 * is a View whose parents do not block descendants focus.
3858 *
3859 * Only {@link #VISIBLE} views are considered focusable.
3860 *
3861 * @return True if the view is focusable or if the view contains a focusable
3862 * View, false otherwise.
3863 *
3864 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3865 */
3866 public boolean hasFocusable() {
3867 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3868 }
3869
3870 /**
3871 * Called by the view system when the focus state of this view changes.
3872 * When the focus change event is caused by directional navigation, direction
3873 * and previouslyFocusedRect provide insight into where the focus is coming from.
3874 * When overriding, be sure to call up through to the super class so that
3875 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003876 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003877 * @param gainFocus True if the View has focus; false otherwise.
3878 * @param direction The direction focus has moved when requestFocus()
3879 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003880 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3881 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3882 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003883 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3884 * system, of the previously focused view. If applicable, this will be
3885 * passed in as finer grained information about where the focus is coming
3886 * from (in addition to direction). Will be <code>null</code> otherwise.
3887 */
3888 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003889 if (gainFocus) {
3890 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3891 }
3892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003893 InputMethodManager imm = InputMethodManager.peekInstance();
3894 if (!gainFocus) {
3895 if (isPressed()) {
3896 setPressed(false);
3897 }
3898 if (imm != null && mAttachInfo != null
3899 && mAttachInfo.mHasWindowFocus) {
3900 imm.focusOut(this);
3901 }
Romain Guya2431d02009-04-30 16:30:00 -07003902 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003903 } else if (imm != null && mAttachInfo != null
3904 && mAttachInfo.mHasWindowFocus) {
3905 imm.focusIn(this);
3906 }
Romain Guy8506ab42009-06-11 17:35:47 -07003907
Romain Guy0fd89bf2011-01-26 15:41:30 -08003908 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003909 ListenerInfo li = mListenerInfo;
3910 if (li != null && li.mOnFocusChangeListener != null) {
3911 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003912 }
Joe Malin32736f02011-01-19 16:14:20 -08003913
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003914 if (mAttachInfo != null) {
3915 mAttachInfo.mKeyDispatchState.reset(this);
3916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003917 }
3918
3919 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003920 * Sends an accessibility event of the given type. If accessiiblity is
3921 * not enabled this method has no effect. The default implementation calls
3922 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3923 * to populate information about the event source (this View), then calls
3924 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3925 * populate the text content of the event source including its descendants,
3926 * and last calls
3927 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3928 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003929 * <p>
3930 * If an {@link AccessibilityDelegate} has been specified via calling
3931 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3932 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
3933 * responsible for handling this call.
3934 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003935 *
Scott Mainb303d832011-10-12 16:45:18 -07003936 * @param eventType The type of the event to send, as defined by several types from
3937 * {@link android.view.accessibility.AccessibilityEvent}, such as
3938 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
3939 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003940 *
3941 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3942 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3943 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003944 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07003945 */
3946 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003947 if (mAccessibilityDelegate != null) {
3948 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
3949 } else {
3950 sendAccessibilityEventInternal(eventType);
3951 }
3952 }
3953
3954 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08003955 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
3956 * {@link AccessibilityEvent} to make an announcement which is related to some
3957 * sort of a context change for which none of the events representing UI transitions
3958 * is a good fit. For example, announcing a new page in a book. If accessibility
3959 * is not enabled this method does nothing.
3960 *
3961 * @param text The announcement text.
3962 */
3963 public void announceForAccessibility(CharSequence text) {
3964 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3965 AccessibilityEvent event = AccessibilityEvent.obtain(
3966 AccessibilityEvent.TYPE_ANNOUNCEMENT);
3967 event.getText().add(text);
3968 sendAccessibilityEventUnchecked(event);
3969 }
3970 }
3971
3972 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003973 * @see #sendAccessibilityEvent(int)
3974 *
3975 * Note: Called from the default {@link AccessibilityDelegate}.
3976 */
3977 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003978 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3979 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3980 }
3981 }
3982
3983 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003984 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3985 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003986 * perform a check whether accessibility is enabled.
3987 * <p>
3988 * If an {@link AccessibilityDelegate} has been specified via calling
3989 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3990 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
3991 * is responsible for handling this call.
3992 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003993 *
3994 * @param event The event to send.
3995 *
3996 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003997 */
3998 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003999 if (mAccessibilityDelegate != null) {
4000 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
4001 } else {
4002 sendAccessibilityEventUncheckedInternal(event);
4003 }
4004 }
4005
4006 /**
4007 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4008 *
4009 * Note: Called from the default {@link AccessibilityDelegate}.
4010 */
4011 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004012 if (!isShown()) {
4013 return;
4014 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004015 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004016 // Only a subset of accessibility events populates text content.
4017 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4018 dispatchPopulateAccessibilityEvent(event);
4019 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004020 // In the beginning we called #isShown(), so we know that getParent() is not null.
4021 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004022 }
4023
4024 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004025 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4026 * to its children for adding their text content to the event. Note that the
4027 * event text is populated in a separate dispatch path since we add to the
4028 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004029 * A typical implementation will call
4030 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4031 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4032 * on each child. Override this method if custom population of the event text
4033 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004034 * <p>
4035 * If an {@link AccessibilityDelegate} has been specified via calling
4036 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4037 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4038 * is responsible for handling this call.
4039 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004040 * <p>
4041 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4042 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4043 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004044 *
4045 * @param event The event.
4046 *
4047 * @return True if the event population was completed.
4048 */
4049 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004050 if (mAccessibilityDelegate != null) {
4051 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4052 } else {
4053 return dispatchPopulateAccessibilityEventInternal(event);
4054 }
4055 }
4056
4057 /**
4058 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4059 *
4060 * Note: Called from the default {@link AccessibilityDelegate}.
4061 */
4062 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004063 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004064 return false;
4065 }
4066
4067 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004068 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004069 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004070 * text content. While this method is free to modify event
4071 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004072 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4073 * <p>
4074 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004075 * to the text added by the super implementation:
4076 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004077 * super.onPopulateAccessibilityEvent(event);
4078 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4079 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4080 * mCurrentDate.getTimeInMillis(), flags);
4081 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004082 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004083 * <p>
4084 * If an {@link AccessibilityDelegate} has been specified via calling
4085 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4086 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4087 * is responsible for handling this call.
4088 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004089 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4090 * information to the event, in case the default implementation has basic information to add.
4091 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004092 *
4093 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004094 *
4095 * @see #sendAccessibilityEvent(int)
4096 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004097 */
4098 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004099 if (mAccessibilityDelegate != null) {
4100 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4101 } else {
4102 onPopulateAccessibilityEventInternal(event);
4103 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004104 }
4105
4106 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004107 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4108 *
4109 * Note: Called from the default {@link AccessibilityDelegate}.
4110 */
4111 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4112
4113 }
4114
4115 /**
4116 * Initializes an {@link AccessibilityEvent} with information about
4117 * this View which is the event source. In other words, the source of
4118 * an accessibility event is the view whose state change triggered firing
4119 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004120 * <p>
4121 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004122 * to properties set by the super implementation:
4123 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4124 * super.onInitializeAccessibilityEvent(event);
4125 * event.setPassword(true);
4126 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004127 * <p>
4128 * If an {@link AccessibilityDelegate} has been specified via calling
4129 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4130 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4131 * is responsible for handling this call.
4132 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004133 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4134 * information to the event, in case the default implementation has basic information to add.
4135 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004136 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004137 *
4138 * @see #sendAccessibilityEvent(int)
4139 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4140 */
4141 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004142 if (mAccessibilityDelegate != null) {
4143 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4144 } else {
4145 onInitializeAccessibilityEventInternal(event);
4146 }
4147 }
4148
4149 /**
4150 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4151 *
4152 * Note: Called from the default {@link AccessibilityDelegate}.
4153 */
4154 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004155 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004156 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004157 event.setPackageName(getContext().getPackageName());
4158 event.setEnabled(isEnabled());
4159 event.setContentDescription(mContentDescription);
4160
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004161 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4162 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4163 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4164 FOCUSABLES_ALL);
4165 event.setItemCount(focusablesTempList.size());
4166 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4167 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004168 }
4169 }
4170
4171 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004172 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4173 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4174 * This method is responsible for obtaining an accessibility node info from a
4175 * pool of reusable instances and calling
4176 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4177 * initialize the former.
4178 * <p>
4179 * Note: The client is responsible for recycling the obtained instance by calling
4180 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4181 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004182 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004183 * @return A populated {@link AccessibilityNodeInfo}.
4184 *
4185 * @see AccessibilityNodeInfo
4186 */
4187 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004188 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4189 if (provider != null) {
4190 return provider.createAccessibilityNodeInfo(View.NO_ID);
4191 } else {
4192 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4193 onInitializeAccessibilityNodeInfo(info);
4194 return info;
4195 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004196 }
4197
4198 /**
4199 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4200 * The base implementation sets:
4201 * <ul>
4202 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004203 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4204 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004205 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4206 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4207 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4208 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4209 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4210 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4211 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4212 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4213 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4214 * </ul>
4215 * <p>
4216 * Subclasses should override this method, call the super implementation,
4217 * and set additional attributes.
4218 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004219 * <p>
4220 * If an {@link AccessibilityDelegate} has been specified via calling
4221 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4222 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4223 * is responsible for handling this call.
4224 * </p>
4225 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004226 * @param info The instance to initialize.
4227 */
4228 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004229 if (mAccessibilityDelegate != null) {
4230 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4231 } else {
4232 onInitializeAccessibilityNodeInfoInternal(info);
4233 }
4234 }
4235
4236 /**
4237 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4238 *
4239 * Note: Called from the default {@link AccessibilityDelegate}.
4240 */
4241 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004242 Rect bounds = mAttachInfo.mTmpInvalRect;
4243 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004244 info.setBoundsInParent(bounds);
4245
4246 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4247 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004248 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004249 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4250 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004251
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004252 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
4253 ViewParent parent = getParent();
4254 if (parent instanceof View) {
4255 View parentView = (View) parent;
4256 info.setParent(parentView);
4257 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004258 }
4259
4260 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004261 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004262 info.setContentDescription(getContentDescription());
4263
4264 info.setEnabled(isEnabled());
4265 info.setClickable(isClickable());
4266 info.setFocusable(isFocusable());
4267 info.setFocused(isFocused());
4268 info.setSelected(isSelected());
4269 info.setLongClickable(isLongClickable());
4270
4271 // TODO: These make sense only if we are in an AdapterView but all
4272 // views can be selected. Maybe from accessiiblity perspective
4273 // we should report as selectable view in an AdapterView.
4274 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4275 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4276
4277 if (isFocusable()) {
4278 if (isFocused()) {
4279 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4280 } else {
4281 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4282 }
4283 }
4284 }
4285
4286 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004287 * Sets a delegate for implementing accessibility support via compositon as
4288 * opposed to inheritance. The delegate's primary use is for implementing
4289 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4290 *
4291 * @param delegate The delegate instance.
4292 *
4293 * @see AccessibilityDelegate
4294 */
4295 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4296 mAccessibilityDelegate = delegate;
4297 }
4298
4299 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004300 * Gets the provider for managing a virtual view hierarchy rooted at this View
4301 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4302 * that explore the window content.
4303 * <p>
4304 * If this method returns an instance, this instance is responsible for managing
4305 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4306 * View including the one representing the View itself. Similarly the returned
4307 * instance is responsible for performing accessibility actions on any virtual
4308 * view or the root view itself.
4309 * </p>
4310 * <p>
4311 * If an {@link AccessibilityDelegate} has been specified via calling
4312 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4313 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4314 * is responsible for handling this call.
4315 * </p>
4316 *
4317 * @return The provider.
4318 *
4319 * @see AccessibilityNodeProvider
4320 */
4321 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4322 if (mAccessibilityDelegate != null) {
4323 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4324 } else {
4325 return null;
4326 }
4327 }
4328
4329 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004330 * Gets the unique identifier of this view on the screen for accessibility purposes.
4331 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4332 *
4333 * @return The view accessibility id.
4334 *
4335 * @hide
4336 */
4337 public int getAccessibilityViewId() {
4338 if (mAccessibilityViewId == NO_ID) {
4339 mAccessibilityViewId = sNextAccessibilityViewId++;
4340 }
4341 return mAccessibilityViewId;
4342 }
4343
4344 /**
4345 * Gets the unique identifier of the window in which this View reseides.
4346 *
4347 * @return The window accessibility id.
4348 *
4349 * @hide
4350 */
4351 public int getAccessibilityWindowId() {
4352 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4353 }
4354
4355 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004356 * Gets the {@link View} description. It briefly describes the view and is
4357 * primarily used for accessibility support. Set this property to enable
4358 * better accessibility support for your application. This is especially
4359 * true for views that do not have textual representation (For example,
4360 * ImageButton).
4361 *
4362 * @return The content descriptiopn.
4363 *
4364 * @attr ref android.R.styleable#View_contentDescription
4365 */
4366 public CharSequence getContentDescription() {
4367 return mContentDescription;
4368 }
4369
4370 /**
4371 * Sets the {@link View} description. It briefly describes the view and is
4372 * primarily used for accessibility support. Set this property to enable
4373 * better accessibility support for your application. This is especially
4374 * true for views that do not have textual representation (For example,
4375 * ImageButton).
4376 *
4377 * @param contentDescription The content description.
4378 *
4379 * @attr ref android.R.styleable#View_contentDescription
4380 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004381 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004382 public void setContentDescription(CharSequence contentDescription) {
4383 mContentDescription = contentDescription;
4384 }
4385
4386 /**
Romain Guya2431d02009-04-30 16:30:00 -07004387 * Invoked whenever this view loses focus, either by losing window focus or by losing
4388 * focus within its window. This method can be used to clear any state tied to the
4389 * focus. For instance, if a button is held pressed with the trackball and the window
4390 * loses focus, this method can be used to cancel the press.
4391 *
4392 * Subclasses of View overriding this method should always call super.onFocusLost().
4393 *
4394 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004395 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004396 *
4397 * @hide pending API council approval
4398 */
4399 protected void onFocusLost() {
4400 resetPressedState();
4401 }
4402
4403 private void resetPressedState() {
4404 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4405 return;
4406 }
4407
4408 if (isPressed()) {
4409 setPressed(false);
4410
4411 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004412 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004413 }
4414 }
4415 }
4416
4417 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004418 * Returns true if this view has focus
4419 *
4420 * @return True if this view has focus, false otherwise.
4421 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004422 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004423 public boolean isFocused() {
4424 return (mPrivateFlags & FOCUSED) != 0;
4425 }
4426
4427 /**
4428 * Find the view in the hierarchy rooted at this view that currently has
4429 * focus.
4430 *
4431 * @return The view that currently has focus, or null if no focused view can
4432 * be found.
4433 */
4434 public View findFocus() {
4435 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4436 }
4437
4438 /**
4439 * Change whether this view is one of the set of scrollable containers in
4440 * its window. This will be used to determine whether the window can
4441 * resize or must pan when a soft input area is open -- scrollable
4442 * containers allow the window to use resize mode since the container
4443 * will appropriately shrink.
4444 */
4445 public void setScrollContainer(boolean isScrollContainer) {
4446 if (isScrollContainer) {
4447 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4448 mAttachInfo.mScrollContainers.add(this);
4449 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4450 }
4451 mPrivateFlags |= SCROLL_CONTAINER;
4452 } else {
4453 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4454 mAttachInfo.mScrollContainers.remove(this);
4455 }
4456 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4457 }
4458 }
4459
4460 /**
4461 * Returns the quality of the drawing cache.
4462 *
4463 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4464 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4465 *
4466 * @see #setDrawingCacheQuality(int)
4467 * @see #setDrawingCacheEnabled(boolean)
4468 * @see #isDrawingCacheEnabled()
4469 *
4470 * @attr ref android.R.styleable#View_drawingCacheQuality
4471 */
4472 public int getDrawingCacheQuality() {
4473 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4474 }
4475
4476 /**
4477 * Set the drawing cache quality of this view. This value is used only when the
4478 * drawing cache is enabled
4479 *
4480 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4481 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4482 *
4483 * @see #getDrawingCacheQuality()
4484 * @see #setDrawingCacheEnabled(boolean)
4485 * @see #isDrawingCacheEnabled()
4486 *
4487 * @attr ref android.R.styleable#View_drawingCacheQuality
4488 */
4489 public void setDrawingCacheQuality(int quality) {
4490 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4491 }
4492
4493 /**
4494 * Returns whether the screen should remain on, corresponding to the current
4495 * value of {@link #KEEP_SCREEN_ON}.
4496 *
4497 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4498 *
4499 * @see #setKeepScreenOn(boolean)
4500 *
4501 * @attr ref android.R.styleable#View_keepScreenOn
4502 */
4503 public boolean getKeepScreenOn() {
4504 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4505 }
4506
4507 /**
4508 * Controls whether the screen should remain on, modifying the
4509 * value of {@link #KEEP_SCREEN_ON}.
4510 *
4511 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4512 *
4513 * @see #getKeepScreenOn()
4514 *
4515 * @attr ref android.R.styleable#View_keepScreenOn
4516 */
4517 public void setKeepScreenOn(boolean keepScreenOn) {
4518 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4519 }
4520
4521 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004522 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4523 * @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 -08004524 *
4525 * @attr ref android.R.styleable#View_nextFocusLeft
4526 */
4527 public int getNextFocusLeftId() {
4528 return mNextFocusLeftId;
4529 }
4530
4531 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004532 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4533 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4534 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004535 *
4536 * @attr ref android.R.styleable#View_nextFocusLeft
4537 */
4538 public void setNextFocusLeftId(int nextFocusLeftId) {
4539 mNextFocusLeftId = nextFocusLeftId;
4540 }
4541
4542 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004543 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4544 * @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 -08004545 *
4546 * @attr ref android.R.styleable#View_nextFocusRight
4547 */
4548 public int getNextFocusRightId() {
4549 return mNextFocusRightId;
4550 }
4551
4552 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004553 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4554 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4555 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004556 *
4557 * @attr ref android.R.styleable#View_nextFocusRight
4558 */
4559 public void setNextFocusRightId(int nextFocusRightId) {
4560 mNextFocusRightId = nextFocusRightId;
4561 }
4562
4563 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004564 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4565 * @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 -08004566 *
4567 * @attr ref android.R.styleable#View_nextFocusUp
4568 */
4569 public int getNextFocusUpId() {
4570 return mNextFocusUpId;
4571 }
4572
4573 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004574 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4575 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4576 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004577 *
4578 * @attr ref android.R.styleable#View_nextFocusUp
4579 */
4580 public void setNextFocusUpId(int nextFocusUpId) {
4581 mNextFocusUpId = nextFocusUpId;
4582 }
4583
4584 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004585 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4586 * @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 -08004587 *
4588 * @attr ref android.R.styleable#View_nextFocusDown
4589 */
4590 public int getNextFocusDownId() {
4591 return mNextFocusDownId;
4592 }
4593
4594 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004595 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4596 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4597 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004598 *
4599 * @attr ref android.R.styleable#View_nextFocusDown
4600 */
4601 public void setNextFocusDownId(int nextFocusDownId) {
4602 mNextFocusDownId = nextFocusDownId;
4603 }
4604
4605 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004606 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4607 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4608 *
4609 * @attr ref android.R.styleable#View_nextFocusForward
4610 */
4611 public int getNextFocusForwardId() {
4612 return mNextFocusForwardId;
4613 }
4614
4615 /**
4616 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4617 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4618 * decide automatically.
4619 *
4620 * @attr ref android.R.styleable#View_nextFocusForward
4621 */
4622 public void setNextFocusForwardId(int nextFocusForwardId) {
4623 mNextFocusForwardId = nextFocusForwardId;
4624 }
4625
4626 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004627 * Returns the visibility of this view and all of its ancestors
4628 *
4629 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4630 */
4631 public boolean isShown() {
4632 View current = this;
4633 //noinspection ConstantConditions
4634 do {
4635 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4636 return false;
4637 }
4638 ViewParent parent = current.mParent;
4639 if (parent == null) {
4640 return false; // We are not attached to the view root
4641 }
4642 if (!(parent instanceof View)) {
4643 return true;
4644 }
4645 current = (View) parent;
4646 } while (current != null);
4647
4648 return false;
4649 }
4650
4651 /**
4652 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4653 * is set
4654 *
4655 * @param insets Insets for system windows
4656 *
4657 * @return True if this view applied the insets, false otherwise
4658 */
4659 protected boolean fitSystemWindows(Rect insets) {
4660 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4661 mPaddingLeft = insets.left;
4662 mPaddingTop = insets.top;
4663 mPaddingRight = insets.right;
4664 mPaddingBottom = insets.bottom;
4665 requestLayout();
4666 return true;
4667 }
4668 return false;
4669 }
4670
4671 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004672 * Set whether or not this view should account for system screen decorations
4673 * such as the status bar and inset its content. This allows this view to be
4674 * positioned in absolute screen coordinates and remain visible to the user.
4675 *
4676 * <p>This should only be used by top-level window decor views.
4677 *
4678 * @param fitSystemWindows true to inset content for system screen decorations, false for
4679 * default behavior.
4680 *
4681 * @attr ref android.R.styleable#View_fitsSystemWindows
4682 */
4683 public void setFitsSystemWindows(boolean fitSystemWindows) {
4684 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4685 }
4686
4687 /**
4688 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4689 * will account for system screen decorations such as the status bar and inset its
4690 * content. This allows the view to be positioned in absolute screen coordinates
4691 * and remain visible to the user.
4692 *
4693 * @return true if this view will adjust its content bounds for system screen decorations.
4694 *
4695 * @attr ref android.R.styleable#View_fitsSystemWindows
4696 */
4697 public boolean fitsSystemWindows() {
4698 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4699 }
4700
4701 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004702 * Returns the visibility status for this view.
4703 *
4704 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4705 * @attr ref android.R.styleable#View_visibility
4706 */
4707 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004708 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4709 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4710 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004711 })
4712 public int getVisibility() {
4713 return mViewFlags & VISIBILITY_MASK;
4714 }
4715
4716 /**
4717 * Set the enabled state of this view.
4718 *
4719 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4720 * @attr ref android.R.styleable#View_visibility
4721 */
4722 @RemotableViewMethod
4723 public void setVisibility(int visibility) {
4724 setFlags(visibility, VISIBILITY_MASK);
4725 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4726 }
4727
4728 /**
4729 * Returns the enabled status for this view. The interpretation of the
4730 * enabled state varies by subclass.
4731 *
4732 * @return True if this view is enabled, false otherwise.
4733 */
4734 @ViewDebug.ExportedProperty
4735 public boolean isEnabled() {
4736 return (mViewFlags & ENABLED_MASK) == ENABLED;
4737 }
4738
4739 /**
4740 * Set the enabled state of this view. The interpretation of the enabled
4741 * state varies by subclass.
4742 *
4743 * @param enabled True if this view is enabled, false otherwise.
4744 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004745 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004746 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004747 if (enabled == isEnabled()) return;
4748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004749 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4750
4751 /*
4752 * The View most likely has to change its appearance, so refresh
4753 * the drawable state.
4754 */
4755 refreshDrawableState();
4756
4757 // Invalidate too, since the default behavior for views is to be
4758 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004759 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004760 }
4761
4762 /**
4763 * Set whether this view can receive the focus.
4764 *
4765 * Setting this to false will also ensure that this view is not focusable
4766 * in touch mode.
4767 *
4768 * @param focusable If true, this view can receive the focus.
4769 *
4770 * @see #setFocusableInTouchMode(boolean)
4771 * @attr ref android.R.styleable#View_focusable
4772 */
4773 public void setFocusable(boolean focusable) {
4774 if (!focusable) {
4775 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4776 }
4777 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4778 }
4779
4780 /**
4781 * Set whether this view can receive focus while in touch mode.
4782 *
4783 * Setting this to true will also ensure that this view is focusable.
4784 *
4785 * @param focusableInTouchMode If true, this view can receive the focus while
4786 * in touch mode.
4787 *
4788 * @see #setFocusable(boolean)
4789 * @attr ref android.R.styleable#View_focusableInTouchMode
4790 */
4791 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4792 // Focusable in touch mode should always be set before the focusable flag
4793 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4794 // which, in touch mode, will not successfully request focus on this view
4795 // because the focusable in touch mode flag is not set
4796 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4797 if (focusableInTouchMode) {
4798 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4799 }
4800 }
4801
4802 /**
4803 * Set whether this view should have sound effects enabled for events such as
4804 * clicking and touching.
4805 *
4806 * <p>You may wish to disable sound effects for a view if you already play sounds,
4807 * for instance, a dial key that plays dtmf tones.
4808 *
4809 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4810 * @see #isSoundEffectsEnabled()
4811 * @see #playSoundEffect(int)
4812 * @attr ref android.R.styleable#View_soundEffectsEnabled
4813 */
4814 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4815 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4816 }
4817
4818 /**
4819 * @return whether this view should have sound effects enabled for events such as
4820 * clicking and touching.
4821 *
4822 * @see #setSoundEffectsEnabled(boolean)
4823 * @see #playSoundEffect(int)
4824 * @attr ref android.R.styleable#View_soundEffectsEnabled
4825 */
4826 @ViewDebug.ExportedProperty
4827 public boolean isSoundEffectsEnabled() {
4828 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4829 }
4830
4831 /**
4832 * Set whether this view should have haptic feedback for events such as
4833 * long presses.
4834 *
4835 * <p>You may wish to disable haptic feedback if your view already controls
4836 * its own haptic feedback.
4837 *
4838 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4839 * @see #isHapticFeedbackEnabled()
4840 * @see #performHapticFeedback(int)
4841 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4842 */
4843 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4844 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4845 }
4846
4847 /**
4848 * @return whether this view should have haptic feedback enabled for events
4849 * long presses.
4850 *
4851 * @see #setHapticFeedbackEnabled(boolean)
4852 * @see #performHapticFeedback(int)
4853 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4854 */
4855 @ViewDebug.ExportedProperty
4856 public boolean isHapticFeedbackEnabled() {
4857 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4858 }
4859
4860 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004861 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004862 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004863 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4864 * {@link #LAYOUT_DIRECTION_RTL},
4865 * {@link #LAYOUT_DIRECTION_INHERIT} or
4866 * {@link #LAYOUT_DIRECTION_LOCALE}.
4867 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08004868 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004869 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004870 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4871 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4872 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4873 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004874 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004875 public int getLayoutDirection() {
4876 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004877 }
4878
4879 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004880 * Set the layout direction for this view. This will propagate a reset of layout direction
4881 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004882 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004883 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4884 * {@link #LAYOUT_DIRECTION_RTL},
4885 * {@link #LAYOUT_DIRECTION_INHERIT} or
4886 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004887 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004888 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08004889 */
4890 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004891 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004892 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004893 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004894 // Setting the flag will also request a layout.
4895 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4896 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004897 }
4898
4899 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004900 * Returns the resolved layout direction for this view.
4901 *
4902 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4903 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004904 */
4905 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4906 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4907 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4908 })
4909 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004910 resolveLayoutDirectionIfNeeded();
4911 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004912 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4913 }
4914
4915 /**
4916 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4917 * layout attribute and/or the inherited value from the parent.</p>
4918 *
4919 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004920 */
4921 @ViewDebug.ExportedProperty(category = "layout")
4922 public boolean isLayoutRtl() {
4923 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4924 }
4925
4926 /**
Adam Powell539ee872012-02-03 19:00:49 -08004927 * Indicates whether the view is currently tracking transient state that the
4928 * app should not need to concern itself with saving and restoring, but that
4929 * the framework should take special note to preserve when possible.
4930 *
4931 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08004932 */
4933 @ViewDebug.ExportedProperty(category = "layout")
4934 public boolean hasTransientState() {
4935 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
4936 }
4937
4938 /**
4939 * Set whether this view is currently tracking transient state that the
4940 * framework should attempt to preserve when possible.
4941 *
4942 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08004943 */
4944 public void setHasTransientState(boolean hasTransientState) {
4945 if (hasTransientState() == hasTransientState) return;
4946
4947 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
4948 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
4949 if (mParent != null) {
4950 try {
4951 mParent.childHasTransientStateChanged(this, hasTransientState);
4952 } catch (AbstractMethodError e) {
4953 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
4954 " does not fully implement ViewParent", e);
4955 }
4956 }
4957 }
4958
4959 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004960 * If this view doesn't do any drawing on its own, set this flag to
4961 * allow further optimizations. By default, this flag is not set on
4962 * View, but could be set on some View subclasses such as ViewGroup.
4963 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004964 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4965 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004966 *
4967 * @param willNotDraw whether or not this View draw on its own
4968 */
4969 public void setWillNotDraw(boolean willNotDraw) {
4970 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4971 }
4972
4973 /**
4974 * Returns whether or not this View draws on its own.
4975 *
4976 * @return true if this view has nothing to draw, false otherwise
4977 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004978 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004979 public boolean willNotDraw() {
4980 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4981 }
4982
4983 /**
4984 * When a View's drawing cache is enabled, drawing is redirected to an
4985 * offscreen bitmap. Some views, like an ImageView, must be able to
4986 * bypass this mechanism if they already draw a single bitmap, to avoid
4987 * unnecessary usage of the memory.
4988 *
4989 * @param willNotCacheDrawing true if this view does not cache its
4990 * drawing, false otherwise
4991 */
4992 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4993 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4994 }
4995
4996 /**
4997 * Returns whether or not this View can cache its drawing or not.
4998 *
4999 * @return true if this view does not cache its drawing, false otherwise
5000 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005001 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005002 public boolean willNotCacheDrawing() {
5003 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5004 }
5005
5006 /**
5007 * Indicates whether this view reacts to click events or not.
5008 *
5009 * @return true if the view is clickable, false otherwise
5010 *
5011 * @see #setClickable(boolean)
5012 * @attr ref android.R.styleable#View_clickable
5013 */
5014 @ViewDebug.ExportedProperty
5015 public boolean isClickable() {
5016 return (mViewFlags & CLICKABLE) == CLICKABLE;
5017 }
5018
5019 /**
5020 * Enables or disables click events for this view. When a view
5021 * is clickable it will change its state to "pressed" on every click.
5022 * Subclasses should set the view clickable to visually react to
5023 * user's clicks.
5024 *
5025 * @param clickable true to make the view clickable, false otherwise
5026 *
5027 * @see #isClickable()
5028 * @attr ref android.R.styleable#View_clickable
5029 */
5030 public void setClickable(boolean clickable) {
5031 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5032 }
5033
5034 /**
5035 * Indicates whether this view reacts to long click events or not.
5036 *
5037 * @return true if the view is long clickable, false otherwise
5038 *
5039 * @see #setLongClickable(boolean)
5040 * @attr ref android.R.styleable#View_longClickable
5041 */
5042 public boolean isLongClickable() {
5043 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5044 }
5045
5046 /**
5047 * Enables or disables long click events for this view. When a view is long
5048 * clickable it reacts to the user holding down the button for a longer
5049 * duration than a tap. This event can either launch the listener or a
5050 * context menu.
5051 *
5052 * @param longClickable true to make the view long clickable, false otherwise
5053 * @see #isLongClickable()
5054 * @attr ref android.R.styleable#View_longClickable
5055 */
5056 public void setLongClickable(boolean longClickable) {
5057 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5058 }
5059
5060 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005061 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005062 *
5063 * @see #isClickable()
5064 * @see #setClickable(boolean)
5065 *
5066 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5067 * the View's internal state from a previously set "pressed" state.
5068 */
5069 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005070 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005072 if (pressed) {
5073 mPrivateFlags |= PRESSED;
5074 } else {
5075 mPrivateFlags &= ~PRESSED;
5076 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005077
5078 if (needsRefresh) {
5079 refreshDrawableState();
5080 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005081 dispatchSetPressed(pressed);
5082 }
5083
5084 /**
5085 * Dispatch setPressed to all of this View's children.
5086 *
5087 * @see #setPressed(boolean)
5088 *
5089 * @param pressed The new pressed state
5090 */
5091 protected void dispatchSetPressed(boolean pressed) {
5092 }
5093
5094 /**
5095 * Indicates whether the view is currently in pressed state. Unless
5096 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5097 * the pressed state.
5098 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005099 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005100 * @see #isClickable()
5101 * @see #setClickable(boolean)
5102 *
5103 * @return true if the view is currently pressed, false otherwise
5104 */
5105 public boolean isPressed() {
5106 return (mPrivateFlags & PRESSED) == PRESSED;
5107 }
5108
5109 /**
5110 * Indicates whether this view will save its state (that is,
5111 * whether its {@link #onSaveInstanceState} method will be called).
5112 *
5113 * @return Returns true if the view state saving is enabled, else false.
5114 *
5115 * @see #setSaveEnabled(boolean)
5116 * @attr ref android.R.styleable#View_saveEnabled
5117 */
5118 public boolean isSaveEnabled() {
5119 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5120 }
5121
5122 /**
5123 * Controls whether the saving of this view's state is
5124 * enabled (that is, whether its {@link #onSaveInstanceState} method
5125 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005126 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005127 * for its state to be saved. This flag can only disable the
5128 * saving of this view; any child views may still have their state saved.
5129 *
5130 * @param enabled Set to false to <em>disable</em> state saving, or true
5131 * (the default) to allow it.
5132 *
5133 * @see #isSaveEnabled()
5134 * @see #setId(int)
5135 * @see #onSaveInstanceState()
5136 * @attr ref android.R.styleable#View_saveEnabled
5137 */
5138 public void setSaveEnabled(boolean enabled) {
5139 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5140 }
5141
Jeff Brown85a31762010-09-01 17:01:00 -07005142 /**
5143 * Gets whether the framework should discard touches when the view's
5144 * window is obscured by another visible window.
5145 * Refer to the {@link View} security documentation for more details.
5146 *
5147 * @return True if touch filtering is enabled.
5148 *
5149 * @see #setFilterTouchesWhenObscured(boolean)
5150 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5151 */
5152 @ViewDebug.ExportedProperty
5153 public boolean getFilterTouchesWhenObscured() {
5154 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5155 }
5156
5157 /**
5158 * Sets whether the framework should discard touches when the view's
5159 * window is obscured by another visible window.
5160 * Refer to the {@link View} security documentation for more details.
5161 *
5162 * @param enabled True if touch filtering should be enabled.
5163 *
5164 * @see #getFilterTouchesWhenObscured
5165 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5166 */
5167 public void setFilterTouchesWhenObscured(boolean enabled) {
5168 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5169 FILTER_TOUCHES_WHEN_OBSCURED);
5170 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005171
5172 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005173 * Indicates whether the entire hierarchy under this view will save its
5174 * state when a state saving traversal occurs from its parent. The default
5175 * is true; if false, these views will not be saved unless
5176 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5177 *
5178 * @return Returns true if the view state saving from parent is enabled, else false.
5179 *
5180 * @see #setSaveFromParentEnabled(boolean)
5181 */
5182 public boolean isSaveFromParentEnabled() {
5183 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5184 }
5185
5186 /**
5187 * Controls whether the entire hierarchy under this view will save its
5188 * state when a state saving traversal occurs from its parent. The default
5189 * is true; if false, these views will not be saved unless
5190 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5191 *
5192 * @param enabled Set to false to <em>disable</em> state saving, or true
5193 * (the default) to allow it.
5194 *
5195 * @see #isSaveFromParentEnabled()
5196 * @see #setId(int)
5197 * @see #onSaveInstanceState()
5198 */
5199 public void setSaveFromParentEnabled(boolean enabled) {
5200 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5201 }
5202
5203
5204 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005205 * Returns whether this View is able to take focus.
5206 *
5207 * @return True if this view can take focus, or false otherwise.
5208 * @attr ref android.R.styleable#View_focusable
5209 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005210 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005211 public final boolean isFocusable() {
5212 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5213 }
5214
5215 /**
5216 * When a view is focusable, it may not want to take focus when in touch mode.
5217 * For example, a button would like focus when the user is navigating via a D-pad
5218 * so that the user can click on it, but once the user starts touching the screen,
5219 * the button shouldn't take focus
5220 * @return Whether the view is focusable in touch mode.
5221 * @attr ref android.R.styleable#View_focusableInTouchMode
5222 */
5223 @ViewDebug.ExportedProperty
5224 public final boolean isFocusableInTouchMode() {
5225 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5226 }
5227
5228 /**
5229 * Find the nearest view in the specified direction that can take focus.
5230 * This does not actually give focus to that view.
5231 *
5232 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5233 *
5234 * @return The nearest focusable in the specified direction, or null if none
5235 * can be found.
5236 */
5237 public View focusSearch(int direction) {
5238 if (mParent != null) {
5239 return mParent.focusSearch(this, direction);
5240 } else {
5241 return null;
5242 }
5243 }
5244
5245 /**
5246 * This method is the last chance for the focused view and its ancestors to
5247 * respond to an arrow key. This is called when the focused view did not
5248 * consume the key internally, nor could the view system find a new view in
5249 * the requested direction to give focus to.
5250 *
5251 * @param focused The currently focused view.
5252 * @param direction The direction focus wants to move. One of FOCUS_UP,
5253 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5254 * @return True if the this view consumed this unhandled move.
5255 */
5256 public boolean dispatchUnhandledMove(View focused, int direction) {
5257 return false;
5258 }
5259
5260 /**
5261 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005262 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005263 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005264 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5265 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005266 * @return The user specified next view, or null if there is none.
5267 */
5268 View findUserSetNextFocus(View root, int direction) {
5269 switch (direction) {
5270 case FOCUS_LEFT:
5271 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005272 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005273 case FOCUS_RIGHT:
5274 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005275 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005276 case FOCUS_UP:
5277 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005278 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005279 case FOCUS_DOWN:
5280 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005281 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005282 case FOCUS_FORWARD:
5283 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005284 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005285 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005286 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005287 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005288 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005289 @Override
5290 public boolean apply(View t) {
5291 return t.mNextFocusForwardId == id;
5292 }
5293 });
5294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005295 }
5296 return null;
5297 }
5298
Jeff Brown4dfbec22011-08-15 14:55:37 -07005299 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5300 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5301 @Override
5302 public boolean apply(View t) {
5303 return t.mID == childViewId;
5304 }
5305 });
5306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005307 if (result == null) {
5308 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5309 + "by user for id " + childViewId);
5310 }
5311 return result;
5312 }
5313
5314 /**
5315 * Find and return all focusable views that are descendants of this view,
5316 * possibly including this view if it is focusable itself.
5317 *
5318 * @param direction The direction of the focus
5319 * @return A list of focusable views
5320 */
5321 public ArrayList<View> getFocusables(int direction) {
5322 ArrayList<View> result = new ArrayList<View>(24);
5323 addFocusables(result, direction);
5324 return result;
5325 }
5326
5327 /**
5328 * Add any focusable views that are descendants of this view (possibly
5329 * including this view if it is focusable itself) to views. If we are in touch mode,
5330 * only add views that are also focusable in touch mode.
5331 *
5332 * @param views Focusable views found so far
5333 * @param direction The direction of the focus
5334 */
5335 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005336 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5337 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005338
svetoslavganov75986cf2009-05-14 22:28:01 -07005339 /**
5340 * Adds any focusable views that are descendants of this view (possibly
5341 * including this view if it is focusable itself) to views. This method
5342 * adds all focusable views regardless if we are in touch mode or
5343 * only views focusable in touch mode if we are in touch mode depending on
5344 * the focusable mode paramater.
5345 *
5346 * @param views Focusable views found so far or null if all we are interested is
5347 * the number of focusables.
5348 * @param direction The direction of the focus.
5349 * @param focusableMode The type of focusables to be added.
5350 *
5351 * @see #FOCUSABLES_ALL
5352 * @see #FOCUSABLES_TOUCH_MODE
5353 */
5354 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5355 if (!isFocusable()) {
5356 return;
5357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005358
svetoslavganov75986cf2009-05-14 22:28:01 -07005359 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5360 isInTouchMode() && !isFocusableInTouchMode()) {
5361 return;
5362 }
5363
5364 if (views != null) {
5365 views.add(this);
5366 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005367 }
5368
5369 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005370 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005371 * The search is performed by either the text that the View renders or the content
5372 * description that describes the view for accessibility purposes and the view does
5373 * not render or both. Clients can specify how the search is to be performed via
5374 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5375 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005376 *
5377 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005378 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005379 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005380 * @see #FIND_VIEWS_WITH_TEXT
5381 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5382 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005383 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005384 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005385 if (getAccessibilityNodeProvider() != null) {
5386 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5387 outViews.add(this);
5388 }
5389 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5390 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005391 String searchedLowerCase = searched.toString().toLowerCase();
5392 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5393 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5394 outViews.add(this);
5395 }
5396 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005397 }
5398
5399 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005400 * Find and return all touchable views that are descendants of this view,
5401 * possibly including this view if it is touchable itself.
5402 *
5403 * @return A list of touchable views
5404 */
5405 public ArrayList<View> getTouchables() {
5406 ArrayList<View> result = new ArrayList<View>();
5407 addTouchables(result);
5408 return result;
5409 }
5410
5411 /**
5412 * Add any touchable views that are descendants of this view (possibly
5413 * including this view if it is touchable itself) to views.
5414 *
5415 * @param views Touchable views found so far
5416 */
5417 public void addTouchables(ArrayList<View> views) {
5418 final int viewFlags = mViewFlags;
5419
5420 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5421 && (viewFlags & ENABLED_MASK) == ENABLED) {
5422 views.add(this);
5423 }
5424 }
5425
5426 /**
5427 * Call this to try to give focus to a specific view or to one of its
5428 * descendants.
5429 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005430 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5431 * false), or if it is focusable and it is not focusable in touch mode
5432 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005433 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005434 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005435 * have focus, and you want your parent to look for the next one.
5436 *
5437 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5438 * {@link #FOCUS_DOWN} and <code>null</code>.
5439 *
5440 * @return Whether this view or one of its descendants actually took focus.
5441 */
5442 public final boolean requestFocus() {
5443 return requestFocus(View.FOCUS_DOWN);
5444 }
5445
5446
5447 /**
5448 * Call this to try to give focus to a specific view or to one of its
5449 * descendants and give it a hint about what direction focus is heading.
5450 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005451 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5452 * false), or if it is focusable and it is not focusable in touch mode
5453 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005454 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005455 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005456 * have focus, and you want your parent to look for the next one.
5457 *
5458 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5459 * <code>null</code> set for the previously focused rectangle.
5460 *
5461 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5462 * @return Whether this view or one of its descendants actually took focus.
5463 */
5464 public final boolean requestFocus(int direction) {
5465 return requestFocus(direction, null);
5466 }
5467
5468 /**
5469 * Call this to try to give focus to a specific view or to one of its descendants
5470 * and give it hints about the direction and a specific rectangle that the focus
5471 * is coming from. The rectangle can help give larger views a finer grained hint
5472 * about where focus is coming from, and therefore, where to show selection, or
5473 * forward focus change internally.
5474 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005475 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5476 * false), or if it is focusable and it is not focusable in touch mode
5477 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005478 *
5479 * A View will not take focus if it is not visible.
5480 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005481 * A View will not take focus if one of its parents has
5482 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5483 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005484 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005485 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005486 * have focus, and you want your parent to look for the next one.
5487 *
5488 * You may wish to override this method if your custom {@link View} has an internal
5489 * {@link View} that it wishes to forward the request to.
5490 *
5491 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5492 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5493 * to give a finer grained hint about where focus is coming from. May be null
5494 * if there is no hint.
5495 * @return Whether this view or one of its descendants actually took focus.
5496 */
5497 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5498 // need to be focusable
5499 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5500 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5501 return false;
5502 }
5503
5504 // need to be focusable in touch mode if in touch mode
5505 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005506 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5507 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005508 }
5509
5510 // need to not have any parents blocking us
5511 if (hasAncestorThatBlocksDescendantFocus()) {
5512 return false;
5513 }
5514
5515 handleFocusGainInternal(direction, previouslyFocusedRect);
5516 return true;
5517 }
5518
5519 /**
5520 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5521 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5522 * touch mode to request focus when they are touched.
5523 *
5524 * @return Whether this view or one of its descendants actually took focus.
5525 *
5526 * @see #isInTouchMode()
5527 *
5528 */
5529 public final boolean requestFocusFromTouch() {
5530 // Leave touch mode if we need to
5531 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005532 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005533 if (viewRoot != null) {
5534 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005535 }
5536 }
5537 return requestFocus(View.FOCUS_DOWN);
5538 }
5539
5540 /**
5541 * @return Whether any ancestor of this view blocks descendant focus.
5542 */
5543 private boolean hasAncestorThatBlocksDescendantFocus() {
5544 ViewParent ancestor = mParent;
5545 while (ancestor instanceof ViewGroup) {
5546 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5547 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5548 return true;
5549 } else {
5550 ancestor = vgAncestor.getParent();
5551 }
5552 }
5553 return false;
5554 }
5555
5556 /**
Romain Guya440b002010-02-24 15:57:54 -08005557 * @hide
5558 */
5559 public void dispatchStartTemporaryDetach() {
5560 onStartTemporaryDetach();
5561 }
5562
5563 /**
5564 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005565 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5566 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005567 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005568 */
5569 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005570 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005571 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005572 }
5573
5574 /**
5575 * @hide
5576 */
5577 public void dispatchFinishTemporaryDetach() {
5578 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005579 }
Romain Guy8506ab42009-06-11 17:35:47 -07005580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005581 /**
5582 * Called after {@link #onStartTemporaryDetach} when the container is done
5583 * changing the view.
5584 */
5585 public void onFinishTemporaryDetach() {
5586 }
Romain Guy8506ab42009-06-11 17:35:47 -07005587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005588 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005589 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5590 * for this view's window. Returns null if the view is not currently attached
5591 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005592 * just use the standard high-level event callbacks like
5593 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005594 */
5595 public KeyEvent.DispatcherState getKeyDispatcherState() {
5596 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5597 }
Joe Malin32736f02011-01-19 16:14:20 -08005598
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005599 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005600 * Dispatch a key event before it is processed by any input method
5601 * associated with the view hierarchy. This can be used to intercept
5602 * key events in special situations before the IME consumes them; a
5603 * typical example would be handling the BACK key to update the application's
5604 * UI instead of allowing the IME to see it and close itself.
5605 *
5606 * @param event The key event to be dispatched.
5607 * @return True if the event was handled, false otherwise.
5608 */
5609 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5610 return onKeyPreIme(event.getKeyCode(), event);
5611 }
5612
5613 /**
5614 * Dispatch a key event to the next view on the focus path. This path runs
5615 * from the top of the view tree down to the currently focused view. If this
5616 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5617 * the next node down the focus path. This method also fires any key
5618 * listeners.
5619 *
5620 * @param event The key event to be dispatched.
5621 * @return True if the event was handled, false otherwise.
5622 */
5623 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005624 if (mInputEventConsistencyVerifier != null) {
5625 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005627
Jeff Brown21bc5c92011-02-28 18:27:14 -08005628 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005629 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005630 ListenerInfo li = mListenerInfo;
5631 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5632 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005633 return true;
5634 }
5635
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005636 if (event.dispatch(this, mAttachInfo != null
5637 ? mAttachInfo.mKeyDispatchState : null, this)) {
5638 return true;
5639 }
5640
5641 if (mInputEventConsistencyVerifier != null) {
5642 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5643 }
5644 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005645 }
5646
5647 /**
5648 * Dispatches a key shortcut event.
5649 *
5650 * @param event The key event to be dispatched.
5651 * @return True if the event was handled by the view, false otherwise.
5652 */
5653 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5654 return onKeyShortcut(event.getKeyCode(), event);
5655 }
5656
5657 /**
5658 * Pass the touch screen motion event down to the target view, or this
5659 * view if it is the target.
5660 *
5661 * @param event The motion event to be dispatched.
5662 * @return True if the event was handled by the view, false otherwise.
5663 */
5664 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005665 if (mInputEventConsistencyVerifier != null) {
5666 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5667 }
5668
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005669 if (onFilterTouchEventForSecurity(event)) {
5670 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005671 ListenerInfo li = mListenerInfo;
5672 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5673 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005674 return true;
5675 }
5676
5677 if (onTouchEvent(event)) {
5678 return true;
5679 }
Jeff Brown85a31762010-09-01 17:01:00 -07005680 }
5681
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005682 if (mInputEventConsistencyVerifier != null) {
5683 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005684 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005685 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005686 }
5687
5688 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005689 * Filter the touch event to apply security policies.
5690 *
5691 * @param event The motion event to be filtered.
5692 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005693 *
Jeff Brown85a31762010-09-01 17:01:00 -07005694 * @see #getFilterTouchesWhenObscured
5695 */
5696 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005697 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005698 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5699 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5700 // Window is obscured, drop this touch.
5701 return false;
5702 }
5703 return true;
5704 }
5705
5706 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005707 * Pass a trackball motion event down to the focused view.
5708 *
5709 * @param event The motion event to be dispatched.
5710 * @return True if the event was handled by the view, false otherwise.
5711 */
5712 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005713 if (mInputEventConsistencyVerifier != null) {
5714 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5715 }
5716
Romain Guy02ccac62011-06-24 13:20:23 -07005717 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005718 }
5719
5720 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005721 * Dispatch a generic motion event.
5722 * <p>
5723 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5724 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005725 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005726 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005727 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005728 *
5729 * @param event The motion event to be dispatched.
5730 * @return True if the event was handled by the view, false otherwise.
5731 */
5732 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005733 if (mInputEventConsistencyVerifier != null) {
5734 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5735 }
5736
Jeff Browna032cc02011-03-07 16:56:21 -08005737 final int source = event.getSource();
5738 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5739 final int action = event.getAction();
5740 if (action == MotionEvent.ACTION_HOVER_ENTER
5741 || action == MotionEvent.ACTION_HOVER_MOVE
5742 || action == MotionEvent.ACTION_HOVER_EXIT) {
5743 if (dispatchHoverEvent(event)) {
5744 return true;
5745 }
5746 } else if (dispatchGenericPointerEvent(event)) {
5747 return true;
5748 }
5749 } else if (dispatchGenericFocusedEvent(event)) {
5750 return true;
5751 }
5752
Jeff Brown10b62902011-06-20 16:40:37 -07005753 if (dispatchGenericMotionEventInternal(event)) {
5754 return true;
5755 }
5756
5757 if (mInputEventConsistencyVerifier != null) {
5758 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5759 }
5760 return false;
5761 }
5762
5763 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005764 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005765 ListenerInfo li = mListenerInfo;
5766 if (li != null && li.mOnGenericMotionListener != null
5767 && (mViewFlags & ENABLED_MASK) == ENABLED
5768 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005769 return true;
5770 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005771
5772 if (onGenericMotionEvent(event)) {
5773 return true;
5774 }
5775
5776 if (mInputEventConsistencyVerifier != null) {
5777 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5778 }
5779 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005780 }
5781
5782 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005783 * Dispatch a hover event.
5784 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005785 * Do not call this method directly.
5786 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005787 * </p>
5788 *
5789 * @param event The motion event to be dispatched.
5790 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005791 */
5792 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005793 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005794 ListenerInfo li = mListenerInfo;
5795 if (li != null && li.mOnHoverListener != null
5796 && (mViewFlags & ENABLED_MASK) == ENABLED
5797 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07005798 return true;
5799 }
5800
Jeff Browna032cc02011-03-07 16:56:21 -08005801 return onHoverEvent(event);
5802 }
5803
5804 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005805 * Returns true if the view has a child to which it has recently sent
5806 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5807 * it does not have a hovered child, then it must be the innermost hovered view.
5808 * @hide
5809 */
5810 protected boolean hasHoveredChild() {
5811 return false;
5812 }
5813
5814 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005815 * Dispatch a generic motion event to the view under the first pointer.
5816 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005817 * Do not call this method directly.
5818 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005819 * </p>
5820 *
5821 * @param event The motion event to be dispatched.
5822 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005823 */
5824 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5825 return false;
5826 }
5827
5828 /**
5829 * Dispatch a generic motion event to the currently focused view.
5830 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005831 * Do not call this method directly.
5832 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005833 * </p>
5834 *
5835 * @param event The motion event to be dispatched.
5836 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005837 */
5838 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5839 return false;
5840 }
5841
5842 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005843 * Dispatch a pointer event.
5844 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005845 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5846 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5847 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005848 * and should not be expected to handle other pointing device features.
5849 * </p>
5850 *
5851 * @param event The motion event to be dispatched.
5852 * @return True if the event was handled by the view, false otherwise.
5853 * @hide
5854 */
5855 public final boolean dispatchPointerEvent(MotionEvent event) {
5856 if (event.isTouchEvent()) {
5857 return dispatchTouchEvent(event);
5858 } else {
5859 return dispatchGenericMotionEvent(event);
5860 }
5861 }
5862
5863 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005864 * Called when the window containing this view gains or loses window focus.
5865 * ViewGroups should override to route to their children.
5866 *
5867 * @param hasFocus True if the window containing this view now has focus,
5868 * false otherwise.
5869 */
5870 public void dispatchWindowFocusChanged(boolean hasFocus) {
5871 onWindowFocusChanged(hasFocus);
5872 }
5873
5874 /**
5875 * Called when the window containing this view gains or loses focus. Note
5876 * that this is separate from view focus: to receive key events, both
5877 * your view and its window must have focus. If a window is displayed
5878 * on top of yours that takes input focus, then your own window will lose
5879 * focus but the view focus will remain unchanged.
5880 *
5881 * @param hasWindowFocus True if the window containing this view now has
5882 * focus, false otherwise.
5883 */
5884 public void onWindowFocusChanged(boolean hasWindowFocus) {
5885 InputMethodManager imm = InputMethodManager.peekInstance();
5886 if (!hasWindowFocus) {
5887 if (isPressed()) {
5888 setPressed(false);
5889 }
5890 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5891 imm.focusOut(this);
5892 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005893 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005894 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005895 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005896 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5897 imm.focusIn(this);
5898 }
5899 refreshDrawableState();
5900 }
5901
5902 /**
5903 * Returns true if this view is in a window that currently has window focus.
5904 * Note that this is not the same as the view itself having focus.
5905 *
5906 * @return True if this view is in a window that currently has window focus.
5907 */
5908 public boolean hasWindowFocus() {
5909 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5910 }
5911
5912 /**
Adam Powell326d8082009-12-09 15:10:07 -08005913 * Dispatch a view visibility change down the view hierarchy.
5914 * ViewGroups should override to route to their children.
5915 * @param changedView The view whose visibility changed. Could be 'this' or
5916 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005917 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5918 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005919 */
5920 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5921 onVisibilityChanged(changedView, visibility);
5922 }
5923
5924 /**
5925 * Called when the visibility of the view or an ancestor of the view is changed.
5926 * @param changedView The view whose visibility changed. Could be 'this' or
5927 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005928 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5929 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005930 */
5931 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005932 if (visibility == VISIBLE) {
5933 if (mAttachInfo != null) {
5934 initialAwakenScrollBars();
5935 } else {
5936 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5937 }
5938 }
Adam Powell326d8082009-12-09 15:10:07 -08005939 }
5940
5941 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005942 * Dispatch a hint about whether this view is displayed. For instance, when
5943 * a View moves out of the screen, it might receives a display hint indicating
5944 * the view is not displayed. Applications should not <em>rely</em> on this hint
5945 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005946 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005947 * @param hint A hint about whether or not this view is displayed:
5948 * {@link #VISIBLE} or {@link #INVISIBLE}.
5949 */
5950 public void dispatchDisplayHint(int hint) {
5951 onDisplayHint(hint);
5952 }
5953
5954 /**
5955 * Gives this view a hint about whether is displayed or not. For instance, when
5956 * a View moves out of the screen, it might receives a display hint indicating
5957 * the view is not displayed. Applications should not <em>rely</em> on this hint
5958 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005959 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005960 * @param hint A hint about whether or not this view is displayed:
5961 * {@link #VISIBLE} or {@link #INVISIBLE}.
5962 */
5963 protected void onDisplayHint(int hint) {
5964 }
5965
5966 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005967 * Dispatch a window visibility change down the view hierarchy.
5968 * ViewGroups should override to route to their children.
5969 *
5970 * @param visibility The new visibility of the window.
5971 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005972 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005973 */
5974 public void dispatchWindowVisibilityChanged(int visibility) {
5975 onWindowVisibilityChanged(visibility);
5976 }
5977
5978 /**
5979 * Called when the window containing has change its visibility
5980 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5981 * that this tells you whether or not your window is being made visible
5982 * to the window manager; this does <em>not</em> tell you whether or not
5983 * your window is obscured by other windows on the screen, even if it
5984 * is itself visible.
5985 *
5986 * @param visibility The new visibility of the window.
5987 */
5988 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005989 if (visibility == VISIBLE) {
5990 initialAwakenScrollBars();
5991 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005992 }
5993
5994 /**
5995 * Returns the current visibility of the window this view is attached to
5996 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5997 *
5998 * @return Returns the current visibility of the view's window.
5999 */
6000 public int getWindowVisibility() {
6001 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
6002 }
6003
6004 /**
6005 * Retrieve the overall visible display size in which the window this view is
6006 * attached to has been positioned in. This takes into account screen
6007 * decorations above the window, for both cases where the window itself
6008 * is being position inside of them or the window is being placed under
6009 * then and covered insets are used for the window to position its content
6010 * inside. In effect, this tells you the available area where content can
6011 * be placed and remain visible to users.
6012 *
6013 * <p>This function requires an IPC back to the window manager to retrieve
6014 * the requested information, so should not be used in performance critical
6015 * code like drawing.
6016 *
6017 * @param outRect Filled in with the visible display frame. If the view
6018 * is not attached to a window, this is simply the raw display size.
6019 */
6020 public void getWindowVisibleDisplayFrame(Rect outRect) {
6021 if (mAttachInfo != null) {
6022 try {
6023 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6024 } catch (RemoteException e) {
6025 return;
6026 }
6027 // XXX This is really broken, and probably all needs to be done
6028 // in the window manager, and we need to know more about whether
6029 // we want the area behind or in front of the IME.
6030 final Rect insets = mAttachInfo.mVisibleInsets;
6031 outRect.left += insets.left;
6032 outRect.top += insets.top;
6033 outRect.right -= insets.right;
6034 outRect.bottom -= insets.bottom;
6035 return;
6036 }
6037 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006038 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 }
6040
6041 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006042 * Dispatch a notification about a resource configuration change down
6043 * the view hierarchy.
6044 * ViewGroups should override to route to their children.
6045 *
6046 * @param newConfig The new resource configuration.
6047 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006048 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006049 */
6050 public void dispatchConfigurationChanged(Configuration newConfig) {
6051 onConfigurationChanged(newConfig);
6052 }
6053
6054 /**
6055 * Called when the current configuration of the resources being used
6056 * by the application have changed. You can use this to decide when
6057 * to reload resources that can changed based on orientation and other
6058 * configuration characterstics. You only need to use this if you are
6059 * not relying on the normal {@link android.app.Activity} mechanism of
6060 * recreating the activity instance upon a configuration change.
6061 *
6062 * @param newConfig The new resource configuration.
6063 */
6064 protected void onConfigurationChanged(Configuration newConfig) {
6065 }
6066
6067 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006068 * Private function to aggregate all per-view attributes in to the view
6069 * root.
6070 */
6071 void dispatchCollectViewAttributes(int visibility) {
6072 performCollectViewAttributes(visibility);
6073 }
6074
6075 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08006076 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006077 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
6078 mAttachInfo.mKeepScreenOn = true;
6079 }
6080 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006081 ListenerInfo li = mListenerInfo;
6082 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006083 mAttachInfo.mHasSystemUiListeners = true;
6084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006085 }
6086 }
6087
6088 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006089 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006090 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006091 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6092 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006093 ai.mRecomputeGlobalAttributes = true;
6094 }
6095 }
6096 }
6097
6098 /**
6099 * Returns whether the device is currently in touch mode. Touch mode is entered
6100 * once the user begins interacting with the device by touch, and affects various
6101 * things like whether focus is always visible to the user.
6102 *
6103 * @return Whether the device is in touch mode.
6104 */
6105 @ViewDebug.ExportedProperty
6106 public boolean isInTouchMode() {
6107 if (mAttachInfo != null) {
6108 return mAttachInfo.mInTouchMode;
6109 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006110 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006111 }
6112 }
6113
6114 /**
6115 * Returns the context the view is running in, through which it can
6116 * access the current theme, resources, etc.
6117 *
6118 * @return The view's Context.
6119 */
6120 @ViewDebug.CapturedViewProperty
6121 public final Context getContext() {
6122 return mContext;
6123 }
6124
6125 /**
6126 * Handle a key event before it is processed by any input method
6127 * associated with the view hierarchy. This can be used to intercept
6128 * key events in special situations before the IME consumes them; a
6129 * typical example would be handling the BACK key to update the application's
6130 * UI instead of allowing the IME to see it and close itself.
6131 *
6132 * @param keyCode The value in event.getKeyCode().
6133 * @param event Description of the key event.
6134 * @return If you handled the event, return true. If you want to allow the
6135 * event to be handled by the next receiver, return false.
6136 */
6137 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
6138 return false;
6139 }
6140
6141 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006142 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
6143 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006144 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
6145 * is released, if the view is enabled and clickable.
6146 *
6147 * @param keyCode A key code that represents the button pressed, from
6148 * {@link android.view.KeyEvent}.
6149 * @param event The KeyEvent object that defines the button action.
6150 */
6151 public boolean onKeyDown(int keyCode, KeyEvent event) {
6152 boolean result = false;
6153
6154 switch (keyCode) {
6155 case KeyEvent.KEYCODE_DPAD_CENTER:
6156 case KeyEvent.KEYCODE_ENTER: {
6157 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6158 return true;
6159 }
6160 // Long clickable items don't necessarily have to be clickable
6161 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
6162 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
6163 (event.getRepeatCount() == 0)) {
6164 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006165 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006166 return true;
6167 }
6168 break;
6169 }
6170 }
6171 return result;
6172 }
6173
6174 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006175 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
6176 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
6177 * the event).
6178 */
6179 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
6180 return false;
6181 }
6182
6183 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006184 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
6185 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006186 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
6187 * {@link KeyEvent#KEYCODE_ENTER} is released.
6188 *
6189 * @param keyCode A key code that represents the button pressed, from
6190 * {@link android.view.KeyEvent}.
6191 * @param event The KeyEvent object that defines the button action.
6192 */
6193 public boolean onKeyUp(int keyCode, KeyEvent event) {
6194 boolean result = false;
6195
6196 switch (keyCode) {
6197 case KeyEvent.KEYCODE_DPAD_CENTER:
6198 case KeyEvent.KEYCODE_ENTER: {
6199 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6200 return true;
6201 }
6202 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
6203 setPressed(false);
6204
6205 if (!mHasPerformedLongPress) {
6206 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006207 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006208
6209 result = performClick();
6210 }
6211 }
6212 break;
6213 }
6214 }
6215 return result;
6216 }
6217
6218 /**
6219 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6220 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6221 * the event).
6222 *
6223 * @param keyCode A key code that represents the button pressed, from
6224 * {@link android.view.KeyEvent}.
6225 * @param repeatCount The number of times the action was made.
6226 * @param event The KeyEvent object that defines the button action.
6227 */
6228 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6229 return false;
6230 }
6231
6232 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006233 * Called on the focused view when a key shortcut event is not handled.
6234 * Override this method to implement local key shortcuts for the View.
6235 * Key shortcuts can also be implemented by setting the
6236 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006237 *
6238 * @param keyCode The value in event.getKeyCode().
6239 * @param event Description of the key event.
6240 * @return If you handled the event, return true. If you want to allow the
6241 * event to be handled by the next receiver, return false.
6242 */
6243 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6244 return false;
6245 }
6246
6247 /**
6248 * Check whether the called view is a text editor, in which case it
6249 * would make sense to automatically display a soft input window for
6250 * it. Subclasses should override this if they implement
6251 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006252 * a call on that method would return a non-null InputConnection, and
6253 * they are really a first-class editor that the user would normally
6254 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006255 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006256 * <p>The default implementation always returns false. This does
6257 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6258 * will not be called or the user can not otherwise perform edits on your
6259 * view; it is just a hint to the system that this is not the primary
6260 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006261 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006262 * @return Returns true if this view is a text editor, else false.
6263 */
6264 public boolean onCheckIsTextEditor() {
6265 return false;
6266 }
Romain Guy8506ab42009-06-11 17:35:47 -07006267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 /**
6269 * Create a new InputConnection for an InputMethod to interact
6270 * with the view. The default implementation returns null, since it doesn't
6271 * support input methods. You can override this to implement such support.
6272 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006273 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006274 * <p>When implementing this, you probably also want to implement
6275 * {@link #onCheckIsTextEditor()} to indicate you will return a
6276 * non-null InputConnection.
6277 *
6278 * @param outAttrs Fill in with attribute information about the connection.
6279 */
6280 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6281 return null;
6282 }
6283
6284 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006285 * Called by the {@link android.view.inputmethod.InputMethodManager}
6286 * when a view who is not the current
6287 * input connection target is trying to make a call on the manager. The
6288 * default implementation returns false; you can override this to return
6289 * true for certain views if you are performing InputConnection proxying
6290 * to them.
6291 * @param view The View that is making the InputMethodManager call.
6292 * @return Return true to allow the call, false to reject.
6293 */
6294 public boolean checkInputConnectionProxy(View view) {
6295 return false;
6296 }
Romain Guy8506ab42009-06-11 17:35:47 -07006297
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006298 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006299 * Show the context menu for this view. It is not safe to hold on to the
6300 * menu after returning from this method.
6301 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006302 * You should normally not overload this method. Overload
6303 * {@link #onCreateContextMenu(ContextMenu)} or define an
6304 * {@link OnCreateContextMenuListener} to add items to the context menu.
6305 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006306 * @param menu The context menu to populate
6307 */
6308 public void createContextMenu(ContextMenu menu) {
6309 ContextMenuInfo menuInfo = getContextMenuInfo();
6310
6311 // Sets the current menu info so all items added to menu will have
6312 // my extra info set.
6313 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6314
6315 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006316 ListenerInfo li = mListenerInfo;
6317 if (li != null && li.mOnCreateContextMenuListener != null) {
6318 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006319 }
6320
6321 // Clear the extra information so subsequent items that aren't mine don't
6322 // have my extra info.
6323 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6324
6325 if (mParent != null) {
6326 mParent.createContextMenu(menu);
6327 }
6328 }
6329
6330 /**
6331 * Views should implement this if they have extra information to associate
6332 * with the context menu. The return result is supplied as a parameter to
6333 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6334 * callback.
6335 *
6336 * @return Extra information about the item for which the context menu
6337 * should be shown. This information will vary across different
6338 * subclasses of View.
6339 */
6340 protected ContextMenuInfo getContextMenuInfo() {
6341 return null;
6342 }
6343
6344 /**
6345 * Views should implement this if the view itself is going to add items to
6346 * the context menu.
6347 *
6348 * @param menu the context menu to populate
6349 */
6350 protected void onCreateContextMenu(ContextMenu menu) {
6351 }
6352
6353 /**
6354 * Implement this method to handle trackball motion events. The
6355 * <em>relative</em> movement of the trackball since the last event
6356 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6357 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6358 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6359 * they will often be fractional values, representing the more fine-grained
6360 * movement information available from a trackball).
6361 *
6362 * @param event The motion event.
6363 * @return True if the event was handled, false otherwise.
6364 */
6365 public boolean onTrackballEvent(MotionEvent event) {
6366 return false;
6367 }
6368
6369 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006370 * Implement this method to handle generic motion events.
6371 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006372 * Generic motion events describe joystick movements, mouse hovers, track pad
6373 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006374 * {@link MotionEvent#getSource() source} of the motion event specifies
6375 * the class of input that was received. Implementations of this method
6376 * must examine the bits in the source before processing the event.
6377 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006378 * </p><p>
6379 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6380 * are delivered to the view under the pointer. All other generic motion events are
6381 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006382 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07006383 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006384 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006385 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6386 * // process the joystick movement...
6387 * return true;
6388 * }
6389 * }
6390 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6391 * switch (event.getAction()) {
6392 * case MotionEvent.ACTION_HOVER_MOVE:
6393 * // process the mouse hover movement...
6394 * return true;
6395 * case MotionEvent.ACTION_SCROLL:
6396 * // process the scroll wheel movement...
6397 * return true;
6398 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006399 * }
6400 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07006401 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08006402 *
6403 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006404 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006405 */
6406 public boolean onGenericMotionEvent(MotionEvent event) {
6407 return false;
6408 }
6409
6410 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006411 * Implement this method to handle hover events.
6412 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006413 * This method is called whenever a pointer is hovering into, over, or out of the
6414 * bounds of a view and the view is not currently being touched.
6415 * Hover events are represented as pointer events with action
6416 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6417 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6418 * </p>
6419 * <ul>
6420 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6421 * when the pointer enters the bounds of the view.</li>
6422 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6423 * when the pointer has already entered the bounds of the view and has moved.</li>
6424 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6425 * when the pointer has exited the bounds of the view or when the pointer is
6426 * about to go down due to a button click, tap, or similar user action that
6427 * causes the view to be touched.</li>
6428 * </ul>
6429 * <p>
6430 * The view should implement this method to return true to indicate that it is
6431 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006432 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006433 * The default implementation calls {@link #setHovered} to update the hovered state
6434 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006435 * is enabled and is clickable. The default implementation also sends hover
6436 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006437 * </p>
6438 *
6439 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006440 * @return True if the view handled the hover event.
6441 *
6442 * @see #isHovered
6443 * @see #setHovered
6444 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006445 */
6446 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006447 // The root view may receive hover (or touch) events that are outside the bounds of
6448 // the window. This code ensures that we only send accessibility events for
6449 // hovers that are actually within the bounds of the root view.
6450 final int action = event.getAction();
6451 if (!mSendingHoverAccessibilityEvents) {
6452 if ((action == MotionEvent.ACTION_HOVER_ENTER
6453 || action == MotionEvent.ACTION_HOVER_MOVE)
6454 && !hasHoveredChild()
6455 && pointInView(event.getX(), event.getY())) {
6456 mSendingHoverAccessibilityEvents = true;
6457 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6458 }
6459 } else {
6460 if (action == MotionEvent.ACTION_HOVER_EXIT
6461 || (action == MotionEvent.ACTION_HOVER_MOVE
6462 && !pointInView(event.getX(), event.getY()))) {
6463 mSendingHoverAccessibilityEvents = false;
6464 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6465 }
Jeff Browna1b24182011-07-28 13:38:24 -07006466 }
6467
Jeff Brown87b7f802011-06-21 18:35:45 -07006468 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006469 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006470 case MotionEvent.ACTION_HOVER_ENTER:
6471 setHovered(true);
6472 break;
6473 case MotionEvent.ACTION_HOVER_EXIT:
6474 setHovered(false);
6475 break;
6476 }
Jeff Browna1b24182011-07-28 13:38:24 -07006477
6478 // Dispatch the event to onGenericMotionEvent before returning true.
6479 // This is to provide compatibility with existing applications that
6480 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6481 // break because of the new default handling for hoverable views
6482 // in onHoverEvent.
6483 // Note that onGenericMotionEvent will be called by default when
6484 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6485 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006486 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006487 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006488 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006489 }
6490
6491 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006492 * Returns true if the view should handle {@link #onHoverEvent}
6493 * by calling {@link #setHovered} to change its hovered state.
6494 *
6495 * @return True if the view is hoverable.
6496 */
6497 private boolean isHoverable() {
6498 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006499 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006500 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6501 return false;
6502 }
6503
6504 return (viewFlags & CLICKABLE) == CLICKABLE
6505 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6506 }
6507
6508 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006509 * Returns true if the view is currently hovered.
6510 *
6511 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006512 *
6513 * @see #setHovered
6514 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006515 */
Jeff Brown10b62902011-06-20 16:40:37 -07006516 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006517 public boolean isHovered() {
6518 return (mPrivateFlags & HOVERED) != 0;
6519 }
6520
6521 /**
6522 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006523 * <p>
6524 * Calling this method also changes the drawable state of the view. This
6525 * enables the view to react to hover by using different drawable resources
6526 * to change its appearance.
6527 * </p><p>
6528 * The {@link #onHoverChanged} method is called when the hovered state changes.
6529 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006530 *
6531 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006532 *
6533 * @see #isHovered
6534 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006535 */
6536 public void setHovered(boolean hovered) {
6537 if (hovered) {
6538 if ((mPrivateFlags & HOVERED) == 0) {
6539 mPrivateFlags |= HOVERED;
6540 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006541 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006542 }
6543 } else {
6544 if ((mPrivateFlags & HOVERED) != 0) {
6545 mPrivateFlags &= ~HOVERED;
6546 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006547 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006548 }
6549 }
6550 }
6551
6552 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006553 * Implement this method to handle hover state changes.
6554 * <p>
6555 * This method is called whenever the hover state changes as a result of a
6556 * call to {@link #setHovered}.
6557 * </p>
6558 *
6559 * @param hovered The current hover state, as returned by {@link #isHovered}.
6560 *
6561 * @see #isHovered
6562 * @see #setHovered
6563 */
6564 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006565 }
6566
6567 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006568 * Implement this method to handle touch screen motion events.
6569 *
6570 * @param event The motion event.
6571 * @return True if the event was handled, false otherwise.
6572 */
6573 public boolean onTouchEvent(MotionEvent event) {
6574 final int viewFlags = mViewFlags;
6575
6576 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006577 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08006578 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006580 // A disabled view that is clickable still consumes the touch
6581 // events, it just doesn't respond to them.
6582 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6583 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6584 }
6585
6586 if (mTouchDelegate != null) {
6587 if (mTouchDelegate.onTouchEvent(event)) {
6588 return true;
6589 }
6590 }
6591
6592 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6593 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6594 switch (event.getAction()) {
6595 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006596 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6597 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006598 // take focus if we don't have it already and we should in
6599 // touch mode.
6600 boolean focusTaken = false;
6601 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6602 focusTaken = requestFocus();
6603 }
6604
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006605 if (prepressed) {
6606 // The button is being released before we actually
6607 // showed it as pressed. Make it show the pressed
6608 // state now (before scheduling the click) to ensure
6609 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08006610 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006611 }
Joe Malin32736f02011-01-19 16:14:20 -08006612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006613 if (!mHasPerformedLongPress) {
6614 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006615 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006616
6617 // Only perform take click actions if we were in the pressed state
6618 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006619 // Use a Runnable and post this rather than calling
6620 // performClick directly. This lets other visual state
6621 // of the view update before click actions start.
6622 if (mPerformClick == null) {
6623 mPerformClick = new PerformClick();
6624 }
6625 if (!post(mPerformClick)) {
6626 performClick();
6627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006628 }
6629 }
6630
6631 if (mUnsetPressedState == null) {
6632 mUnsetPressedState = new UnsetPressedState();
6633 }
6634
Adam Powelle14579b2009-12-16 18:39:52 -08006635 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006636 postDelayed(mUnsetPressedState,
6637 ViewConfiguration.getPressedStateDuration());
6638 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006639 // If the post failed, unpress right now
6640 mUnsetPressedState.run();
6641 }
Adam Powelle14579b2009-12-16 18:39:52 -08006642 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006643 }
6644 break;
6645
6646 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006647 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006648
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006649 if (performButtonActionOnTouchDown(event)) {
6650 break;
6651 }
6652
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006653 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006654 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006655
6656 // For views inside a scrolling container, delay the pressed feedback for
6657 // a short period in case this is a scroll.
6658 if (isInScrollingContainer) {
6659 mPrivateFlags |= PREPRESSED;
6660 if (mPendingCheckForTap == null) {
6661 mPendingCheckForTap = new CheckForTap();
6662 }
6663 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6664 } else {
6665 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08006666 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006667 checkForLongClick(0);
6668 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006669 break;
6670
6671 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08006672 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08006673 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006674 break;
6675
6676 case MotionEvent.ACTION_MOVE:
6677 final int x = (int) event.getX();
6678 final int y = (int) event.getY();
6679
6680 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006681 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006682 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006683 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006684 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006685 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006686 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006687
Adam Powell4d6f0662012-02-21 15:11:11 -08006688 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006690 }
6691 break;
6692 }
6693 return true;
6694 }
6695
6696 return false;
6697 }
6698
6699 /**
Adam Powell10298662011-08-14 18:26:30 -07006700 * @hide
6701 */
6702 public boolean isInScrollingContainer() {
6703 ViewParent p = getParent();
6704 while (p != null && p instanceof ViewGroup) {
6705 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6706 return true;
6707 }
6708 p = p.getParent();
6709 }
6710 return false;
6711 }
6712
6713 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006714 * Remove the longpress detection timer.
6715 */
6716 private void removeLongPressCallback() {
6717 if (mPendingCheckForLongPress != null) {
6718 removeCallbacks(mPendingCheckForLongPress);
6719 }
6720 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006721
6722 /**
6723 * Remove the pending click action
6724 */
6725 private void removePerformClickCallback() {
6726 if (mPerformClick != null) {
6727 removeCallbacks(mPerformClick);
6728 }
6729 }
6730
Adam Powelle14579b2009-12-16 18:39:52 -08006731 /**
Romain Guya440b002010-02-24 15:57:54 -08006732 * Remove the prepress detection timer.
6733 */
6734 private void removeUnsetPressCallback() {
6735 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6736 setPressed(false);
6737 removeCallbacks(mUnsetPressedState);
6738 }
6739 }
6740
6741 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006742 * Remove the tap detection timer.
6743 */
6744 private void removeTapCallback() {
6745 if (mPendingCheckForTap != null) {
6746 mPrivateFlags &= ~PREPRESSED;
6747 removeCallbacks(mPendingCheckForTap);
6748 }
6749 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006750
6751 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006752 * Cancels a pending long press. Your subclass can use this if you
6753 * want the context menu to come up if the user presses and holds
6754 * at the same place, but you don't want it to come up if they press
6755 * and then move around enough to cause scrolling.
6756 */
6757 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006758 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006759
6760 /*
6761 * The prepressed state handled by the tap callback is a display
6762 * construct, but the tap callback will post a long press callback
6763 * less its own timeout. Remove it here.
6764 */
6765 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006766 }
6767
6768 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006769 * Remove the pending callback for sending a
6770 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6771 */
6772 private void removeSendViewScrolledAccessibilityEventCallback() {
6773 if (mSendViewScrolledAccessibilityEvent != null) {
6774 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6775 }
6776 }
6777
6778 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006779 * Sets the TouchDelegate for this View.
6780 */
6781 public void setTouchDelegate(TouchDelegate delegate) {
6782 mTouchDelegate = delegate;
6783 }
6784
6785 /**
6786 * Gets the TouchDelegate for this View.
6787 */
6788 public TouchDelegate getTouchDelegate() {
6789 return mTouchDelegate;
6790 }
6791
6792 /**
6793 * Set flags controlling behavior of this view.
6794 *
6795 * @param flags Constant indicating the value which should be set
6796 * @param mask Constant indicating the bit range that should be changed
6797 */
6798 void setFlags(int flags, int mask) {
6799 int old = mViewFlags;
6800 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6801
6802 int changed = mViewFlags ^ old;
6803 if (changed == 0) {
6804 return;
6805 }
6806 int privateFlags = mPrivateFlags;
6807
6808 /* Check if the FOCUSABLE bit has changed */
6809 if (((changed & FOCUSABLE_MASK) != 0) &&
6810 ((privateFlags & HAS_BOUNDS) !=0)) {
6811 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6812 && ((privateFlags & FOCUSED) != 0)) {
6813 /* Give up focus if we are no longer focusable */
6814 clearFocus();
6815 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6816 && ((privateFlags & FOCUSED) == 0)) {
6817 /*
6818 * Tell the view system that we are now available to take focus
6819 * if no one else already has it.
6820 */
6821 if (mParent != null) mParent.focusableViewAvailable(this);
6822 }
6823 }
6824
6825 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6826 if ((changed & VISIBILITY_MASK) != 0) {
6827 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006828 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006829 * it was not visible. Marking it drawn ensures that the invalidation will
6830 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006831 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006832 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006833 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006834
6835 needGlobalAttributesUpdate(true);
6836
6837 // a view becoming visible is worth notifying the parent
6838 // about in case nothing has focus. even if this specific view
6839 // isn't focusable, it may contain something that is, so let
6840 // the root view try to give this focus if nothing else does.
6841 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6842 mParent.focusableViewAvailable(this);
6843 }
6844 }
6845 }
6846
6847 /* Check if the GONE bit has changed */
6848 if ((changed & GONE) != 0) {
6849 needGlobalAttributesUpdate(false);
6850 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006851
Romain Guyecd80ee2009-12-03 17:13:02 -08006852 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6853 if (hasFocus()) clearFocus();
6854 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006855 if (mParent instanceof View) {
6856 // GONE views noop invalidation, so invalidate the parent
6857 ((View) mParent).invalidate(true);
6858 }
6859 // Mark the view drawn to ensure that it gets invalidated properly the next
6860 // time it is visible and gets invalidated
6861 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006862 }
6863 if (mAttachInfo != null) {
6864 mAttachInfo.mViewVisibilityChanged = true;
6865 }
6866 }
6867
6868 /* Check if the VISIBLE bit has changed */
6869 if ((changed & INVISIBLE) != 0) {
6870 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006871 /*
6872 * If this view is becoming invisible, set the DRAWN flag so that
6873 * the next invalidate() will not be skipped.
6874 */
6875 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006876
6877 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6878 // root view becoming invisible shouldn't clear focus
6879 if (getRootView() != this) {
6880 clearFocus();
6881 }
6882 }
6883 if (mAttachInfo != null) {
6884 mAttachInfo.mViewVisibilityChanged = true;
6885 }
6886 }
6887
Adam Powell326d8082009-12-09 15:10:07 -08006888 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006889 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08006890 ((ViewGroup) mParent).onChildVisibilityChanged(this,
6891 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08006892 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006893 } else if (mParent != null) {
6894 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006895 }
Adam Powell326d8082009-12-09 15:10:07 -08006896 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6897 }
6898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006899 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6900 destroyDrawingCache();
6901 }
6902
6903 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6904 destroyDrawingCache();
6905 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006906 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006907 }
6908
6909 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6910 destroyDrawingCache();
6911 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6912 }
6913
6914 if ((changed & DRAW_MASK) != 0) {
6915 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6916 if (mBGDrawable != null) {
6917 mPrivateFlags &= ~SKIP_DRAW;
6918 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6919 } else {
6920 mPrivateFlags |= SKIP_DRAW;
6921 }
6922 } else {
6923 mPrivateFlags &= ~SKIP_DRAW;
6924 }
6925 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006926 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006927 }
6928
6929 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006930 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006931 mParent.recomputeViewAttributes(this);
6932 }
6933 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006934
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006935 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006936 requestLayout();
6937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006938 }
6939
6940 /**
6941 * Change the view's z order in the tree, so it's on top of other sibling
6942 * views
6943 */
6944 public void bringToFront() {
6945 if (mParent != null) {
6946 mParent.bringChildToFront(this);
6947 }
6948 }
6949
6950 /**
6951 * This is called in response to an internal scroll in this view (i.e., the
6952 * view scrolled its own contents). This is typically as a result of
6953 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6954 * called.
6955 *
6956 * @param l Current horizontal scroll origin.
6957 * @param t Current vertical scroll origin.
6958 * @param oldl Previous horizontal scroll origin.
6959 * @param oldt Previous vertical scroll origin.
6960 */
6961 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006962 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6963 postSendViewScrolledAccessibilityEventCallback();
6964 }
6965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006966 mBackgroundSizeChanged = true;
6967
6968 final AttachInfo ai = mAttachInfo;
6969 if (ai != null) {
6970 ai.mViewScrollChanged = true;
6971 }
6972 }
6973
6974 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006975 * Interface definition for a callback to be invoked when the layout bounds of a view
6976 * changes due to layout processing.
6977 */
6978 public interface OnLayoutChangeListener {
6979 /**
6980 * Called when the focus state of a view has changed.
6981 *
6982 * @param v The view whose state has changed.
6983 * @param left The new value of the view's left property.
6984 * @param top The new value of the view's top property.
6985 * @param right The new value of the view's right property.
6986 * @param bottom The new value of the view's bottom property.
6987 * @param oldLeft The previous value of the view's left property.
6988 * @param oldTop The previous value of the view's top property.
6989 * @param oldRight The previous value of the view's right property.
6990 * @param oldBottom The previous value of the view's bottom property.
6991 */
6992 void onLayoutChange(View v, int left, int top, int right, int bottom,
6993 int oldLeft, int oldTop, int oldRight, int oldBottom);
6994 }
6995
6996 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006997 * This is called during layout when the size of this view has changed. If
6998 * you were just added to the view hierarchy, you're called with the old
6999 * values of 0.
7000 *
7001 * @param w Current width of this view.
7002 * @param h Current height of this view.
7003 * @param oldw Old width of this view.
7004 * @param oldh Old height of this view.
7005 */
7006 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
7007 }
7008
7009 /**
7010 * Called by draw to draw the child views. This may be overridden
7011 * by derived classes to gain control just before its children are drawn
7012 * (but after its own view has been drawn).
7013 * @param canvas the canvas on which to draw the view
7014 */
7015 protected void dispatchDraw(Canvas canvas) {
7016 }
7017
7018 /**
7019 * Gets the parent of this view. Note that the parent is a
7020 * ViewParent and not necessarily a View.
7021 *
7022 * @return Parent of this view.
7023 */
7024 public final ViewParent getParent() {
7025 return mParent;
7026 }
7027
7028 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007029 * Set the horizontal scrolled position of your view. This will cause a call to
7030 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7031 * invalidated.
7032 * @param value the x position to scroll to
7033 */
7034 public void setScrollX(int value) {
7035 scrollTo(value, mScrollY);
7036 }
7037
7038 /**
7039 * Set the vertical scrolled position of your view. This will cause a call to
7040 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7041 * invalidated.
7042 * @param value the y position to scroll to
7043 */
7044 public void setScrollY(int value) {
7045 scrollTo(mScrollX, value);
7046 }
7047
7048 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007049 * Return the scrolled left position of this view. This is the left edge of
7050 * the displayed part of your view. You do not need to draw any pixels
7051 * farther left, since those are outside of the frame of your view on
7052 * screen.
7053 *
7054 * @return The left edge of the displayed part of your view, in pixels.
7055 */
7056 public final int getScrollX() {
7057 return mScrollX;
7058 }
7059
7060 /**
7061 * Return the scrolled top position of this view. This is the top edge of
7062 * the displayed part of your view. You do not need to draw any pixels above
7063 * it, since those are outside of the frame of your view on screen.
7064 *
7065 * @return The top edge of the displayed part of your view, in pixels.
7066 */
7067 public final int getScrollY() {
7068 return mScrollY;
7069 }
7070
7071 /**
7072 * Return the width of the your view.
7073 *
7074 * @return The width of your view, in pixels.
7075 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007076 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007077 public final int getWidth() {
7078 return mRight - mLeft;
7079 }
7080
7081 /**
7082 * Return the height of your view.
7083 *
7084 * @return The height of your view, in pixels.
7085 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007086 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007087 public final int getHeight() {
7088 return mBottom - mTop;
7089 }
7090
7091 /**
7092 * Return the visible drawing bounds of your view. Fills in the output
7093 * rectangle with the values from getScrollX(), getScrollY(),
7094 * getWidth(), and getHeight().
7095 *
7096 * @param outRect The (scrolled) drawing bounds of the view.
7097 */
7098 public void getDrawingRect(Rect outRect) {
7099 outRect.left = mScrollX;
7100 outRect.top = mScrollY;
7101 outRect.right = mScrollX + (mRight - mLeft);
7102 outRect.bottom = mScrollY + (mBottom - mTop);
7103 }
7104
7105 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007106 * Like {@link #getMeasuredWidthAndState()}, but only returns the
7107 * raw width component (that is the result is masked by
7108 * {@link #MEASURED_SIZE_MASK}).
7109 *
7110 * @return The raw measured width of this view.
7111 */
7112 public final int getMeasuredWidth() {
7113 return mMeasuredWidth & MEASURED_SIZE_MASK;
7114 }
7115
7116 /**
7117 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007118 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007119 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007120 * This should be used during measurement and layout calculations only. Use
7121 * {@link #getWidth()} to see how wide a view is after layout.
7122 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007123 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007124 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08007125 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007126 return mMeasuredWidth;
7127 }
7128
7129 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007130 * Like {@link #getMeasuredHeightAndState()}, but only returns the
7131 * raw width component (that is the result is masked by
7132 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007133 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007134 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007135 */
7136 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08007137 return mMeasuredHeight & MEASURED_SIZE_MASK;
7138 }
7139
7140 /**
7141 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007142 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007143 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
7144 * This should be used during measurement and layout calculations only. Use
7145 * {@link #getHeight()} to see how wide a view is after layout.
7146 *
7147 * @return The measured width of this view as a bit mask.
7148 */
7149 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007150 return mMeasuredHeight;
7151 }
7152
7153 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007154 * Return only the state bits of {@link #getMeasuredWidthAndState()}
7155 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
7156 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
7157 * and the height component is at the shifted bits
7158 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
7159 */
7160 public final int getMeasuredState() {
7161 return (mMeasuredWidth&MEASURED_STATE_MASK)
7162 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
7163 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
7164 }
7165
7166 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007167 * The transform matrix of this view, which is calculated based on the current
7168 * roation, scale, and pivot properties.
7169 *
7170 * @see #getRotation()
7171 * @see #getScaleX()
7172 * @see #getScaleY()
7173 * @see #getPivotX()
7174 * @see #getPivotY()
7175 * @return The current transform matrix for the view
7176 */
7177 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007178 if (mTransformationInfo != null) {
7179 updateMatrix();
7180 return mTransformationInfo.mMatrix;
7181 }
7182 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07007183 }
7184
7185 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007186 * Utility function to determine if the value is far enough away from zero to be
7187 * considered non-zero.
7188 * @param value A floating point value to check for zero-ness
7189 * @return whether the passed-in value is far enough away from zero to be considered non-zero
7190 */
7191 private static boolean nonzero(float value) {
7192 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
7193 }
7194
7195 /**
Jeff Brown86671742010-09-30 20:00:15 -07007196 * Returns true if the transform matrix is the identity matrix.
7197 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08007198 *
Romain Guy33e72ae2010-07-17 12:40:29 -07007199 * @return True if the transform matrix is the identity matrix, false otherwise.
7200 */
Jeff Brown86671742010-09-30 20:00:15 -07007201 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007202 if (mTransformationInfo != null) {
7203 updateMatrix();
7204 return mTransformationInfo.mMatrixIsIdentity;
7205 }
7206 return true;
7207 }
7208
7209 void ensureTransformationInfo() {
7210 if (mTransformationInfo == null) {
7211 mTransformationInfo = new TransformationInfo();
7212 }
Jeff Brown86671742010-09-30 20:00:15 -07007213 }
7214
7215 /**
7216 * Recomputes the transform matrix if necessary.
7217 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007218 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007219 final TransformationInfo info = mTransformationInfo;
7220 if (info == null) {
7221 return;
7222 }
7223 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007224 // transform-related properties have changed since the last time someone
7225 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007226
7227 // Figure out if we need to update the pivot point
7228 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007229 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7230 info.mPrevWidth = mRight - mLeft;
7231 info.mPrevHeight = mBottom - mTop;
7232 info.mPivotX = info.mPrevWidth / 2f;
7233 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007234 }
7235 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007236 info.mMatrix.reset();
7237 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7238 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7239 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7240 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007241 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007242 if (info.mCamera == null) {
7243 info.mCamera = new Camera();
7244 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007245 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007246 info.mCamera.save();
7247 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7248 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7249 info.mCamera.getMatrix(info.matrix3D);
7250 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7251 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7252 info.mPivotY + info.mTranslationY);
7253 info.mMatrix.postConcat(info.matrix3D);
7254 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007255 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007256 info.mMatrixDirty = false;
7257 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7258 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007259 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007260 }
7261
7262 /**
7263 * Utility method to retrieve the inverse of the current mMatrix property.
7264 * We cache the matrix to avoid recalculating it when transform properties
7265 * have not changed.
7266 *
7267 * @return The inverse of the current matrix of this view.
7268 */
Jeff Brown86671742010-09-30 20:00:15 -07007269 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007270 final TransformationInfo info = mTransformationInfo;
7271 if (info != null) {
7272 updateMatrix();
7273 if (info.mInverseMatrixDirty) {
7274 if (info.mInverseMatrix == null) {
7275 info.mInverseMatrix = new Matrix();
7276 }
7277 info.mMatrix.invert(info.mInverseMatrix);
7278 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007279 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007280 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007281 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007282 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007283 }
7284
7285 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007286 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7287 * views are drawn) from the camera to this view. The camera's distance
7288 * affects 3D transformations, for instance rotations around the X and Y
7289 * axis. If the rotationX or rotationY properties are changed and this view is
7290 * large (more than half the size of the screen), it is recommended to always
7291 * use a camera distance that's greater than the height (X axis rotation) or
7292 * the width (Y axis rotation) of this view.</p>
7293 *
7294 * <p>The distance of the camera from the view plane can have an affect on the
7295 * perspective distortion of the view when it is rotated around the x or y axis.
7296 * For example, a large distance will result in a large viewing angle, and there
7297 * will not be much perspective distortion of the view as it rotates. A short
7298 * distance may cause much more perspective distortion upon rotation, and can
7299 * also result in some drawing artifacts if the rotated view ends up partially
7300 * behind the camera (which is why the recommendation is to use a distance at
7301 * least as far as the size of the view, if the view is to be rotated.)</p>
7302 *
7303 * <p>The distance is expressed in "depth pixels." The default distance depends
7304 * on the screen density. For instance, on a medium density display, the
7305 * default distance is 1280. On a high density display, the default distance
7306 * is 1920.</p>
7307 *
7308 * <p>If you want to specify a distance that leads to visually consistent
7309 * results across various densities, use the following formula:</p>
7310 * <pre>
7311 * float scale = context.getResources().getDisplayMetrics().density;
7312 * view.setCameraDistance(distance * scale);
7313 * </pre>
7314 *
7315 * <p>The density scale factor of a high density display is 1.5,
7316 * and 1920 = 1280 * 1.5.</p>
7317 *
7318 * @param distance The distance in "depth pixels", if negative the opposite
7319 * value is used
7320 *
7321 * @see #setRotationX(float)
7322 * @see #setRotationY(float)
7323 */
7324 public void setCameraDistance(float distance) {
7325 invalidateParentCaches();
7326 invalidate(false);
7327
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007328 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007329 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007330 final TransformationInfo info = mTransformationInfo;
7331 if (info.mCamera == null) {
7332 info.mCamera = new Camera();
7333 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007334 }
7335
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007336 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7337 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007338
7339 invalidate(false);
7340 }
7341
7342 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007343 * The degrees that the view is rotated around the pivot point.
7344 *
Romain Guya5364ee2011-02-24 14:46:04 -08007345 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007346 * @see #getPivotX()
7347 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007348 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007349 * @return The degrees of rotation.
7350 */
Chet Haasea5531132012-02-02 13:41:44 -08007351 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007352 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007353 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007354 }
7355
7356 /**
Chet Haase897247b2010-09-09 14:54:47 -07007357 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7358 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007359 *
7360 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007361 *
7362 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007363 * @see #getPivotX()
7364 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007365 * @see #setRotationX(float)
7366 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007367 *
7368 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007369 */
7370 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007371 ensureTransformationInfo();
7372 final TransformationInfo info = mTransformationInfo;
7373 if (info.mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007374 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007375 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007376 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007377 info.mRotation = rotation;
7378 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007379 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007380 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007381 }
7382 }
7383
7384 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007385 * The degrees that the view is rotated around the vertical axis through the pivot point.
7386 *
7387 * @see #getPivotX()
7388 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007389 * @see #setRotationY(float)
7390 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007391 * @return The degrees of Y rotation.
7392 */
Chet Haasea5531132012-02-02 13:41:44 -08007393 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007394 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007395 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007396 }
7397
7398 /**
Chet Haase897247b2010-09-09 14:54:47 -07007399 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7400 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7401 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007402 *
7403 * When rotating large views, it is recommended to adjust the camera distance
7404 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007405 *
7406 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007407 *
7408 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007409 * @see #getPivotX()
7410 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007411 * @see #setRotation(float)
7412 * @see #setRotationX(float)
7413 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007414 *
7415 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007416 */
7417 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007418 ensureTransformationInfo();
7419 final TransformationInfo info = mTransformationInfo;
7420 if (info.mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007421 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007422 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007423 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007424 info.mRotationY = rotationY;
7425 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007426 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007427 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007428 }
7429 }
7430
7431 /**
7432 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7433 *
7434 * @see #getPivotX()
7435 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007436 * @see #setRotationX(float)
7437 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007438 * @return The degrees of X rotation.
7439 */
Chet Haasea5531132012-02-02 13:41:44 -08007440 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007441 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007442 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007443 }
7444
7445 /**
Chet Haase897247b2010-09-09 14:54:47 -07007446 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7447 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7448 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007449 *
7450 * When rotating large views, it is recommended to adjust the camera distance
7451 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007452 *
7453 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007454 *
7455 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007456 * @see #getPivotX()
7457 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007458 * @see #setRotation(float)
7459 * @see #setRotationY(float)
7460 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007461 *
7462 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007463 */
7464 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007465 ensureTransformationInfo();
7466 final TransformationInfo info = mTransformationInfo;
7467 if (info.mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007468 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007469 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007470 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007471 info.mRotationX = rotationX;
7472 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007473 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007474 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007475 }
7476 }
7477
7478 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007479 * The amount that the view is scaled in x around the pivot point, as a proportion of
7480 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7481 *
Joe Onorato93162322010-09-16 15:42:01 -04007482 * <p>By default, this is 1.0f.
7483 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007484 * @see #getPivotX()
7485 * @see #getPivotY()
7486 * @return The scaling factor.
7487 */
Chet Haasea5531132012-02-02 13:41:44 -08007488 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007489 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007490 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007491 }
7492
7493 /**
7494 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7495 * the view's unscaled width. A value of 1 means that no scaling is applied.
7496 *
7497 * @param scaleX The scaling factor.
7498 * @see #getPivotX()
7499 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007500 *
7501 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007502 */
7503 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007504 ensureTransformationInfo();
7505 final TransformationInfo info = mTransformationInfo;
7506 if (info.mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007507 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007508 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007509 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007510 info.mScaleX = scaleX;
7511 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007512 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007513 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007514 }
7515 }
7516
7517 /**
7518 * The amount that the view is scaled in y around the pivot point, as a proportion of
7519 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7520 *
Joe Onorato93162322010-09-16 15:42:01 -04007521 * <p>By default, this is 1.0f.
7522 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007523 * @see #getPivotX()
7524 * @see #getPivotY()
7525 * @return The scaling factor.
7526 */
Chet Haasea5531132012-02-02 13:41:44 -08007527 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007528 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007529 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007530 }
7531
7532 /**
7533 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7534 * the view's unscaled width. A value of 1 means that no scaling is applied.
7535 *
7536 * @param scaleY The scaling factor.
7537 * @see #getPivotX()
7538 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007539 *
7540 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007541 */
7542 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007543 ensureTransformationInfo();
7544 final TransformationInfo info = mTransformationInfo;
7545 if (info.mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007546 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007547 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007548 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007549 info.mScaleY = scaleY;
7550 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007551 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007552 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007553 }
7554 }
7555
7556 /**
7557 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7558 * and {@link #setScaleX(float) scaled}.
7559 *
7560 * @see #getRotation()
7561 * @see #getScaleX()
7562 * @see #getScaleY()
7563 * @see #getPivotY()
7564 * @return The x location of the pivot point.
7565 */
Chet Haasea5531132012-02-02 13:41:44 -08007566 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007567 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007568 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007569 }
7570
7571 /**
7572 * Sets the x location of the point around which the view is
7573 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007574 * By default, the pivot point is centered on the object.
7575 * Setting this property disables this behavior and causes the view to use only the
7576 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007577 *
7578 * @param pivotX The x location of the pivot point.
7579 * @see #getRotation()
7580 * @see #getScaleX()
7581 * @see #getScaleY()
7582 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007583 *
7584 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007585 */
7586 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007587 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007588 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007589 final TransformationInfo info = mTransformationInfo;
7590 if (info.mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007591 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007592 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007593 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007594 info.mPivotX = pivotX;
7595 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007596 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007597 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007598 }
7599 }
7600
7601 /**
7602 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7603 * and {@link #setScaleY(float) scaled}.
7604 *
7605 * @see #getRotation()
7606 * @see #getScaleX()
7607 * @see #getScaleY()
7608 * @see #getPivotY()
7609 * @return The y location of the pivot point.
7610 */
Chet Haasea5531132012-02-02 13:41:44 -08007611 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007612 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007613 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007614 }
7615
7616 /**
7617 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007618 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7619 * Setting this property disables this behavior and causes the view to use only the
7620 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007621 *
7622 * @param pivotY The y location of the pivot point.
7623 * @see #getRotation()
7624 * @see #getScaleX()
7625 * @see #getScaleY()
7626 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007627 *
7628 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007629 */
7630 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007631 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007632 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007633 final TransformationInfo info = mTransformationInfo;
7634 if (info.mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007635 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007636 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007637 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007638 info.mPivotY = pivotY;
7639 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007640 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007641 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007642 }
7643 }
7644
7645 /**
7646 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7647 * completely transparent and 1 means the view is completely opaque.
7648 *
Joe Onorato93162322010-09-16 15:42:01 -04007649 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007650 * @return The opacity of the view.
7651 */
Chet Haasea5531132012-02-02 13:41:44 -08007652 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007653 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007654 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007655 }
7656
7657 /**
Romain Guy171c5922011-01-06 10:04:23 -08007658 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7659 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007660 *
Romain Guy171c5922011-01-06 10:04:23 -08007661 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7662 * responsible for applying the opacity itself. Otherwise, calling this method is
7663 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007664 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007665 *
Chet Haasea5531132012-02-02 13:41:44 -08007666 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
7667 * performance implications. It is generally best to use the alpha property sparingly and
7668 * transiently, as in the case of fading animations.</p>
7669 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007670 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007671 *
Joe Malin32736f02011-01-19 16:14:20 -08007672 * @see #setLayerType(int, android.graphics.Paint)
7673 *
Chet Haase73066682010-11-29 15:55:32 -08007674 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007675 */
7676 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007677 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007678 if (mTransformationInfo.mAlpha != alpha) {
7679 mTransformationInfo.mAlpha = alpha;
7680 invalidateParentCaches();
7681 if (onSetAlpha((int) (alpha * 255))) {
7682 mPrivateFlags |= ALPHA_SET;
7683 // subclass is handling alpha - don't optimize rendering cache invalidation
7684 invalidate(true);
7685 } else {
7686 mPrivateFlags &= ~ALPHA_SET;
7687 invalidate(false);
7688 }
Chet Haaseed032702010-10-01 14:05:54 -07007689 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007690 }
7691
7692 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007693 * Faster version of setAlpha() which performs the same steps except there are
7694 * no calls to invalidate(). The caller of this function should perform proper invalidation
7695 * on the parent and this object. The return value indicates whether the subclass handles
7696 * alpha (the return value for onSetAlpha()).
7697 *
7698 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007699 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
7700 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08007701 */
7702 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007703 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007704 if (mTransformationInfo.mAlpha != alpha) {
7705 mTransformationInfo.mAlpha = alpha;
7706 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7707 if (subclassHandlesAlpha) {
7708 mPrivateFlags |= ALPHA_SET;
7709 return true;
7710 } else {
7711 mPrivateFlags &= ~ALPHA_SET;
7712 }
Chet Haasea00f3862011-02-22 06:34:40 -08007713 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007714 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08007715 }
7716
7717 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007718 * Top position of this view relative to its parent.
7719 *
7720 * @return The top of this view, in pixels.
7721 */
7722 @ViewDebug.CapturedViewProperty
7723 public final int getTop() {
7724 return mTop;
7725 }
7726
7727 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007728 * Sets the top position of this view relative to its parent. This method is meant to be called
7729 * by the layout system and should not generally be called otherwise, because the property
7730 * may be changed at any time by the layout.
7731 *
7732 * @param top The top of this view, in pixels.
7733 */
7734 public final void setTop(int top) {
7735 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007736 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007737 final boolean matrixIsIdentity = mTransformationInfo == null
7738 || mTransformationInfo.mMatrixIsIdentity;
7739 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007740 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007741 int minTop;
7742 int yLoc;
7743 if (top < mTop) {
7744 minTop = top;
7745 yLoc = top - mTop;
7746 } else {
7747 minTop = mTop;
7748 yLoc = 0;
7749 }
Chet Haasee9140a72011-02-16 16:23:29 -08007750 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007751 }
7752 } else {
7753 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007754 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007755 }
7756
Chet Haaseed032702010-10-01 14:05:54 -07007757 int width = mRight - mLeft;
7758 int oldHeight = mBottom - mTop;
7759
Chet Haase21cd1382010-09-01 17:42:29 -07007760 mTop = top;
7761
Chet Haaseed032702010-10-01 14:05:54 -07007762 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7763
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007764 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007765 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7766 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007767 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007768 }
Chet Haase21cd1382010-09-01 17:42:29 -07007769 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007770 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007771 }
Chet Haase55dbb652010-12-21 20:15:08 -08007772 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007773 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007774 }
7775 }
7776
7777 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007778 * Bottom position of this view relative to its parent.
7779 *
7780 * @return The bottom of this view, in pixels.
7781 */
7782 @ViewDebug.CapturedViewProperty
7783 public final int getBottom() {
7784 return mBottom;
7785 }
7786
7787 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007788 * True if this view has changed since the last time being drawn.
7789 *
7790 * @return The dirty state of this view.
7791 */
7792 public boolean isDirty() {
7793 return (mPrivateFlags & DIRTY_MASK) != 0;
7794 }
7795
7796 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007797 * Sets the bottom position of this view relative to its parent. This method is meant to be
7798 * called by the layout system and should not generally be called otherwise, because the
7799 * property may be changed at any time by the layout.
7800 *
7801 * @param bottom The bottom of this view, in pixels.
7802 */
7803 public final void setBottom(int bottom) {
7804 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007805 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007806 final boolean matrixIsIdentity = mTransformationInfo == null
7807 || mTransformationInfo.mMatrixIsIdentity;
7808 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007809 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007810 int maxBottom;
7811 if (bottom < mBottom) {
7812 maxBottom = mBottom;
7813 } else {
7814 maxBottom = bottom;
7815 }
Chet Haasee9140a72011-02-16 16:23:29 -08007816 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007817 }
7818 } else {
7819 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007820 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007821 }
7822
Chet Haaseed032702010-10-01 14:05:54 -07007823 int width = mRight - mLeft;
7824 int oldHeight = mBottom - mTop;
7825
Chet Haase21cd1382010-09-01 17:42:29 -07007826 mBottom = bottom;
7827
Chet Haaseed032702010-10-01 14:05:54 -07007828 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7829
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007830 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007831 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7832 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007833 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007834 }
Chet Haase21cd1382010-09-01 17:42:29 -07007835 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007836 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007837 }
Chet Haase55dbb652010-12-21 20:15:08 -08007838 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007839 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007840 }
7841 }
7842
7843 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007844 * Left position of this view relative to its parent.
7845 *
7846 * @return The left edge of this view, in pixels.
7847 */
7848 @ViewDebug.CapturedViewProperty
7849 public final int getLeft() {
7850 return mLeft;
7851 }
7852
7853 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007854 * Sets the left position of this view relative to its parent. This method is meant to be called
7855 * by the layout system and should not generally be called otherwise, because the property
7856 * may be changed at any time by the layout.
7857 *
7858 * @param left The bottom of this view, in pixels.
7859 */
7860 public final void setLeft(int left) {
7861 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007862 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007863 final boolean matrixIsIdentity = mTransformationInfo == null
7864 || mTransformationInfo.mMatrixIsIdentity;
7865 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007866 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007867 int minLeft;
7868 int xLoc;
7869 if (left < mLeft) {
7870 minLeft = left;
7871 xLoc = left - mLeft;
7872 } else {
7873 minLeft = mLeft;
7874 xLoc = 0;
7875 }
Chet Haasee9140a72011-02-16 16:23:29 -08007876 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007877 }
7878 } else {
7879 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007880 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007881 }
7882
Chet Haaseed032702010-10-01 14:05:54 -07007883 int oldWidth = mRight - mLeft;
7884 int height = mBottom - mTop;
7885
Chet Haase21cd1382010-09-01 17:42:29 -07007886 mLeft = left;
7887
Chet Haaseed032702010-10-01 14:05:54 -07007888 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7889
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007890 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007891 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7892 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007893 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007894 }
Chet Haase21cd1382010-09-01 17:42:29 -07007895 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007896 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007897 }
Chet Haase55dbb652010-12-21 20:15:08 -08007898 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007899 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007900 }
7901 }
7902
7903 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007904 * Right position of this view relative to its parent.
7905 *
7906 * @return The right edge of this view, in pixels.
7907 */
7908 @ViewDebug.CapturedViewProperty
7909 public final int getRight() {
7910 return mRight;
7911 }
7912
7913 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007914 * Sets the right position of this view relative to its parent. This method is meant to be called
7915 * by the layout system and should not generally be called otherwise, because the property
7916 * may be changed at any time by the layout.
7917 *
7918 * @param right The bottom of this view, in pixels.
7919 */
7920 public final void setRight(int right) {
7921 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007922 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007923 final boolean matrixIsIdentity = mTransformationInfo == null
7924 || mTransformationInfo.mMatrixIsIdentity;
7925 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007926 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007927 int maxRight;
7928 if (right < mRight) {
7929 maxRight = mRight;
7930 } else {
7931 maxRight = right;
7932 }
Chet Haasee9140a72011-02-16 16:23:29 -08007933 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007934 }
7935 } else {
7936 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007937 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007938 }
7939
Chet Haaseed032702010-10-01 14:05:54 -07007940 int oldWidth = mRight - mLeft;
7941 int height = mBottom - mTop;
7942
Chet Haase21cd1382010-09-01 17:42:29 -07007943 mRight = right;
7944
Chet Haaseed032702010-10-01 14:05:54 -07007945 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7946
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007947 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007948 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7949 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007950 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007951 }
Chet Haase21cd1382010-09-01 17:42:29 -07007952 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007953 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007954 }
Chet Haase55dbb652010-12-21 20:15:08 -08007955 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007956 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007957 }
7958 }
7959
7960 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007961 * The visual x position of this view, in pixels. This is equivalent to the
7962 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007963 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007964 *
Chet Haasedf030d22010-07-30 17:22:38 -07007965 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007966 */
Chet Haasea5531132012-02-02 13:41:44 -08007967 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007968 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007969 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007970 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007971
Chet Haasedf030d22010-07-30 17:22:38 -07007972 /**
7973 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7974 * {@link #setTranslationX(float) translationX} property to be the difference between
7975 * the x value passed in and the current {@link #getLeft() left} property.
7976 *
7977 * @param x The visual x position of this view, in pixels.
7978 */
7979 public void setX(float x) {
7980 setTranslationX(x - mLeft);
7981 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007982
Chet Haasedf030d22010-07-30 17:22:38 -07007983 /**
7984 * The visual y position of this view, in pixels. This is equivalent to the
7985 * {@link #setTranslationY(float) translationY} property plus the current
7986 * {@link #getTop() top} property.
7987 *
7988 * @return The visual y position of this view, in pixels.
7989 */
Chet Haasea5531132012-02-02 13:41:44 -08007990 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007991 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007992 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007993 }
7994
7995 /**
7996 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7997 * {@link #setTranslationY(float) translationY} property to be the difference between
7998 * the y value passed in and the current {@link #getTop() top} property.
7999 *
8000 * @param y The visual y position of this view, in pixels.
8001 */
8002 public void setY(float y) {
8003 setTranslationY(y - mTop);
8004 }
8005
8006
8007 /**
8008 * The horizontal location of this view relative to its {@link #getLeft() left} position.
8009 * This position is post-layout, in addition to wherever the object's
8010 * layout placed it.
8011 *
8012 * @return The horizontal position of this view relative to its left position, in pixels.
8013 */
Chet Haasea5531132012-02-02 13:41:44 -08008014 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008015 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008016 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07008017 }
8018
8019 /**
8020 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
8021 * This effectively positions the object post-layout, in addition to wherever the object's
8022 * layout placed it.
8023 *
8024 * @param translationX The horizontal position of this view relative to its left position,
8025 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008026 *
8027 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07008028 */
8029 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008030 ensureTransformationInfo();
8031 final TransformationInfo info = mTransformationInfo;
8032 if (info.mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008033 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07008034 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07008035 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008036 info.mTranslationX = translationX;
8037 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07008038 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008039 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008040 }
8041 }
8042
8043 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008044 * The horizontal location of this view relative to its {@link #getTop() top} position.
8045 * This position is post-layout, in addition to wherever the object's
8046 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008047 *
Chet Haasedf030d22010-07-30 17:22:38 -07008048 * @return The vertical position of this view relative to its top position,
8049 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008050 */
Chet Haasea5531132012-02-02 13:41:44 -08008051 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008052 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008053 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008054 }
8055
8056 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008057 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
8058 * This effectively positions the object post-layout, in addition to wherever the object's
8059 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008060 *
Chet Haasedf030d22010-07-30 17:22:38 -07008061 * @param translationY The vertical position of this view relative to its top position,
8062 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008063 *
8064 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07008065 */
Chet Haasedf030d22010-07-30 17:22:38 -07008066 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008067 ensureTransformationInfo();
8068 final TransformationInfo info = mTransformationInfo;
8069 if (info.mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008070 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07008071 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07008072 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008073 info.mTranslationY = translationY;
8074 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07008075 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008076 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07008077 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008078 }
8079
8080 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008081 * Hit rectangle in parent's coordinates
8082 *
8083 * @param outRect The hit rectangle of the view.
8084 */
8085 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07008086 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008087 final TransformationInfo info = mTransformationInfo;
8088 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008089 outRect.set(mLeft, mTop, mRight, mBottom);
8090 } else {
8091 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008092 tmpRect.set(-info.mPivotX, -info.mPivotY,
8093 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
8094 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07008095 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
8096 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07008097 }
8098 }
8099
8100 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07008101 * Determines whether the given point, in local coordinates is inside the view.
8102 */
8103 /*package*/ final boolean pointInView(float localX, float localY) {
8104 return localX >= 0 && localX < (mRight - mLeft)
8105 && localY >= 0 && localY < (mBottom - mTop);
8106 }
8107
8108 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008109 * Utility method to determine whether the given point, in local coordinates,
8110 * is inside the view, where the area of the view is expanded by the slop factor.
8111 * This method is called while processing touch-move events to determine if the event
8112 * is still within the view.
8113 */
8114 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008115 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07008116 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008117 }
8118
8119 /**
8120 * When a view has focus and the user navigates away from it, the next view is searched for
8121 * starting from the rectangle filled in by this method.
8122 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008123 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
8124 * of the view. However, if your view maintains some idea of internal selection,
8125 * such as a cursor, or a selected row or column, you should override this method and
8126 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008127 *
8128 * @param r The rectangle to fill in, in this view's coordinates.
8129 */
8130 public void getFocusedRect(Rect r) {
8131 getDrawingRect(r);
8132 }
8133
8134 /**
8135 * If some part of this view is not clipped by any of its parents, then
8136 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01008137 * coordinates (without taking possible View rotations into account), offset
8138 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
8139 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008140 *
8141 * @param r If true is returned, r holds the global coordinates of the
8142 * visible portion of this view.
8143 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8144 * between this view and its root. globalOffet may be null.
8145 * @return true if r is non-empty (i.e. part of the view is visible at the
8146 * root level.
8147 */
8148 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8149 int width = mRight - mLeft;
8150 int height = mBottom - mTop;
8151 if (width > 0 && height > 0) {
8152 r.set(0, 0, width, height);
8153 if (globalOffset != null) {
8154 globalOffset.set(-mScrollX, -mScrollY);
8155 }
8156 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8157 }
8158 return false;
8159 }
8160
8161 public final boolean getGlobalVisibleRect(Rect r) {
8162 return getGlobalVisibleRect(r, null);
8163 }
8164
8165 public final boolean getLocalVisibleRect(Rect r) {
8166 Point offset = new Point();
8167 if (getGlobalVisibleRect(r, offset)) {
8168 r.offset(-offset.x, -offset.y); // make r local
8169 return true;
8170 }
8171 return false;
8172 }
8173
8174 /**
8175 * Offset this view's vertical location by the specified number of pixels.
8176 *
8177 * @param offset the number of pixels to offset the view by
8178 */
8179 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008180 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008181 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008182 final boolean matrixIsIdentity = mTransformationInfo == null
8183 || mTransformationInfo.mMatrixIsIdentity;
8184 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008185 final ViewParent p = mParent;
8186 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008187 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008188 int minTop;
8189 int maxBottom;
8190 int yLoc;
8191 if (offset < 0) {
8192 minTop = mTop + offset;
8193 maxBottom = mBottom;
8194 yLoc = offset;
8195 } else {
8196 minTop = mTop;
8197 maxBottom = mBottom + offset;
8198 yLoc = 0;
8199 }
8200 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8201 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008202 }
8203 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008204 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008205 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008206
Chet Haasec3aa3612010-06-17 08:50:37 -07008207 mTop += offset;
8208 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008209
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008210 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008211 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008212 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008213 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008214 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008215 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008216 }
8217
8218 /**
8219 * Offset this view's horizontal location by the specified amount of pixels.
8220 *
8221 * @param offset the numer of pixels to offset the view by
8222 */
8223 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008224 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008225 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008226 final boolean matrixIsIdentity = mTransformationInfo == null
8227 || mTransformationInfo.mMatrixIsIdentity;
8228 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008229 final ViewParent p = mParent;
8230 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008231 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008232 int minLeft;
8233 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008234 if (offset < 0) {
8235 minLeft = mLeft + offset;
8236 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008237 } else {
8238 minLeft = mLeft;
8239 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008240 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008241 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008242 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008243 }
8244 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008245 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008246 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008247
Chet Haasec3aa3612010-06-17 08:50:37 -07008248 mLeft += offset;
8249 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008250
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008251 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008252 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008253 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008254 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008255 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008257 }
8258
8259 /**
8260 * Get the LayoutParams associated with this view. All views should have
8261 * layout parameters. These supply parameters to the <i>parent</i> of this
8262 * view specifying how it should be arranged. There are many subclasses of
8263 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8264 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008265 *
8266 * This method may return null if this View is not attached to a parent
8267 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8268 * was not invoked successfully. When a View is attached to a parent
8269 * ViewGroup, this method must not return null.
8270 *
8271 * @return The LayoutParams associated with this view, or null if no
8272 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008273 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008274 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008275 public ViewGroup.LayoutParams getLayoutParams() {
8276 return mLayoutParams;
8277 }
8278
8279 /**
8280 * Set the layout parameters associated with this view. These supply
8281 * parameters to the <i>parent</i> of this view specifying how it should be
8282 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8283 * correspond to the different subclasses of ViewGroup that are responsible
8284 * for arranging their children.
8285 *
Romain Guy01c174b2011-02-22 11:51:06 -08008286 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008287 */
8288 public void setLayoutParams(ViewGroup.LayoutParams params) {
8289 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008290 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008291 }
8292 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08008293 if (mParent instanceof ViewGroup) {
8294 ((ViewGroup) mParent).onSetLayoutParams(this, params);
8295 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008296 requestLayout();
8297 }
8298
8299 /**
8300 * Set the scrolled position of your view. This will cause a call to
8301 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8302 * invalidated.
8303 * @param x the x position to scroll to
8304 * @param y the y position to scroll to
8305 */
8306 public void scrollTo(int x, int y) {
8307 if (mScrollX != x || mScrollY != y) {
8308 int oldX = mScrollX;
8309 int oldY = mScrollY;
8310 mScrollX = x;
8311 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008312 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008313 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008314 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008315 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008317 }
8318 }
8319
8320 /**
8321 * Move the scrolled position of your view. This will cause a call to
8322 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8323 * invalidated.
8324 * @param x the amount of pixels to scroll by horizontally
8325 * @param y the amount of pixels to scroll by vertically
8326 */
8327 public void scrollBy(int x, int y) {
8328 scrollTo(mScrollX + x, mScrollY + y);
8329 }
8330
8331 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008332 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8333 * animation to fade the scrollbars out after a default delay. If a subclass
8334 * provides animated scrolling, the start delay should equal the duration
8335 * of the scrolling animation.</p>
8336 *
8337 * <p>The animation starts only if at least one of the scrollbars is
8338 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8339 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8340 * this method returns true, and false otherwise. If the animation is
8341 * started, this method calls {@link #invalidate()}; in that case the
8342 * caller should not call {@link #invalidate()}.</p>
8343 *
8344 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008345 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008346 *
8347 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8348 * and {@link #scrollTo(int, int)}.</p>
8349 *
8350 * @return true if the animation is played, false otherwise
8351 *
8352 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008353 * @see #scrollBy(int, int)
8354 * @see #scrollTo(int, int)
8355 * @see #isHorizontalScrollBarEnabled()
8356 * @see #isVerticalScrollBarEnabled()
8357 * @see #setHorizontalScrollBarEnabled(boolean)
8358 * @see #setVerticalScrollBarEnabled(boolean)
8359 */
8360 protected boolean awakenScrollBars() {
8361 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008362 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008363 }
8364
8365 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008366 * Trigger the scrollbars to draw.
8367 * This method differs from awakenScrollBars() only in its default duration.
8368 * initialAwakenScrollBars() will show the scroll bars for longer than
8369 * usual to give the user more of a chance to notice them.
8370 *
8371 * @return true if the animation is played, false otherwise.
8372 */
8373 private boolean initialAwakenScrollBars() {
8374 return mScrollCache != null &&
8375 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8376 }
8377
8378 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008379 * <p>
8380 * Trigger the scrollbars to draw. When invoked this method starts an
8381 * animation to fade the scrollbars out after a fixed delay. If a subclass
8382 * provides animated scrolling, the start delay should equal the duration of
8383 * the scrolling animation.
8384 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008385 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008386 * <p>
8387 * The animation starts only if at least one of the scrollbars is enabled,
8388 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8389 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8390 * this method returns true, and false otherwise. If the animation is
8391 * started, this method calls {@link #invalidate()}; in that case the caller
8392 * should not call {@link #invalidate()}.
8393 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008394 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008395 * <p>
8396 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008397 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008398 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008399 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008400 * @param startDelay the delay, in milliseconds, after which the animation
8401 * should start; when the delay is 0, the animation starts
8402 * immediately
8403 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008404 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008405 * @see #scrollBy(int, int)
8406 * @see #scrollTo(int, int)
8407 * @see #isHorizontalScrollBarEnabled()
8408 * @see #isVerticalScrollBarEnabled()
8409 * @see #setHorizontalScrollBarEnabled(boolean)
8410 * @see #setVerticalScrollBarEnabled(boolean)
8411 */
8412 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008413 return awakenScrollBars(startDelay, true);
8414 }
Joe Malin32736f02011-01-19 16:14:20 -08008415
Mike Cleron290947b2009-09-29 18:34:32 -07008416 /**
8417 * <p>
8418 * Trigger the scrollbars to draw. When invoked this method starts an
8419 * animation to fade the scrollbars out after a fixed delay. If a subclass
8420 * provides animated scrolling, the start delay should equal the duration of
8421 * the scrolling animation.
8422 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008423 *
Mike Cleron290947b2009-09-29 18:34:32 -07008424 * <p>
8425 * The animation starts only if at least one of the scrollbars is enabled,
8426 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8427 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8428 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008429 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008430 * is set to true; in that case the caller
8431 * should not call {@link #invalidate()}.
8432 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008433 *
Mike Cleron290947b2009-09-29 18:34:32 -07008434 * <p>
8435 * This method should be invoked everytime a subclass directly updates the
8436 * scroll parameters.
8437 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008438 *
Mike Cleron290947b2009-09-29 18:34:32 -07008439 * @param startDelay the delay, in milliseconds, after which the animation
8440 * should start; when the delay is 0, the animation starts
8441 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008442 *
Mike Cleron290947b2009-09-29 18:34:32 -07008443 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008444 *
Mike Cleron290947b2009-09-29 18:34:32 -07008445 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008446 *
Mike Cleron290947b2009-09-29 18:34:32 -07008447 * @see #scrollBy(int, int)
8448 * @see #scrollTo(int, int)
8449 * @see #isHorizontalScrollBarEnabled()
8450 * @see #isVerticalScrollBarEnabled()
8451 * @see #setHorizontalScrollBarEnabled(boolean)
8452 * @see #setVerticalScrollBarEnabled(boolean)
8453 */
8454 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008455 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008456
Mike Cleronf116bf82009-09-27 19:14:12 -07008457 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8458 return false;
8459 }
8460
8461 if (scrollCache.scrollBar == null) {
8462 scrollCache.scrollBar = new ScrollBarDrawable();
8463 }
8464
8465 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8466
Mike Cleron290947b2009-09-29 18:34:32 -07008467 if (invalidate) {
8468 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008469 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008470 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008471
8472 if (scrollCache.state == ScrollabilityCache.OFF) {
8473 // FIXME: this is copied from WindowManagerService.
8474 // We should get this value from the system when it
8475 // is possible to do so.
8476 final int KEY_REPEAT_FIRST_DELAY = 750;
8477 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8478 }
8479
8480 // Tell mScrollCache when we should start fading. This may
8481 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008482 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008483 scrollCache.fadeStartTime = fadeStartTime;
8484 scrollCache.state = ScrollabilityCache.ON;
8485
8486 // Schedule our fader to run, unscheduling any old ones first
8487 if (mAttachInfo != null) {
8488 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8489 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8490 }
8491
8492 return true;
8493 }
8494
8495 return false;
8496 }
8497
8498 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008499 * Do not invalidate views which are not visible and which are not running an animation. They
8500 * will not get drawn and they should not set dirty flags as if they will be drawn
8501 */
8502 private boolean skipInvalidate() {
8503 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8504 (!(mParent instanceof ViewGroup) ||
8505 !((ViewGroup) mParent).isViewTransitioning(this));
8506 }
8507 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008508 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008509 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8510 * in the future. This must be called from a UI thread. To call from a non-UI
8511 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008512 *
8513 * WARNING: This method is destructive to dirty.
8514 * @param dirty the rectangle representing the bounds of the dirty region
8515 */
8516 public void invalidate(Rect dirty) {
8517 if (ViewDebug.TRACE_HIERARCHY) {
8518 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8519 }
8520
Chet Haaseaceafe62011-08-26 15:44:33 -07008521 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008522 return;
8523 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008524 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008525 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8526 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008527 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008528 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008529 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008530 final ViewParent p = mParent;
8531 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008532 //noinspection PointlessBooleanExpression,ConstantConditions
8533 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8534 if (p != null && ai != null && ai.mHardwareAccelerated) {
8535 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008536 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008537 p.invalidateChild(this, null);
8538 return;
8539 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008541 if (p != null && ai != null) {
8542 final int scrollX = mScrollX;
8543 final int scrollY = mScrollY;
8544 final Rect r = ai.mTmpInvalRect;
8545 r.set(dirty.left - scrollX, dirty.top - scrollY,
8546 dirty.right - scrollX, dirty.bottom - scrollY);
8547 mParent.invalidateChild(this, r);
8548 }
8549 }
8550 }
8551
8552 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008553 * Mark the area defined by the rect (l,t,r,b) as needing to be drawn.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008554 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008555 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8556 * will be called at some point in the future. This must be called from
8557 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008558 * @param l the left position of the dirty region
8559 * @param t the top position of the dirty region
8560 * @param r the right position of the dirty region
8561 * @param b the bottom position of the dirty region
8562 */
8563 public void invalidate(int l, int t, int r, int b) {
8564 if (ViewDebug.TRACE_HIERARCHY) {
8565 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8566 }
8567
Chet Haaseaceafe62011-08-26 15:44:33 -07008568 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008569 return;
8570 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008571 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008572 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8573 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008574 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008575 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008576 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008577 final ViewParent p = mParent;
8578 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008579 //noinspection PointlessBooleanExpression,ConstantConditions
8580 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8581 if (p != null && ai != null && ai.mHardwareAccelerated) {
8582 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008583 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008584 p.invalidateChild(this, null);
8585 return;
8586 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008588 if (p != null && ai != null && l < r && t < b) {
8589 final int scrollX = mScrollX;
8590 final int scrollY = mScrollY;
8591 final Rect tmpr = ai.mTmpInvalRect;
8592 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8593 p.invalidateChild(this, tmpr);
8594 }
8595 }
8596 }
8597
8598 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008599 * Invalidate the whole view. If the view is visible,
8600 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8601 * the future. This must be called from a UI thread. To call from a non-UI thread,
8602 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008603 */
8604 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008605 invalidate(true);
8606 }
Joe Malin32736f02011-01-19 16:14:20 -08008607
Chet Haaseed032702010-10-01 14:05:54 -07008608 /**
8609 * This is where the invalidate() work actually happens. A full invalidate()
8610 * causes the drawing cache to be invalidated, but this function can be called with
8611 * invalidateCache set to false to skip that invalidation step for cases that do not
8612 * need it (for example, a component that remains at the same dimensions with the same
8613 * content).
8614 *
8615 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8616 * well. This is usually true for a full invalidate, but may be set to false if the
8617 * View's contents or dimensions have not changed.
8618 */
Romain Guy849d0a32011-02-01 17:20:48 -08008619 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008620 if (ViewDebug.TRACE_HIERARCHY) {
8621 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8622 }
8623
Chet Haaseaceafe62011-08-26 15:44:33 -07008624 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008625 return;
8626 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008627 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008628 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008629 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8630 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008631 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008632 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008633 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008634 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008635 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008637 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008638 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008639 //noinspection PointlessBooleanExpression,ConstantConditions
8640 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8641 if (p != null && ai != null && ai.mHardwareAccelerated) {
8642 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008643 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008644 p.invalidateChild(this, null);
8645 return;
8646 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008647 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008649 if (p != null && ai != null) {
8650 final Rect r = ai.mTmpInvalRect;
8651 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8652 // Don't call invalidate -- we don't want to internally scroll
8653 // our own bounds
8654 p.invalidateChild(this, r);
8655 }
8656 }
8657 }
8658
8659 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008660 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008661 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8662 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008663 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8664 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008665 *
8666 * @hide
8667 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008668 protected void invalidateParentCaches() {
8669 if (mParent instanceof View) {
8670 ((View) mParent).mPrivateFlags |= INVALIDATED;
8671 }
8672 }
Joe Malin32736f02011-01-19 16:14:20 -08008673
Romain Guy0fd89bf2011-01-26 15:41:30 -08008674 /**
8675 * Used to indicate that the parent of this view should be invalidated. This functionality
8676 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8677 * which is necessary when various parent-managed properties of the view change, such as
8678 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8679 * an invalidation event to the parent.
8680 *
8681 * @hide
8682 */
8683 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008684 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008685 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008686 }
8687 }
8688
8689 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008690 * Indicates whether this View is opaque. An opaque View guarantees that it will
8691 * draw all the pixels overlapping its bounds using a fully opaque color.
8692 *
8693 * Subclasses of View should override this method whenever possible to indicate
8694 * whether an instance is opaque. Opaque Views are treated in a special way by
8695 * the View hierarchy, possibly allowing it to perform optimizations during
8696 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008697 *
Romain Guy24443ea2009-05-11 11:56:30 -07008698 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008699 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008700 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008701 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008702 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008703 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8704 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008705 }
8706
Adam Powell20232d02010-12-08 21:08:53 -08008707 /**
8708 * @hide
8709 */
8710 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008711 // Opaque if:
8712 // - Has a background
8713 // - Background is opaque
8714 // - Doesn't have scrollbars or scrollbars are inside overlay
8715
8716 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8717 mPrivateFlags |= OPAQUE_BACKGROUND;
8718 } else {
8719 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8720 }
8721
8722 final int flags = mViewFlags;
8723 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8724 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8725 mPrivateFlags |= OPAQUE_SCROLLBARS;
8726 } else {
8727 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8728 }
8729 }
8730
8731 /**
8732 * @hide
8733 */
8734 protected boolean hasOpaqueScrollbars() {
8735 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008736 }
8737
8738 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008739 * @return A handler associated with the thread running the View. This
8740 * handler can be used to pump events in the UI events queue.
8741 */
8742 public Handler getHandler() {
8743 if (mAttachInfo != null) {
8744 return mAttachInfo.mHandler;
8745 }
8746 return null;
8747 }
8748
8749 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08008750 * Gets the view root associated with the View.
8751 * @return The view root, or null if none.
8752 * @hide
8753 */
8754 public ViewRootImpl getViewRootImpl() {
8755 if (mAttachInfo != null) {
8756 return mAttachInfo.mViewRootImpl;
8757 }
8758 return null;
8759 }
8760
8761 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008762 * <p>Causes the Runnable to be added to the message queue.
8763 * The runnable will be run on the user interface thread.</p>
8764 *
8765 * <p>This method can be invoked from outside of the UI thread
8766 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008767 *
8768 * @param action The Runnable that will be executed.
8769 *
8770 * @return Returns true if the Runnable was successfully placed in to the
8771 * message queue. Returns false on failure, usually because the
8772 * looper processing the message queue is exiting.
8773 */
8774 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008775 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008776 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008777 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008778 }
Jeff Browna175a5b2012-02-15 19:18:31 -08008779 // Assume that post will succeed later
8780 ViewRootImpl.getRunQueue().post(action);
8781 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008782 }
8783
8784 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008785 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008786 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008787 * The runnable will be run on the user interface thread.</p>
8788 *
8789 * <p>This method can be invoked from outside of the UI thread
8790 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008791 *
8792 * @param action The Runnable that will be executed.
8793 * @param delayMillis The delay (in milliseconds) until the Runnable
8794 * will be executed.
8795 *
8796 * @return true if the Runnable was successfully placed in to the
8797 * message queue. Returns false on failure, usually because the
8798 * looper processing the message queue is exiting. Note that a
8799 * result of true does not mean the Runnable will be processed --
8800 * if the looper is quit before the delivery time of the message
8801 * occurs then the message will be dropped.
8802 */
8803 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008804 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008805 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008806 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008807 }
Jeff Browna175a5b2012-02-15 19:18:31 -08008808 // Assume that post will succeed later
8809 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
8810 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008811 }
8812
8813 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08008814 * <p>Causes the Runnable to execute on the next animation time step.
8815 * The runnable will be run on the user interface thread.</p>
8816 *
8817 * <p>This method can be invoked from outside of the UI thread
8818 * only when this View is attached to a window.</p>
8819 *
8820 * @param action The Runnable that will be executed.
8821 *
8822 * @hide
8823 */
8824 public void postOnAnimation(Runnable action) {
8825 final AttachInfo attachInfo = mAttachInfo;
8826 if (attachInfo != null) {
8827 attachInfo.mViewRootImpl.mChoreographer.postAnimationCallback(action, null);
8828 } else {
8829 // Assume that post will succeed later
8830 ViewRootImpl.getRunQueue().post(action);
8831 }
8832 }
8833
8834 /**
8835 * <p>Causes the Runnable to execute on the next animation time step,
8836 * after the specified amount of time elapses.
8837 * The runnable will be run on the user interface thread.</p>
8838 *
8839 * <p>This method can be invoked from outside of the UI thread
8840 * only when this View is attached to a window.</p>
8841 *
8842 * @param action The Runnable that will be executed.
8843 * @param delayMillis The delay (in milliseconds) until the Runnable
8844 * will be executed.
8845 *
8846 * @hide
8847 */
8848 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
8849 final AttachInfo attachInfo = mAttachInfo;
8850 if (attachInfo != null) {
8851 attachInfo.mViewRootImpl.mChoreographer.postAnimationCallbackDelayed(
8852 action, null, delayMillis);
8853 } else {
8854 // Assume that post will succeed later
8855 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
8856 }
8857 }
8858
8859 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008860 * <p>Removes the specified Runnable from the message queue.</p>
8861 *
8862 * <p>This method can be invoked from outside of the UI thread
8863 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008864 *
8865 * @param action The Runnable to remove from the message handling queue
8866 *
8867 * @return true if this view could ask the Handler to remove the Runnable,
8868 * false otherwise. When the returned value is true, the Runnable
8869 * may or may not have been actually removed from the message queue
8870 * (for instance, if the Runnable was not in the queue already.)
8871 */
8872 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -08008873 if (action != null) {
8874 final AttachInfo attachInfo = mAttachInfo;
8875 if (attachInfo != null) {
8876 attachInfo.mHandler.removeCallbacks(action);
8877 attachInfo.mViewRootImpl.mChoreographer.removeAnimationCallbacks(action, null);
8878 } else {
8879 // Assume that post will succeed later
8880 ViewRootImpl.getRunQueue().removeCallbacks(action);
8881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008883 return true;
8884 }
8885
8886 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008887 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8888 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008889 *
Romain Guye63a4f32011-08-11 11:33:31 -07008890 * <p>This method can be invoked from outside of the UI thread
8891 * only when this View is attached to a window.</p>
8892 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008893 * @see #invalidate()
8894 */
8895 public void postInvalidate() {
8896 postInvalidateDelayed(0);
8897 }
8898
8899 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008900 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8901 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8902 *
8903 * <p>This method can be invoked from outside of the UI thread
8904 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008905 *
8906 * @param left The left coordinate of the rectangle to invalidate.
8907 * @param top The top coordinate of the rectangle to invalidate.
8908 * @param right The right coordinate of the rectangle to invalidate.
8909 * @param bottom The bottom coordinate of the rectangle to invalidate.
8910 *
8911 * @see #invalidate(int, int, int, int)
8912 * @see #invalidate(Rect)
8913 */
8914 public void postInvalidate(int left, int top, int right, int bottom) {
8915 postInvalidateDelayed(0, left, top, right, bottom);
8916 }
8917
8918 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008919 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8920 * loop. Waits for the specified amount of time.</p>
8921 *
8922 * <p>This method can be invoked from outside of the UI thread
8923 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008924 *
8925 * @param delayMilliseconds the duration in milliseconds to delay the
8926 * invalidation by
8927 */
8928 public void postInvalidateDelayed(long delayMilliseconds) {
8929 // We try only with the AttachInfo because there's no point in invalidating
8930 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08008931 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008932 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008933 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008934 }
8935 }
8936
8937 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008938 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8939 * through the event loop. Waits for the specified amount of time.</p>
8940 *
8941 * <p>This method can be invoked from outside of the UI thread
8942 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008943 *
8944 * @param delayMilliseconds the duration in milliseconds to delay the
8945 * invalidation by
8946 * @param left The left coordinate of the rectangle to invalidate.
8947 * @param top The top coordinate of the rectangle to invalidate.
8948 * @param right The right coordinate of the rectangle to invalidate.
8949 * @param bottom The bottom coordinate of the rectangle to invalidate.
8950 */
8951 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8952 int right, int bottom) {
8953
8954 // We try only with the AttachInfo because there's no point in invalidating
8955 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08008956 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008957 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008958 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8959 info.target = this;
8960 info.left = left;
8961 info.top = top;
8962 info.right = right;
8963 info.bottom = bottom;
8964
Jeff Browna175a5b2012-02-15 19:18:31 -08008965 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008966 }
8967 }
8968
8969 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -08008970 * <p>Cause an invalidate to happen on the next animation time step, typically the
8971 * next display frame.</p>
8972 *
8973 * <p>This method can be invoked from outside of the UI thread
8974 * only when this View is attached to a window.</p>
8975 *
8976 * @hide
8977 */
8978 public void postInvalidateOnAnimation() {
8979 // We try only with the AttachInfo because there's no point in invalidating
8980 // if we are not attached to our window
8981 final AttachInfo attachInfo = mAttachInfo;
8982 if (attachInfo != null) {
8983 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
8984 }
8985 }
8986
8987 /**
8988 * <p>Cause an invalidate of the specified area to happen on the next animation
8989 * time step, typically the next display frame.</p>
8990 *
8991 * <p>This method can be invoked from outside of the UI thread
8992 * only when this View is attached to a window.</p>
8993 *
8994 * @param left The left coordinate of the rectangle to invalidate.
8995 * @param top The top coordinate of the rectangle to invalidate.
8996 * @param right The right coordinate of the rectangle to invalidate.
8997 * @param bottom The bottom coordinate of the rectangle to invalidate.
8998 *
8999 * @hide
9000 */
9001 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
9002 // We try only with the AttachInfo because there's no point in invalidating
9003 // if we are not attached to our window
9004 final AttachInfo attachInfo = mAttachInfo;
9005 if (attachInfo != null) {
9006 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
9007 info.target = this;
9008 info.left = left;
9009 info.top = top;
9010 info.right = right;
9011 info.bottom = bottom;
9012
9013 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
9014 }
9015 }
9016
9017 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07009018 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
9019 * This event is sent at most once every
9020 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
9021 */
9022 private void postSendViewScrolledAccessibilityEventCallback() {
9023 if (mSendViewScrolledAccessibilityEvent == null) {
9024 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
9025 }
9026 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
9027 mSendViewScrolledAccessibilityEvent.mIsPending = true;
9028 postDelayed(mSendViewScrolledAccessibilityEvent,
9029 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
9030 }
9031 }
9032
9033 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009034 * Called by a parent to request that a child update its values for mScrollX
9035 * and mScrollY if necessary. This will typically be done if the child is
9036 * animating a scroll using a {@link android.widget.Scroller Scroller}
9037 * object.
9038 */
9039 public void computeScroll() {
9040 }
9041
9042 /**
9043 * <p>Indicate whether the horizontal edges are faded when the view is
9044 * scrolled horizontally.</p>
9045 *
9046 * @return true if the horizontal edges should are faded on scroll, false
9047 * otherwise
9048 *
9049 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009050 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009051 */
9052 public boolean isHorizontalFadingEdgeEnabled() {
9053 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
9054 }
9055
9056 /**
9057 * <p>Define whether the horizontal edges should be faded when this view
9058 * is scrolled horizontally.</p>
9059 *
9060 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
9061 * be faded when the view is scrolled
9062 * horizontally
9063 *
9064 * @see #isHorizontalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009065 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009066 */
9067 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
9068 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
9069 if (horizontalFadingEdgeEnabled) {
9070 initScrollCache();
9071 }
9072
9073 mViewFlags ^= FADING_EDGE_HORIZONTAL;
9074 }
9075 }
9076
9077 /**
9078 * <p>Indicate whether the vertical edges are faded when the view is
9079 * scrolled horizontally.</p>
9080 *
9081 * @return true if the vertical edges should are faded on scroll, false
9082 * otherwise
9083 *
9084 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009085 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009086 */
9087 public boolean isVerticalFadingEdgeEnabled() {
9088 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
9089 }
9090
9091 /**
9092 * <p>Define whether the vertical edges should be faded when this view
9093 * is scrolled vertically.</p>
9094 *
9095 * @param verticalFadingEdgeEnabled true if the vertical edges should
9096 * be faded when the view is scrolled
9097 * vertically
9098 *
9099 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009100 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009101 */
9102 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
9103 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
9104 if (verticalFadingEdgeEnabled) {
9105 initScrollCache();
9106 }
9107
9108 mViewFlags ^= FADING_EDGE_VERTICAL;
9109 }
9110 }
9111
9112 /**
9113 * Returns the strength, or intensity, of the top faded edge. The strength is
9114 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9115 * returns 0.0 or 1.0 but no value in between.
9116 *
9117 * Subclasses should override this method to provide a smoother fade transition
9118 * when scrolling occurs.
9119 *
9120 * @return the intensity of the top fade as a float between 0.0f and 1.0f
9121 */
9122 protected float getTopFadingEdgeStrength() {
9123 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
9124 }
9125
9126 /**
9127 * Returns the strength, or intensity, of the bottom faded edge. The strength is
9128 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9129 * returns 0.0 or 1.0 but no value in between.
9130 *
9131 * Subclasses should override this method to provide a smoother fade transition
9132 * when scrolling occurs.
9133 *
9134 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
9135 */
9136 protected float getBottomFadingEdgeStrength() {
9137 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
9138 computeVerticalScrollRange() ? 1.0f : 0.0f;
9139 }
9140
9141 /**
9142 * Returns the strength, or intensity, of the left faded edge. The strength is
9143 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9144 * returns 0.0 or 1.0 but no value in between.
9145 *
9146 * Subclasses should override this method to provide a smoother fade transition
9147 * when scrolling occurs.
9148 *
9149 * @return the intensity of the left fade as a float between 0.0f and 1.0f
9150 */
9151 protected float getLeftFadingEdgeStrength() {
9152 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
9153 }
9154
9155 /**
9156 * Returns the strength, or intensity, of the right faded edge. The strength is
9157 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9158 * returns 0.0 or 1.0 but no value in between.
9159 *
9160 * Subclasses should override this method to provide a smoother fade transition
9161 * when scrolling occurs.
9162 *
9163 * @return the intensity of the right fade as a float between 0.0f and 1.0f
9164 */
9165 protected float getRightFadingEdgeStrength() {
9166 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
9167 computeHorizontalScrollRange() ? 1.0f : 0.0f;
9168 }
9169
9170 /**
9171 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
9172 * scrollbar is not drawn by default.</p>
9173 *
9174 * @return true if the horizontal scrollbar should be painted, false
9175 * otherwise
9176 *
9177 * @see #setHorizontalScrollBarEnabled(boolean)
9178 */
9179 public boolean isHorizontalScrollBarEnabled() {
9180 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9181 }
9182
9183 /**
9184 * <p>Define whether the horizontal scrollbar should be drawn or not. The
9185 * scrollbar is not drawn by default.</p>
9186 *
9187 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
9188 * be painted
9189 *
9190 * @see #isHorizontalScrollBarEnabled()
9191 */
9192 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
9193 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
9194 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009195 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009196 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009197 }
9198 }
9199
9200 /**
9201 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9202 * scrollbar is not drawn by default.</p>
9203 *
9204 * @return true if the vertical scrollbar should be painted, false
9205 * otherwise
9206 *
9207 * @see #setVerticalScrollBarEnabled(boolean)
9208 */
9209 public boolean isVerticalScrollBarEnabled() {
9210 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9211 }
9212
9213 /**
9214 * <p>Define whether the vertical scrollbar should be drawn or not. The
9215 * scrollbar is not drawn by default.</p>
9216 *
9217 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9218 * be painted
9219 *
9220 * @see #isVerticalScrollBarEnabled()
9221 */
9222 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9223 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9224 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009225 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009226 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009227 }
9228 }
9229
Adam Powell20232d02010-12-08 21:08:53 -08009230 /**
9231 * @hide
9232 */
9233 protected void recomputePadding() {
9234 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009235 }
Joe Malin32736f02011-01-19 16:14:20 -08009236
Mike Cleronfe81d382009-09-28 14:22:16 -07009237 /**
9238 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009239 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009240 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009241 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009242 */
9243 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9244 initScrollCache();
9245 final ScrollabilityCache scrollabilityCache = mScrollCache;
9246 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009247 if (fadeScrollbars) {
9248 scrollabilityCache.state = ScrollabilityCache.OFF;
9249 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009250 scrollabilityCache.state = ScrollabilityCache.ON;
9251 }
9252 }
Joe Malin32736f02011-01-19 16:14:20 -08009253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009254 /**
Joe Malin32736f02011-01-19 16:14:20 -08009255 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009256 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009257 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009258 * @return true if scrollbar fading is enabled
9259 */
9260 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009261 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009262 }
Joe Malin32736f02011-01-19 16:14:20 -08009263
Mike Cleron52f0a642009-09-28 18:21:37 -07009264 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009265 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9266 * inset. When inset, they add to the padding of the view. And the scrollbars
9267 * can be drawn inside the padding area or on the edge of the view. For example,
9268 * if a view has a background drawable and you want to draw the scrollbars
9269 * inside the padding specified by the drawable, you can use
9270 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9271 * appear at the edge of the view, ignoring the padding, then you can use
9272 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9273 * @param style the style of the scrollbars. Should be one of
9274 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9275 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9276 * @see #SCROLLBARS_INSIDE_OVERLAY
9277 * @see #SCROLLBARS_INSIDE_INSET
9278 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9279 * @see #SCROLLBARS_OUTSIDE_INSET
9280 */
9281 public void setScrollBarStyle(int style) {
9282 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9283 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009284 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009285 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009286 }
9287 }
9288
9289 /**
9290 * <p>Returns the current scrollbar style.</p>
9291 * @return the current scrollbar style
9292 * @see #SCROLLBARS_INSIDE_OVERLAY
9293 * @see #SCROLLBARS_INSIDE_INSET
9294 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9295 * @see #SCROLLBARS_OUTSIDE_INSET
9296 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009297 @ViewDebug.ExportedProperty(mapping = {
9298 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9299 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9300 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9301 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9302 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009303 public int getScrollBarStyle() {
9304 return mViewFlags & SCROLLBARS_STYLE_MASK;
9305 }
9306
9307 /**
9308 * <p>Compute the horizontal range that the horizontal scrollbar
9309 * represents.</p>
9310 *
9311 * <p>The range is expressed in arbitrary units that must be the same as the
9312 * units used by {@link #computeHorizontalScrollExtent()} and
9313 * {@link #computeHorizontalScrollOffset()}.</p>
9314 *
9315 * <p>The default range is the drawing width of this view.</p>
9316 *
9317 * @return the total horizontal range represented by the horizontal
9318 * scrollbar
9319 *
9320 * @see #computeHorizontalScrollExtent()
9321 * @see #computeHorizontalScrollOffset()
9322 * @see android.widget.ScrollBarDrawable
9323 */
9324 protected int computeHorizontalScrollRange() {
9325 return getWidth();
9326 }
9327
9328 /**
9329 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9330 * within the horizontal range. This value is used to compute the position
9331 * of the thumb within the scrollbar's track.</p>
9332 *
9333 * <p>The range is expressed in arbitrary units that must be the same as the
9334 * units used by {@link #computeHorizontalScrollRange()} and
9335 * {@link #computeHorizontalScrollExtent()}.</p>
9336 *
9337 * <p>The default offset is the scroll offset of this view.</p>
9338 *
9339 * @return the horizontal offset of the scrollbar's thumb
9340 *
9341 * @see #computeHorizontalScrollRange()
9342 * @see #computeHorizontalScrollExtent()
9343 * @see android.widget.ScrollBarDrawable
9344 */
9345 protected int computeHorizontalScrollOffset() {
9346 return mScrollX;
9347 }
9348
9349 /**
9350 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9351 * within the horizontal range. This value is used to compute the length
9352 * of the thumb within the scrollbar's track.</p>
9353 *
9354 * <p>The range is expressed in arbitrary units that must be the same as the
9355 * units used by {@link #computeHorizontalScrollRange()} and
9356 * {@link #computeHorizontalScrollOffset()}.</p>
9357 *
9358 * <p>The default extent is the drawing width of this view.</p>
9359 *
9360 * @return the horizontal extent of the scrollbar's thumb
9361 *
9362 * @see #computeHorizontalScrollRange()
9363 * @see #computeHorizontalScrollOffset()
9364 * @see android.widget.ScrollBarDrawable
9365 */
9366 protected int computeHorizontalScrollExtent() {
9367 return getWidth();
9368 }
9369
9370 /**
9371 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9372 *
9373 * <p>The range is expressed in arbitrary units that must be the same as the
9374 * units used by {@link #computeVerticalScrollExtent()} and
9375 * {@link #computeVerticalScrollOffset()}.</p>
9376 *
9377 * @return the total vertical range represented by the vertical scrollbar
9378 *
9379 * <p>The default range is the drawing height of this view.</p>
9380 *
9381 * @see #computeVerticalScrollExtent()
9382 * @see #computeVerticalScrollOffset()
9383 * @see android.widget.ScrollBarDrawable
9384 */
9385 protected int computeVerticalScrollRange() {
9386 return getHeight();
9387 }
9388
9389 /**
9390 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9391 * within the horizontal range. This value is used to compute the position
9392 * of the thumb within the scrollbar's track.</p>
9393 *
9394 * <p>The range is expressed in arbitrary units that must be the same as the
9395 * units used by {@link #computeVerticalScrollRange()} and
9396 * {@link #computeVerticalScrollExtent()}.</p>
9397 *
9398 * <p>The default offset is the scroll offset of this view.</p>
9399 *
9400 * @return the vertical offset of the scrollbar's thumb
9401 *
9402 * @see #computeVerticalScrollRange()
9403 * @see #computeVerticalScrollExtent()
9404 * @see android.widget.ScrollBarDrawable
9405 */
9406 protected int computeVerticalScrollOffset() {
9407 return mScrollY;
9408 }
9409
9410 /**
9411 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9412 * within the vertical range. This value is used to compute the length
9413 * of the thumb within the scrollbar's track.</p>
9414 *
9415 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009416 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009417 * {@link #computeVerticalScrollOffset()}.</p>
9418 *
9419 * <p>The default extent is the drawing height of this view.</p>
9420 *
9421 * @return the vertical extent of the scrollbar's thumb
9422 *
9423 * @see #computeVerticalScrollRange()
9424 * @see #computeVerticalScrollOffset()
9425 * @see android.widget.ScrollBarDrawable
9426 */
9427 protected int computeVerticalScrollExtent() {
9428 return getHeight();
9429 }
9430
9431 /**
Adam Powell69159442011-06-13 17:53:06 -07009432 * Check if this view can be scrolled horizontally in a certain direction.
9433 *
9434 * @param direction Negative to check scrolling left, positive to check scrolling right.
9435 * @return true if this view can be scrolled in the specified direction, false otherwise.
9436 */
9437 public boolean canScrollHorizontally(int direction) {
9438 final int offset = computeHorizontalScrollOffset();
9439 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9440 if (range == 0) return false;
9441 if (direction < 0) {
9442 return offset > 0;
9443 } else {
9444 return offset < range - 1;
9445 }
9446 }
9447
9448 /**
9449 * Check if this view can be scrolled vertically in a certain direction.
9450 *
9451 * @param direction Negative to check scrolling up, positive to check scrolling down.
9452 * @return true if this view can be scrolled in the specified direction, false otherwise.
9453 */
9454 public boolean canScrollVertically(int direction) {
9455 final int offset = computeVerticalScrollOffset();
9456 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9457 if (range == 0) return false;
9458 if (direction < 0) {
9459 return offset > 0;
9460 } else {
9461 return offset < range - 1;
9462 }
9463 }
9464
9465 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009466 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9467 * scrollbars are painted only if they have been awakened first.</p>
9468 *
9469 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009470 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009471 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009472 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009473 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009474 // scrollbars are drawn only when the animation is running
9475 final ScrollabilityCache cache = mScrollCache;
9476 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009477
Mike Cleronf116bf82009-09-27 19:14:12 -07009478 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009479
Mike Cleronf116bf82009-09-27 19:14:12 -07009480 if (state == ScrollabilityCache.OFF) {
9481 return;
9482 }
Joe Malin32736f02011-01-19 16:14:20 -08009483
Mike Cleronf116bf82009-09-27 19:14:12 -07009484 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009485
Mike Cleronf116bf82009-09-27 19:14:12 -07009486 if (state == ScrollabilityCache.FADING) {
9487 // We're fading -- get our fade interpolation
9488 if (cache.interpolatorValues == null) {
9489 cache.interpolatorValues = new float[1];
9490 }
Joe Malin32736f02011-01-19 16:14:20 -08009491
Mike Cleronf116bf82009-09-27 19:14:12 -07009492 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009493
Mike Cleronf116bf82009-09-27 19:14:12 -07009494 // Stops the animation if we're done
9495 if (cache.scrollBarInterpolator.timeToValues(values) ==
9496 Interpolator.Result.FREEZE_END) {
9497 cache.state = ScrollabilityCache.OFF;
9498 } else {
9499 cache.scrollBar.setAlpha(Math.round(values[0]));
9500 }
Joe Malin32736f02011-01-19 16:14:20 -08009501
9502 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009503 // drawing. We only want this when we're fading so that
9504 // we prevent excessive redraws
9505 invalidate = true;
9506 } else {
9507 // We're just on -- but we may have been fading before so
9508 // reset alpha
9509 cache.scrollBar.setAlpha(255);
9510 }
9511
Joe Malin32736f02011-01-19 16:14:20 -08009512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009513 final int viewFlags = mViewFlags;
9514
9515 final boolean drawHorizontalScrollBar =
9516 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9517 final boolean drawVerticalScrollBar =
9518 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9519 && !isVerticalScrollBarHidden();
9520
9521 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9522 final int width = mRight - mLeft;
9523 final int height = mBottom - mTop;
9524
9525 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009526
Mike Reede8853fc2009-09-04 14:01:48 -04009527 final int scrollX = mScrollX;
9528 final int scrollY = mScrollY;
9529 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9530
Mike Cleronf116bf82009-09-27 19:14:12 -07009531 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009533 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009534 int size = scrollBar.getSize(false);
9535 if (size <= 0) {
9536 size = cache.scrollBarSize;
9537 }
9538
Mike Cleronf116bf82009-09-27 19:14:12 -07009539 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009540 computeHorizontalScrollOffset(),
9541 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009542 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009543 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009544 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009545 left = scrollX + (mPaddingLeft & inside);
9546 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9547 bottom = top + size;
9548 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9549 if (invalidate) {
9550 invalidate(left, top, right, bottom);
9551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009552 }
9553
9554 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009555 int size = scrollBar.getSize(true);
9556 if (size <= 0) {
9557 size = cache.scrollBarSize;
9558 }
9559
Mike Reede8853fc2009-09-04 14:01:48 -04009560 scrollBar.setParameters(computeVerticalScrollRange(),
9561 computeVerticalScrollOffset(),
9562 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009563 switch (mVerticalScrollbarPosition) {
9564 default:
9565 case SCROLLBAR_POSITION_DEFAULT:
9566 case SCROLLBAR_POSITION_RIGHT:
9567 left = scrollX + width - size - (mUserPaddingRight & inside);
9568 break;
9569 case SCROLLBAR_POSITION_LEFT:
9570 left = scrollX + (mUserPaddingLeft & inside);
9571 break;
9572 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009573 top = scrollY + (mPaddingTop & inside);
9574 right = left + size;
9575 bottom = scrollY + height - (mUserPaddingBottom & inside);
9576 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9577 if (invalidate) {
9578 invalidate(left, top, right, bottom);
9579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009580 }
9581 }
9582 }
9583 }
Romain Guy8506ab42009-06-11 17:35:47 -07009584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009585 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009586 * 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 -08009587 * FastScroller is visible.
9588 * @return whether to temporarily hide the vertical scrollbar
9589 * @hide
9590 */
9591 protected boolean isVerticalScrollBarHidden() {
9592 return false;
9593 }
9594
9595 /**
9596 * <p>Draw the horizontal scrollbar if
9597 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9598 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009599 * @param canvas the canvas on which to draw the scrollbar
9600 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009601 *
9602 * @see #isHorizontalScrollBarEnabled()
9603 * @see #computeHorizontalScrollRange()
9604 * @see #computeHorizontalScrollExtent()
9605 * @see #computeHorizontalScrollOffset()
9606 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009607 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009608 */
Romain Guy8fb95422010-08-17 18:38:51 -07009609 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9610 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009611 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009612 scrollBar.draw(canvas);
9613 }
Mike Reede8853fc2009-09-04 14:01:48 -04009614
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009615 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009616 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9617 * returns true.</p>
9618 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009619 * @param canvas the canvas on which to draw the scrollbar
9620 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009621 *
9622 * @see #isVerticalScrollBarEnabled()
9623 * @see #computeVerticalScrollRange()
9624 * @see #computeVerticalScrollExtent()
9625 * @see #computeVerticalScrollOffset()
9626 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009627 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009628 */
Romain Guy8fb95422010-08-17 18:38:51 -07009629 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9630 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009631 scrollBar.setBounds(l, t, r, b);
9632 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009633 }
9634
9635 /**
9636 * Implement this to do your drawing.
9637 *
9638 * @param canvas the canvas on which the background will be drawn
9639 */
9640 protected void onDraw(Canvas canvas) {
9641 }
9642
9643 /*
9644 * Caller is responsible for calling requestLayout if necessary.
9645 * (This allows addViewInLayout to not request a new layout.)
9646 */
9647 void assignParent(ViewParent parent) {
9648 if (mParent == null) {
9649 mParent = parent;
9650 } else if (parent == null) {
9651 mParent = null;
9652 } else {
9653 throw new RuntimeException("view " + this + " being added, but"
9654 + " it already has a parent");
9655 }
9656 }
9657
9658 /**
9659 * This is called when the view is attached to a window. At this point it
9660 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009661 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9662 * however it may be called any time before the first onDraw -- including
9663 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009664 *
9665 * @see #onDetachedFromWindow()
9666 */
9667 protected void onAttachedToWindow() {
9668 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9669 mParent.requestTransparentRegion(this);
9670 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009671 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9672 initialAwakenScrollBars();
9673 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9674 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009675 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009676 // Order is important here: LayoutDirection MUST be resolved before Padding
9677 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009678 resolveLayoutDirectionIfNeeded();
9679 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009680 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009681 if (isFocused()) {
9682 InputMethodManager imm = InputMethodManager.peekInstance();
9683 imm.focusIn(this);
9684 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009685 }
Cibu Johny86666632010-02-22 13:01:02 -08009686
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009687 /**
Romain Guybb9908b2012-03-08 11:14:07 -08009688 * @see #onScreenStateChanged(int)
9689 */
9690 void dispatchScreenStateChanged(int screenState) {
9691 onScreenStateChanged(screenState);
9692 }
9693
9694 /**
9695 * This method is called whenever the state of the screen this view is
9696 * attached to changes. A state change will usually occurs when the screen
9697 * turns on or off (whether it happens automatically or the user does it
9698 * manually.)
9699 *
9700 * @param screenState The new state of the screen. Can be either
9701 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
9702 */
9703 public void onScreenStateChanged(int screenState) {
9704 }
9705
9706 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009707 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9708 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009709 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009710 private void resolveLayoutDirectionIfNeeded() {
9711 // Do not resolve if it is not needed
9712 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9713
9714 // Clear any previous layout direction resolution
9715 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9716
9717 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009718 switch (getLayoutDirection()) {
9719 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009720 // We cannot do the resolution if there is no parent
9721 if (mParent == null) return;
9722
Cibu Johny86666632010-02-22 13:01:02 -08009723 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009724 if (mParent instanceof ViewGroup) {
9725 ViewGroup viewGroup = ((ViewGroup) mParent);
9726
9727 // Check if the parent view group can resolve
9728 if (! viewGroup.canResolveLayoutDirection()) {
9729 return;
9730 }
9731 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9732 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9733 }
Cibu Johny86666632010-02-22 13:01:02 -08009734 }
9735 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009736 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009737 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009738 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009739 case LAYOUT_DIRECTION_LOCALE:
9740 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009741 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009742 }
9743 break;
9744 default:
9745 // Nothing to do, LTR by default
9746 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009747
9748 // Set to resolved
9749 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009750 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08009751 // Resolve padding
9752 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009753 }
9754
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009755 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009756 * Called when layout direction has been resolved.
9757 *
9758 * The default implementation does nothing.
9759 */
9760 public void onResolvedLayoutDirectionChanged() {
9761 }
9762
9763 /**
9764 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009765 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009766 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009767 // If the user specified the absolute padding (either with android:padding or
9768 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9769 // use the default padding or the padding from the background drawable
9770 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009771 int resolvedLayoutDirection = getResolvedLayoutDirection();
9772 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009773 case LAYOUT_DIRECTION_RTL:
9774 // Start user padding override Right user padding. Otherwise, if Right user
9775 // padding is not defined, use the default Right padding. If Right user padding
9776 // is defined, just use it.
9777 if (mUserPaddingStart >= 0) {
9778 mUserPaddingRight = mUserPaddingStart;
9779 } else if (mUserPaddingRight < 0) {
9780 mUserPaddingRight = mPaddingRight;
9781 }
9782 if (mUserPaddingEnd >= 0) {
9783 mUserPaddingLeft = mUserPaddingEnd;
9784 } else if (mUserPaddingLeft < 0) {
9785 mUserPaddingLeft = mPaddingLeft;
9786 }
9787 break;
9788 case LAYOUT_DIRECTION_LTR:
9789 default:
9790 // Start user padding override Left user padding. Otherwise, if Left user
9791 // padding is not defined, use the default left padding. If Left user padding
9792 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009793 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009794 mUserPaddingLeft = mUserPaddingStart;
9795 } else if (mUserPaddingLeft < 0) {
9796 mUserPaddingLeft = mPaddingLeft;
9797 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009798 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009799 mUserPaddingRight = mUserPaddingEnd;
9800 } else if (mUserPaddingRight < 0) {
9801 mUserPaddingRight = mPaddingRight;
9802 }
9803 }
9804
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009805 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9806
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08009807 if(isPaddingRelative()) {
9808 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
9809 } else {
9810 recomputePadding();
9811 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009812 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009813 }
9814
9815 /**
9816 * Resolve padding depending on the layout direction. Subclasses that care about
9817 * padding resolution should override this method. The default implementation does
9818 * nothing.
9819 *
9820 * @param layoutDirection the direction of the layout
9821 *
Fabrice Di Meglio085585f2012-03-05 13:33:57 -08009822 * {@link #LAYOUT_DIRECTION_LTR}
9823 * {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009824 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009825 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009826 }
9827
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009828 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009829 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009830 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009831 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009832 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009833 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009834 switch (getLayoutDirection()) {
9835 case LAYOUT_DIRECTION_INHERIT:
9836 return (mParent != null);
9837 default:
9838 return true;
9839 }
9840 }
9841
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009842 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009843 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
9844 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009845 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009846 public void resetResolvedLayoutDirection() {
9847 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009848 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009849 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -08009850 // Reset also the text direction
9851 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009852 }
9853
9854 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009855 * Called during reset of resolved layout direction.
9856 *
9857 * Subclasses need to override this method to clear cached information that depends on the
9858 * resolved layout direction, or to inform child views that inherit their layout direction.
9859 *
9860 * The default implementation does nothing.
9861 */
9862 public void onResolvedLayoutDirectionReset() {
9863 }
9864
9865 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009866 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009867 *
9868 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009869 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009870 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009871 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -08009872 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009873 }
9874
9875 /**
9876 * This is called when the view is detached from a window. At this point it
9877 * no longer has a surface for drawing.
9878 *
9879 * @see #onAttachedToWindow()
9880 */
9881 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009882 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009883
Romain Guya440b002010-02-24 15:57:54 -08009884 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009885 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009886 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009887 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009889 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009890
Romain Guy6d7475d2011-07-27 16:28:21 -07009891 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009892
Chet Haasedaf98e92011-01-10 14:10:36 -08009893 if (mDisplayList != null) {
9894 mDisplayList.invalidate();
9895 }
9896
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009897 if (mAttachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009898 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009899 }
9900
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009901 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009902
9903 resetResolvedLayoutDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009904 }
9905
9906 /**
9907 * @return The number of times this view has been attached to a window
9908 */
9909 protected int getWindowAttachCount() {
9910 return mWindowAttachCount;
9911 }
9912
9913 /**
9914 * Retrieve a unique token identifying the window this view is attached to.
9915 * @return Return the window's token for use in
9916 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9917 */
9918 public IBinder getWindowToken() {
9919 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9920 }
9921
9922 /**
9923 * Retrieve a unique token identifying the top-level "real" window of
9924 * the window that this view is attached to. That is, this is like
9925 * {@link #getWindowToken}, except if the window this view in is a panel
9926 * window (attached to another containing window), then the token of
9927 * the containing window is returned instead.
9928 *
9929 * @return Returns the associated window token, either
9930 * {@link #getWindowToken()} or the containing window's token.
9931 */
9932 public IBinder getApplicationWindowToken() {
9933 AttachInfo ai = mAttachInfo;
9934 if (ai != null) {
9935 IBinder appWindowToken = ai.mPanelParentWindowToken;
9936 if (appWindowToken == null) {
9937 appWindowToken = ai.mWindowToken;
9938 }
9939 return appWindowToken;
9940 }
9941 return null;
9942 }
9943
9944 /**
9945 * Retrieve private session object this view hierarchy is using to
9946 * communicate with the window manager.
9947 * @return the session object to communicate with the window manager
9948 */
9949 /*package*/ IWindowSession getWindowSession() {
9950 return mAttachInfo != null ? mAttachInfo.mSession : null;
9951 }
9952
9953 /**
9954 * @param info the {@link android.view.View.AttachInfo} to associated with
9955 * this view
9956 */
9957 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9958 //System.out.println("Attached! " + this);
9959 mAttachInfo = info;
9960 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009961 // We will need to evaluate the drawable state at least once.
9962 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009963 if (mFloatingTreeObserver != null) {
9964 info.mTreeObserver.merge(mFloatingTreeObserver);
9965 mFloatingTreeObserver = null;
9966 }
9967 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9968 mAttachInfo.mScrollContainers.add(this);
9969 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9970 }
9971 performCollectViewAttributes(visibility);
9972 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009973
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009974 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009975 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009976 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009977 if (listeners != null && listeners.size() > 0) {
9978 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9979 // perform the dispatching. The iterator is a safe guard against listeners that
9980 // could mutate the list by calling the various add/remove methods. This prevents
9981 // the array from being modified while we iterate it.
9982 for (OnAttachStateChangeListener listener : listeners) {
9983 listener.onViewAttachedToWindow(this);
9984 }
9985 }
9986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009987 int vis = info.mWindowVisibility;
9988 if (vis != GONE) {
9989 onWindowVisibilityChanged(vis);
9990 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009991 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9992 // If nobody has evaluated the drawable state yet, then do it now.
9993 refreshDrawableState();
9994 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009995 }
9996
9997 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009998 AttachInfo info = mAttachInfo;
9999 if (info != null) {
10000 int vis = info.mWindowVisibility;
10001 if (vis != GONE) {
10002 onWindowVisibilityChanged(GONE);
10003 }
10004 }
10005
10006 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080010007
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010008 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080010009 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010010 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080010011 if (listeners != null && listeners.size() > 0) {
10012 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
10013 // perform the dispatching. The iterator is a safe guard against listeners that
10014 // could mutate the list by calling the various add/remove methods. This prevents
10015 // the array from being modified while we iterate it.
10016 for (OnAttachStateChangeListener listener : listeners) {
10017 listener.onViewDetachedFromWindow(this);
10018 }
10019 }
10020
Romain Guy01d5edc2011-01-28 11:28:53 -080010021 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010022 mAttachInfo.mScrollContainers.remove(this);
10023 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
10024 }
Romain Guy01d5edc2011-01-28 11:28:53 -080010025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010026 mAttachInfo = null;
10027 }
10028
10029 /**
10030 * Store this view hierarchy's frozen state into the given container.
10031 *
10032 * @param container The SparseArray in which to save the view's state.
10033 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010034 * @see #restoreHierarchyState(android.util.SparseArray)
10035 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10036 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010037 */
10038 public void saveHierarchyState(SparseArray<Parcelable> container) {
10039 dispatchSaveInstanceState(container);
10040 }
10041
10042 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010043 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
10044 * this view and its children. May be overridden to modify how freezing happens to a
10045 * 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 -080010046 *
10047 * @param container The SparseArray in which to save the view's state.
10048 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010049 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10050 * @see #saveHierarchyState(android.util.SparseArray)
10051 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010052 */
10053 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
10054 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
10055 mPrivateFlags &= ~SAVE_STATE_CALLED;
10056 Parcelable state = onSaveInstanceState();
10057 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10058 throw new IllegalStateException(
10059 "Derived class did not call super.onSaveInstanceState()");
10060 }
10061 if (state != null) {
10062 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
10063 // + ": " + state);
10064 container.put(mID, state);
10065 }
10066 }
10067 }
10068
10069 /**
10070 * Hook allowing a view to generate a representation of its internal state
10071 * that can later be used to create a new instance with that same state.
10072 * This state should only contain information that is not persistent or can
10073 * not be reconstructed later. For example, you will never store your
10074 * current position on screen because that will be computed again when a
10075 * new instance of the view is placed in its view hierarchy.
10076 * <p>
10077 * Some examples of things you may store here: the current cursor position
10078 * in a text view (but usually not the text itself since that is stored in a
10079 * content provider or other persistent storage), the currently selected
10080 * item in a list view.
10081 *
10082 * @return Returns a Parcelable object containing the view's current dynamic
10083 * state, or null if there is nothing interesting to save. The
10084 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010085 * @see #onRestoreInstanceState(android.os.Parcelable)
10086 * @see #saveHierarchyState(android.util.SparseArray)
10087 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010088 * @see #setSaveEnabled(boolean)
10089 */
10090 protected Parcelable onSaveInstanceState() {
10091 mPrivateFlags |= SAVE_STATE_CALLED;
10092 return BaseSavedState.EMPTY_STATE;
10093 }
10094
10095 /**
10096 * Restore this view hierarchy's frozen state from the given container.
10097 *
10098 * @param container The SparseArray which holds previously frozen states.
10099 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010100 * @see #saveHierarchyState(android.util.SparseArray)
10101 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10102 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010103 */
10104 public void restoreHierarchyState(SparseArray<Parcelable> container) {
10105 dispatchRestoreInstanceState(container);
10106 }
10107
10108 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010109 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
10110 * state for this view and its children. May be overridden to modify how restoring
10111 * happens to a view's children; for example, some views may want to not store state
10112 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010113 *
10114 * @param container The SparseArray which holds previously saved state.
10115 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010116 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10117 * @see #restoreHierarchyState(android.util.SparseArray)
10118 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010119 */
10120 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
10121 if (mID != NO_ID) {
10122 Parcelable state = container.get(mID);
10123 if (state != null) {
10124 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
10125 // + ": " + state);
10126 mPrivateFlags &= ~SAVE_STATE_CALLED;
10127 onRestoreInstanceState(state);
10128 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10129 throw new IllegalStateException(
10130 "Derived class did not call super.onRestoreInstanceState()");
10131 }
10132 }
10133 }
10134 }
10135
10136 /**
10137 * Hook allowing a view to re-apply a representation of its internal state that had previously
10138 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
10139 * null state.
10140 *
10141 * @param state The frozen state that had previously been returned by
10142 * {@link #onSaveInstanceState}.
10143 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010144 * @see #onSaveInstanceState()
10145 * @see #restoreHierarchyState(android.util.SparseArray)
10146 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010147 */
10148 protected void onRestoreInstanceState(Parcelable state) {
10149 mPrivateFlags |= SAVE_STATE_CALLED;
10150 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080010151 throw new IllegalArgumentException("Wrong state class, expecting View State but "
10152 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080010153 + "when two views of different type have the same id in the same hierarchy. "
10154 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080010155 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010156 }
10157 }
10158
10159 /**
10160 * <p>Return the time at which the drawing of the view hierarchy started.</p>
10161 *
10162 * @return the drawing start time in milliseconds
10163 */
10164 public long getDrawingTime() {
10165 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
10166 }
10167
10168 /**
10169 * <p>Enables or disables the duplication of the parent's state into this view. When
10170 * duplication is enabled, this view gets its drawable state from its parent rather
10171 * than from its own internal properties.</p>
10172 *
10173 * <p>Note: in the current implementation, setting this property to true after the
10174 * view was added to a ViewGroup might have no effect at all. This property should
10175 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
10176 *
10177 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
10178 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010179 *
Gilles Debunnefb817032011-01-13 13:52:49 -080010180 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
10181 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010182 *
10183 * @param enabled True to enable duplication of the parent's drawable state, false
10184 * to disable it.
10185 *
10186 * @see #getDrawableState()
10187 * @see #isDuplicateParentStateEnabled()
10188 */
10189 public void setDuplicateParentStateEnabled(boolean enabled) {
10190 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
10191 }
10192
10193 /**
10194 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
10195 *
10196 * @return True if this view's drawable state is duplicated from the parent,
10197 * false otherwise
10198 *
10199 * @see #getDrawableState()
10200 * @see #setDuplicateParentStateEnabled(boolean)
10201 */
10202 public boolean isDuplicateParentStateEnabled() {
10203 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
10204 }
10205
10206 /**
Romain Guy171c5922011-01-06 10:04:23 -080010207 * <p>Specifies the type of layer backing this view. The layer can be
10208 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
10209 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010210 *
Romain Guy171c5922011-01-06 10:04:23 -080010211 * <p>A layer is associated with an optional {@link android.graphics.Paint}
10212 * instance that controls how the layer is composed on screen. The following
10213 * properties of the paint are taken into account when composing the layer:</p>
10214 * <ul>
10215 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
10216 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
10217 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
10218 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080010219 *
Romain Guy171c5922011-01-06 10:04:23 -080010220 * <p>If this view has an alpha value set to < 1.0 by calling
10221 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
10222 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
10223 * equivalent to setting a hardware layer on this view and providing a paint with
10224 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080010225 *
Romain Guy171c5922011-01-06 10:04:23 -080010226 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
10227 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
10228 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010229 *
Romain Guy171c5922011-01-06 10:04:23 -080010230 * @param layerType The ype of layer to use with this view, must be one of
10231 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10232 * {@link #LAYER_TYPE_HARDWARE}
10233 * @param paint The paint used to compose the layer. This argument is optional
10234 * and can be null. It is ignored when the layer type is
10235 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080010236 *
10237 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080010238 * @see #LAYER_TYPE_NONE
10239 * @see #LAYER_TYPE_SOFTWARE
10240 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080010241 * @see #setAlpha(float)
10242 *
Romain Guy171c5922011-01-06 10:04:23 -080010243 * @attr ref android.R.styleable#View_layerType
10244 */
10245 public void setLayerType(int layerType, Paint paint) {
10246 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080010247 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080010248 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
10249 }
Chet Haasedaf98e92011-01-10 14:10:36 -080010250
Romain Guyd6cd5722011-01-17 14:42:41 -080010251 if (layerType == mLayerType) {
10252 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10253 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010254 invalidateParentCaches();
10255 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010256 }
10257 return;
10258 }
Romain Guy171c5922011-01-06 10:04:23 -080010259
10260 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010261 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010262 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010263 destroyLayer();
Romain Guy31f2c2e2011-11-21 10:55:41 -080010264 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010265 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010266 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010267 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010268 default:
10269 break;
Romain Guy171c5922011-01-06 10:04:23 -080010270 }
10271
10272 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010273 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10274 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10275 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010276
Romain Guy0fd89bf2011-01-26 15:41:30 -080010277 invalidateParentCaches();
10278 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010279 }
10280
10281 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010282 * Indicates whether this view has a static layer. A view with layer type
10283 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10284 * dynamic.
10285 */
10286 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080010287 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070010288 }
10289
10290 /**
Romain Guy171c5922011-01-06 10:04:23 -080010291 * Indicates what type of layer is currently associated with this view. By default
10292 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10293 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10294 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010295 *
Romain Guy171c5922011-01-06 10:04:23 -080010296 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10297 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010298 *
10299 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010300 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010301 * @see #LAYER_TYPE_NONE
10302 * @see #LAYER_TYPE_SOFTWARE
10303 * @see #LAYER_TYPE_HARDWARE
10304 */
10305 public int getLayerType() {
10306 return mLayerType;
10307 }
Joe Malin32736f02011-01-19 16:14:20 -080010308
Romain Guy6c319ca2011-01-11 14:29:25 -080010309 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010310 * Forces this view's layer to be created and this view to be rendered
10311 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10312 * invoking this method will have no effect.
10313 *
10314 * This method can for instance be used to render a view into its layer before
10315 * starting an animation. If this view is complex, rendering into the layer
10316 * before starting the animation will avoid skipping frames.
10317 *
10318 * @throws IllegalStateException If this view is not attached to a window
10319 *
10320 * @see #setLayerType(int, android.graphics.Paint)
10321 */
10322 public void buildLayer() {
10323 if (mLayerType == LAYER_TYPE_NONE) return;
10324
10325 if (mAttachInfo == null) {
10326 throw new IllegalStateException("This view must be attached to a window first");
10327 }
10328
10329 switch (mLayerType) {
10330 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080010331 if (mAttachInfo.mHardwareRenderer != null &&
10332 mAttachInfo.mHardwareRenderer.isEnabled() &&
10333 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080010334 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080010335 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010336 break;
10337 case LAYER_TYPE_SOFTWARE:
10338 buildDrawingCache(true);
10339 break;
10340 }
10341 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010342
10343 // Make sure the HardwareRenderer.validate() was invoked before calling this method
10344 void flushLayer() {
10345 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
10346 mHardwareLayer.flush();
10347 }
10348 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010349
10350 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010351 * <p>Returns a hardware layer that can be used to draw this view again
10352 * without executing its draw method.</p>
10353 *
10354 * @return A HardwareLayer ready to render, or null if an error occurred.
10355 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080010356 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010357 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10358 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010359 return null;
10360 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010361
10362 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080010363
10364 final int width = mRight - mLeft;
10365 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010366
Romain Guy6c319ca2011-01-11 14:29:25 -080010367 if (width == 0 || height == 0) {
10368 return null;
10369 }
10370
10371 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10372 if (mHardwareLayer == null) {
10373 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10374 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070010375 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080010376 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10377 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070010378 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080010379 }
10380
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010381 // The layer is not valid if the underlying GPU resources cannot be allocated
10382 if (!mHardwareLayer.isValid()) {
10383 return null;
10384 }
10385
Michael Jurka7e52caf2012-03-06 15:57:06 -080010386 mHardwareLayer.redraw(getDisplayList(), mLocalDirtyRect);
10387 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010388 }
10389
10390 return mHardwareLayer;
10391 }
Romain Guy171c5922011-01-06 10:04:23 -080010392
Romain Guy589b0bb2011-10-10 13:57:47 -070010393 /**
10394 * Destroys this View's hardware layer if possible.
10395 *
10396 * @return True if the layer was destroyed, false otherwise.
10397 *
10398 * @see #setLayerType(int, android.graphics.Paint)
10399 * @see #LAYER_TYPE_HARDWARE
10400 */
Romain Guy65b345f2011-07-27 18:51:50 -070010401 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010402 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010403 AttachInfo info = mAttachInfo;
10404 if (info != null && info.mHardwareRenderer != null &&
10405 info.mHardwareRenderer.isEnabled() && info.mHardwareRenderer.validate()) {
10406 mHardwareLayer.destroy();
10407 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080010408
Romain Guy9c4b79a2011-11-10 19:23:58 -080010409 invalidate(true);
10410 invalidateParentCaches();
10411 }
Romain Guy65b345f2011-07-27 18:51:50 -070010412 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010413 }
Romain Guy65b345f2011-07-27 18:51:50 -070010414 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010415 }
10416
Romain Guy171c5922011-01-06 10:04:23 -080010417 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080010418 * Destroys all hardware rendering resources. This method is invoked
10419 * when the system needs to reclaim resources. Upon execution of this
10420 * method, you should free any OpenGL resources created by the view.
10421 *
10422 * Note: you <strong>must</strong> call
10423 * <code>super.destroyHardwareResources()</code> when overriding
10424 * this method.
10425 *
10426 * @hide
10427 */
10428 protected void destroyHardwareResources() {
10429 destroyLayer();
10430 }
10431
10432 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010433 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10434 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10435 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10436 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10437 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10438 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010439 *
Romain Guy171c5922011-01-06 10:04:23 -080010440 * <p>Enabling the drawing cache is similar to
10441 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010442 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10443 * drawing cache has no effect on rendering because the system uses a different mechanism
10444 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10445 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10446 * for information on how to enable software and hardware layers.</p>
10447 *
10448 * <p>This API can be used to manually generate
10449 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10450 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010451 *
10452 * @param enabled true to enable the drawing cache, false otherwise
10453 *
10454 * @see #isDrawingCacheEnabled()
10455 * @see #getDrawingCache()
10456 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010457 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010458 */
10459 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010460 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010461 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10462 }
10463
10464 /**
10465 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10466 *
10467 * @return true if the drawing cache is enabled
10468 *
10469 * @see #setDrawingCacheEnabled(boolean)
10470 * @see #getDrawingCache()
10471 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010472 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010473 public boolean isDrawingCacheEnabled() {
10474 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10475 }
10476
10477 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010478 * Debugging utility which recursively outputs the dirty state of a view and its
10479 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010480 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010481 * @hide
10482 */
Romain Guy676b1732011-02-14 14:45:33 -080010483 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010484 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10485 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10486 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10487 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10488 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10489 if (clear) {
10490 mPrivateFlags &= clearMask;
10491 }
10492 if (this instanceof ViewGroup) {
10493 ViewGroup parent = (ViewGroup) this;
10494 final int count = parent.getChildCount();
10495 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010496 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010497 child.outputDirtyFlags(indent + " ", clear, clearMask);
10498 }
10499 }
10500 }
10501
10502 /**
10503 * This method is used by ViewGroup to cause its children to restore or recreate their
10504 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10505 * to recreate its own display list, which would happen if it went through the normal
10506 * draw/dispatchDraw mechanisms.
10507 *
10508 * @hide
10509 */
10510 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010511
10512 /**
10513 * A view that is not attached or hardware accelerated cannot create a display list.
10514 * This method checks these conditions and returns the appropriate result.
10515 *
10516 * @return true if view has the ability to create a display list, false otherwise.
10517 *
10518 * @hide
10519 */
10520 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010521 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010522 }
Joe Malin32736f02011-01-19 16:14:20 -080010523
Chet Haasedaf98e92011-01-10 14:10:36 -080010524 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080010525 * @return The HardwareRenderer associated with that view or null if hardware rendering
10526 * is not supported or this this has not been attached to a window.
10527 *
10528 * @hide
10529 */
10530 public HardwareRenderer getHardwareRenderer() {
10531 if (mAttachInfo != null) {
10532 return mAttachInfo.mHardwareRenderer;
10533 }
10534 return null;
10535 }
10536
10537 /**
Romain Guyb051e892010-09-28 19:09:36 -070010538 * <p>Returns a display list that can be used to draw this view again
10539 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010540 *
Romain Guyb051e892010-09-28 19:09:36 -070010541 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010542 *
10543 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010544 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010545 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -080010546 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -070010547 return null;
10548 }
10549
Chet Haasedaf98e92011-01-10 14:10:36 -080010550 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10551 mDisplayList == null || !mDisplayList.isValid() ||
10552 mRecreateDisplayList)) {
10553 // Don't need to recreate the display list, just need to tell our
10554 // children to restore/recreate theirs
10555 if (mDisplayList != null && mDisplayList.isValid() &&
10556 !mRecreateDisplayList) {
10557 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10558 mPrivateFlags &= ~DIRTY_MASK;
10559 dispatchGetDisplayList();
10560
10561 return mDisplayList;
10562 }
10563
10564 // If we got here, we're recreating it. Mark it as such to ensure that
10565 // we copy in child display lists into ours in drawChild()
10566 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -080010567 if (mDisplayList == null) {
Romain Guy13631f32012-01-30 17:41:55 -080010568 final String name = getClass().getSimpleName();
10569 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
Chet Haasedaf98e92011-01-10 14:10:36 -080010570 // If we're creating a new display list, make sure our parent gets invalidated
10571 // since they will need to recreate their display list to account for this
10572 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -080010573 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -080010574 }
Romain Guyb051e892010-09-28 19:09:36 -070010575
10576 final HardwareCanvas canvas = mDisplayList.start();
Romain Guye080af32011-09-08 15:03:39 -070010577 int restoreCount = 0;
Romain Guyb051e892010-09-28 19:09:36 -070010578 try {
10579 int width = mRight - mLeft;
10580 int height = mBottom - mTop;
10581
10582 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -080010583 // The dirty rect should always be null for a display list
10584 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -070010585
Chet Haasedaf98e92011-01-10 14:10:36 -080010586 computeScroll();
Romain Guye080af32011-09-08 15:03:39 -070010587
10588 restoreCount = canvas.save();
Chet Haasedaf98e92011-01-10 14:10:36 -080010589 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010590 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -070010591 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -080010592
Romain Guyb051e892010-09-28 19:09:36 -070010593 // Fast path for layouts with no backgrounds
10594 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -070010595 dispatchDraw(canvas);
10596 } else {
10597 draw(canvas);
10598 }
Romain Guyb051e892010-09-28 19:09:36 -070010599 } finally {
Romain Guye080af32011-09-08 15:03:39 -070010600 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -070010601 canvas.onPostDraw();
10602
10603 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -070010604 }
Chet Haase77785f92011-01-25 23:22:09 -080010605 } else {
10606 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10607 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -070010608 }
10609
10610 return mDisplayList;
10611 }
10612
10613 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010614 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010615 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010616 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010617 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010618 * @see #getDrawingCache(boolean)
10619 */
10620 public Bitmap getDrawingCache() {
10621 return getDrawingCache(false);
10622 }
10623
10624 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010625 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10626 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10627 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10628 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10629 * request the drawing cache by calling this method and draw it on screen if the
10630 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010631 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010632 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10633 * this method will create a bitmap of the same size as this view. Because this bitmap
10634 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10635 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10636 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10637 * size than the view. This implies that your application must be able to handle this
10638 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010639 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010640 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10641 * the current density of the screen when the application is in compatibility
10642 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010643 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010644 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010645 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010646 * @see #setDrawingCacheEnabled(boolean)
10647 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010648 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010649 * @see #destroyDrawingCache()
10650 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010651 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010652 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10653 return null;
10654 }
10655 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010656 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010657 }
Romain Guy02890fd2010-08-06 17:58:44 -070010658 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010659 }
10660
10661 /**
10662 * <p>Frees the resources used by the drawing cache. If you call
10663 * {@link #buildDrawingCache()} manually without calling
10664 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10665 * should cleanup the cache with this method afterwards.</p>
10666 *
10667 * @see #setDrawingCacheEnabled(boolean)
10668 * @see #buildDrawingCache()
10669 * @see #getDrawingCache()
10670 */
10671 public void destroyDrawingCache() {
10672 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010673 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010674 mDrawingCache = null;
10675 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010676 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010677 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010678 mUnscaledDrawingCache = null;
10679 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010680 }
10681
10682 /**
10683 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010684 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010685 * view will always be drawn on top of a solid color.
10686 *
10687 * @param color The background color to use for the drawing cache's bitmap
10688 *
10689 * @see #setDrawingCacheEnabled(boolean)
10690 * @see #buildDrawingCache()
10691 * @see #getDrawingCache()
10692 */
10693 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010694 if (color != mDrawingCacheBackgroundColor) {
10695 mDrawingCacheBackgroundColor = color;
10696 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010698 }
10699
10700 /**
10701 * @see #setDrawingCacheBackgroundColor(int)
10702 *
10703 * @return The background color to used for the drawing cache's bitmap
10704 */
10705 public int getDrawingCacheBackgroundColor() {
10706 return mDrawingCacheBackgroundColor;
10707 }
10708
10709 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010710 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010711 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010712 * @see #buildDrawingCache(boolean)
10713 */
10714 public void buildDrawingCache() {
10715 buildDrawingCache(false);
10716 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010717
Romain Guyfbd8f692009-06-26 14:51:58 -070010718 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010719 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10720 *
10721 * <p>If you call {@link #buildDrawingCache()} manually without calling
10722 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10723 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010724 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010725 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10726 * this method will create a bitmap of the same size as this view. Because this bitmap
10727 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10728 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10729 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10730 * size than the view. This implies that your application must be able to handle this
10731 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010732 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010733 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10734 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010735 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010736 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010737 *
10738 * @see #getDrawingCache()
10739 * @see #destroyDrawingCache()
10740 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010741 public void buildDrawingCache(boolean autoScale) {
10742 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010743 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010744 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010745
10746 if (ViewDebug.TRACE_HIERARCHY) {
10747 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010749
Romain Guy8506ab42009-06-11 17:35:47 -070010750 int width = mRight - mLeft;
10751 int height = mBottom - mTop;
10752
10753 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010754 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010755
Romain Guye1123222009-06-29 14:24:56 -070010756 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010757 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10758 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010760
10761 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010762 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010763 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010764
10765 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010766 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010767 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010768 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10769 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010770 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010771 return;
10772 }
10773
10774 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010775 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010776
10777 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010778 Bitmap.Config quality;
10779 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010780 // Never pick ARGB_4444 because it looks awful
10781 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010782 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10783 case DRAWING_CACHE_QUALITY_AUTO:
10784 quality = Bitmap.Config.ARGB_8888;
10785 break;
10786 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010787 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010788 break;
10789 case DRAWING_CACHE_QUALITY_HIGH:
10790 quality = Bitmap.Config.ARGB_8888;
10791 break;
10792 default:
10793 quality = Bitmap.Config.ARGB_8888;
10794 break;
10795 }
10796 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010797 // Optimization for translucent windows
10798 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010799 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010800 }
10801
10802 // Try to cleanup memory
10803 if (bitmap != null) bitmap.recycle();
10804
10805 try {
10806 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010807 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010808 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010809 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010810 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010811 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010812 }
Adam Powell26153a32010-11-08 15:22:27 -080010813 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010814 } catch (OutOfMemoryError e) {
10815 // If there is not enough memory to create the bitmap cache, just
10816 // ignore the issue as bitmap caches are not required to draw the
10817 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010818 if (autoScale) {
10819 mDrawingCache = null;
10820 } else {
10821 mUnscaledDrawingCache = null;
10822 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010823 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010824 return;
10825 }
10826
10827 clear = drawingCacheBackgroundColor != 0;
10828 }
10829
10830 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010831 if (attachInfo != null) {
10832 canvas = attachInfo.mCanvas;
10833 if (canvas == null) {
10834 canvas = new Canvas();
10835 }
10836 canvas.setBitmap(bitmap);
10837 // Temporarily clobber the cached Canvas in case one of our children
10838 // is also using a drawing cache. Without this, the children would
10839 // steal the canvas by attaching their own bitmap to it and bad, bad
10840 // thing would happen (invisible views, corrupted drawings, etc.)
10841 attachInfo.mCanvas = null;
10842 } else {
10843 // This case should hopefully never or seldom happen
10844 canvas = new Canvas(bitmap);
10845 }
10846
10847 if (clear) {
10848 bitmap.eraseColor(drawingCacheBackgroundColor);
10849 }
10850
10851 computeScroll();
10852 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010853
Romain Guye1123222009-06-29 14:24:56 -070010854 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010855 final float scale = attachInfo.mApplicationScale;
10856 canvas.scale(scale, scale);
10857 }
Joe Malin32736f02011-01-19 16:14:20 -080010858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010859 canvas.translate(-mScrollX, -mScrollY);
10860
Romain Guy5bcdff42009-05-14 21:27:18 -070010861 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010862 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10863 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010864 mPrivateFlags |= DRAWING_CACHE_VALID;
10865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010866
10867 // Fast path for layouts with no backgrounds
10868 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10869 if (ViewDebug.TRACE_HIERARCHY) {
10870 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10871 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010872 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010873 dispatchDraw(canvas);
10874 } else {
10875 draw(canvas);
10876 }
10877
10878 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010879 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010880
10881 if (attachInfo != null) {
10882 // Restore the cached Canvas for our siblings
10883 attachInfo.mCanvas = canvas;
10884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010885 }
10886 }
10887
10888 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010889 * Create a snapshot of the view into a bitmap. We should probably make
10890 * some form of this public, but should think about the API.
10891 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010892 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010893 int width = mRight - mLeft;
10894 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010895
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010896 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010897 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010898 width = (int) ((width * scale) + 0.5f);
10899 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010900
Romain Guy8c11e312009-09-14 15:15:30 -070010901 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010902 if (bitmap == null) {
10903 throw new OutOfMemoryError();
10904 }
10905
Romain Guyc529d8d2011-09-06 15:01:39 -070010906 Resources resources = getResources();
10907 if (resources != null) {
10908 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
10909 }
Joe Malin32736f02011-01-19 16:14:20 -080010910
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010911 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010912 if (attachInfo != null) {
10913 canvas = attachInfo.mCanvas;
10914 if (canvas == null) {
10915 canvas = new Canvas();
10916 }
10917 canvas.setBitmap(bitmap);
10918 // Temporarily clobber the cached Canvas in case one of our children
10919 // is also using a drawing cache. Without this, the children would
10920 // steal the canvas by attaching their own bitmap to it and bad, bad
10921 // things would happen (invisible views, corrupted drawings, etc.)
10922 attachInfo.mCanvas = null;
10923 } else {
10924 // This case should hopefully never or seldom happen
10925 canvas = new Canvas(bitmap);
10926 }
10927
Romain Guy5bcdff42009-05-14 21:27:18 -070010928 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010929 bitmap.eraseColor(backgroundColor);
10930 }
10931
10932 computeScroll();
10933 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010934 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010935 canvas.translate(-mScrollX, -mScrollY);
10936
Romain Guy5bcdff42009-05-14 21:27:18 -070010937 // Temporarily remove the dirty mask
10938 int flags = mPrivateFlags;
10939 mPrivateFlags &= ~DIRTY_MASK;
10940
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010941 // Fast path for layouts with no backgrounds
10942 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10943 dispatchDraw(canvas);
10944 } else {
10945 draw(canvas);
10946 }
10947
Romain Guy5bcdff42009-05-14 21:27:18 -070010948 mPrivateFlags = flags;
10949
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010950 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010951 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010952
10953 if (attachInfo != null) {
10954 // Restore the cached Canvas for our siblings
10955 attachInfo.mCanvas = canvas;
10956 }
Romain Guy8506ab42009-06-11 17:35:47 -070010957
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010958 return bitmap;
10959 }
10960
10961 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010962 * Indicates whether this View is currently in edit mode. A View is usually
10963 * in edit mode when displayed within a developer tool. For instance, if
10964 * this View is being drawn by a visual user interface builder, this method
10965 * should return true.
10966 *
10967 * Subclasses should check the return value of this method to provide
10968 * different behaviors if their normal behavior might interfere with the
10969 * host environment. For instance: the class spawns a thread in its
10970 * constructor, the drawing code relies on device-specific features, etc.
10971 *
10972 * This method is usually checked in the drawing code of custom widgets.
10973 *
10974 * @return True if this View is in edit mode, false otherwise.
10975 */
10976 public boolean isInEditMode() {
10977 return false;
10978 }
10979
10980 /**
10981 * If the View draws content inside its padding and enables fading edges,
10982 * it needs to support padding offsets. Padding offsets are added to the
10983 * fading edges to extend the length of the fade so that it covers pixels
10984 * drawn inside the padding.
10985 *
10986 * Subclasses of this class should override this method if they need
10987 * to draw content inside the padding.
10988 *
10989 * @return True if padding offset must be applied, false otherwise.
10990 *
10991 * @see #getLeftPaddingOffset()
10992 * @see #getRightPaddingOffset()
10993 * @see #getTopPaddingOffset()
10994 * @see #getBottomPaddingOffset()
10995 *
10996 * @since CURRENT
10997 */
10998 protected boolean isPaddingOffsetRequired() {
10999 return false;
11000 }
11001
11002 /**
11003 * Amount by which to extend the left fading region. Called only when
11004 * {@link #isPaddingOffsetRequired()} returns true.
11005 *
11006 * @return The left padding offset in pixels.
11007 *
11008 * @see #isPaddingOffsetRequired()
11009 *
11010 * @since CURRENT
11011 */
11012 protected int getLeftPaddingOffset() {
11013 return 0;
11014 }
11015
11016 /**
11017 * Amount by which to extend the right fading region. Called only when
11018 * {@link #isPaddingOffsetRequired()} returns true.
11019 *
11020 * @return The right padding offset in pixels.
11021 *
11022 * @see #isPaddingOffsetRequired()
11023 *
11024 * @since CURRENT
11025 */
11026 protected int getRightPaddingOffset() {
11027 return 0;
11028 }
11029
11030 /**
11031 * Amount by which to extend the top fading region. Called only when
11032 * {@link #isPaddingOffsetRequired()} returns true.
11033 *
11034 * @return The top padding offset in pixels.
11035 *
11036 * @see #isPaddingOffsetRequired()
11037 *
11038 * @since CURRENT
11039 */
11040 protected int getTopPaddingOffset() {
11041 return 0;
11042 }
11043
11044 /**
11045 * Amount by which to extend the bottom fading region. Called only when
11046 * {@link #isPaddingOffsetRequired()} returns true.
11047 *
11048 * @return The bottom padding offset in pixels.
11049 *
11050 * @see #isPaddingOffsetRequired()
11051 *
11052 * @since CURRENT
11053 */
11054 protected int getBottomPaddingOffset() {
11055 return 0;
11056 }
11057
11058 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070011059 * @hide
11060 * @param offsetRequired
11061 */
11062 protected int getFadeTop(boolean offsetRequired) {
11063 int top = mPaddingTop;
11064 if (offsetRequired) top += getTopPaddingOffset();
11065 return top;
11066 }
11067
11068 /**
11069 * @hide
11070 * @param offsetRequired
11071 */
11072 protected int getFadeHeight(boolean offsetRequired) {
11073 int padding = mPaddingTop;
11074 if (offsetRequired) padding += getTopPaddingOffset();
11075 return mBottom - mTop - mPaddingBottom - padding;
11076 }
11077
11078 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011079 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070011080 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011081 *
Romain Guy2bffd262010-09-12 17:40:02 -070011082 * <p>Even if this method returns true, it does not mean that every call
11083 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
11084 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011085 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070011086 * window is hardware accelerated,
11087 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
11088 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011089 *
Romain Guy2bffd262010-09-12 17:40:02 -070011090 * @return True if the view is attached to a window and the window is
11091 * hardware accelerated; false in any other case.
11092 */
11093 public boolean isHardwareAccelerated() {
11094 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
11095 }
Joe Malin32736f02011-01-19 16:14:20 -080011096
Romain Guy2bffd262010-09-12 17:40:02 -070011097 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080011098 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
11099 * case of an active Animation being run on the view.
11100 */
11101 private boolean drawAnimation(ViewGroup parent, long drawingTime,
11102 Animation a, boolean scalingRequired) {
11103 Transformation invalidationTransform;
11104 final int flags = parent.mGroupFlags;
11105 final boolean initialized = a.isInitialized();
11106 if (!initialized) {
11107 a.initialize(mRight - mLeft, mBottom - mTop, getWidth(), getHeight());
11108 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
11109 onAnimationStart();
11110 }
11111
11112 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
11113 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
11114 if (parent.mInvalidationTransformation == null) {
11115 parent.mInvalidationTransformation = new Transformation();
11116 }
11117 invalidationTransform = parent.mInvalidationTransformation;
11118 a.getTransformation(drawingTime, invalidationTransform, 1f);
11119 } else {
11120 invalidationTransform = parent.mChildTransformation;
11121 }
11122 if (more) {
11123 if (!a.willChangeBounds()) {
11124 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
11125 parent.FLAG_OPTIMIZE_INVALIDATE) {
11126 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
11127 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
11128 // The child need to draw an animation, potentially offscreen, so
11129 // make sure we do not cancel invalidate requests
11130 parent.mPrivateFlags |= DRAW_ANIMATION;
11131 parent.invalidate(mLeft, mTop, mRight, mBottom);
11132 }
11133 } else {
11134 if (parent.mInvalidateRegion == null) {
11135 parent.mInvalidateRegion = new RectF();
11136 }
11137 final RectF region = parent.mInvalidateRegion;
11138 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
11139 invalidationTransform);
11140
11141 // The child need to draw an animation, potentially offscreen, so
11142 // make sure we do not cancel invalidate requests
11143 parent.mPrivateFlags |= DRAW_ANIMATION;
11144
11145 final int left = mLeft + (int) region.left;
11146 final int top = mTop + (int) region.top;
11147 parent.invalidate(left, top, left + (int) (region.width() + .5f),
11148 top + (int) (region.height() + .5f));
11149 }
11150 }
11151 return more;
11152 }
11153
11154 /**
Chet Haase64a48c12012-02-13 16:33:29 -080011155 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
11156 * This draw() method is an implementation detail and is not intended to be overridden or
11157 * to be called from anywhere else other than ViewGroup.drawChild().
11158 */
11159 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
11160 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080011161 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080011162 final int flags = parent.mGroupFlags;
11163
11164 if ((flags & parent.FLAG_CLEAR_TRANSFORMATION) == parent.FLAG_CLEAR_TRANSFORMATION) {
11165 parent.mChildTransformation.clear();
11166 parent.mGroupFlags &= ~parent.FLAG_CLEAR_TRANSFORMATION;
11167 }
11168
11169 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080011170 boolean concatMatrix = false;
11171
11172 boolean scalingRequired = false;
11173 boolean caching;
11174 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
11175
11176 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
11177 if ((flags & parent.FLAG_CHILDREN_DRAWN_WITH_CACHE) == parent.FLAG_CHILDREN_DRAWN_WITH_CACHE ||
11178 (flags & parent.FLAG_ALWAYS_DRAWN_WITH_CACHE) == parent.FLAG_ALWAYS_DRAWN_WITH_CACHE) {
11179 caching = true;
11180 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
11181 } else {
11182 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
11183 }
11184
Chet Haasebcca79a2012-02-14 08:45:14 -080011185 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080011186 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011187 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080011188 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080011189 transformToApply = parent.mChildTransformation;
Chet Haase64a48c12012-02-13 16:33:29 -080011190 } else if ((flags & parent.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) ==
11191 parent.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011192 final boolean hasTransform =
11193 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080011194 if (hasTransform) {
11195 final int transformType = parent.mChildTransformation.getTransformationType();
11196 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
11197 parent.mChildTransformation : null;
11198 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
11199 }
11200 }
11201
11202 concatMatrix |= !childHasIdentityMatrix;
11203
11204 // Sets the flag as early as possible to allow draw() implementations
11205 // to call invalidate() successfully when doing animations
11206 mPrivateFlags |= DRAWN;
11207
Chet Haasebcca79a2012-02-14 08:45:14 -080011208 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080011209 (mPrivateFlags & DRAW_ANIMATION) == 0) {
11210 return more;
11211 }
11212
11213 if (hardwareAccelerated) {
11214 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
11215 // retain the flag's value temporarily in the mRecreateDisplayList flag
11216 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
11217 mPrivateFlags &= ~INVALIDATED;
11218 }
11219
11220 computeScroll();
11221
11222 final int sx = mScrollX;
11223 final int sy = mScrollY;
11224
11225 DisplayList displayList = null;
11226 Bitmap cache = null;
11227 boolean hasDisplayList = false;
11228 if (caching) {
11229 if (!hardwareAccelerated) {
11230 if (layerType != LAYER_TYPE_NONE) {
11231 layerType = LAYER_TYPE_SOFTWARE;
11232 buildDrawingCache(true);
11233 }
11234 cache = getDrawingCache(true);
11235 } else {
11236 switch (layerType) {
11237 case LAYER_TYPE_SOFTWARE:
11238 buildDrawingCache(true);
11239 cache = getDrawingCache(true);
11240 break;
11241 case LAYER_TYPE_NONE:
11242 // Delay getting the display list until animation-driven alpha values are
11243 // set up and possibly passed on to the view
11244 hasDisplayList = canHaveDisplayList();
11245 break;
11246 }
11247 }
11248 }
11249
11250 final boolean hasNoCache = cache == null || hasDisplayList;
11251 final boolean offsetForScroll = cache == null && !hasDisplayList &&
11252 layerType != LAYER_TYPE_HARDWARE;
11253
11254 final int restoreTo = canvas.save();
11255 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011256 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080011257 } else {
Chet Haasebcca79a2012-02-14 08:45:14 -080011258 canvas.translate(mLeft, mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080011259 if (scalingRequired) {
11260 // mAttachInfo cannot be null, otherwise scalingRequired == false
11261 final float scale = 1.0f / mAttachInfo.mApplicationScale;
11262 canvas.scale(scale, scale);
11263 }
11264 }
11265
11266 float alpha = getAlpha();
11267 if (transformToApply != null || alpha < 1.0f || !hasIdentityMatrix()) {
11268 if (transformToApply != null || !childHasIdentityMatrix) {
11269 int transX = 0;
11270 int transY = 0;
11271
11272 if (offsetForScroll) {
11273 transX = -sx;
11274 transY = -sy;
11275 }
11276
11277 if (transformToApply != null) {
11278 if (concatMatrix) {
11279 // Undo the scroll translation, apply the transformation matrix,
11280 // then redo the scroll translate to get the correct result.
11281 canvas.translate(-transX, -transY);
11282 canvas.concat(transformToApply.getMatrix());
11283 canvas.translate(transX, transY);
11284 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11285 }
11286
11287 float transformAlpha = transformToApply.getAlpha();
11288 if (transformAlpha < 1.0f) {
11289 alpha *= transformToApply.getAlpha();
11290 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11291 }
11292 }
11293
11294 if (!childHasIdentityMatrix) {
11295 canvas.translate(-transX, -transY);
11296 canvas.concat(getMatrix());
11297 canvas.translate(transX, transY);
11298 }
11299 }
11300
11301 if (alpha < 1.0f) {
11302 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11303 if (hasNoCache) {
11304 final int multipliedAlpha = (int) (255 * alpha);
11305 if (!onSetAlpha(multipliedAlpha)) {
11306 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11307 if ((flags & parent.FLAG_CLIP_CHILDREN) == parent.FLAG_CLIP_CHILDREN ||
11308 layerType != LAYER_TYPE_NONE) {
11309 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
11310 }
11311 if (layerType == LAYER_TYPE_NONE) {
11312 final int scrollX = hasDisplayList ? 0 : sx;
11313 final int scrollY = hasDisplayList ? 0 : sy;
Chet Haasebcca79a2012-02-14 08:45:14 -080011314 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
11315 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080011316 }
11317 } else {
11318 // Alpha is handled by the child directly, clobber the layer's alpha
11319 mPrivateFlags |= ALPHA_SET;
11320 }
11321 }
11322 }
11323 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11324 onSetAlpha(255);
11325 mPrivateFlags &= ~ALPHA_SET;
11326 }
11327
11328 if ((flags & parent.FLAG_CLIP_CHILDREN) == parent.FLAG_CLIP_CHILDREN) {
11329 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011330 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080011331 } else {
11332 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011333 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080011334 } else {
11335 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
11336 }
11337 }
11338 }
11339
11340 if (hasDisplayList) {
11341 displayList = getDisplayList();
11342 if (!displayList.isValid()) {
11343 // Uncommon, but possible. If a view is removed from the hierarchy during the call
11344 // to getDisplayList(), the display list will be marked invalid and we should not
11345 // try to use it again.
11346 displayList = null;
11347 hasDisplayList = false;
11348 }
11349 }
11350
11351 if (hasNoCache) {
11352 boolean layerRendered = false;
11353 if (layerType == LAYER_TYPE_HARDWARE) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011354 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080011355 if (layer != null && layer.isValid()) {
11356 mLayerPaint.setAlpha((int) (alpha * 255));
11357 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
11358 layerRendered = true;
11359 } else {
11360 final int scrollX = hasDisplayList ? 0 : sx;
11361 final int scrollY = hasDisplayList ? 0 : sy;
11362 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080011363 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080011364 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11365 }
11366 }
11367
11368 if (!layerRendered) {
11369 if (!hasDisplayList) {
11370 // Fast path for layouts with no backgrounds
11371 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11372 if (ViewDebug.TRACE_HIERARCHY) {
11373 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
11374 }
11375 mPrivateFlags &= ~DIRTY_MASK;
11376 dispatchDraw(canvas);
11377 } else {
11378 draw(canvas);
11379 }
11380 } else {
11381 mPrivateFlags &= ~DIRTY_MASK;
Chet Haasebcca79a2012-02-14 08:45:14 -080011382 ((HardwareCanvas) canvas).drawDisplayList(displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -080011383 mRight - mLeft, mBottom - mTop, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080011384 }
11385 }
11386 } else if (cache != null) {
11387 mPrivateFlags &= ~DIRTY_MASK;
11388 Paint cachePaint;
11389
11390 if (layerType == LAYER_TYPE_NONE) {
11391 cachePaint = parent.mCachePaint;
11392 if (cachePaint == null) {
11393 cachePaint = new Paint();
11394 cachePaint.setDither(false);
11395 parent.mCachePaint = cachePaint;
11396 }
11397 if (alpha < 1.0f) {
11398 cachePaint.setAlpha((int) (alpha * 255));
11399 parent.mGroupFlags |= parent.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haasebcca79a2012-02-14 08:45:14 -080011400 } else if ((flags & parent.FLAG_ALPHA_LOWER_THAN_ONE) ==
11401 parent.FLAG_ALPHA_LOWER_THAN_ONE) {
Chet Haase64a48c12012-02-13 16:33:29 -080011402 cachePaint.setAlpha(255);
11403 parent.mGroupFlags &= ~parent.FLAG_ALPHA_LOWER_THAN_ONE;
11404 }
11405 } else {
11406 cachePaint = mLayerPaint;
11407 cachePaint.setAlpha((int) (alpha * 255));
11408 }
11409 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
11410 }
11411
11412 canvas.restoreToCount(restoreTo);
11413
11414 if (a != null && !more) {
11415 if (!hardwareAccelerated && !a.getFillAfter()) {
11416 onSetAlpha(255);
11417 }
11418 parent.finishAnimatingView(this, a);
11419 }
11420
11421 if (more && hardwareAccelerated) {
11422 // invalidation is the trigger to recreate display lists, so if we're using
11423 // display lists to render, force an invalidate to allow the animation to
11424 // continue drawing another frame
11425 parent.invalidate(true);
11426 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11427 // alpha animations should cause the child to recreate its display list
11428 invalidate(true);
11429 }
11430 }
11431
11432 mRecreateDisplayList = false;
11433
11434 return more;
11435 }
11436
11437 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011438 * Manually render this view (and all of its children) to the given Canvas.
11439 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011440 * called. When implementing a view, implement
11441 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
11442 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011443 *
11444 * @param canvas The Canvas to which the View is rendered.
11445 */
11446 public void draw(Canvas canvas) {
11447 if (ViewDebug.TRACE_HIERARCHY) {
11448 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
11449 }
11450
Romain Guy5bcdff42009-05-14 21:27:18 -070011451 final int privateFlags = mPrivateFlags;
11452 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
11453 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
11454 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070011455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011456 /*
11457 * Draw traversal performs several drawing steps which must be executed
11458 * in the appropriate order:
11459 *
11460 * 1. Draw the background
11461 * 2. If necessary, save the canvas' layers to prepare for fading
11462 * 3. Draw view's content
11463 * 4. Draw children
11464 * 5. If necessary, draw the fading edges and restore layers
11465 * 6. Draw decorations (scrollbars for instance)
11466 */
11467
11468 // Step 1, draw the background, if needed
11469 int saveCount;
11470
Romain Guy24443ea2009-05-11 11:56:30 -070011471 if (!dirtyOpaque) {
11472 final Drawable background = mBGDrawable;
11473 if (background != null) {
11474 final int scrollX = mScrollX;
11475 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011476
Romain Guy24443ea2009-05-11 11:56:30 -070011477 if (mBackgroundSizeChanged) {
11478 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
11479 mBackgroundSizeChanged = false;
11480 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011481
Romain Guy24443ea2009-05-11 11:56:30 -070011482 if ((scrollX | scrollY) == 0) {
11483 background.draw(canvas);
11484 } else {
11485 canvas.translate(scrollX, scrollY);
11486 background.draw(canvas);
11487 canvas.translate(-scrollX, -scrollY);
11488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011489 }
11490 }
11491
11492 // skip step 2 & 5 if possible (common case)
11493 final int viewFlags = mViewFlags;
11494 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
11495 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
11496 if (!verticalEdges && !horizontalEdges) {
11497 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011498 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011499
11500 // Step 4, draw the children
11501 dispatchDraw(canvas);
11502
11503 // Step 6, draw decorations (scrollbars)
11504 onDrawScrollBars(canvas);
11505
11506 // we're done...
11507 return;
11508 }
11509
11510 /*
11511 * Here we do the full fledged routine...
11512 * (this is an uncommon case where speed matters less,
11513 * this is why we repeat some of the tests that have been
11514 * done above)
11515 */
11516
11517 boolean drawTop = false;
11518 boolean drawBottom = false;
11519 boolean drawLeft = false;
11520 boolean drawRight = false;
11521
11522 float topFadeStrength = 0.0f;
11523 float bottomFadeStrength = 0.0f;
11524 float leftFadeStrength = 0.0f;
11525 float rightFadeStrength = 0.0f;
11526
11527 // Step 2, save the canvas' layers
11528 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011529
11530 final boolean offsetRequired = isPaddingOffsetRequired();
11531 if (offsetRequired) {
11532 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011533 }
11534
11535 int left = mScrollX + paddingLeft;
11536 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070011537 int top = mScrollY + getFadeTop(offsetRequired);
11538 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011539
11540 if (offsetRequired) {
11541 right += getRightPaddingOffset();
11542 bottom += getBottomPaddingOffset();
11543 }
11544
11545 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011546 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
11547 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011548
11549 // clip the fade length if top and bottom fades overlap
11550 // overlapping fades produce odd-looking artifacts
11551 if (verticalEdges && (top + length > bottom - length)) {
11552 length = (bottom - top) / 2;
11553 }
11554
11555 // also clip horizontal fades if necessary
11556 if (horizontalEdges && (left + length > right - length)) {
11557 length = (right - left) / 2;
11558 }
11559
11560 if (verticalEdges) {
11561 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011562 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011563 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011564 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011565 }
11566
11567 if (horizontalEdges) {
11568 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011569 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011570 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011571 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011572 }
11573
11574 saveCount = canvas.getSaveCount();
11575
11576 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070011577 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011578 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11579
11580 if (drawTop) {
11581 canvas.saveLayer(left, top, right, top + length, null, flags);
11582 }
11583
11584 if (drawBottom) {
11585 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
11586 }
11587
11588 if (drawLeft) {
11589 canvas.saveLayer(left, top, left + length, bottom, null, flags);
11590 }
11591
11592 if (drawRight) {
11593 canvas.saveLayer(right - length, top, right, bottom, null, flags);
11594 }
11595 } else {
11596 scrollabilityCache.setFadeColor(solidColor);
11597 }
11598
11599 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011600 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011601
11602 // Step 4, draw the children
11603 dispatchDraw(canvas);
11604
11605 // Step 5, draw the fade effect and restore layers
11606 final Paint p = scrollabilityCache.paint;
11607 final Matrix matrix = scrollabilityCache.matrix;
11608 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011609
11610 if (drawTop) {
11611 matrix.setScale(1, fadeHeight * topFadeStrength);
11612 matrix.postTranslate(left, top);
11613 fade.setLocalMatrix(matrix);
11614 canvas.drawRect(left, top, right, top + length, p);
11615 }
11616
11617 if (drawBottom) {
11618 matrix.setScale(1, fadeHeight * bottomFadeStrength);
11619 matrix.postRotate(180);
11620 matrix.postTranslate(left, bottom);
11621 fade.setLocalMatrix(matrix);
11622 canvas.drawRect(left, bottom - length, right, bottom, p);
11623 }
11624
11625 if (drawLeft) {
11626 matrix.setScale(1, fadeHeight * leftFadeStrength);
11627 matrix.postRotate(-90);
11628 matrix.postTranslate(left, top);
11629 fade.setLocalMatrix(matrix);
11630 canvas.drawRect(left, top, left + length, bottom, p);
11631 }
11632
11633 if (drawRight) {
11634 matrix.setScale(1, fadeHeight * rightFadeStrength);
11635 matrix.postRotate(90);
11636 matrix.postTranslate(right, top);
11637 fade.setLocalMatrix(matrix);
11638 canvas.drawRect(right - length, top, right, bottom, p);
11639 }
11640
11641 canvas.restoreToCount(saveCount);
11642
11643 // Step 6, draw decorations (scrollbars)
11644 onDrawScrollBars(canvas);
11645 }
11646
11647 /**
11648 * Override this if your view is known to always be drawn on top of a solid color background,
11649 * and needs to draw fading edges. Returning a non-zero color enables the view system to
11650 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
11651 * should be set to 0xFF.
11652 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011653 * @see #setVerticalFadingEdgeEnabled(boolean)
11654 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011655 *
11656 * @return The known solid color background for this view, or 0 if the color may vary
11657 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011658 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011659 public int getSolidColor() {
11660 return 0;
11661 }
11662
11663 /**
11664 * Build a human readable string representation of the specified view flags.
11665 *
11666 * @param flags the view flags to convert to a string
11667 * @return a String representing the supplied flags
11668 */
11669 private static String printFlags(int flags) {
11670 String output = "";
11671 int numFlags = 0;
11672 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
11673 output += "TAKES_FOCUS";
11674 numFlags++;
11675 }
11676
11677 switch (flags & VISIBILITY_MASK) {
11678 case INVISIBLE:
11679 if (numFlags > 0) {
11680 output += " ";
11681 }
11682 output += "INVISIBLE";
11683 // USELESS HERE numFlags++;
11684 break;
11685 case GONE:
11686 if (numFlags > 0) {
11687 output += " ";
11688 }
11689 output += "GONE";
11690 // USELESS HERE numFlags++;
11691 break;
11692 default:
11693 break;
11694 }
11695 return output;
11696 }
11697
11698 /**
11699 * Build a human readable string representation of the specified private
11700 * view flags.
11701 *
11702 * @param privateFlags the private view flags to convert to a string
11703 * @return a String representing the supplied flags
11704 */
11705 private static String printPrivateFlags(int privateFlags) {
11706 String output = "";
11707 int numFlags = 0;
11708
11709 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11710 output += "WANTS_FOCUS";
11711 numFlags++;
11712 }
11713
11714 if ((privateFlags & FOCUSED) == FOCUSED) {
11715 if (numFlags > 0) {
11716 output += " ";
11717 }
11718 output += "FOCUSED";
11719 numFlags++;
11720 }
11721
11722 if ((privateFlags & SELECTED) == SELECTED) {
11723 if (numFlags > 0) {
11724 output += " ";
11725 }
11726 output += "SELECTED";
11727 numFlags++;
11728 }
11729
11730 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
11731 if (numFlags > 0) {
11732 output += " ";
11733 }
11734 output += "IS_ROOT_NAMESPACE";
11735 numFlags++;
11736 }
11737
11738 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
11739 if (numFlags > 0) {
11740 output += " ";
11741 }
11742 output += "HAS_BOUNDS";
11743 numFlags++;
11744 }
11745
11746 if ((privateFlags & DRAWN) == DRAWN) {
11747 if (numFlags > 0) {
11748 output += " ";
11749 }
11750 output += "DRAWN";
11751 // USELESS HERE numFlags++;
11752 }
11753 return output;
11754 }
11755
11756 /**
11757 * <p>Indicates whether or not this view's layout will be requested during
11758 * the next hierarchy layout pass.</p>
11759 *
11760 * @return true if the layout will be forced during next layout pass
11761 */
11762 public boolean isLayoutRequested() {
11763 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
11764 }
11765
11766 /**
11767 * Assign a size and position to a view and all of its
11768 * descendants
11769 *
11770 * <p>This is the second phase of the layout mechanism.
11771 * (The first is measuring). In this phase, each parent calls
11772 * layout on all of its children to position them.
11773 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080011774 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011775 *
Chet Haase9c087442011-01-12 16:20:16 -080011776 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011777 * Derived classes with children should override
11778 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080011779 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011780 *
11781 * @param l Left position, relative to parent
11782 * @param t Top position, relative to parent
11783 * @param r Right position, relative to parent
11784 * @param b Bottom position, relative to parent
11785 */
Romain Guy5429e1d2010-09-07 12:38:00 -070011786 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080011787 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070011788 int oldL = mLeft;
11789 int oldT = mTop;
11790 int oldB = mBottom;
11791 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011792 boolean changed = setFrame(l, t, r, b);
11793 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
11794 if (ViewDebug.TRACE_HIERARCHY) {
11795 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
11796 }
11797
11798 onLayout(changed, l, t, r, b);
11799 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070011800
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011801 ListenerInfo li = mListenerInfo;
11802 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070011803 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011804 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070011805 int numListeners = listenersCopy.size();
11806 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070011807 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070011808 }
11809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011810 }
11811 mPrivateFlags &= ~FORCE_LAYOUT;
11812 }
11813
11814 /**
11815 * Called from layout when this view should
11816 * assign a size and position to each of its children.
11817 *
11818 * Derived classes with children should override
11819 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070011820 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011821 * @param changed This is a new size or position for this view
11822 * @param left Left position, relative to parent
11823 * @param top Top position, relative to parent
11824 * @param right Right position, relative to parent
11825 * @param bottom Bottom position, relative to parent
11826 */
11827 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
11828 }
11829
11830 /**
11831 * Assign a size and position to this view.
11832 *
11833 * This is called from layout.
11834 *
11835 * @param left Left position, relative to parent
11836 * @param top Top position, relative to parent
11837 * @param right Right position, relative to parent
11838 * @param bottom Bottom position, relative to parent
11839 * @return true if the new size and position are different than the
11840 * previous ones
11841 * {@hide}
11842 */
11843 protected boolean setFrame(int left, int top, int right, int bottom) {
11844 boolean changed = false;
11845
11846 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070011847 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011848 + right + "," + bottom + ")");
11849 }
11850
11851 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
11852 changed = true;
11853
11854 // Remember our drawn bit
11855 int drawn = mPrivateFlags & DRAWN;
11856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011857 int oldWidth = mRight - mLeft;
11858 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070011859 int newWidth = right - left;
11860 int newHeight = bottom - top;
11861 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
11862
11863 // Invalidate our old position
11864 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011865
11866 mLeft = left;
11867 mTop = top;
11868 mRight = right;
11869 mBottom = bottom;
11870
11871 mPrivateFlags |= HAS_BOUNDS;
11872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011873
Chet Haase75755e22011-07-18 17:48:25 -070011874 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011875 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
11876 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011877 if (mTransformationInfo != null) {
11878 mTransformationInfo.mMatrixDirty = true;
11879 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011881 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
11882 }
11883
11884 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
11885 // If we are visible, force the DRAWN bit to on so that
11886 // this invalidate will go through (at least to our parent).
11887 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011888 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011889 // the DRAWN bit.
11890 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070011891 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080011892 // parent display list may need to be recreated based on a change in the bounds
11893 // of any child
11894 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011895 }
11896
11897 // Reset drawn bit to original value (invalidate turns it off)
11898 mPrivateFlags |= drawn;
11899
11900 mBackgroundSizeChanged = true;
11901 }
11902 return changed;
11903 }
11904
11905 /**
11906 * Finalize inflating a view from XML. This is called as the last phase
11907 * of inflation, after all child views have been added.
11908 *
11909 * <p>Even if the subclass overrides onFinishInflate, they should always be
11910 * sure to call the super method, so that we get called.
11911 */
11912 protected void onFinishInflate() {
11913 }
11914
11915 /**
11916 * Returns the resources associated with this view.
11917 *
11918 * @return Resources object.
11919 */
11920 public Resources getResources() {
11921 return mResources;
11922 }
11923
11924 /**
11925 * Invalidates the specified Drawable.
11926 *
11927 * @param drawable the drawable to invalidate
11928 */
11929 public void invalidateDrawable(Drawable drawable) {
11930 if (verifyDrawable(drawable)) {
11931 final Rect dirty = drawable.getBounds();
11932 final int scrollX = mScrollX;
11933 final int scrollY = mScrollY;
11934
11935 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11936 dirty.right + scrollX, dirty.bottom + scrollY);
11937 }
11938 }
11939
11940 /**
11941 * Schedules an action on a drawable to occur at a specified time.
11942 *
11943 * @param who the recipient of the action
11944 * @param what the action to run on the drawable
11945 * @param when the time at which the action must occur. Uses the
11946 * {@link SystemClock#uptimeMillis} timebase.
11947 */
11948 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080011949 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080011950 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080011951 if (mAttachInfo != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080011952 mAttachInfo.mViewRootImpl.mChoreographer.postAnimationCallbackDelayed(
11953 what, who, Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080011954 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080011955 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080011956 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011957 }
11958 }
11959
11960 /**
11961 * Cancels a scheduled action on a drawable.
11962 *
11963 * @param who the recipient of the action
11964 * @param what the action to cancel
11965 */
11966 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080011967 if (verifyDrawable(who) && what != null) {
11968 if (mAttachInfo != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080011969 mAttachInfo.mViewRootImpl.mChoreographer.removeAnimationCallbacks(what, who);
Adam Powell37419d72011-11-10 11:32:09 -080011970 } else {
11971 ViewRootImpl.getRunQueue().removeCallbacks(what);
11972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011973 }
11974 }
11975
11976 /**
11977 * Unschedule any events associated with the given Drawable. This can be
11978 * used when selecting a new Drawable into a view, so that the previous
11979 * one is completely unscheduled.
11980 *
11981 * @param who The Drawable to unschedule.
11982 *
11983 * @see #drawableStateChanged
11984 */
11985 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080011986 if (mAttachInfo != null && who != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080011987 mAttachInfo.mViewRootImpl.mChoreographer.removeAnimationCallbacks(null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011988 }
11989 }
11990
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011991 /**
11992 * Return the layout direction of a given Drawable.
11993 *
11994 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011995 */
11996 public int getResolvedLayoutDirection(Drawable who) {
11997 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011998 }
11999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012000 /**
12001 * If your view subclass is displaying its own Drawable objects, it should
12002 * override this function and return true for any Drawable it is
12003 * displaying. This allows animations for those drawables to be
12004 * scheduled.
12005 *
12006 * <p>Be sure to call through to the super class when overriding this
12007 * function.
12008 *
12009 * @param who The Drawable to verify. Return true if it is one you are
12010 * displaying, else return the result of calling through to the
12011 * super class.
12012 *
12013 * @return boolean If true than the Drawable is being displayed in the
12014 * view; else false and it is not allowed to animate.
12015 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012016 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
12017 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012018 */
12019 protected boolean verifyDrawable(Drawable who) {
12020 return who == mBGDrawable;
12021 }
12022
12023 /**
12024 * This function is called whenever the state of the view changes in such
12025 * a way that it impacts the state of drawables being shown.
12026 *
12027 * <p>Be sure to call through to the superclass when overriding this
12028 * function.
12029 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012030 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012031 */
12032 protected void drawableStateChanged() {
12033 Drawable d = mBGDrawable;
12034 if (d != null && d.isStateful()) {
12035 d.setState(getDrawableState());
12036 }
12037 }
12038
12039 /**
12040 * Call this to force a view to update its drawable state. This will cause
12041 * drawableStateChanged to be called on this view. Views that are interested
12042 * in the new state should call getDrawableState.
12043 *
12044 * @see #drawableStateChanged
12045 * @see #getDrawableState
12046 */
12047 public void refreshDrawableState() {
12048 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
12049 drawableStateChanged();
12050
12051 ViewParent parent = mParent;
12052 if (parent != null) {
12053 parent.childDrawableStateChanged(this);
12054 }
12055 }
12056
12057 /**
12058 * Return an array of resource IDs of the drawable states representing the
12059 * current state of the view.
12060 *
12061 * @return The current drawable state
12062 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012063 * @see Drawable#setState(int[])
12064 * @see #drawableStateChanged()
12065 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012066 */
12067 public final int[] getDrawableState() {
12068 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
12069 return mDrawableState;
12070 } else {
12071 mDrawableState = onCreateDrawableState(0);
12072 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
12073 return mDrawableState;
12074 }
12075 }
12076
12077 /**
12078 * Generate the new {@link android.graphics.drawable.Drawable} state for
12079 * this view. This is called by the view
12080 * system when the cached Drawable state is determined to be invalid. To
12081 * retrieve the current state, you should use {@link #getDrawableState}.
12082 *
12083 * @param extraSpace if non-zero, this is the number of extra entries you
12084 * would like in the returned array in which you can place your own
12085 * states.
12086 *
12087 * @return Returns an array holding the current {@link Drawable} state of
12088 * the view.
12089 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012090 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012091 */
12092 protected int[] onCreateDrawableState(int extraSpace) {
12093 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
12094 mParent instanceof View) {
12095 return ((View) mParent).onCreateDrawableState(extraSpace);
12096 }
12097
12098 int[] drawableState;
12099
12100 int privateFlags = mPrivateFlags;
12101
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012102 int viewStateIndex = 0;
12103 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
12104 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
12105 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070012106 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012107 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
12108 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070012109 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
12110 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012111 // This is set if HW acceleration is requested, even if the current
12112 // process doesn't allow it. This is just to allow app preview
12113 // windows to better match their app.
12114 viewStateIndex |= VIEW_STATE_ACCELERATED;
12115 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070012116 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012117
Christopher Tate3d4bf172011-03-28 16:16:46 -070012118 final int privateFlags2 = mPrivateFlags2;
12119 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
12120 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
12121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012122 drawableState = VIEW_STATE_SETS[viewStateIndex];
12123
12124 //noinspection ConstantIfStatement
12125 if (false) {
12126 Log.i("View", "drawableStateIndex=" + viewStateIndex);
12127 Log.i("View", toString()
12128 + " pressed=" + ((privateFlags & PRESSED) != 0)
12129 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
12130 + " fo=" + hasFocus()
12131 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012132 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012133 + ": " + Arrays.toString(drawableState));
12134 }
12135
12136 if (extraSpace == 0) {
12137 return drawableState;
12138 }
12139
12140 final int[] fullState;
12141 if (drawableState != null) {
12142 fullState = new int[drawableState.length + extraSpace];
12143 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
12144 } else {
12145 fullState = new int[extraSpace];
12146 }
12147
12148 return fullState;
12149 }
12150
12151 /**
12152 * Merge your own state values in <var>additionalState</var> into the base
12153 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012154 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012155 *
12156 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012157 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012158 * own additional state values.
12159 *
12160 * @param additionalState The additional state values you would like
12161 * added to <var>baseState</var>; this array is not modified.
12162 *
12163 * @return As a convenience, the <var>baseState</var> array you originally
12164 * passed into the function is returned.
12165 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012166 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012167 */
12168 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
12169 final int N = baseState.length;
12170 int i = N - 1;
12171 while (i >= 0 && baseState[i] == 0) {
12172 i--;
12173 }
12174 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
12175 return baseState;
12176 }
12177
12178 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070012179 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
12180 * on all Drawable objects associated with this view.
12181 */
12182 public void jumpDrawablesToCurrentState() {
12183 if (mBGDrawable != null) {
12184 mBGDrawable.jumpToCurrentState();
12185 }
12186 }
12187
12188 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012189 * Sets the background color for this view.
12190 * @param color the color of the background
12191 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012192 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012193 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070012194 if (mBGDrawable instanceof ColorDrawable) {
12195 ((ColorDrawable) mBGDrawable).setColor(color);
12196 } else {
12197 setBackgroundDrawable(new ColorDrawable(color));
12198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012199 }
12200
12201 /**
12202 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070012203 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012204 * @param resid The identifier of the resource.
12205 * @attr ref android.R.styleable#View_background
12206 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012207 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012208 public void setBackgroundResource(int resid) {
12209 if (resid != 0 && resid == mBackgroundResource) {
12210 return;
12211 }
12212
12213 Drawable d= null;
12214 if (resid != 0) {
12215 d = mResources.getDrawable(resid);
12216 }
12217 setBackgroundDrawable(d);
12218
12219 mBackgroundResource = resid;
12220 }
12221
12222 /**
12223 * Set the background to a given Drawable, or remove the background. If the
12224 * background has padding, this View's padding is set to the background's
12225 * padding. However, when a background is removed, this View's padding isn't
12226 * touched. If setting the padding is desired, please use
12227 * {@link #setPadding(int, int, int, int)}.
12228 *
12229 * @param d The Drawable to use as the background, or null to remove the
12230 * background
12231 */
12232 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070012233 if (d == mBGDrawable) {
12234 return;
12235 }
12236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012237 boolean requestLayout = false;
12238
12239 mBackgroundResource = 0;
12240
12241 /*
12242 * Regardless of whether we're setting a new background or not, we want
12243 * to clear the previous drawable.
12244 */
12245 if (mBGDrawable != null) {
12246 mBGDrawable.setCallback(null);
12247 unscheduleDrawable(mBGDrawable);
12248 }
12249
12250 if (d != null) {
12251 Rect padding = sThreadLocal.get();
12252 if (padding == null) {
12253 padding = new Rect();
12254 sThreadLocal.set(padding);
12255 }
12256 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012257 switch (d.getResolvedLayoutDirectionSelf()) {
12258 case LAYOUT_DIRECTION_RTL:
12259 setPadding(padding.right, padding.top, padding.left, padding.bottom);
12260 break;
12261 case LAYOUT_DIRECTION_LTR:
12262 default:
12263 setPadding(padding.left, padding.top, padding.right, padding.bottom);
12264 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012265 }
12266
12267 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
12268 // if it has a different minimum size, we should layout again
12269 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
12270 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
12271 requestLayout = true;
12272 }
12273
12274 d.setCallback(this);
12275 if (d.isStateful()) {
12276 d.setState(getDrawableState());
12277 }
12278 d.setVisible(getVisibility() == VISIBLE, false);
12279 mBGDrawable = d;
12280
12281 if ((mPrivateFlags & SKIP_DRAW) != 0) {
12282 mPrivateFlags &= ~SKIP_DRAW;
12283 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
12284 requestLayout = true;
12285 }
12286 } else {
12287 /* Remove the background */
12288 mBGDrawable = null;
12289
12290 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
12291 /*
12292 * This view ONLY drew the background before and we're removing
12293 * the background, so now it won't draw anything
12294 * (hence we SKIP_DRAW)
12295 */
12296 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
12297 mPrivateFlags |= SKIP_DRAW;
12298 }
12299
12300 /*
12301 * When the background is set, we try to apply its padding to this
12302 * View. When the background is removed, we don't touch this View's
12303 * padding. This is noted in the Javadocs. Hence, we don't need to
12304 * requestLayout(), the invalidate() below is sufficient.
12305 */
12306
12307 // The old background's minimum size could have affected this
12308 // View's layout, so let's requestLayout
12309 requestLayout = true;
12310 }
12311
Romain Guy8f1344f52009-05-15 16:03:59 -070012312 computeOpaqueFlags();
12313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012314 if (requestLayout) {
12315 requestLayout();
12316 }
12317
12318 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012319 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012320 }
12321
12322 /**
12323 * Gets the background drawable
12324 * @return The drawable used as the background for this view, if any.
12325 */
12326 public Drawable getBackground() {
12327 return mBGDrawable;
12328 }
12329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012330 /**
12331 * Sets the padding. The view may add on the space required to display
12332 * the scrollbars, depending on the style and visibility of the scrollbars.
12333 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
12334 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
12335 * from the values set in this call.
12336 *
12337 * @attr ref android.R.styleable#View_padding
12338 * @attr ref android.R.styleable#View_paddingBottom
12339 * @attr ref android.R.styleable#View_paddingLeft
12340 * @attr ref android.R.styleable#View_paddingRight
12341 * @attr ref android.R.styleable#View_paddingTop
12342 * @param left the left padding in pixels
12343 * @param top the top padding in pixels
12344 * @param right the right padding in pixels
12345 * @param bottom the bottom padding in pixels
12346 */
12347 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012348 mUserPaddingStart = -1;
12349 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012350 mUserPaddingRelative = false;
12351
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012352 internalSetPadding(left, top, right, bottom);
12353 }
12354
12355 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080012356 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012357 mUserPaddingRight = right;
12358 mUserPaddingBottom = bottom;
12359
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012360 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012361 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070012362
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012363 // Common case is there are no scroll bars.
12364 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012365 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080012366 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012367 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080012368 switch (mVerticalScrollbarPosition) {
12369 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012370 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
12371 left += offset;
12372 } else {
12373 right += offset;
12374 }
12375 break;
Adam Powell20232d02010-12-08 21:08:53 -080012376 case SCROLLBAR_POSITION_RIGHT:
12377 right += offset;
12378 break;
12379 case SCROLLBAR_POSITION_LEFT:
12380 left += offset;
12381 break;
12382 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012383 }
Adam Powell20232d02010-12-08 21:08:53 -080012384 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012385 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
12386 ? 0 : getHorizontalScrollbarHeight();
12387 }
12388 }
Romain Guy8506ab42009-06-11 17:35:47 -070012389
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012390 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012391 changed = true;
12392 mPaddingLeft = left;
12393 }
12394 if (mPaddingTop != top) {
12395 changed = true;
12396 mPaddingTop = top;
12397 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012398 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012399 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012400 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012401 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012402 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012403 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012404 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012405 }
12406
12407 if (changed) {
12408 requestLayout();
12409 }
12410 }
12411
12412 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012413 * Sets the relative padding. The view may add on the space required to display
12414 * the scrollbars, depending on the style and visibility of the scrollbars.
12415 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
12416 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
12417 * from the values set in this call.
12418 *
12419 * @attr ref android.R.styleable#View_padding
12420 * @attr ref android.R.styleable#View_paddingBottom
12421 * @attr ref android.R.styleable#View_paddingStart
12422 * @attr ref android.R.styleable#View_paddingEnd
12423 * @attr ref android.R.styleable#View_paddingTop
12424 * @param start the start padding in pixels
12425 * @param top the top padding in pixels
12426 * @param end the end padding in pixels
12427 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012428 */
12429 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070012430 mUserPaddingStart = start;
12431 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012432 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070012433
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012434 switch(getResolvedLayoutDirection()) {
12435 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012436 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012437 break;
12438 case LAYOUT_DIRECTION_LTR:
12439 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012440 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012441 }
12442 }
12443
12444 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012445 * Returns the top padding of this view.
12446 *
12447 * @return the top padding in pixels
12448 */
12449 public int getPaddingTop() {
12450 return mPaddingTop;
12451 }
12452
12453 /**
12454 * Returns the bottom padding of this view. If there are inset and enabled
12455 * scrollbars, this value may include the space required to display the
12456 * scrollbars as well.
12457 *
12458 * @return the bottom padding in pixels
12459 */
12460 public int getPaddingBottom() {
12461 return mPaddingBottom;
12462 }
12463
12464 /**
12465 * Returns the left padding of this view. If there are inset and enabled
12466 * scrollbars, this value may include the space required to display the
12467 * scrollbars as well.
12468 *
12469 * @return the left padding in pixels
12470 */
12471 public int getPaddingLeft() {
12472 return mPaddingLeft;
12473 }
12474
12475 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012476 * Returns the start padding of this view. If there are inset and enabled
12477 * scrollbars, this value may include the space required to display the
12478 * scrollbars as well.
12479 *
12480 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012481 */
12482 public int getPaddingStart() {
12483 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12484 mPaddingRight : mPaddingLeft;
12485 }
12486
12487 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012488 * Returns the right padding of this view. If there are inset and enabled
12489 * scrollbars, this value may include the space required to display the
12490 * scrollbars as well.
12491 *
12492 * @return the right padding in pixels
12493 */
12494 public int getPaddingRight() {
12495 return mPaddingRight;
12496 }
12497
12498 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012499 * Returns the end padding of this view. If there are inset and enabled
12500 * scrollbars, this value may include the space required to display the
12501 * scrollbars as well.
12502 *
12503 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012504 */
12505 public int getPaddingEnd() {
12506 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12507 mPaddingLeft : mPaddingRight;
12508 }
12509
12510 /**
12511 * Return if the padding as been set thru relative values
12512 * {@link #setPaddingRelative(int, int, int, int)} or thru
12513 * @attr ref android.R.styleable#View_paddingStart or
12514 * @attr ref android.R.styleable#View_paddingEnd
12515 *
12516 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012517 */
12518 public boolean isPaddingRelative() {
12519 return mUserPaddingRelative;
12520 }
12521
12522 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012523 * Changes the selection state of this view. A view can be selected or not.
12524 * Note that selection is not the same as focus. Views are typically
12525 * selected in the context of an AdapterView like ListView or GridView;
12526 * the selected view is the view that is highlighted.
12527 *
12528 * @param selected true if the view must be selected, false otherwise
12529 */
12530 public void setSelected(boolean selected) {
12531 if (((mPrivateFlags & SELECTED) != 0) != selected) {
12532 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070012533 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080012534 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012535 refreshDrawableState();
12536 dispatchSetSelected(selected);
12537 }
12538 }
12539
12540 /**
12541 * Dispatch setSelected to all of this View's children.
12542 *
12543 * @see #setSelected(boolean)
12544 *
12545 * @param selected The new selected state
12546 */
12547 protected void dispatchSetSelected(boolean selected) {
12548 }
12549
12550 /**
12551 * Indicates the selection state of this view.
12552 *
12553 * @return true if the view is selected, false otherwise
12554 */
12555 @ViewDebug.ExportedProperty
12556 public boolean isSelected() {
12557 return (mPrivateFlags & SELECTED) != 0;
12558 }
12559
12560 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012561 * Changes the activated state of this view. A view can be activated or not.
12562 * Note that activation is not the same as selection. Selection is
12563 * a transient property, representing the view (hierarchy) the user is
12564 * currently interacting with. Activation is a longer-term state that the
12565 * user can move views in and out of. For example, in a list view with
12566 * single or multiple selection enabled, the views in the current selection
12567 * set are activated. (Um, yeah, we are deeply sorry about the terminology
12568 * here.) The activated state is propagated down to children of the view it
12569 * is set on.
12570 *
12571 * @param activated true if the view must be activated, false otherwise
12572 */
12573 public void setActivated(boolean activated) {
12574 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
12575 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012576 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012577 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070012578 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012579 }
12580 }
12581
12582 /**
12583 * Dispatch setActivated to all of this View's children.
12584 *
12585 * @see #setActivated(boolean)
12586 *
12587 * @param activated The new activated state
12588 */
12589 protected void dispatchSetActivated(boolean activated) {
12590 }
12591
12592 /**
12593 * Indicates the activation state of this view.
12594 *
12595 * @return true if the view is activated, false otherwise
12596 */
12597 @ViewDebug.ExportedProperty
12598 public boolean isActivated() {
12599 return (mPrivateFlags & ACTIVATED) != 0;
12600 }
12601
12602 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012603 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
12604 * observer can be used to get notifications when global events, like
12605 * layout, happen.
12606 *
12607 * The returned ViewTreeObserver observer is not guaranteed to remain
12608 * valid for the lifetime of this View. If the caller of this method keeps
12609 * a long-lived reference to ViewTreeObserver, it should always check for
12610 * the return value of {@link ViewTreeObserver#isAlive()}.
12611 *
12612 * @return The ViewTreeObserver for this view's hierarchy.
12613 */
12614 public ViewTreeObserver getViewTreeObserver() {
12615 if (mAttachInfo != null) {
12616 return mAttachInfo.mTreeObserver;
12617 }
12618 if (mFloatingTreeObserver == null) {
12619 mFloatingTreeObserver = new ViewTreeObserver();
12620 }
12621 return mFloatingTreeObserver;
12622 }
12623
12624 /**
12625 * <p>Finds the topmost view in the current view hierarchy.</p>
12626 *
12627 * @return the topmost view containing this view
12628 */
12629 public View getRootView() {
12630 if (mAttachInfo != null) {
12631 final View v = mAttachInfo.mRootView;
12632 if (v != null) {
12633 return v;
12634 }
12635 }
Romain Guy8506ab42009-06-11 17:35:47 -070012636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012637 View parent = this;
12638
12639 while (parent.mParent != null && parent.mParent instanceof View) {
12640 parent = (View) parent.mParent;
12641 }
12642
12643 return parent;
12644 }
12645
12646 /**
12647 * <p>Computes the coordinates of this view on the screen. The argument
12648 * must be an array of two integers. After the method returns, the array
12649 * contains the x and y location in that order.</p>
12650 *
12651 * @param location an array of two integers in which to hold the coordinates
12652 */
12653 public void getLocationOnScreen(int[] location) {
12654 getLocationInWindow(location);
12655
12656 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070012657 if (info != null) {
12658 location[0] += info.mWindowLeft;
12659 location[1] += info.mWindowTop;
12660 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012661 }
12662
12663 /**
12664 * <p>Computes the coordinates of this view in its window. The argument
12665 * must be an array of two integers. After the method returns, the array
12666 * contains the x and y location in that order.</p>
12667 *
12668 * @param location an array of two integers in which to hold the coordinates
12669 */
12670 public void getLocationInWindow(int[] location) {
12671 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012672 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012673 }
12674
Gilles Debunne6583ce52011-12-06 18:09:02 -080012675 if (mAttachInfo == null) {
12676 // When the view is not attached to a window, this method does not make sense
12677 location[0] = location[1] = 0;
12678 return;
12679 }
12680
Gilles Debunnecea45132011-11-24 02:19:27 +010012681 float[] position = mAttachInfo.mTmpTransformLocation;
12682 position[0] = position[1] = 0.0f;
12683
12684 if (!hasIdentityMatrix()) {
12685 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012687
Gilles Debunnecea45132011-11-24 02:19:27 +010012688 position[0] += mLeft;
12689 position[1] += mTop;
12690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012691 ViewParent viewParent = mParent;
12692 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012693 final View view = (View) viewParent;
12694
12695 position[0] -= view.mScrollX;
12696 position[1] -= view.mScrollY;
12697
12698 if (!view.hasIdentityMatrix()) {
12699 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012700 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012701
12702 position[0] += view.mLeft;
12703 position[1] += view.mTop;
12704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012705 viewParent = view.mParent;
12706 }
Romain Guy8506ab42009-06-11 17:35:47 -070012707
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012708 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012709 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010012710 final ViewRootImpl vr = (ViewRootImpl) viewParent;
12711 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012712 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012713
12714 location[0] = (int) (position[0] + 0.5f);
12715 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012716 }
12717
12718 /**
12719 * {@hide}
12720 * @param id the id of the view to be found
12721 * @return the view of the specified id, null if cannot be found
12722 */
12723 protected View findViewTraversal(int id) {
12724 if (id == mID) {
12725 return this;
12726 }
12727 return null;
12728 }
12729
12730 /**
12731 * {@hide}
12732 * @param tag the tag of the view to be found
12733 * @return the view of specified tag, null if cannot be found
12734 */
12735 protected View findViewWithTagTraversal(Object tag) {
12736 if (tag != null && tag.equals(mTag)) {
12737 return this;
12738 }
12739 return null;
12740 }
12741
12742 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012743 * {@hide}
12744 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070012745 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080012746 * @return The first view that matches the predicate or null.
12747 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070012748 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080012749 if (predicate.apply(this)) {
12750 return this;
12751 }
12752 return null;
12753 }
12754
12755 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012756 * Look for a child view with the given id. If this view has the given
12757 * id, return this view.
12758 *
12759 * @param id The id to search for.
12760 * @return The view that has the given id in the hierarchy or null
12761 */
12762 public final View findViewById(int id) {
12763 if (id < 0) {
12764 return null;
12765 }
12766 return findViewTraversal(id);
12767 }
12768
12769 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070012770 * Finds a view by its unuque and stable accessibility id.
12771 *
12772 * @param accessibilityId The searched accessibility id.
12773 * @return The found view.
12774 */
12775 final View findViewByAccessibilityId(int accessibilityId) {
12776 if (accessibilityId < 0) {
12777 return null;
12778 }
12779 return findViewByAccessibilityIdTraversal(accessibilityId);
12780 }
12781
12782 /**
12783 * Performs the traversal to find a view by its unuque and stable accessibility id.
12784 *
12785 * <strong>Note:</strong>This method does not stop at the root namespace
12786 * boundary since the user can touch the screen at an arbitrary location
12787 * potentially crossing the root namespace bounday which will send an
12788 * accessibility event to accessibility services and they should be able
12789 * to obtain the event source. Also accessibility ids are guaranteed to be
12790 * unique in the window.
12791 *
12792 * @param accessibilityId The accessibility id.
12793 * @return The found view.
12794 */
12795 View findViewByAccessibilityIdTraversal(int accessibilityId) {
12796 if (getAccessibilityViewId() == accessibilityId) {
12797 return this;
12798 }
12799 return null;
12800 }
12801
12802 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012803 * Look for a child view with the given tag. If this view has the given
12804 * tag, return this view.
12805 *
12806 * @param tag The tag to search for, using "tag.equals(getTag())".
12807 * @return The View that has the given tag in the hierarchy or null
12808 */
12809 public final View findViewWithTag(Object tag) {
12810 if (tag == null) {
12811 return null;
12812 }
12813 return findViewWithTagTraversal(tag);
12814 }
12815
12816 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012817 * {@hide}
12818 * Look for a child view that matches the specified predicate.
12819 * If this view matches the predicate, return this view.
12820 *
12821 * @param predicate The predicate to evaluate.
12822 * @return The first view that matches the predicate or null.
12823 */
12824 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070012825 return findViewByPredicateTraversal(predicate, null);
12826 }
12827
12828 /**
12829 * {@hide}
12830 * Look for a child view that matches the specified predicate,
12831 * starting with the specified view and its descendents and then
12832 * recusively searching the ancestors and siblings of that view
12833 * until this view is reached.
12834 *
12835 * This method is useful in cases where the predicate does not match
12836 * a single unique view (perhaps multiple views use the same id)
12837 * and we are trying to find the view that is "closest" in scope to the
12838 * starting view.
12839 *
12840 * @param start The view to start from.
12841 * @param predicate The predicate to evaluate.
12842 * @return The first view that matches the predicate or null.
12843 */
12844 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
12845 View childToSkip = null;
12846 for (;;) {
12847 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
12848 if (view != null || start == this) {
12849 return view;
12850 }
12851
12852 ViewParent parent = start.getParent();
12853 if (parent == null || !(parent instanceof View)) {
12854 return null;
12855 }
12856
12857 childToSkip = start;
12858 start = (View) parent;
12859 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080012860 }
12861
12862 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012863 * Sets the identifier for this view. The identifier does not have to be
12864 * unique in this view's hierarchy. The identifier should be a positive
12865 * number.
12866 *
12867 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070012868 * @see #getId()
12869 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012870 *
12871 * @param id a number used to identify the view
12872 *
12873 * @attr ref android.R.styleable#View_id
12874 */
12875 public void setId(int id) {
12876 mID = id;
12877 }
12878
12879 /**
12880 * {@hide}
12881 *
12882 * @param isRoot true if the view belongs to the root namespace, false
12883 * otherwise
12884 */
12885 public void setIsRootNamespace(boolean isRoot) {
12886 if (isRoot) {
12887 mPrivateFlags |= IS_ROOT_NAMESPACE;
12888 } else {
12889 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
12890 }
12891 }
12892
12893 /**
12894 * {@hide}
12895 *
12896 * @return true if the view belongs to the root namespace, false otherwise
12897 */
12898 public boolean isRootNamespace() {
12899 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
12900 }
12901
12902 /**
12903 * Returns this view's identifier.
12904 *
12905 * @return a positive integer used to identify the view or {@link #NO_ID}
12906 * if the view has no ID
12907 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012908 * @see #setId(int)
12909 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012910 * @attr ref android.R.styleable#View_id
12911 */
12912 @ViewDebug.CapturedViewProperty
12913 public int getId() {
12914 return mID;
12915 }
12916
12917 /**
12918 * Returns this view's tag.
12919 *
12920 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070012921 *
12922 * @see #setTag(Object)
12923 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012924 */
12925 @ViewDebug.ExportedProperty
12926 public Object getTag() {
12927 return mTag;
12928 }
12929
12930 /**
12931 * Sets the tag associated with this view. A tag can be used to mark
12932 * a view in its hierarchy and does not have to be unique within the
12933 * hierarchy. Tags can also be used to store data within a view without
12934 * resorting to another data structure.
12935 *
12936 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070012937 *
12938 * @see #getTag()
12939 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012940 */
12941 public void setTag(final Object tag) {
12942 mTag = tag;
12943 }
12944
12945 /**
Romain Guyd90a3312009-05-06 14:54:28 -070012946 * Returns the tag associated with this view and the specified key.
12947 *
12948 * @param key The key identifying the tag
12949 *
12950 * @return the Object stored in this view as a tag
12951 *
12952 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070012953 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070012954 */
12955 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012956 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070012957 return null;
12958 }
12959
12960 /**
12961 * Sets a tag associated with this view and a key. A tag can be used
12962 * to mark a view in its hierarchy and does not have to be unique within
12963 * the hierarchy. Tags can also be used to store data within a view
12964 * without resorting to another data structure.
12965 *
12966 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070012967 * application to ensure it is unique (see the <a
12968 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
12969 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070012970 * the Android framework or not associated with any package will cause
12971 * an {@link IllegalArgumentException} to be thrown.
12972 *
12973 * @param key The key identifying the tag
12974 * @param tag An Object to tag the view with
12975 *
12976 * @throws IllegalArgumentException If they specified key is not valid
12977 *
12978 * @see #setTag(Object)
12979 * @see #getTag(int)
12980 */
12981 public void setTag(int key, final Object tag) {
12982 // If the package id is 0x00 or 0x01, it's either an undefined package
12983 // or a framework id
12984 if ((key >>> 24) < 2) {
12985 throw new IllegalArgumentException("The key must be an application-specific "
12986 + "resource id.");
12987 }
12988
Adam Powell2b2f6d62011-09-23 11:15:39 -070012989 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012990 }
12991
12992 /**
12993 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12994 * framework id.
12995 *
12996 * @hide
12997 */
12998 public void setTagInternal(int key, Object tag) {
12999 if ((key >>> 24) != 0x1) {
13000 throw new IllegalArgumentException("The key must be a framework-specific "
13001 + "resource id.");
13002 }
13003
Adam Powell2b2f6d62011-09-23 11:15:39 -070013004 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013005 }
13006
Adam Powell2b2f6d62011-09-23 11:15:39 -070013007 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070013008 if (mKeyedTags == null) {
13009 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070013010 }
13011
Adam Powell7db82ac2011-09-22 19:44:04 -070013012 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013013 }
13014
13015 /**
Romain Guy13922e02009-05-12 17:56:14 -070013016 * @param consistency The type of consistency. See ViewDebug for more information.
13017 *
13018 * @hide
13019 */
13020 protected boolean dispatchConsistencyCheck(int consistency) {
13021 return onConsistencyCheck(consistency);
13022 }
13023
13024 /**
13025 * Method that subclasses should implement to check their consistency. The type of
13026 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070013027 *
Romain Guy13922e02009-05-12 17:56:14 -070013028 * @param consistency The type of consistency. See ViewDebug for more information.
13029 *
13030 * @throws IllegalStateException if the view is in an inconsistent state.
13031 *
13032 * @hide
13033 */
13034 protected boolean onConsistencyCheck(int consistency) {
13035 boolean result = true;
13036
13037 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
13038 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
13039
13040 if (checkLayout) {
13041 if (getParent() == null) {
13042 result = false;
13043 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13044 "View " + this + " does not have a parent.");
13045 }
13046
13047 if (mAttachInfo == null) {
13048 result = false;
13049 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13050 "View " + this + " is not attached to a window.");
13051 }
13052 }
13053
13054 if (checkDrawing) {
13055 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
13056 // from their draw() method
13057
13058 if ((mPrivateFlags & DRAWN) != DRAWN &&
13059 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
13060 result = false;
13061 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13062 "View " + this + " was invalidated but its drawing cache is valid.");
13063 }
13064 }
13065
13066 return result;
13067 }
13068
13069 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013070 * Prints information about this view in the log output, with the tag
13071 * {@link #VIEW_LOG_TAG}.
13072 *
13073 * @hide
13074 */
13075 public void debug() {
13076 debug(0);
13077 }
13078
13079 /**
13080 * Prints information about this view in the log output, with the tag
13081 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
13082 * indentation defined by the <code>depth</code>.
13083 *
13084 * @param depth the indentation level
13085 *
13086 * @hide
13087 */
13088 protected void debug(int depth) {
13089 String output = debugIndent(depth - 1);
13090
13091 output += "+ " + this;
13092 int id = getId();
13093 if (id != -1) {
13094 output += " (id=" + id + ")";
13095 }
13096 Object tag = getTag();
13097 if (tag != null) {
13098 output += " (tag=" + tag + ")";
13099 }
13100 Log.d(VIEW_LOG_TAG, output);
13101
13102 if ((mPrivateFlags & FOCUSED) != 0) {
13103 output = debugIndent(depth) + " FOCUSED";
13104 Log.d(VIEW_LOG_TAG, output);
13105 }
13106
13107 output = debugIndent(depth);
13108 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
13109 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
13110 + "} ";
13111 Log.d(VIEW_LOG_TAG, output);
13112
13113 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
13114 || mPaddingBottom != 0) {
13115 output = debugIndent(depth);
13116 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
13117 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
13118 Log.d(VIEW_LOG_TAG, output);
13119 }
13120
13121 output = debugIndent(depth);
13122 output += "mMeasureWidth=" + mMeasuredWidth +
13123 " mMeasureHeight=" + mMeasuredHeight;
13124 Log.d(VIEW_LOG_TAG, output);
13125
13126 output = debugIndent(depth);
13127 if (mLayoutParams == null) {
13128 output += "BAD! no layout params";
13129 } else {
13130 output = mLayoutParams.debug(output);
13131 }
13132 Log.d(VIEW_LOG_TAG, output);
13133
13134 output = debugIndent(depth);
13135 output += "flags={";
13136 output += View.printFlags(mViewFlags);
13137 output += "}";
13138 Log.d(VIEW_LOG_TAG, output);
13139
13140 output = debugIndent(depth);
13141 output += "privateFlags={";
13142 output += View.printPrivateFlags(mPrivateFlags);
13143 output += "}";
13144 Log.d(VIEW_LOG_TAG, output);
13145 }
13146
13147 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013148 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013149 *
13150 * @param depth the indentation level
13151 * @return a String containing (depth * 2 + 3) * 2 white spaces
13152 *
13153 * @hide
13154 */
13155 protected static String debugIndent(int depth) {
13156 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
13157 for (int i = 0; i < (depth * 2) + 3; i++) {
13158 spaces.append(' ').append(' ');
13159 }
13160 return spaces.toString();
13161 }
13162
13163 /**
13164 * <p>Return the offset of the widget's text baseline from the widget's top
13165 * boundary. If this widget does not support baseline alignment, this
13166 * method returns -1. </p>
13167 *
13168 * @return the offset of the baseline within the widget's bounds or -1
13169 * if baseline alignment is not supported
13170 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070013171 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013172 public int getBaseline() {
13173 return -1;
13174 }
13175
13176 /**
13177 * Call this when something has changed which has invalidated the
13178 * layout of this view. This will schedule a layout pass of the view
13179 * tree.
13180 */
13181 public void requestLayout() {
13182 if (ViewDebug.TRACE_HIERARCHY) {
13183 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
13184 }
13185
13186 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013187 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013188
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070013189 if (mParent != null) {
13190 if (mLayoutParams != null) {
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080013191 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070013192 }
13193 if (!mParent.isLayoutRequested()) {
13194 mParent.requestLayout();
13195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013196 }
13197 }
13198
13199 /**
13200 * Forces this view to be laid out during the next layout pass.
13201 * This method does not call requestLayout() or forceLayout()
13202 * on the parent.
13203 */
13204 public void forceLayout() {
13205 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013206 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013207 }
13208
13209 /**
13210 * <p>
13211 * This is called to find out how big a view should be. The parent
13212 * supplies constraint information in the width and height parameters.
13213 * </p>
13214 *
13215 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080013216 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013217 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080013218 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013219 * </p>
13220 *
13221 *
13222 * @param widthMeasureSpec Horizontal space requirements as imposed by the
13223 * parent
13224 * @param heightMeasureSpec Vertical space requirements as imposed by the
13225 * parent
13226 *
13227 * @see #onMeasure(int, int)
13228 */
13229 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
13230 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
13231 widthMeasureSpec != mOldWidthMeasureSpec ||
13232 heightMeasureSpec != mOldHeightMeasureSpec) {
13233
13234 // first clears the measured dimension flag
13235 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
13236
13237 if (ViewDebug.TRACE_HIERARCHY) {
13238 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
13239 }
13240
13241 // measure ourselves, this should set the measured dimension flag back
13242 onMeasure(widthMeasureSpec, heightMeasureSpec);
13243
13244 // flag not set, setMeasuredDimension() was not invoked, we raise
13245 // an exception to warn the developer
13246 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
13247 throw new IllegalStateException("onMeasure() did not set the"
13248 + " measured dimension by calling"
13249 + " setMeasuredDimension()");
13250 }
13251
13252 mPrivateFlags |= LAYOUT_REQUIRED;
13253 }
13254
13255 mOldWidthMeasureSpec = widthMeasureSpec;
13256 mOldHeightMeasureSpec = heightMeasureSpec;
13257 }
13258
13259 /**
13260 * <p>
13261 * Measure the view and its content to determine the measured width and the
13262 * measured height. This method is invoked by {@link #measure(int, int)} and
13263 * should be overriden by subclasses to provide accurate and efficient
13264 * measurement of their contents.
13265 * </p>
13266 *
13267 * <p>
13268 * <strong>CONTRACT:</strong> When overriding this method, you
13269 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
13270 * measured width and height of this view. Failure to do so will trigger an
13271 * <code>IllegalStateException</code>, thrown by
13272 * {@link #measure(int, int)}. Calling the superclass'
13273 * {@link #onMeasure(int, int)} is a valid use.
13274 * </p>
13275 *
13276 * <p>
13277 * The base class implementation of measure defaults to the background size,
13278 * unless a larger size is allowed by the MeasureSpec. Subclasses should
13279 * override {@link #onMeasure(int, int)} to provide better measurements of
13280 * their content.
13281 * </p>
13282 *
13283 * <p>
13284 * If this method is overridden, it is the subclass's responsibility to make
13285 * sure the measured height and width are at least the view's minimum height
13286 * and width ({@link #getSuggestedMinimumHeight()} and
13287 * {@link #getSuggestedMinimumWidth()}).
13288 * </p>
13289 *
13290 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
13291 * The requirements are encoded with
13292 * {@link android.view.View.MeasureSpec}.
13293 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
13294 * The requirements are encoded with
13295 * {@link android.view.View.MeasureSpec}.
13296 *
13297 * @see #getMeasuredWidth()
13298 * @see #getMeasuredHeight()
13299 * @see #setMeasuredDimension(int, int)
13300 * @see #getSuggestedMinimumHeight()
13301 * @see #getSuggestedMinimumWidth()
13302 * @see android.view.View.MeasureSpec#getMode(int)
13303 * @see android.view.View.MeasureSpec#getSize(int)
13304 */
13305 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
13306 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
13307 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
13308 }
13309
13310 /**
13311 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
13312 * measured width and measured height. Failing to do so will trigger an
13313 * exception at measurement time.</p>
13314 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080013315 * @param measuredWidth The measured width of this view. May be a complex
13316 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13317 * {@link #MEASURED_STATE_TOO_SMALL}.
13318 * @param measuredHeight The measured height of this view. May be a complex
13319 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13320 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013321 */
13322 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
13323 mMeasuredWidth = measuredWidth;
13324 mMeasuredHeight = measuredHeight;
13325
13326 mPrivateFlags |= MEASURED_DIMENSION_SET;
13327 }
13328
13329 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080013330 * Merge two states as returned by {@link #getMeasuredState()}.
13331 * @param curState The current state as returned from a view or the result
13332 * of combining multiple views.
13333 * @param newState The new view state to combine.
13334 * @return Returns a new integer reflecting the combination of the two
13335 * states.
13336 */
13337 public static int combineMeasuredStates(int curState, int newState) {
13338 return curState | newState;
13339 }
13340
13341 /**
13342 * Version of {@link #resolveSizeAndState(int, int, int)}
13343 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
13344 */
13345 public static int resolveSize(int size, int measureSpec) {
13346 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
13347 }
13348
13349 /**
13350 * Utility to reconcile a desired size and state, with constraints imposed
13351 * by a MeasureSpec. Will take the desired size, unless a different size
13352 * is imposed by the constraints. The returned value is a compound integer,
13353 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
13354 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
13355 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013356 *
13357 * @param size How big the view wants to be
13358 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080013359 * @return Size information bit mask as defined by
13360 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013361 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080013362 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013363 int result = size;
13364 int specMode = MeasureSpec.getMode(measureSpec);
13365 int specSize = MeasureSpec.getSize(measureSpec);
13366 switch (specMode) {
13367 case MeasureSpec.UNSPECIFIED:
13368 result = size;
13369 break;
13370 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080013371 if (specSize < size) {
13372 result = specSize | MEASURED_STATE_TOO_SMALL;
13373 } else {
13374 result = size;
13375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013376 break;
13377 case MeasureSpec.EXACTLY:
13378 result = specSize;
13379 break;
13380 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080013381 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013382 }
13383
13384 /**
13385 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070013386 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013387 * by the MeasureSpec.
13388 *
13389 * @param size Default size for this view
13390 * @param measureSpec Constraints imposed by the parent
13391 * @return The size this view should be.
13392 */
13393 public static int getDefaultSize(int size, int measureSpec) {
13394 int result = size;
13395 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070013396 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013397
13398 switch (specMode) {
13399 case MeasureSpec.UNSPECIFIED:
13400 result = size;
13401 break;
13402 case MeasureSpec.AT_MOST:
13403 case MeasureSpec.EXACTLY:
13404 result = specSize;
13405 break;
13406 }
13407 return result;
13408 }
13409
13410 /**
13411 * Returns the suggested minimum height that the view should use. This
13412 * returns the maximum of the view's minimum height
13413 * and the background's minimum height
13414 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
13415 * <p>
13416 * When being used in {@link #onMeasure(int, int)}, the caller should still
13417 * ensure the returned height is within the requirements of the parent.
13418 *
13419 * @return The suggested minimum height of the view.
13420 */
13421 protected int getSuggestedMinimumHeight() {
13422 int suggestedMinHeight = mMinHeight;
13423
13424 if (mBGDrawable != null) {
13425 final int bgMinHeight = mBGDrawable.getMinimumHeight();
13426 if (suggestedMinHeight < bgMinHeight) {
13427 suggestedMinHeight = bgMinHeight;
13428 }
13429 }
13430
13431 return suggestedMinHeight;
13432 }
13433
13434 /**
13435 * Returns the suggested minimum width that the view should use. This
13436 * returns the maximum of the view's minimum width)
13437 * and the background's minimum width
13438 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
13439 * <p>
13440 * When being used in {@link #onMeasure(int, int)}, the caller should still
13441 * ensure the returned width is within the requirements of the parent.
13442 *
13443 * @return The suggested minimum width of the view.
13444 */
13445 protected int getSuggestedMinimumWidth() {
13446 int suggestedMinWidth = mMinWidth;
13447
13448 if (mBGDrawable != null) {
13449 final int bgMinWidth = mBGDrawable.getMinimumWidth();
13450 if (suggestedMinWidth < bgMinWidth) {
13451 suggestedMinWidth = bgMinWidth;
13452 }
13453 }
13454
13455 return suggestedMinWidth;
13456 }
13457
13458 /**
13459 * Sets the minimum height of the view. It is not guaranteed the view will
13460 * be able to achieve this minimum height (for example, if its parent layout
13461 * constrains it with less available height).
13462 *
13463 * @param minHeight The minimum height the view will try to be.
13464 */
13465 public void setMinimumHeight(int minHeight) {
13466 mMinHeight = minHeight;
13467 }
13468
13469 /**
13470 * Sets the minimum width of the view. It is not guaranteed the view will
13471 * be able to achieve this minimum width (for example, if its parent layout
13472 * constrains it with less available width).
13473 *
13474 * @param minWidth The minimum width the view will try to be.
13475 */
13476 public void setMinimumWidth(int minWidth) {
13477 mMinWidth = minWidth;
13478 }
13479
13480 /**
13481 * Get the animation currently associated with this view.
13482 *
13483 * @return The animation that is currently playing or
13484 * scheduled to play for this view.
13485 */
13486 public Animation getAnimation() {
13487 return mCurrentAnimation;
13488 }
13489
13490 /**
13491 * Start the specified animation now.
13492 *
13493 * @param animation the animation to start now
13494 */
13495 public void startAnimation(Animation animation) {
13496 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
13497 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013498 invalidateParentCaches();
13499 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013500 }
13501
13502 /**
13503 * Cancels any animations for this view.
13504 */
13505 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080013506 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080013507 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080013508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013509 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013510 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013511 }
13512
13513 /**
13514 * Sets the next animation to play for this view.
13515 * If you want the animation to play immediately, use
13516 * startAnimation. This method provides allows fine-grained
13517 * control over the start time and invalidation, but you
13518 * must make sure that 1) the animation has a start time set, and
13519 * 2) the view will be invalidated when the animation is supposed to
13520 * start.
13521 *
13522 * @param animation The next animation, or null.
13523 */
13524 public void setAnimation(Animation animation) {
13525 mCurrentAnimation = animation;
13526 if (animation != null) {
13527 animation.reset();
13528 }
13529 }
13530
13531 /**
13532 * Invoked by a parent ViewGroup to notify the start of the animation
13533 * currently associated with this view. If you override this method,
13534 * always call super.onAnimationStart();
13535 *
13536 * @see #setAnimation(android.view.animation.Animation)
13537 * @see #getAnimation()
13538 */
13539 protected void onAnimationStart() {
13540 mPrivateFlags |= ANIMATION_STARTED;
13541 }
13542
13543 /**
13544 * Invoked by a parent ViewGroup to notify the end of the animation
13545 * currently associated with this view. If you override this method,
13546 * always call super.onAnimationEnd();
13547 *
13548 * @see #setAnimation(android.view.animation.Animation)
13549 * @see #getAnimation()
13550 */
13551 protected void onAnimationEnd() {
13552 mPrivateFlags &= ~ANIMATION_STARTED;
13553 }
13554
13555 /**
13556 * Invoked if there is a Transform that involves alpha. Subclass that can
13557 * draw themselves with the specified alpha should return true, and then
13558 * respect that alpha when their onDraw() is called. If this returns false
13559 * then the view may be redirected to draw into an offscreen buffer to
13560 * fulfill the request, which will look fine, but may be slower than if the
13561 * subclass handles it internally. The default implementation returns false.
13562 *
13563 * @param alpha The alpha (0..255) to apply to the view's drawing
13564 * @return true if the view can draw with the specified alpha.
13565 */
13566 protected boolean onSetAlpha(int alpha) {
13567 return false;
13568 }
13569
13570 /**
13571 * This is used by the RootView to perform an optimization when
13572 * the view hierarchy contains one or several SurfaceView.
13573 * SurfaceView is always considered transparent, but its children are not,
13574 * therefore all View objects remove themselves from the global transparent
13575 * region (passed as a parameter to this function).
13576 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013577 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013578 *
13579 * @return Returns true if the effective visibility of the view at this
13580 * point is opaque, regardless of the transparent region; returns false
13581 * if it is possible for underlying windows to be seen behind the view.
13582 *
13583 * {@hide}
13584 */
13585 public boolean gatherTransparentRegion(Region region) {
13586 final AttachInfo attachInfo = mAttachInfo;
13587 if (region != null && attachInfo != null) {
13588 final int pflags = mPrivateFlags;
13589 if ((pflags & SKIP_DRAW) == 0) {
13590 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
13591 // remove it from the transparent region.
13592 final int[] location = attachInfo.mTransparentLocation;
13593 getLocationInWindow(location);
13594 region.op(location[0], location[1], location[0] + mRight - mLeft,
13595 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
13596 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
13597 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
13598 // exists, so we remove the background drawable's non-transparent
13599 // parts from this transparent region.
13600 applyDrawableToTransparentRegion(mBGDrawable, region);
13601 }
13602 }
13603 return true;
13604 }
13605
13606 /**
13607 * Play a sound effect for this view.
13608 *
13609 * <p>The framework will play sound effects for some built in actions, such as
13610 * clicking, but you may wish to play these effects in your widget,
13611 * for instance, for internal navigation.
13612 *
13613 * <p>The sound effect will only be played if sound effects are enabled by the user, and
13614 * {@link #isSoundEffectsEnabled()} is true.
13615 *
13616 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
13617 */
13618 public void playSoundEffect(int soundConstant) {
13619 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
13620 return;
13621 }
13622 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
13623 }
13624
13625 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013626 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013627 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013628 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013629 *
13630 * <p>The framework will provide haptic feedback for some built in actions,
13631 * such as long presses, but you may wish to provide feedback for your
13632 * own widget.
13633 *
13634 * <p>The feedback will only be performed if
13635 * {@link #isHapticFeedbackEnabled()} is true.
13636 *
13637 * @param feedbackConstant One of the constants defined in
13638 * {@link HapticFeedbackConstants}
13639 */
13640 public boolean performHapticFeedback(int feedbackConstant) {
13641 return performHapticFeedback(feedbackConstant, 0);
13642 }
13643
13644 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013645 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013646 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013647 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013648 *
13649 * @param feedbackConstant One of the constants defined in
13650 * {@link HapticFeedbackConstants}
13651 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
13652 */
13653 public boolean performHapticFeedback(int feedbackConstant, int flags) {
13654 if (mAttachInfo == null) {
13655 return false;
13656 }
Romain Guyf607bdc2010-09-10 19:20:06 -070013657 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070013658 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013659 && !isHapticFeedbackEnabled()) {
13660 return false;
13661 }
Romain Guy812ccbe2010-06-01 14:07:24 -070013662 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
13663 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013664 }
13665
13666 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013667 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013668 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13669 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013670 */
13671 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040013672 if (visibility != mSystemUiVisibility) {
13673 mSystemUiVisibility = visibility;
13674 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13675 mParent.recomputeViewAttributes(this);
13676 }
Joe Onorato664644d2011-01-23 17:53:23 -080013677 }
13678 }
13679
13680 /**
13681 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013682 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13683 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013684 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080013685 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080013686 return mSystemUiVisibility;
13687 }
13688
Scott Mainec6331b2011-05-24 16:55:56 -070013689 /**
13690 * Set a listener to receive callbacks when the visibility of the system bar changes.
13691 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
13692 */
Joe Onorato664644d2011-01-23 17:53:23 -080013693 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013694 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080013695 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13696 mParent.recomputeViewAttributes(this);
13697 }
13698 }
13699
13700 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013701 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
13702 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080013703 */
13704 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013705 ListenerInfo li = mListenerInfo;
13706 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
13707 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080013708 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080013709 }
13710 }
13711
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013712 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
13713 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
13714 if (val != mSystemUiVisibility) {
13715 setSystemUiVisibility(val);
13716 }
13717 }
13718
Joe Onorato664644d2011-01-23 17:53:23 -080013719 /**
Joe Malin32736f02011-01-19 16:14:20 -080013720 * Creates an image that the system displays during the drag and drop
13721 * operation. This is called a &quot;drag shadow&quot;. The default implementation
13722 * for a DragShadowBuilder based on a View returns an image that has exactly the same
13723 * appearance as the given View. The default also positions the center of the drag shadow
13724 * directly under the touch point. If no View is provided (the constructor with no parameters
13725 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
13726 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
13727 * default is an invisible drag shadow.
13728 * <p>
13729 * You are not required to use the View you provide to the constructor as the basis of the
13730 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
13731 * anything you want as the drag shadow.
13732 * </p>
13733 * <p>
13734 * You pass a DragShadowBuilder object to the system when you start the drag. The system
13735 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
13736 * size and position of the drag shadow. It uses this data to construct a
13737 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
13738 * so that your application can draw the shadow image in the Canvas.
13739 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070013740 *
13741 * <div class="special reference">
13742 * <h3>Developer Guides</h3>
13743 * <p>For a guide to implementing drag and drop features, read the
13744 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
13745 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070013746 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013747 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070013748 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070013749
13750 /**
Joe Malin32736f02011-01-19 16:14:20 -080013751 * Constructs a shadow image builder based on a View. By default, the resulting drag
13752 * shadow will have the same appearance and dimensions as the View, with the touch point
13753 * over the center of the View.
13754 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070013755 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013756 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070013757 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070013758 }
13759
Christopher Tate17ed60c2011-01-18 12:50:26 -080013760 /**
13761 * Construct a shadow builder object with no associated View. This
13762 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
13763 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
13764 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080013765 * reference to any View object. If they are not overridden, then the result is an
13766 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080013767 */
13768 public DragShadowBuilder() {
13769 mView = new WeakReference<View>(null);
13770 }
13771
13772 /**
13773 * Returns the View object that had been passed to the
13774 * {@link #View.DragShadowBuilder(View)}
13775 * constructor. If that View parameter was {@code null} or if the
13776 * {@link #View.DragShadowBuilder()}
13777 * constructor was used to instantiate the builder object, this method will return
13778 * null.
13779 *
13780 * @return The View object associate with this builder object.
13781 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070013782 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070013783 final public View getView() {
13784 return mView.get();
13785 }
13786
Christopher Tate2c095f32010-10-04 14:13:40 -070013787 /**
Joe Malin32736f02011-01-19 16:14:20 -080013788 * Provides the metrics for the shadow image. These include the dimensions of
13789 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070013790 * be centered under the touch location while dragging.
13791 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013792 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080013793 * same as the dimensions of the View itself and centers the shadow under
13794 * the touch point.
13795 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013796 *
Joe Malin32736f02011-01-19 16:14:20 -080013797 * @param shadowSize A {@link android.graphics.Point} containing the width and height
13798 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
13799 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
13800 * image.
13801 *
13802 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
13803 * shadow image that should be underneath the touch point during the drag and drop
13804 * operation. Your application must set {@link android.graphics.Point#x} to the
13805 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070013806 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013807 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070013808 final View view = mView.get();
13809 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013810 shadowSize.set(view.getWidth(), view.getHeight());
13811 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070013812 } else {
13813 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
13814 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013815 }
13816
13817 /**
Joe Malin32736f02011-01-19 16:14:20 -080013818 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
13819 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013820 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070013821 *
Joe Malin32736f02011-01-19 16:14:20 -080013822 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070013823 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013824 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070013825 final View view = mView.get();
13826 if (view != null) {
13827 view.draw(canvas);
13828 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013829 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070013830 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013831 }
13832 }
13833
13834 /**
Joe Malin32736f02011-01-19 16:14:20 -080013835 * Starts a drag and drop operation. When your application calls this method, it passes a
13836 * {@link android.view.View.DragShadowBuilder} object to the system. The
13837 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
13838 * to get metrics for the drag shadow, and then calls the object's
13839 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
13840 * <p>
13841 * Once the system has the drag shadow, it begins the drag and drop operation by sending
13842 * drag events to all the View objects in your application that are currently visible. It does
13843 * this either by calling the View object's drag listener (an implementation of
13844 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
13845 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
13846 * Both are passed a {@link android.view.DragEvent} object that has a
13847 * {@link android.view.DragEvent#getAction()} value of
13848 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
13849 * </p>
13850 * <p>
13851 * Your application can invoke startDrag() on any attached View object. The View object does not
13852 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
13853 * be related to the View the user selected for dragging.
13854 * </p>
13855 * @param data A {@link android.content.ClipData} object pointing to the data to be
13856 * transferred by the drag and drop operation.
13857 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
13858 * drag shadow.
13859 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
13860 * drop operation. This Object is put into every DragEvent object sent by the system during the
13861 * current drag.
13862 * <p>
13863 * myLocalState is a lightweight mechanism for the sending information from the dragged View
13864 * to the target Views. For example, it can contain flags that differentiate between a
13865 * a copy operation and a move operation.
13866 * </p>
13867 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
13868 * so the parameter should be set to 0.
13869 * @return {@code true} if the method completes successfully, or
13870 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
13871 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070013872 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013873 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013874 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070013875 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013876 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070013877 }
13878 boolean okay = false;
13879
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013880 Point shadowSize = new Point();
13881 Point shadowTouchPoint = new Point();
13882 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070013883
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013884 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
13885 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
13886 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070013887 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013888
Chris Tatea32dcf72010-10-14 12:13:50 -070013889 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013890 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
13891 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070013892 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013893 Surface surface = new Surface();
13894 try {
13895 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013896 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070013897 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070013898 + " surface=" + surface);
13899 if (token != null) {
13900 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070013901 try {
Chris Tate6b391282010-10-14 15:48:59 -070013902 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013903 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070013904 } finally {
13905 surface.unlockCanvasAndPost(canvas);
13906 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013907
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013908 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080013909
13910 // Cache the local state object for delivery with DragEvents
13911 root.setLocalDragState(myLocalState);
13912
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013913 // repurpose 'shadowSize' for the last touch point
13914 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070013915
Christopher Tatea53146c2010-09-07 11:57:52 -070013916 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013917 shadowSize.x, shadowSize.y,
13918 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070013919 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070013920
13921 // Off and running! Release our local surface instance; the drag
13922 // shadow surface is now managed by the system process.
13923 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070013924 }
13925 } catch (Exception e) {
13926 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
13927 surface.destroy();
13928 }
13929
13930 return okay;
13931 }
13932
Christopher Tatea53146c2010-09-07 11:57:52 -070013933 /**
Joe Malin32736f02011-01-19 16:14:20 -080013934 * Handles drag events sent by the system following a call to
13935 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
13936 *<p>
13937 * When the system calls this method, it passes a
13938 * {@link android.view.DragEvent} object. A call to
13939 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
13940 * in DragEvent. The method uses these to determine what is happening in the drag and drop
13941 * operation.
13942 * @param event The {@link android.view.DragEvent} sent by the system.
13943 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
13944 * in DragEvent, indicating the type of drag event represented by this object.
13945 * @return {@code true} if the method was successful, otherwise {@code false}.
13946 * <p>
13947 * The method should return {@code true} in response to an action type of
13948 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
13949 * operation.
13950 * </p>
13951 * <p>
13952 * The method should also return {@code true} in response to an action type of
13953 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
13954 * {@code false} if it didn't.
13955 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013956 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070013957 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070013958 return false;
13959 }
13960
13961 /**
Joe Malin32736f02011-01-19 16:14:20 -080013962 * Detects if this View is enabled and has a drag event listener.
13963 * If both are true, then it calls the drag event listener with the
13964 * {@link android.view.DragEvent} it received. If the drag event listener returns
13965 * {@code true}, then dispatchDragEvent() returns {@code true}.
13966 * <p>
13967 * For all other cases, the method calls the
13968 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
13969 * method and returns its result.
13970 * </p>
13971 * <p>
13972 * This ensures that a drag event is always consumed, even if the View does not have a drag
13973 * event listener. However, if the View has a listener and the listener returns true, then
13974 * onDragEvent() is not called.
13975 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013976 */
13977 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080013978 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013979 ListenerInfo li = mListenerInfo;
13980 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
13981 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070013982 return true;
13983 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013984 return onDragEvent(event);
13985 }
13986
Christopher Tate3d4bf172011-03-28 16:16:46 -070013987 boolean canAcceptDrag() {
13988 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
13989 }
13990
Christopher Tatea53146c2010-09-07 11:57:52 -070013991 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013992 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13993 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013994 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013995 */
13996 public void onCloseSystemDialogs(String reason) {
13997 }
Joe Malin32736f02011-01-19 16:14:20 -080013998
Dianne Hackbornffa42482009-09-23 22:20:11 -070013999 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014000 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070014001 * update a Region being computed for
14002 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014003 * that any non-transparent parts of the Drawable are removed from the
14004 * given transparent region.
14005 *
14006 * @param dr The Drawable whose transparency is to be applied to the region.
14007 * @param region A Region holding the current transparency information,
14008 * where any parts of the region that are set are considered to be
14009 * transparent. On return, this region will be modified to have the
14010 * transparency information reduced by the corresponding parts of the
14011 * Drawable that are not transparent.
14012 * {@hide}
14013 */
14014 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
14015 if (DBG) {
14016 Log.i("View", "Getting transparent region for: " + this);
14017 }
14018 final Region r = dr.getTransparentRegion();
14019 final Rect db = dr.getBounds();
14020 final AttachInfo attachInfo = mAttachInfo;
14021 if (r != null && attachInfo != null) {
14022 final int w = getRight()-getLeft();
14023 final int h = getBottom()-getTop();
14024 if (db.left > 0) {
14025 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
14026 r.op(0, 0, db.left, h, Region.Op.UNION);
14027 }
14028 if (db.right < w) {
14029 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
14030 r.op(db.right, 0, w, h, Region.Op.UNION);
14031 }
14032 if (db.top > 0) {
14033 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
14034 r.op(0, 0, w, db.top, Region.Op.UNION);
14035 }
14036 if (db.bottom < h) {
14037 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
14038 r.op(0, db.bottom, w, h, Region.Op.UNION);
14039 }
14040 final int[] location = attachInfo.mTransparentLocation;
14041 getLocationInWindow(location);
14042 r.translate(location[0], location[1]);
14043 region.op(r, Region.Op.INTERSECT);
14044 } else {
14045 region.op(db, Region.Op.DIFFERENCE);
14046 }
14047 }
14048
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014049 private void checkForLongClick(int delayOffset) {
14050 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
14051 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014052
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014053 if (mPendingCheckForLongPress == null) {
14054 mPendingCheckForLongPress = new CheckForLongPress();
14055 }
14056 mPendingCheckForLongPress.rememberWindowAttachCount();
14057 postDelayed(mPendingCheckForLongPress,
14058 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014060 }
14061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014062 /**
14063 * Inflate a view from an XML resource. This convenience method wraps the {@link
14064 * LayoutInflater} class, which provides a full range of options for view inflation.
14065 *
14066 * @param context The Context object for your activity or application.
14067 * @param resource The resource ID to inflate
14068 * @param root A view group that will be the parent. Used to properly inflate the
14069 * layout_* parameters.
14070 * @see LayoutInflater
14071 */
14072 public static View inflate(Context context, int resource, ViewGroup root) {
14073 LayoutInflater factory = LayoutInflater.from(context);
14074 return factory.inflate(resource, root);
14075 }
Romain Guy33e72ae2010-07-17 12:40:29 -070014076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014077 /**
Adam Powell637d3372010-08-25 14:37:03 -070014078 * Scroll the view with standard behavior for scrolling beyond the normal
14079 * content boundaries. Views that call this method should override
14080 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
14081 * results of an over-scroll operation.
14082 *
14083 * Views can use this method to handle any touch or fling-based scrolling.
14084 *
14085 * @param deltaX Change in X in pixels
14086 * @param deltaY Change in Y in pixels
14087 * @param scrollX Current X scroll value in pixels before applying deltaX
14088 * @param scrollY Current Y scroll value in pixels before applying deltaY
14089 * @param scrollRangeX Maximum content scroll range along the X axis
14090 * @param scrollRangeY Maximum content scroll range along the Y axis
14091 * @param maxOverScrollX Number of pixels to overscroll by in either direction
14092 * along the X axis.
14093 * @param maxOverScrollY Number of pixels to overscroll by in either direction
14094 * along the Y axis.
14095 * @param isTouchEvent true if this scroll operation is the result of a touch event.
14096 * @return true if scrolling was clamped to an over-scroll boundary along either
14097 * axis, false otherwise.
14098 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070014099 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070014100 protected boolean overScrollBy(int deltaX, int deltaY,
14101 int scrollX, int scrollY,
14102 int scrollRangeX, int scrollRangeY,
14103 int maxOverScrollX, int maxOverScrollY,
14104 boolean isTouchEvent) {
14105 final int overScrollMode = mOverScrollMode;
14106 final boolean canScrollHorizontal =
14107 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
14108 final boolean canScrollVertical =
14109 computeVerticalScrollRange() > computeVerticalScrollExtent();
14110 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
14111 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
14112 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
14113 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
14114
14115 int newScrollX = scrollX + deltaX;
14116 if (!overScrollHorizontal) {
14117 maxOverScrollX = 0;
14118 }
14119
14120 int newScrollY = scrollY + deltaY;
14121 if (!overScrollVertical) {
14122 maxOverScrollY = 0;
14123 }
14124
14125 // Clamp values if at the limits and record
14126 final int left = -maxOverScrollX;
14127 final int right = maxOverScrollX + scrollRangeX;
14128 final int top = -maxOverScrollY;
14129 final int bottom = maxOverScrollY + scrollRangeY;
14130
14131 boolean clampedX = false;
14132 if (newScrollX > right) {
14133 newScrollX = right;
14134 clampedX = true;
14135 } else if (newScrollX < left) {
14136 newScrollX = left;
14137 clampedX = true;
14138 }
14139
14140 boolean clampedY = false;
14141 if (newScrollY > bottom) {
14142 newScrollY = bottom;
14143 clampedY = true;
14144 } else if (newScrollY < top) {
14145 newScrollY = top;
14146 clampedY = true;
14147 }
14148
14149 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
14150
14151 return clampedX || clampedY;
14152 }
14153
14154 /**
14155 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
14156 * respond to the results of an over-scroll operation.
14157 *
14158 * @param scrollX New X scroll value in pixels
14159 * @param scrollY New Y scroll value in pixels
14160 * @param clampedX True if scrollX was clamped to an over-scroll boundary
14161 * @param clampedY True if scrollY was clamped to an over-scroll boundary
14162 */
14163 protected void onOverScrolled(int scrollX, int scrollY,
14164 boolean clampedX, boolean clampedY) {
14165 // Intentionally empty.
14166 }
14167
14168 /**
14169 * Returns the over-scroll mode for this view. The result will be
14170 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14171 * (allow over-scrolling only if the view content is larger than the container),
14172 * or {@link #OVER_SCROLL_NEVER}.
14173 *
14174 * @return This view's over-scroll mode.
14175 */
14176 public int getOverScrollMode() {
14177 return mOverScrollMode;
14178 }
14179
14180 /**
14181 * Set the over-scroll mode for this view. Valid over-scroll modes are
14182 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14183 * (allow over-scrolling only if the view content is larger than the container),
14184 * or {@link #OVER_SCROLL_NEVER}.
14185 *
14186 * Setting the over-scroll mode of a view will have an effect only if the
14187 * view is capable of scrolling.
14188 *
14189 * @param overScrollMode The new over-scroll mode for this view.
14190 */
14191 public void setOverScrollMode(int overScrollMode) {
14192 if (overScrollMode != OVER_SCROLL_ALWAYS &&
14193 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
14194 overScrollMode != OVER_SCROLL_NEVER) {
14195 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
14196 }
14197 mOverScrollMode = overScrollMode;
14198 }
14199
14200 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014201 * Gets a scale factor that determines the distance the view should scroll
14202 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
14203 * @return The vertical scroll scale factor.
14204 * @hide
14205 */
14206 protected float getVerticalScrollFactor() {
14207 if (mVerticalScrollFactor == 0) {
14208 TypedValue outValue = new TypedValue();
14209 if (!mContext.getTheme().resolveAttribute(
14210 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
14211 throw new IllegalStateException(
14212 "Expected theme to define listPreferredItemHeight.");
14213 }
14214 mVerticalScrollFactor = outValue.getDimension(
14215 mContext.getResources().getDisplayMetrics());
14216 }
14217 return mVerticalScrollFactor;
14218 }
14219
14220 /**
14221 * Gets a scale factor that determines the distance the view should scroll
14222 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
14223 * @return The horizontal scroll scale factor.
14224 * @hide
14225 */
14226 protected float getHorizontalScrollFactor() {
14227 // TODO: Should use something else.
14228 return getVerticalScrollFactor();
14229 }
14230
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014231 /**
14232 * Return the value specifying the text direction or policy that was set with
14233 * {@link #setTextDirection(int)}.
14234 *
14235 * @return the defined text direction. It can be one of:
14236 *
14237 * {@link #TEXT_DIRECTION_INHERIT},
14238 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14239 * {@link #TEXT_DIRECTION_ANY_RTL},
14240 * {@link #TEXT_DIRECTION_LTR},
14241 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014242 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014243 */
14244 public int getTextDirection() {
14245 return mTextDirection;
14246 }
14247
14248 /**
14249 * Set the text direction.
14250 *
14251 * @param textDirection the direction to set. Should be one of:
14252 *
14253 * {@link #TEXT_DIRECTION_INHERIT},
14254 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14255 * {@link #TEXT_DIRECTION_ANY_RTL},
14256 * {@link #TEXT_DIRECTION_LTR},
14257 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014258 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014259 */
14260 public void setTextDirection(int textDirection) {
14261 if (textDirection != mTextDirection) {
14262 mTextDirection = textDirection;
14263 resetResolvedTextDirection();
14264 requestLayout();
14265 }
14266 }
14267
14268 /**
14269 * Return the resolved text direction.
14270 *
14271 * @return the resolved text direction. Return one of:
14272 *
Doug Feltcb3791202011-07-07 11:57:48 -070014273 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14274 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014275 * {@link #TEXT_DIRECTION_LTR},
14276 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014277 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014278 */
14279 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070014280 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014281 resolveTextDirection();
14282 }
14283 return mResolvedTextDirection;
14284 }
14285
14286 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014287 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
14288 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014289 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014290 public void resolveTextDirection() {
14291 if (mResolvedTextDirection != TEXT_DIRECTION_INHERIT) {
14292 // Resolution has already been done.
14293 return;
14294 }
Doug Feltcb3791202011-07-07 11:57:48 -070014295 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
14296 mResolvedTextDirection = mTextDirection;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014297 } else if (mParent != null && mParent instanceof ViewGroup) {
Doug Feltcb3791202011-07-07 11:57:48 -070014298 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014299 } else {
14300 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Doug Feltcb3791202011-07-07 11:57:48 -070014301 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014302 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014303 }
14304
14305 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014306 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014307 * resolution should override this method.
14308 *
14309 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014310 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014311 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014312 }
14313
14314 /**
14315 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014316 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014317 * reset is done.
14318 */
14319 public void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070014320 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014321 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014322 }
14323
14324 /**
14325 * Called when text direction is reset. Subclasses that care about text direction reset should
14326 * override this method and do a reset of the text direction of their children. The default
14327 * implementation does nothing.
14328 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014329 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014330 }
14331
Chet Haaseb39f0512011-05-24 14:36:40 -070014332 //
14333 // Properties
14334 //
14335 /**
14336 * A Property wrapper around the <code>alpha</code> functionality handled by the
14337 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
14338 */
Chet Haased47f1532011-12-16 11:18:52 -080014339 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014340 @Override
14341 public void setValue(View object, float value) {
14342 object.setAlpha(value);
14343 }
14344
14345 @Override
14346 public Float get(View object) {
14347 return object.getAlpha();
14348 }
14349 };
14350
14351 /**
14352 * A Property wrapper around the <code>translationX</code> functionality handled by the
14353 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
14354 */
Chet Haased47f1532011-12-16 11:18:52 -080014355 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014356 @Override
14357 public void setValue(View object, float value) {
14358 object.setTranslationX(value);
14359 }
14360
14361 @Override
14362 public Float get(View object) {
14363 return object.getTranslationX();
14364 }
14365 };
14366
14367 /**
14368 * A Property wrapper around the <code>translationY</code> functionality handled by the
14369 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
14370 */
Chet Haased47f1532011-12-16 11:18:52 -080014371 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014372 @Override
14373 public void setValue(View object, float value) {
14374 object.setTranslationY(value);
14375 }
14376
14377 @Override
14378 public Float get(View object) {
14379 return object.getTranslationY();
14380 }
14381 };
14382
14383 /**
14384 * A Property wrapper around the <code>x</code> functionality handled by the
14385 * {@link View#setX(float)} and {@link View#getX()} methods.
14386 */
Chet Haased47f1532011-12-16 11:18:52 -080014387 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014388 @Override
14389 public void setValue(View object, float value) {
14390 object.setX(value);
14391 }
14392
14393 @Override
14394 public Float get(View object) {
14395 return object.getX();
14396 }
14397 };
14398
14399 /**
14400 * A Property wrapper around the <code>y</code> functionality handled by the
14401 * {@link View#setY(float)} and {@link View#getY()} methods.
14402 */
Chet Haased47f1532011-12-16 11:18:52 -080014403 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014404 @Override
14405 public void setValue(View object, float value) {
14406 object.setY(value);
14407 }
14408
14409 @Override
14410 public Float get(View object) {
14411 return object.getY();
14412 }
14413 };
14414
14415 /**
14416 * A Property wrapper around the <code>rotation</code> functionality handled by the
14417 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
14418 */
Chet Haased47f1532011-12-16 11:18:52 -080014419 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014420 @Override
14421 public void setValue(View object, float value) {
14422 object.setRotation(value);
14423 }
14424
14425 @Override
14426 public Float get(View object) {
14427 return object.getRotation();
14428 }
14429 };
14430
14431 /**
14432 * A Property wrapper around the <code>rotationX</code> functionality handled by the
14433 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
14434 */
Chet Haased47f1532011-12-16 11:18:52 -080014435 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014436 @Override
14437 public void setValue(View object, float value) {
14438 object.setRotationX(value);
14439 }
14440
14441 @Override
14442 public Float get(View object) {
14443 return object.getRotationX();
14444 }
14445 };
14446
14447 /**
14448 * A Property wrapper around the <code>rotationY</code> functionality handled by the
14449 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
14450 */
Chet Haased47f1532011-12-16 11:18:52 -080014451 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014452 @Override
14453 public void setValue(View object, float value) {
14454 object.setRotationY(value);
14455 }
14456
14457 @Override
14458 public Float get(View object) {
14459 return object.getRotationY();
14460 }
14461 };
14462
14463 /**
14464 * A Property wrapper around the <code>scaleX</code> functionality handled by the
14465 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
14466 */
Chet Haased47f1532011-12-16 11:18:52 -080014467 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014468 @Override
14469 public void setValue(View object, float value) {
14470 object.setScaleX(value);
14471 }
14472
14473 @Override
14474 public Float get(View object) {
14475 return object.getScaleX();
14476 }
14477 };
14478
14479 /**
14480 * A Property wrapper around the <code>scaleY</code> functionality handled by the
14481 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
14482 */
Chet Haased47f1532011-12-16 11:18:52 -080014483 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014484 @Override
14485 public void setValue(View object, float value) {
14486 object.setScaleY(value);
14487 }
14488
14489 @Override
14490 public Float get(View object) {
14491 return object.getScaleY();
14492 }
14493 };
14494
Jeff Brown33bbfd22011-02-24 20:55:35 -080014495 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014496 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
14497 * Each MeasureSpec represents a requirement for either the width or the height.
14498 * A MeasureSpec is comprised of a size and a mode. There are three possible
14499 * modes:
14500 * <dl>
14501 * <dt>UNSPECIFIED</dt>
14502 * <dd>
14503 * The parent has not imposed any constraint on the child. It can be whatever size
14504 * it wants.
14505 * </dd>
14506 *
14507 * <dt>EXACTLY</dt>
14508 * <dd>
14509 * The parent has determined an exact size for the child. The child is going to be
14510 * given those bounds regardless of how big it wants to be.
14511 * </dd>
14512 *
14513 * <dt>AT_MOST</dt>
14514 * <dd>
14515 * The child can be as large as it wants up to the specified size.
14516 * </dd>
14517 * </dl>
14518 *
14519 * MeasureSpecs are implemented as ints to reduce object allocation. This class
14520 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
14521 */
14522 public static class MeasureSpec {
14523 private static final int MODE_SHIFT = 30;
14524 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
14525
14526 /**
14527 * Measure specification mode: The parent has not imposed any constraint
14528 * on the child. It can be whatever size it wants.
14529 */
14530 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
14531
14532 /**
14533 * Measure specification mode: The parent has determined an exact size
14534 * for the child. The child is going to be given those bounds regardless
14535 * of how big it wants to be.
14536 */
14537 public static final int EXACTLY = 1 << MODE_SHIFT;
14538
14539 /**
14540 * Measure specification mode: The child can be as large as it wants up
14541 * to the specified size.
14542 */
14543 public static final int AT_MOST = 2 << MODE_SHIFT;
14544
14545 /**
14546 * Creates a measure specification based on the supplied size and mode.
14547 *
14548 * The mode must always be one of the following:
14549 * <ul>
14550 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
14551 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
14552 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
14553 * </ul>
14554 *
14555 * @param size the size of the measure specification
14556 * @param mode the mode of the measure specification
14557 * @return the measure specification based on size and mode
14558 */
14559 public static int makeMeasureSpec(int size, int mode) {
14560 return size + mode;
14561 }
14562
14563 /**
14564 * Extracts the mode from the supplied measure specification.
14565 *
14566 * @param measureSpec the measure specification to extract the mode from
14567 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
14568 * {@link android.view.View.MeasureSpec#AT_MOST} or
14569 * {@link android.view.View.MeasureSpec#EXACTLY}
14570 */
14571 public static int getMode(int measureSpec) {
14572 return (measureSpec & MODE_MASK);
14573 }
14574
14575 /**
14576 * Extracts the size from the supplied measure specification.
14577 *
14578 * @param measureSpec the measure specification to extract the size from
14579 * @return the size in pixels defined in the supplied measure specification
14580 */
14581 public static int getSize(int measureSpec) {
14582 return (measureSpec & ~MODE_MASK);
14583 }
14584
14585 /**
14586 * Returns a String representation of the specified measure
14587 * specification.
14588 *
14589 * @param measureSpec the measure specification to convert to a String
14590 * @return a String with the following format: "MeasureSpec: MODE SIZE"
14591 */
14592 public static String toString(int measureSpec) {
14593 int mode = getMode(measureSpec);
14594 int size = getSize(measureSpec);
14595
14596 StringBuilder sb = new StringBuilder("MeasureSpec: ");
14597
14598 if (mode == UNSPECIFIED)
14599 sb.append("UNSPECIFIED ");
14600 else if (mode == EXACTLY)
14601 sb.append("EXACTLY ");
14602 else if (mode == AT_MOST)
14603 sb.append("AT_MOST ");
14604 else
14605 sb.append(mode).append(" ");
14606
14607 sb.append(size);
14608 return sb.toString();
14609 }
14610 }
14611
14612 class CheckForLongPress implements Runnable {
14613
14614 private int mOriginalWindowAttachCount;
14615
14616 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070014617 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014618 && mOriginalWindowAttachCount == mWindowAttachCount) {
14619 if (performLongClick()) {
14620 mHasPerformedLongPress = true;
14621 }
14622 }
14623 }
14624
14625 public void rememberWindowAttachCount() {
14626 mOriginalWindowAttachCount = mWindowAttachCount;
14627 }
14628 }
Joe Malin32736f02011-01-19 16:14:20 -080014629
Adam Powelle14579b2009-12-16 18:39:52 -080014630 private final class CheckForTap implements Runnable {
14631 public void run() {
14632 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080014633 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014634 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080014635 }
14636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014637
Adam Powella35d7682010-03-12 14:48:13 -080014638 private final class PerformClick implements Runnable {
14639 public void run() {
14640 performClick();
14641 }
14642 }
14643
Dianne Hackborn63042d62011-01-26 18:56:29 -080014644 /** @hide */
14645 public void hackTurnOffWindowResizeAnim(boolean off) {
14646 mAttachInfo.mTurnOffWindowResizeAnim = off;
14647 }
Joe Malin32736f02011-01-19 16:14:20 -080014648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014649 /**
Chet Haasea00f3862011-02-22 06:34:40 -080014650 * This method returns a ViewPropertyAnimator object, which can be used to animate
14651 * specific properties on this View.
14652 *
14653 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
14654 */
14655 public ViewPropertyAnimator animate() {
14656 if (mAnimator == null) {
14657 mAnimator = new ViewPropertyAnimator(this);
14658 }
14659 return mAnimator;
14660 }
14661
14662 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014663 * Interface definition for a callback to be invoked when a key event is
14664 * dispatched to this view. The callback will be invoked before the key
14665 * event is given to the view.
14666 */
14667 public interface OnKeyListener {
14668 /**
14669 * Called when a key is dispatched to a view. This allows listeners to
14670 * get a chance to respond before the target view.
14671 *
14672 * @param v The view the key has been dispatched to.
14673 * @param keyCode The code for the physical key that was pressed
14674 * @param event The KeyEvent object containing full information about
14675 * the event.
14676 * @return True if the listener has consumed the event, false otherwise.
14677 */
14678 boolean onKey(View v, int keyCode, KeyEvent event);
14679 }
14680
14681 /**
14682 * Interface definition for a callback to be invoked when a touch event is
14683 * dispatched to this view. The callback will be invoked before the touch
14684 * event is given to the view.
14685 */
14686 public interface OnTouchListener {
14687 /**
14688 * Called when a touch event is dispatched to a view. This allows listeners to
14689 * get a chance to respond before the target view.
14690 *
14691 * @param v The view the touch event has been dispatched to.
14692 * @param event The MotionEvent object containing full information about
14693 * the event.
14694 * @return True if the listener has consumed the event, false otherwise.
14695 */
14696 boolean onTouch(View v, MotionEvent event);
14697 }
14698
14699 /**
Jeff Brown10b62902011-06-20 16:40:37 -070014700 * Interface definition for a callback to be invoked when a hover event is
14701 * dispatched to this view. The callback will be invoked before the hover
14702 * event is given to the view.
14703 */
14704 public interface OnHoverListener {
14705 /**
14706 * Called when a hover event is dispatched to a view. This allows listeners to
14707 * get a chance to respond before the target view.
14708 *
14709 * @param v The view the hover event has been dispatched to.
14710 * @param event The MotionEvent object containing full information about
14711 * the event.
14712 * @return True if the listener has consumed the event, false otherwise.
14713 */
14714 boolean onHover(View v, MotionEvent event);
14715 }
14716
14717 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014718 * Interface definition for a callback to be invoked when a generic motion event is
14719 * dispatched to this view. The callback will be invoked before the generic motion
14720 * event is given to the view.
14721 */
14722 public interface OnGenericMotionListener {
14723 /**
14724 * Called when a generic motion event is dispatched to a view. This allows listeners to
14725 * get a chance to respond before the target view.
14726 *
14727 * @param v The view the generic motion event has been dispatched to.
14728 * @param event The MotionEvent object containing full information about
14729 * the event.
14730 * @return True if the listener has consumed the event, false otherwise.
14731 */
14732 boolean onGenericMotion(View v, MotionEvent event);
14733 }
14734
14735 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014736 * Interface definition for a callback to be invoked when a view has been clicked and held.
14737 */
14738 public interface OnLongClickListener {
14739 /**
14740 * Called when a view has been clicked and held.
14741 *
14742 * @param v The view that was clicked and held.
14743 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080014744 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014745 */
14746 boolean onLongClick(View v);
14747 }
14748
14749 /**
Chris Tate32affef2010-10-18 15:29:21 -070014750 * Interface definition for a callback to be invoked when a drag is being dispatched
14751 * to this view. The callback will be invoked before the hosting view's own
14752 * onDrag(event) method. If the listener wants to fall back to the hosting view's
14753 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070014754 *
14755 * <div class="special reference">
14756 * <h3>Developer Guides</h3>
14757 * <p>For a guide to implementing drag and drop features, read the
14758 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
14759 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070014760 */
14761 public interface OnDragListener {
14762 /**
14763 * Called when a drag event is dispatched to a view. This allows listeners
14764 * to get a chance to override base View behavior.
14765 *
Joe Malin32736f02011-01-19 16:14:20 -080014766 * @param v The View that received the drag event.
14767 * @param event The {@link android.view.DragEvent} object for the drag event.
14768 * @return {@code true} if the drag event was handled successfully, or {@code false}
14769 * if the drag event was not handled. Note that {@code false} will trigger the View
14770 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070014771 */
14772 boolean onDrag(View v, DragEvent event);
14773 }
14774
14775 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014776 * Interface definition for a callback to be invoked when the focus state of
14777 * a view changed.
14778 */
14779 public interface OnFocusChangeListener {
14780 /**
14781 * Called when the focus state of a view has changed.
14782 *
14783 * @param v The view whose state has changed.
14784 * @param hasFocus The new focus state of v.
14785 */
14786 void onFocusChange(View v, boolean hasFocus);
14787 }
14788
14789 /**
14790 * Interface definition for a callback to be invoked when a view is clicked.
14791 */
14792 public interface OnClickListener {
14793 /**
14794 * Called when a view has been clicked.
14795 *
14796 * @param v The view that was clicked.
14797 */
14798 void onClick(View v);
14799 }
14800
14801 /**
14802 * Interface definition for a callback to be invoked when the context menu
14803 * for this view is being built.
14804 */
14805 public interface OnCreateContextMenuListener {
14806 /**
14807 * Called when the context menu for this view is being built. It is not
14808 * safe to hold onto the menu after this method returns.
14809 *
14810 * @param menu The context menu that is being built
14811 * @param v The view for which the context menu is being built
14812 * @param menuInfo Extra information about the item for which the
14813 * context menu should be shown. This information will vary
14814 * depending on the class of v.
14815 */
14816 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
14817 }
14818
Joe Onorato664644d2011-01-23 17:53:23 -080014819 /**
14820 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014821 * visibility. This reports <strong>global</strong> changes to the system UI
14822 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080014823 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070014824 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080014825 */
14826 public interface OnSystemUiVisibilityChangeListener {
14827 /**
14828 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070014829 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080014830 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040014831 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014832 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
14833 * <strong>global</strong> state of the UI visibility flags, not what your
14834 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080014835 */
14836 public void onSystemUiVisibilityChange(int visibility);
14837 }
14838
Adam Powell4afd62b2011-02-18 15:02:18 -080014839 /**
14840 * Interface definition for a callback to be invoked when this view is attached
14841 * or detached from its window.
14842 */
14843 public interface OnAttachStateChangeListener {
14844 /**
14845 * Called when the view is attached to a window.
14846 * @param v The view that was attached
14847 */
14848 public void onViewAttachedToWindow(View v);
14849 /**
14850 * Called when the view is detached from a window.
14851 * @param v The view that was detached
14852 */
14853 public void onViewDetachedFromWindow(View v);
14854 }
14855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014856 private final class UnsetPressedState implements Runnable {
14857 public void run() {
14858 setPressed(false);
14859 }
14860 }
14861
14862 /**
14863 * Base class for derived classes that want to save and restore their own
14864 * state in {@link android.view.View#onSaveInstanceState()}.
14865 */
14866 public static class BaseSavedState extends AbsSavedState {
14867 /**
14868 * Constructor used when reading from a parcel. Reads the state of the superclass.
14869 *
14870 * @param source
14871 */
14872 public BaseSavedState(Parcel source) {
14873 super(source);
14874 }
14875
14876 /**
14877 * Constructor called by derived classes when creating their SavedState objects
14878 *
14879 * @param superState The state of the superclass of this view
14880 */
14881 public BaseSavedState(Parcelable superState) {
14882 super(superState);
14883 }
14884
14885 public static final Parcelable.Creator<BaseSavedState> CREATOR =
14886 new Parcelable.Creator<BaseSavedState>() {
14887 public BaseSavedState createFromParcel(Parcel in) {
14888 return new BaseSavedState(in);
14889 }
14890
14891 public BaseSavedState[] newArray(int size) {
14892 return new BaseSavedState[size];
14893 }
14894 };
14895 }
14896
14897 /**
14898 * A set of information given to a view when it is attached to its parent
14899 * window.
14900 */
14901 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014902 interface Callbacks {
14903 void playSoundEffect(int effectId);
14904 boolean performHapticFeedback(int effectId, boolean always);
14905 }
14906
14907 /**
14908 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
14909 * to a Handler. This class contains the target (View) to invalidate and
14910 * the coordinates of the dirty rectangle.
14911 *
14912 * For performance purposes, this class also implements a pool of up to
14913 * POOL_LIMIT objects that get reused. This reduces memory allocations
14914 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014915 */
Romain Guyd928d682009-03-31 17:52:16 -070014916 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014917 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070014918 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
14919 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070014920 public InvalidateInfo newInstance() {
14921 return new InvalidateInfo();
14922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014923
Romain Guyd928d682009-03-31 17:52:16 -070014924 public void onAcquired(InvalidateInfo element) {
14925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014926
Romain Guyd928d682009-03-31 17:52:16 -070014927 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070014928 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070014929 }
14930 }, POOL_LIMIT)
14931 );
14932
14933 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014934 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014935
14936 View target;
14937
14938 int left;
14939 int top;
14940 int right;
14941 int bottom;
14942
Romain Guyd928d682009-03-31 17:52:16 -070014943 public void setNextPoolable(InvalidateInfo element) {
14944 mNext = element;
14945 }
14946
14947 public InvalidateInfo getNextPoolable() {
14948 return mNext;
14949 }
14950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014951 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070014952 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014953 }
14954
14955 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070014956 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014957 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014958
14959 public boolean isPooled() {
14960 return mIsPooled;
14961 }
14962
14963 public void setPooled(boolean isPooled) {
14964 mIsPooled = isPooled;
14965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014966 }
14967
14968 final IWindowSession mSession;
14969
14970 final IWindow mWindow;
14971
14972 final IBinder mWindowToken;
14973
14974 final Callbacks mRootCallbacks;
14975
Romain Guy59a12ca2011-06-09 17:48:21 -070014976 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080014977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014978 /**
14979 * The top view of the hierarchy.
14980 */
14981 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070014982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014983 IBinder mPanelParentWindowToken;
14984 Surface mSurface;
14985
Romain Guyb051e892010-09-28 19:09:36 -070014986 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014987 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070014988 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080014989
Romain Guy7e4e5612012-03-05 14:37:29 -080014990 boolean mScreenOn;
14991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014992 /**
Romain Guy8506ab42009-06-11 17:35:47 -070014993 * Scale factor used by the compatibility mode
14994 */
14995 float mApplicationScale;
14996
14997 /**
14998 * Indicates whether the application is in compatibility mode
14999 */
15000 boolean mScalingRequired;
15001
15002 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015003 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080015004 */
15005 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080015006
Dianne Hackborn63042d62011-01-26 18:56:29 -080015007 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015008 * Left position of this view's window
15009 */
15010 int mWindowLeft;
15011
15012 /**
15013 * Top position of this view's window
15014 */
15015 int mWindowTop;
15016
15017 /**
Adam Powell26153a32010-11-08 15:22:27 -080015018 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070015019 */
Adam Powell26153a32010-11-08 15:22:27 -080015020 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070015021
15022 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015023 * For windows that are full-screen but using insets to layout inside
15024 * of the screen decorations, these are the current insets for the
15025 * content of the window.
15026 */
15027 final Rect mContentInsets = new Rect();
15028
15029 /**
15030 * For windows that are full-screen but using insets to layout inside
15031 * of the screen decorations, these are the current insets for the
15032 * actual visible parts of the window.
15033 */
15034 final Rect mVisibleInsets = new Rect();
15035
15036 /**
15037 * The internal insets given by this window. This value is
15038 * supplied by the client (through
15039 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
15040 * be given to the window manager when changed to be used in laying
15041 * out windows behind it.
15042 */
15043 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
15044 = new ViewTreeObserver.InternalInsetsInfo();
15045
15046 /**
15047 * All views in the window's hierarchy that serve as scroll containers,
15048 * used to determine if the window can be resized or must be panned
15049 * to adjust for a soft input area.
15050 */
15051 final ArrayList<View> mScrollContainers = new ArrayList<View>();
15052
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070015053 final KeyEvent.DispatcherState mKeyDispatchState
15054 = new KeyEvent.DispatcherState();
15055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015056 /**
15057 * Indicates whether the view's window currently has the focus.
15058 */
15059 boolean mHasWindowFocus;
15060
15061 /**
15062 * The current visibility of the window.
15063 */
15064 int mWindowVisibility;
15065
15066 /**
15067 * Indicates the time at which drawing started to occur.
15068 */
15069 long mDrawingTime;
15070
15071 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070015072 * Indicates whether or not ignoring the DIRTY_MASK flags.
15073 */
15074 boolean mIgnoreDirtyState;
15075
15076 /**
Romain Guy02ccac62011-06-24 13:20:23 -070015077 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
15078 * to avoid clearing that flag prematurely.
15079 */
15080 boolean mSetIgnoreDirtyState = false;
15081
15082 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015083 * Indicates whether the view's window is currently in touch mode.
15084 */
15085 boolean mInTouchMode;
15086
15087 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015088 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015089 * the next time it performs a traversal
15090 */
15091 boolean mRecomputeGlobalAttributes;
15092
15093 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015094 * Always report new attributes at next traversal.
15095 */
15096 boolean mForceReportNewAttributes;
15097
15098 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015099 * Set during a traveral if any views want to keep the screen on.
15100 */
15101 boolean mKeepScreenOn;
15102
15103 /**
Joe Onorato664644d2011-01-23 17:53:23 -080015104 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
15105 */
15106 int mSystemUiVisibility;
15107
15108 /**
15109 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
15110 * attached.
15111 */
15112 boolean mHasSystemUiListeners;
15113
15114 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015115 * Set if the visibility of any views has changed.
15116 */
15117 boolean mViewVisibilityChanged;
15118
15119 /**
15120 * Set to true if a view has been scrolled.
15121 */
15122 boolean mViewScrollChanged;
15123
15124 /**
15125 * Global to the view hierarchy used as a temporary for dealing with
15126 * x/y points in the transparent region computations.
15127 */
15128 final int[] mTransparentLocation = new int[2];
15129
15130 /**
15131 * Global to the view hierarchy used as a temporary for dealing with
15132 * x/y points in the ViewGroup.invalidateChild implementation.
15133 */
15134 final int[] mInvalidateChildLocation = new int[2];
15135
Chet Haasec3aa3612010-06-17 08:50:37 -070015136
15137 /**
15138 * Global to the view hierarchy used as a temporary for dealing with
15139 * x/y location when view is transformed.
15140 */
15141 final float[] mTmpTransformLocation = new float[2];
15142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015143 /**
15144 * The view tree observer used to dispatch global events like
15145 * layout, pre-draw, touch mode change, etc.
15146 */
15147 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
15148
15149 /**
15150 * A Canvas used by the view hierarchy to perform bitmap caching.
15151 */
15152 Canvas mCanvas;
15153
15154 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080015155 * The view root impl.
15156 */
15157 final ViewRootImpl mViewRootImpl;
15158
15159 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015160 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015161 * handler can be used to pump events in the UI events queue.
15162 */
15163 final Handler mHandler;
15164
15165 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015166 * Temporary for use in computing invalidate rectangles while
15167 * calling up the hierarchy.
15168 */
15169 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070015170
15171 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070015172 * Temporary for use in computing hit areas with transformed views
15173 */
15174 final RectF mTmpTransformRect = new RectF();
15175
15176 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070015177 * Temporary list for use in collecting focusable descendents of a view.
15178 */
15179 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
15180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015181 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070015182 * The id of the window for accessibility purposes.
15183 */
15184 int mAccessibilityWindowId = View.NO_ID;
15185
15186 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015187 * Creates a new set of attachment information with the specified
15188 * events handler and thread.
15189 *
15190 * @param handler the events handler the view must use
15191 */
15192 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080015193 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015194 mSession = session;
15195 mWindow = window;
15196 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080015197 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015198 mHandler = handler;
15199 mRootCallbacks = effectPlayer;
15200 }
15201 }
15202
15203 /**
15204 * <p>ScrollabilityCache holds various fields used by a View when scrolling
15205 * is supported. This avoids keeping too many unused fields in most
15206 * instances of View.</p>
15207 */
Mike Cleronf116bf82009-09-27 19:14:12 -070015208 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080015209
Mike Cleronf116bf82009-09-27 19:14:12 -070015210 /**
15211 * Scrollbars are not visible
15212 */
15213 public static final int OFF = 0;
15214
15215 /**
15216 * Scrollbars are visible
15217 */
15218 public static final int ON = 1;
15219
15220 /**
15221 * Scrollbars are fading away
15222 */
15223 public static final int FADING = 2;
15224
15225 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080015226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015227 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070015228 public int scrollBarDefaultDelayBeforeFade;
15229 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015230
15231 public int scrollBarSize;
15232 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070015233 public float[] interpolatorValues;
15234 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015235
15236 public final Paint paint;
15237 public final Matrix matrix;
15238 public Shader shader;
15239
Mike Cleronf116bf82009-09-27 19:14:12 -070015240 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
15241
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015242 private static final float[] OPAQUE = { 255 };
15243 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080015244
Mike Cleronf116bf82009-09-27 19:14:12 -070015245 /**
15246 * When fading should start. This time moves into the future every time
15247 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
15248 */
15249 public long fadeStartTime;
15250
15251
15252 /**
15253 * The current state of the scrollbars: ON, OFF, or FADING
15254 */
15255 public int state = OFF;
15256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015257 private int mLastColor;
15258
Mike Cleronf116bf82009-09-27 19:14:12 -070015259 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015260 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
15261 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070015262 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
15263 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015264
15265 paint = new Paint();
15266 matrix = new Matrix();
15267 // use use a height of 1, and then wack the matrix each time we
15268 // actually use it.
15269 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015271 paint.setShader(shader);
15272 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070015273 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015274 }
Romain Guy8506ab42009-06-11 17:35:47 -070015275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015276 public void setFadeColor(int color) {
15277 if (color != 0 && color != mLastColor) {
15278 mLastColor = color;
15279 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070015280
Romain Guye55e1a72009-08-27 10:42:26 -070015281 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
15282 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015284 paint.setShader(shader);
15285 // Restore the default transfer mode (src_over)
15286 paint.setXfermode(null);
15287 }
15288 }
Joe Malin32736f02011-01-19 16:14:20 -080015289
Mike Cleronf116bf82009-09-27 19:14:12 -070015290 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070015291 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070015292 if (now >= fadeStartTime) {
15293
15294 // the animation fades the scrollbars out by changing
15295 // the opacity (alpha) from fully opaque to fully
15296 // transparent
15297 int nextFrame = (int) now;
15298 int framesCount = 0;
15299
15300 Interpolator interpolator = scrollBarInterpolator;
15301
15302 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015303 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070015304
15305 // End transparent
15306 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015307 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070015308
15309 state = FADING;
15310
15311 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080015312 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070015313 }
15314 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070015315 }
Mike Cleronf116bf82009-09-27 19:14:12 -070015316
Svetoslav Ganova0156172011-06-26 17:55:44 -070015317 /**
15318 * Resuable callback for sending
15319 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
15320 */
15321 private class SendViewScrolledAccessibilityEvent implements Runnable {
15322 public volatile boolean mIsPending;
15323
15324 public void run() {
15325 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
15326 mIsPending = false;
15327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015328 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015329
15330 /**
15331 * <p>
15332 * This class represents a delegate that can be registered in a {@link View}
15333 * to enhance accessibility support via composition rather via inheritance.
15334 * It is specifically targeted to widget developers that extend basic View
15335 * classes i.e. classes in package android.view, that would like their
15336 * applications to be backwards compatible.
15337 * </p>
15338 * <p>
15339 * A scenario in which a developer would like to use an accessibility delegate
15340 * is overriding a method introduced in a later API version then the minimal API
15341 * version supported by the application. For example, the method
15342 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
15343 * in API version 4 when the accessibility APIs were first introduced. If a
15344 * developer would like his application to run on API version 4 devices (assuming
15345 * all other APIs used by the application are version 4 or lower) and take advantage
15346 * of this method, instead of overriding the method which would break the application's
15347 * backwards compatibility, he can override the corresponding method in this
15348 * delegate and register the delegate in the target View if the API version of
15349 * the system is high enough i.e. the API version is same or higher to the API
15350 * version that introduced
15351 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
15352 * </p>
15353 * <p>
15354 * Here is an example implementation:
15355 * </p>
15356 * <code><pre><p>
15357 * if (Build.VERSION.SDK_INT >= 14) {
15358 * // If the API version is equal of higher than the version in
15359 * // which onInitializeAccessibilityNodeInfo was introduced we
15360 * // register a delegate with a customized implementation.
15361 * View view = findViewById(R.id.view_id);
15362 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
15363 * public void onInitializeAccessibilityNodeInfo(View host,
15364 * AccessibilityNodeInfo info) {
15365 * // Let the default implementation populate the info.
15366 * super.onInitializeAccessibilityNodeInfo(host, info);
15367 * // Set some other information.
15368 * info.setEnabled(host.isEnabled());
15369 * }
15370 * });
15371 * }
15372 * </code></pre></p>
15373 * <p>
15374 * This delegate contains methods that correspond to the accessibility methods
15375 * in View. If a delegate has been specified the implementation in View hands
15376 * off handling to the corresponding method in this delegate. The default
15377 * implementation the delegate methods behaves exactly as the corresponding
15378 * method in View for the case of no accessibility delegate been set. Hence,
15379 * to customize the behavior of a View method, clients can override only the
15380 * corresponding delegate method without altering the behavior of the rest
15381 * accessibility related methods of the host view.
15382 * </p>
15383 */
15384 public static class AccessibilityDelegate {
15385
15386 /**
15387 * Sends an accessibility event of the given type. If accessibility is not
15388 * enabled this method has no effect.
15389 * <p>
15390 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
15391 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
15392 * been set.
15393 * </p>
15394 *
15395 * @param host The View hosting the delegate.
15396 * @param eventType The type of the event to send.
15397 *
15398 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
15399 */
15400 public void sendAccessibilityEvent(View host, int eventType) {
15401 host.sendAccessibilityEventInternal(eventType);
15402 }
15403
15404 /**
15405 * Sends an accessibility event. This method behaves exactly as
15406 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
15407 * empty {@link AccessibilityEvent} and does not perform a check whether
15408 * accessibility is enabled.
15409 * <p>
15410 * The default implementation behaves as
15411 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15412 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
15413 * the case of no accessibility delegate been set.
15414 * </p>
15415 *
15416 * @param host The View hosting the delegate.
15417 * @param event The event to send.
15418 *
15419 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15420 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15421 */
15422 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
15423 host.sendAccessibilityEventUncheckedInternal(event);
15424 }
15425
15426 /**
15427 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
15428 * to its children for adding their text content to the event.
15429 * <p>
15430 * The default implementation behaves as
15431 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15432 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
15433 * the case of no accessibility delegate been set.
15434 * </p>
15435 *
15436 * @param host The View hosting the delegate.
15437 * @param event The event.
15438 * @return True if the event population was completed.
15439 *
15440 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15441 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15442 */
15443 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15444 return host.dispatchPopulateAccessibilityEventInternal(event);
15445 }
15446
15447 /**
15448 * Gives a chance to the host View to populate the accessibility event with its
15449 * text content.
15450 * <p>
15451 * The default implementation behaves as
15452 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
15453 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
15454 * the case of no accessibility delegate been set.
15455 * </p>
15456 *
15457 * @param host The View hosting the delegate.
15458 * @param event The accessibility event which to populate.
15459 *
15460 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
15461 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
15462 */
15463 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15464 host.onPopulateAccessibilityEventInternal(event);
15465 }
15466
15467 /**
15468 * Initializes an {@link AccessibilityEvent} with information about the
15469 * the host View which is the event source.
15470 * <p>
15471 * The default implementation behaves as
15472 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
15473 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
15474 * the case of no accessibility delegate been set.
15475 * </p>
15476 *
15477 * @param host The View hosting the delegate.
15478 * @param event The event to initialize.
15479 *
15480 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
15481 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
15482 */
15483 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
15484 host.onInitializeAccessibilityEventInternal(event);
15485 }
15486
15487 /**
15488 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
15489 * <p>
15490 * The default implementation behaves as
15491 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15492 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
15493 * the case of no accessibility delegate been set.
15494 * </p>
15495 *
15496 * @param host The View hosting the delegate.
15497 * @param info The instance to initialize.
15498 *
15499 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15500 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15501 */
15502 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
15503 host.onInitializeAccessibilityNodeInfoInternal(info);
15504 }
15505
15506 /**
15507 * Called when a child of the host View has requested sending an
15508 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
15509 * to augment the event.
15510 * <p>
15511 * The default implementation behaves as
15512 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15513 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
15514 * the case of no accessibility delegate been set.
15515 * </p>
15516 *
15517 * @param host The View hosting the delegate.
15518 * @param child The child which requests sending the event.
15519 * @param event The event to be sent.
15520 * @return True if the event should be sent
15521 *
15522 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15523 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15524 */
15525 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
15526 AccessibilityEvent event) {
15527 return host.onRequestSendAccessibilityEventInternal(child, event);
15528 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070015529
15530 /**
15531 * Gets the provider for managing a virtual view hierarchy rooted at this View
15532 * and reported to {@link android.accessibilityservice.AccessibilityService}s
15533 * that explore the window content.
15534 * <p>
15535 * The default implementation behaves as
15536 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
15537 * the case of no accessibility delegate been set.
15538 * </p>
15539 *
15540 * @return The provider.
15541 *
15542 * @see AccessibilityNodeProvider
15543 */
15544 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
15545 return null;
15546 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015548}